Skip to content

Commit

Permalink
Rollup merge of rust-lang#120809 - reitermarkus:generic-nonzero-const…
Browse files Browse the repository at this point in the history
…ructors, r=Nilstrieb

Use `transmute_unchecked` in `NonZero::new`.

Tracking issue: rust-lang#120257

See rust-lang#120521 (comment).
  • Loading branch information
matthiaskrgr committed Feb 9, 2024
2 parents 4ba943f + d70d320 commit 376640c
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 376640c

Please sign in to comment.