Skip to content

Commit

Permalink
Use transmute_unchecked in NonZero::new.
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus committed Feb 8, 2024
1 parent c29082f commit d70d320
Showing 1 changed file with 1 addition and 3 deletions.
4 changes: 1 addition & 3 deletions library/core/src/num/nonzero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::intrinsics;
use crate::marker::StructuralEq;
use crate::marker::StructuralPartialEq;
use crate::ops::{BitOr, BitOrAssign, Div, Neg, Rem};
use crate::ptr;
use crate::str::FromStr;

use super::from_str_radix;
Expand Down Expand Up @@ -91,13 +90,12 @@ where
/// Creates a non-zero if the given value is not zero.
#[stable(feature = "nonzero", since = "1.28.0")]
#[rustc_const_stable(feature = "const_nonzero_int_methods", since = "1.47.0")]
#[rustc_allow_const_fn_unstable(const_refs_to_cell)]
#[must_use]
#[inline]
pub const fn new(n: T) -> Option<Self> {
// SAFETY: Memory layout optimization guarantees that `Option<NonZero<T>>` has
// the same layout and size as `T`, with `0` representing `None`.
unsafe { ptr::read(ptr::addr_of!(n).cast()) }
unsafe { intrinsics::transmute_unchecked(n) }
}

/// Creates a non-zero without checking whether the value is non-zero.
Expand Down

0 comments on commit d70d320

Please sign in to comment.