diff options
| author | Markus Reiter <me@reitermark.us> | 2024-02-18 21:29:24 +0100 |
|---|---|---|
| committer | Markus Reiter <me@reitermark.us> | 2024-02-22 15:17:33 +0100 |
| commit | 14ed426eec6939737ea320e50bb28d239c7aee93 (patch) | |
| tree | 8a5ec0d0fb9fcb6b540d4a6ce54289f84850d7f9 /library/core/src/ptr | |
| parent | 52dba5ffe73c25951fd6ae38bf20513002dd7874 (diff) | |
Use generic `NonZero` everywhere in `core`.
Diffstat (limited to 'library/core/src/ptr')
| -rw-r--r-- | library/core/src/ptr/alignment.rs | 16 | ||||
| -rw-r--r-- | library/core/src/ptr/non_null.rs | 8 |
2 files changed, 12 insertions, 12 deletions
diff --git a/library/core/src/ptr/alignment.rs b/library/core/src/ptr/alignment.rs index 6dfecb5a826..bd58c167c2e 100644 --- a/library/core/src/ptr/alignment.rs +++ b/library/core/src/ptr/alignment.rs @@ -1,5 +1,5 @@ use crate::convert::{TryFrom, TryInto}; -use crate::num::{NonZero, NonZeroUsize}; +use crate::num::NonZero; use crate::{cmp, fmt, hash, mem, num}; /// A type storing a `usize` which is a power of two, and thus @@ -87,7 +87,7 @@ impl Alignment { unsafe { mem::transmute::<usize, Alignment>(align) } } - /// Returns the alignment as a [`usize`] + /// Returns the alignment as a [`usize`]. #[unstable(feature = "ptr_alignment_type", issue = "102070")] #[rustc_const_unstable(feature = "ptr_alignment_type", issue = "102070")] #[inline] @@ -95,11 +95,11 @@ impl Alignment { self.0 as usize } - /// Returns the alignment as a [`NonZeroUsize`] + /// Returns the alignment as a <code>[NonZero]<[usize]></code>. #[unstable(feature = "ptr_alignment_type", issue = "102070")] #[rustc_const_unstable(feature = "ptr_alignment_type", issue = "102070")] #[inline] - pub const fn as_nonzero(self) -> NonZeroUsize { + pub const fn as_nonzero(self) -> NonZero<usize> { // SAFETY: All the discriminants are non-zero. unsafe { NonZero::new_unchecked(self.as_usize()) } } @@ -164,11 +164,11 @@ impl fmt::Debug for Alignment { } #[unstable(feature = "ptr_alignment_type", issue = "102070")] -impl TryFrom<NonZeroUsize> for Alignment { +impl TryFrom<NonZero<usize>> for Alignment { type Error = num::TryFromIntError; #[inline] - fn try_from(align: NonZeroUsize) -> Result<Alignment, Self::Error> { + fn try_from(align: NonZero<usize>) -> Result<Alignment, Self::Error> { align.get().try_into() } } @@ -184,9 +184,9 @@ impl TryFrom<usize> for Alignment { } #[unstable(feature = "ptr_alignment_type", issue = "102070")] -impl From<Alignment> for NonZeroUsize { +impl From<Alignment> for NonZero<usize> { #[inline] - fn from(align: Alignment) -> NonZeroUsize { + fn from(align: Alignment) -> NonZero<usize> { align.as_nonzero() } } diff --git a/library/core/src/ptr/non_null.rs b/library/core/src/ptr/non_null.rs index 098ec233855..acb8c552a63 100644 --- a/library/core/src/ptr/non_null.rs +++ b/library/core/src/ptr/non_null.rs @@ -5,7 +5,7 @@ use crate::intrinsics; use crate::intrinsics::assert_unsafe_precondition; use crate::marker::Unsize; use crate::mem::{MaybeUninit, SizedTypeProperties}; -use crate::num::{NonZero, NonZeroUsize}; +use crate::num::NonZero; use crate::ops::{CoerceUnsized, DispatchFromDyn}; use crate::ptr; use crate::ptr::Unique; @@ -291,7 +291,7 @@ impl<T: ?Sized> NonNull<T> { #[must_use] #[inline] #[unstable(feature = "strict_provenance", issue = "95228")] - pub fn addr(self) -> NonZeroUsize { + pub fn addr(self) -> NonZero<usize> { // SAFETY: The pointer is guaranteed by the type to be non-null, // meaning that the address will be non-zero. unsafe { NonZero::new_unchecked(self.pointer.addr()) } @@ -306,7 +306,7 @@ impl<T: ?Sized> NonNull<T> { #[must_use] #[inline] #[unstable(feature = "strict_provenance", issue = "95228")] - pub fn with_addr(self, addr: NonZeroUsize) -> Self { + pub fn with_addr(self, addr: NonZero<usize>) -> Self { // SAFETY: The result of `ptr::from::with_addr` is non-null because `addr` is guaranteed to be non-zero. unsafe { NonNull::new_unchecked(self.pointer.with_addr(addr.get()) as *mut _) } } @@ -320,7 +320,7 @@ impl<T: ?Sized> NonNull<T> { #[must_use] #[inline] #[unstable(feature = "strict_provenance", issue = "95228")] - pub fn map_addr(self, f: impl FnOnce(NonZeroUsize) -> NonZeroUsize) -> Self { + pub fn map_addr(self, f: impl FnOnce(NonZero<usize>) -> NonZero<usize>) -> Self { self.with_addr(f(self.addr())) } |
