Use transmute_unchecked
in NonZero::new
.
This commit is contained in:
parent
c29082fe7d
commit
d70d3204b7
@ -8,7 +8,6 @@
|
||||
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;
|
||||
@ -91,13 +90,12 @@ impl<T> NonZero<T>
|
||||
/// 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.
|
||||
|
Loading…
Reference in New Issue
Block a user