diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index ecfb735fad14a..852e39878a3eb 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -1,6 +1,7 @@ //! Definitions of integer that is known not to equal zero. use crate::fmt; +use crate::hash::{Hash, Hasher}; use crate::ops::{BitOr, BitOrAssign, Div, Rem}; use crate::str::FromStr; @@ -42,7 +43,8 @@ macro_rules! nonzero_integers { #[doc = concat!("`Option<", stringify!($Ty), ">` is guaranteed to be compatible with `", stringify!($Int), "`,")] /// including in FFI. #[$stability] - #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] + #[derive_const(PartialEq, PartialOrd)] + #[derive(Copy, Clone, Eq, Ord)] #[repr(transparent)] #[rustc_layout_scalar_valid_range_start(1)] #[rustc_nonnull_optimization_guaranteed] @@ -105,6 +107,15 @@ macro_rules! nonzero_integers { } } + #[$stability] + #[rustc_const_unstable(feature = "const_hash", issue = "104061")] + impl const Hash for $Ty { + #[inline] + fn hash(&self, state: &mut H) { + self.0.hash(state) + } + } + #[stable(feature = "nonzero_bitor", since = "1.45.0")] #[rustc_const_unstable(feature = "const_ops", issue = "90080")] impl const BitOr for $Ty { diff --git a/library/core/src/ptr/alignment.rs b/library/core/src/ptr/alignment.rs index efe6d4183e3ea..18c5838042e6a 100644 --- a/library/core/src/ptr/alignment.rs +++ b/library/core/src/ptr/alignment.rs @@ -18,7 +18,7 @@ pub struct Alignment(AlignmentEnum); const _: () = assert!(mem::size_of::() == mem::size_of::()); const _: () = assert!(mem::align_of::() == mem::align_of::()); -fn _alignment_can_be_structurally_matched(a: Alignment) -> bool { +const fn _alignment_can_be_structurally_matched(a: Alignment) -> bool { matches!(a, Alignment::MIN) } @@ -118,9 +118,10 @@ impl Alignment { /// assert_eq!(Alignment::of::().log2(), 0); /// assert_eq!(Alignment::new(1024).unwrap().log2(), 10); /// ``` + #[rustc_const_unstable(feature = "ptr_alignment_type", issue = "102070")] #[unstable(feature = "ptr_alignment_type", issue = "102070")] #[inline] - pub fn log2(self) -> u32 { + pub const fn log2(self) -> u32 { self.as_nonzero().trailing_zeros() } } @@ -132,8 +133,9 @@ impl fmt::Debug for Alignment { } } +#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")] #[unstable(feature = "ptr_alignment_type", issue = "102070")] -impl TryFrom for Alignment { +impl const TryFrom for Alignment { type Error = num::TryFromIntError; #[inline] @@ -142,8 +144,9 @@ impl TryFrom for Alignment { } } +#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")] #[unstable(feature = "ptr_alignment_type", issue = "102070")] -impl TryFrom for Alignment { +impl const TryFrom for Alignment { type Error = num::TryFromIntError; #[inline] @@ -152,16 +155,18 @@ impl TryFrom for Alignment { } } +#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")] #[unstable(feature = "ptr_alignment_type", issue = "102070")] -impl From for NonZeroUsize { +impl const From for NonZeroUsize { #[inline] fn from(align: Alignment) -> NonZeroUsize { align.as_nonzero() } } +#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")] #[unstable(feature = "ptr_alignment_type", issue = "102070")] -impl From for usize { +impl const From for usize { #[inline] fn from(align: Alignment) -> usize { align.as_usize() @@ -186,10 +191,11 @@ impl const cmp::PartialOrd for Alignment { } } +#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")] #[unstable(feature = "ptr_alignment_type", issue = "102070")] -impl hash::Hash for Alignment { +impl const hash::Hash for Alignment { #[inline] - fn hash(&self, state: &mut H) { + fn hash(&self, state: &mut H) { self.as_nonzero().hash(state) } }