Skip to content

Commit

Permalink
Remove one unsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
joshlf committed Oct 29, 2023
1 parent 874f937 commit aec9af8
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/race.rs
Expand Up @@ -171,7 +171,21 @@ impl OnceBool {

#[inline]
fn to_usize(value: bool) -> NonZeroUsize {
unsafe { NonZeroUsize::new_unchecked(if value { 1 } else { 2 }) }
const fn new_nonzero_usize(n: usize) -> NonZeroUsize {
match NonZeroUsize::new(n) {
Some(n) => n,
None => unreachable!(),
}
}

const ONE: NonZeroUsize = new_nonzero_usize(1);
const TWO: NonZeroUsize = new_nonzero_usize(2);

if value {
ONE
} else {
TWO
}
}
}

Expand Down

0 comments on commit aec9af8

Please sign in to comment.