Use generic NonZero everywhere in core.
This commit is contained in:
@@ -511,7 +511,7 @@ impl<T, const N: usize> [T; N] {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(array_try_map)]
|
||||
/// #![feature(array_try_map, generic_nonzero)]
|
||||
/// let a = ["1", "2", "3"];
|
||||
/// let b = a.try_map(|v| v.parse::<u32>()).unwrap().map(|v| v + 1);
|
||||
/// assert_eq!(b, [2, 3, 4]);
|
||||
@@ -520,12 +520,12 @@ impl<T, const N: usize> [T; N] {
|
||||
/// let b = a.try_map(|v| v.parse::<u32>());
|
||||
/// assert!(b.is_err());
|
||||
///
|
||||
/// use std::num::NonZeroU32;
|
||||
/// use std::num::NonZero;
|
||||
/// let z = [1, 2, 0, 3, 4];
|
||||
/// assert_eq!(z.try_map(NonZeroU32::new), None);
|
||||
/// assert_eq!(z.try_map(NonZero::new), None);
|
||||
/// let a = [1, 2, 3];
|
||||
/// let b = a.try_map(NonZeroU32::new);
|
||||
/// let c = b.map(|x| x.map(NonZeroU32::get));
|
||||
/// let b = a.try_map(NonZero::new);
|
||||
/// let c = b.map(|x| x.map(NonZero::get));
|
||||
/// assert_eq!(c, Some(a));
|
||||
/// ```
|
||||
#[unstable(feature = "array_try_map", issue = "79711")]
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
use crate::num::{NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize};
|
||||
use crate::num::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize};
|
||||
use crate::num::NonZero;
|
||||
|
||||
/// Types where `==` & `!=` are equivalent to comparing their underlying bytes.
|
||||
///
|
||||
@@ -36,18 +35,18 @@ is_bytewise_comparable!(bool, char, super::Ordering);
|
||||
// SAFETY: Similarly, the `NonZero` type has a niche, but no undef and no pointers,
|
||||
// and they compare like their underlying numeric type.
|
||||
is_bytewise_comparable!(
|
||||
NonZeroU8,
|
||||
NonZeroU16,
|
||||
NonZeroU32,
|
||||
NonZeroU64,
|
||||
NonZeroU128,
|
||||
NonZeroUsize,
|
||||
NonZeroI8,
|
||||
NonZeroI16,
|
||||
NonZeroI32,
|
||||
NonZeroI64,
|
||||
NonZeroI128,
|
||||
NonZeroIsize,
|
||||
NonZero<u8>,
|
||||
NonZero<u16>,
|
||||
NonZero<u32>,
|
||||
NonZero<u64>,
|
||||
NonZero<u128>,
|
||||
NonZero<usize>,
|
||||
NonZero<i8>,
|
||||
NonZero<i16>,
|
||||
NonZero<i32>,
|
||||
NonZero<i64>,
|
||||
NonZero<i128>,
|
||||
NonZero<isize>,
|
||||
);
|
||||
|
||||
// SAFETY: The `NonZero` type has the "null" optimization guaranteed, and thus
|
||||
@@ -55,18 +54,18 @@ is_bytewise_comparable!(
|
||||
// The way `PartialOrd` is defined for `Option` means that this wouldn't work
|
||||
// for `<` or `>` on the signed types, but since we only do `==` it's fine.
|
||||
is_bytewise_comparable!(
|
||||
Option<NonZeroU8>,
|
||||
Option<NonZeroU16>,
|
||||
Option<NonZeroU32>,
|
||||
Option<NonZeroU64>,
|
||||
Option<NonZeroU128>,
|
||||
Option<NonZeroUsize>,
|
||||
Option<NonZeroI8>,
|
||||
Option<NonZeroI16>,
|
||||
Option<NonZeroI32>,
|
||||
Option<NonZeroI64>,
|
||||
Option<NonZeroI128>,
|
||||
Option<NonZeroIsize>,
|
||||
Option<NonZero<u8>>,
|
||||
Option<NonZero<u16>>,
|
||||
Option<NonZero<u32>>,
|
||||
Option<NonZero<u64>>,
|
||||
Option<NonZero<u128>>,
|
||||
Option<NonZero<usize>>,
|
||||
Option<NonZero<i8>>,
|
||||
Option<NonZero<i16>>,
|
||||
Option<NonZero<i32>>,
|
||||
Option<NonZero<i64>>,
|
||||
Option<NonZero<i128>>,
|
||||
Option<NonZero<isize>>,
|
||||
);
|
||||
|
||||
macro_rules! is_bytewise_comparable_array_length {
|
||||
|
||||
@@ -100,7 +100,7 @@ pub trait DoubleEndedIterator: Iterator {
|
||||
/// to `n` times until [`None`] is encountered.
|
||||
///
|
||||
/// `advance_back_by(n)` will return `Ok(())` if the iterator successfully advances by
|
||||
/// `n` elements, or a `Err(NonZeroUsize)` with value `k` if [`None`] is encountered, where `k`
|
||||
/// `n` elements, or a `Err(NonZero<usize>)` with value `k` if [`None`] is encountered, where `k`
|
||||
/// is remaining number of steps that could not be advanced because the iterator ran out.
|
||||
/// If `self` is empty and `n` is non-zero, then this returns `Err(n)`.
|
||||
/// Otherwise, `k` is always less than `n`.
|
||||
@@ -118,8 +118,8 @@ pub trait DoubleEndedIterator: Iterator {
|
||||
/// Basic usage:
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(iter_advance_by)]
|
||||
/// use std::num::NonZeroUsize;
|
||||
/// #![feature(generic_nonzero, iter_advance_by)]
|
||||
/// use std::num::NonZero;
|
||||
///
|
||||
/// let a = [3, 4, 5, 6];
|
||||
/// let mut iter = a.iter();
|
||||
@@ -127,7 +127,7 @@ pub trait DoubleEndedIterator: Iterator {
|
||||
/// assert_eq!(iter.advance_back_by(2), Ok(()));
|
||||
/// assert_eq!(iter.next_back(), Some(&4));
|
||||
/// assert_eq!(iter.advance_back_by(0), Ok(()));
|
||||
/// assert_eq!(iter.advance_back_by(100), Err(NonZeroUsize::new(99).unwrap())); // only `&3` was skipped
|
||||
/// assert_eq!(iter.advance_back_by(100), Err(NonZero::new(99).unwrap())); // only `&3` was skipped
|
||||
/// ```
|
||||
///
|
||||
/// [`Ok(())`]: Ok
|
||||
|
||||
@@ -304,7 +304,7 @@ pub trait Iterator {
|
||||
/// times until [`None`] is encountered.
|
||||
///
|
||||
/// `advance_by(n)` will return `Ok(())` if the iterator successfully advances by
|
||||
/// `n` elements, or a `Err(NonZeroUsize)` with value `k` if [`None`] is encountered,
|
||||
/// `n` elements, or a `Err(NonZero<usize>)` with value `k` if [`None`] is encountered,
|
||||
/// where `k` is remaining number of steps that could not be advanced because the iterator ran out.
|
||||
/// If `self` is empty and `n` is non-zero, then this returns `Err(n)`.
|
||||
/// Otherwise, `k` is always less than `n`.
|
||||
@@ -319,8 +319,8 @@ pub trait Iterator {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(iter_advance_by)]
|
||||
/// use std::num::NonZeroUsize;
|
||||
/// #![feature(generic_nonzero, iter_advance_by)]
|
||||
/// use std::num::NonZero;
|
||||
///
|
||||
/// let a = [1, 2, 3, 4];
|
||||
/// let mut iter = a.iter();
|
||||
@@ -328,7 +328,7 @@ pub trait Iterator {
|
||||
/// assert_eq!(iter.advance_by(2), Ok(()));
|
||||
/// assert_eq!(iter.next(), Some(&3));
|
||||
/// assert_eq!(iter.advance_by(0), Ok(()));
|
||||
/// assert_eq!(iter.advance_by(100), Err(NonZeroUsize::new(99).unwrap())); // only `&4` was skipped
|
||||
/// assert_eq!(iter.advance_by(100), Err(NonZero::new(99).unwrap())); // only `&4` was skipped
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable(feature = "iter_advance_by", reason = "recently added", issue = "77404")]
|
||||
@@ -2969,15 +2969,15 @@ pub trait Iterator {
|
||||
///
|
||||
/// This also supports other types which implement `Try`, not just `Result`.
|
||||
/// ```
|
||||
/// #![feature(try_find)]
|
||||
/// #![feature(generic_nonzero, try_find)]
|
||||
///
|
||||
/// use std::num::NonZeroU32;
|
||||
/// use std::num::NonZero;
|
||||
/// let a = [3, 5, 7, 4, 9, 0, 11];
|
||||
/// let result = a.iter().try_find(|&&x| NonZeroU32::new(x).map(|y| y.is_power_of_two()));
|
||||
/// let result = a.iter().try_find(|&&x| NonZero::new(x).map(|y| y.is_power_of_two()));
|
||||
/// assert_eq!(result, Some(Some(&4)));
|
||||
/// let result = a.iter().take(3).try_find(|&&x| NonZeroU32::new(x).map(|y| y.is_power_of_two()));
|
||||
/// let result = a.iter().take(3).try_find(|&&x| NonZero::new(x).map(|y| y.is_power_of_two()));
|
||||
/// assert_eq!(result, Some(None));
|
||||
/// let result = a.iter().rev().try_find(|&&x| NonZeroU32::new(x).map(|y| y.is_power_of_two()));
|
||||
/// let result = a.iter().rev().try_find(|&&x| NonZero::new(x).map(|y| y.is_power_of_two()));
|
||||
/// assert_eq!(result, None);
|
||||
/// ```
|
||||
#[inline]
|
||||
|
||||
@@ -23,7 +23,7 @@ mod private {
|
||||
|
||||
/// A marker trait for primitive types which can be zero.
|
||||
///
|
||||
/// This is an implementation detail for [`NonZero<T>`](NonZero) which may disappear or be replaced at any time.
|
||||
/// This is an implementation detail for <code>[NonZero]\<T></code> which may disappear or be replaced at any time.
|
||||
#[unstable(
|
||||
feature = "nonzero_internals",
|
||||
reason = "implementation detail which may disappear or be replaced at any time",
|
||||
@@ -507,18 +507,16 @@ macro_rules! nonzero_integer {
|
||||
/// Basic usage:
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(non_zero_count_ones)]
|
||||
/// #![feature(generic_nonzero, non_zero_count_ones)]
|
||||
/// # fn main() { test().unwrap(); }
|
||||
/// # fn test() -> Option<()> {
|
||||
/// # use std::num::*;
|
||||
/// #
|
||||
/// let one = NonZeroU32::new(1)?;
|
||||
/// let three = NonZeroU32::new(3)?;
|
||||
#[doc = concat!("let a = ", stringify!($Ty), "::new(0b100_0000)?;")]
|
||||
#[doc = concat!("let b = ", stringify!($Ty), "::new(0b100_0011)?;")]
|
||||
#[doc = concat!("let a = NonZero::<", stringify!($Int), ">::new(0b100_0000)?;")]
|
||||
#[doc = concat!("let b = NonZero::<", stringify!($Int), ">::new(0b100_0011)?;")]
|
||||
///
|
||||
/// assert_eq!(a.count_ones(), one);
|
||||
/// assert_eq!(b.count_ones(), three);
|
||||
/// assert_eq!(a.count_ones(), NonZero::new(1)?);
|
||||
/// assert_eq!(b.count_ones(), NonZero::new(3)?);
|
||||
/// # Some(())
|
||||
/// # }
|
||||
/// ```
|
||||
@@ -530,7 +528,7 @@ macro_rules! nonzero_integer {
|
||||
#[must_use = "this returns the result of the operation, \
|
||||
without modifying the original"]
|
||||
#[inline(always)]
|
||||
pub const fn count_ones(self) -> NonZeroU32 {
|
||||
pub const fn count_ones(self) -> NonZero<u32> {
|
||||
// SAFETY:
|
||||
// `self` is non-zero, which means it has at least one bit set, which means
|
||||
// that the result of `count_ones` is non-zero.
|
||||
|
||||
@@ -558,6 +558,7 @@ use crate::panicking::{panic, panic_str};
|
||||
use crate::pin::Pin;
|
||||
use crate::{
|
||||
cmp, convert, hint, mem,
|
||||
num::NonZero,
|
||||
ops::{self, ControlFlow, Deref, DerefMut},
|
||||
slice,
|
||||
};
|
||||
@@ -2193,18 +2194,18 @@ macro_rules! non_zero_option {
|
||||
}
|
||||
|
||||
non_zero_option! {
|
||||
#[stable(feature = "nonzero", since = "1.28.0")] crate::num::NonZeroU8;
|
||||
#[stable(feature = "nonzero", since = "1.28.0")] crate::num::NonZeroU16;
|
||||
#[stable(feature = "nonzero", since = "1.28.0")] crate::num::NonZeroU32;
|
||||
#[stable(feature = "nonzero", since = "1.28.0")] crate::num::NonZeroU64;
|
||||
#[stable(feature = "nonzero", since = "1.28.0")] crate::num::NonZeroU128;
|
||||
#[stable(feature = "nonzero", since = "1.28.0")] crate::num::NonZeroUsize;
|
||||
#[stable(feature = "signed_nonzero", since = "1.34.0")] crate::num::NonZeroI8;
|
||||
#[stable(feature = "signed_nonzero", since = "1.34.0")] crate::num::NonZeroI16;
|
||||
#[stable(feature = "signed_nonzero", since = "1.34.0")] crate::num::NonZeroI32;
|
||||
#[stable(feature = "signed_nonzero", since = "1.34.0")] crate::num::NonZeroI64;
|
||||
#[stable(feature = "signed_nonzero", since = "1.34.0")] crate::num::NonZeroI128;
|
||||
#[stable(feature = "signed_nonzero", since = "1.34.0")] crate::num::NonZeroIsize;
|
||||
#[stable(feature = "nonzero", since = "1.28.0")] NonZero<u8>;
|
||||
#[stable(feature = "nonzero", since = "1.28.0")] NonZero<u16>;
|
||||
#[stable(feature = "nonzero", since = "1.28.0")] NonZero<u32>;
|
||||
#[stable(feature = "nonzero", since = "1.28.0")] NonZero<u64>;
|
||||
#[stable(feature = "nonzero", since = "1.28.0")] NonZero<u128>;
|
||||
#[stable(feature = "nonzero", since = "1.28.0")] NonZero<usize>;
|
||||
#[stable(feature = "signed_nonzero", since = "1.34.0")] NonZero<i8>;
|
||||
#[stable(feature = "signed_nonzero", since = "1.34.0")] NonZero<i16>;
|
||||
#[stable(feature = "signed_nonzero", since = "1.34.0")] NonZero<i32>;
|
||||
#[stable(feature = "signed_nonzero", since = "1.34.0")] NonZero<i64>;
|
||||
#[stable(feature = "signed_nonzero", since = "1.34.0")] NonZero<i128>;
|
||||
#[stable(feature = "signed_nonzero", since = "1.34.0")] NonZero<isize>;
|
||||
}
|
||||
|
||||
#[stable(feature = "nonnull", since = "1.25.0")]
|
||||
|
||||
@@ -1577,8 +1577,7 @@ mod prim_ref {}
|
||||
/// - Any two types with size 0 and alignment 1 are ABI-compatible.
|
||||
/// - A `repr(transparent)` type `T` is ABI-compatible with its unique non-trivial field, i.e., the
|
||||
/// unique field that doesn't have size 0 and alignment 1 (if there is such a field).
|
||||
/// - `i32` is ABI-compatible with `NonZeroI32`, and similar for all other integer types with their
|
||||
/// matching `NonZero*` type.
|
||||
/// - `i32` is ABI-compatible with `NonZero<i32>`, and similar for all other integer types.
|
||||
/// - If `T` is guaranteed to be subject to the [null pointer
|
||||
/// optimization](option/index.html#representation), then `T` and `Option<T>` are ABI-compatible.
|
||||
///
|
||||
@@ -1613,9 +1612,9 @@ mod prim_ref {}
|
||||
/// type in the function pointer to the type at the function declaration, and the return value is
|
||||
/// [`transmute`d][mem::transmute] from the type in the declaration to the type in the
|
||||
/// pointer. All the usual caveats and concerns around transmutation apply; for instance, if the
|
||||
/// function expects a `NonZeroI32` and the function pointer uses the ABI-compatible type
|
||||
/// `Option<NonZeroI32>`, and the value used for the argument is `None`, then this call is Undefined
|
||||
/// Behavior since transmuting `None::<NonZeroI32>` to `NonZeroI32` violates the non-zero
|
||||
/// function expects a `NonZero<i32>` and the function pointer uses the ABI-compatible type
|
||||
/// `Option<NonZero<i32>>`, and the value used for the argument is `None`, then this call is Undefined
|
||||
/// Behavior since transmuting `None::<NonZero<i32>>` to `NonZero<i32>` violates the non-zero
|
||||
/// requirement.
|
||||
///
|
||||
/// #### Requirements concerning target features
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()))
|
||||
}
|
||||
|
||||
|
||||
@@ -5,10 +5,10 @@ use crate::fmt::{self, Write};
|
||||
use crate::iter::{Chain, FlatMap, Flatten};
|
||||
use crate::iter::{Copied, Filter, FusedIterator, Map, TrustedLen};
|
||||
use crate::iter::{TrustedRandomAccess, TrustedRandomAccessNoCoerce};
|
||||
use crate::num::NonZero;
|
||||
use crate::ops::Try;
|
||||
use crate::option;
|
||||
use crate::slice::{self, Split as SliceSplit};
|
||||
use core::num::{NonZero, NonZeroUsize};
|
||||
|
||||
use super::from_utf8_unchecked;
|
||||
use super::pattern::Pattern;
|
||||
@@ -51,7 +51,7 @@ impl<'a> Iterator for Chars<'a> {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn advance_by(&mut self, mut remainder: usize) -> Result<(), NonZeroUsize> {
|
||||
fn advance_by(&mut self, mut remainder: usize) -> Result<(), NonZero<usize>> {
|
||||
const CHUNK_SIZE: usize = 32;
|
||||
|
||||
if remainder >= CHUNK_SIZE {
|
||||
|
||||
Reference in New Issue
Block a user