Skip to content

Commit

Permalink
Remove try_with_rng
Browse files Browse the repository at this point in the history
The local function try_with_rng in global_rng can be removed,
because it is not possible to access the thread local storage RNG
twice with given functions.
  • Loading branch information
stoeckmann committed Feb 29, 2024
1 parent 034e668 commit 5ed81c6
Showing 1 changed file with 1 addition and 13 deletions.
14 changes: 1 addition & 13 deletions src/global_rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Rng {
/// Creates a new random number generator.
#[inline]
pub fn new() -> Rng {
try_with_rng(Rng::fork).unwrap_or_else(|_| Rng::with_seed(0x4d595df4d0f33173))
with_rng(Rng::fork)
}
}

Expand All @@ -42,18 +42,6 @@ fn with_rng<R>(f: impl FnOnce(&mut Rng) -> R) -> R {
})
}

/// Try to run an operation with the current thread-local generator.
#[inline]
fn try_with_rng<R>(f: impl FnOnce(&mut Rng) -> R) -> Result<R, std::thread::AccessError> {
RNG.try_with(|rng| {
let current = rng.replace(Rng(0));

let mut restore = RestoreOnDrop { rng, current };

f(&mut restore.current)
})
}

/// Make sure the original RNG is restored even on panic.
struct RestoreOnDrop<'a> {
rng: &'a Cell<Rng>,
Expand Down

0 comments on commit 5ed81c6

Please sign in to comment.