Switch NonZero
alias direction.
This commit is contained in:
parent
8af70c7a18
commit
4f0ce6fca2
4 changed files with 52 additions and 19 deletions
|
@ -61,7 +61,15 @@ pub use dec2flt::ParseFloatError;
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
pub use error::ParseIntError;
|
pub use error::ParseIntError;
|
||||||
|
|
||||||
pub(crate) use nonzero::NonZero;
|
#[unstable(
|
||||||
|
feature = "nonzero_internals",
|
||||||
|
reason = "implementation detail which may disappear or be replaced at any time",
|
||||||
|
issue = "none"
|
||||||
|
)]
|
||||||
|
pub use nonzero::ZeroablePrimitive;
|
||||||
|
|
||||||
|
#[unstable(feature = "generic_nonzero", issue = "120257")]
|
||||||
|
pub use nonzero::NonZero;
|
||||||
|
|
||||||
#[stable(feature = "nonzero", since = "1.28.0")]
|
#[stable(feature = "nonzero", since = "1.28.0")]
|
||||||
pub use nonzero::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize};
|
pub use nonzero::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize};
|
||||||
|
|
|
@ -3,6 +3,8 @@
|
||||||
use crate::cmp::Ordering;
|
use crate::cmp::Ordering;
|
||||||
use crate::fmt;
|
use crate::fmt;
|
||||||
use crate::hash::{Hash, Hasher};
|
use crate::hash::{Hash, Hasher};
|
||||||
|
#[cfg(bootstrap)]
|
||||||
|
use crate::marker::StructuralEq;
|
||||||
use crate::marker::StructuralPartialEq;
|
use crate::marker::StructuralPartialEq;
|
||||||
use crate::ops::{BitOr, BitOrAssign, Div, Neg, Rem};
|
use crate::ops::{BitOr, BitOrAssign, Div, Neg, Rem};
|
||||||
use crate::str::FromStr;
|
use crate::str::FromStr;
|
||||||
|
@ -30,9 +32,7 @@ mod private {
|
||||||
issue = "none"
|
issue = "none"
|
||||||
)]
|
)]
|
||||||
#[const_trait]
|
#[const_trait]
|
||||||
pub trait ZeroablePrimitive: Sized + Copy + private::Sealed {
|
pub trait ZeroablePrimitive: Sized + Copy + private::Sealed {}
|
||||||
type NonZero;
|
|
||||||
}
|
|
||||||
|
|
||||||
macro_rules! impl_zeroable_primitive {
|
macro_rules! impl_zeroable_primitive {
|
||||||
($NonZero:ident ( $primitive:ty )) => {
|
($NonZero:ident ( $primitive:ty )) => {
|
||||||
|
@ -48,9 +48,7 @@ macro_rules! impl_zeroable_primitive {
|
||||||
reason = "implementation detail which may disappear or be replaced at any time",
|
reason = "implementation detail which may disappear or be replaced at any time",
|
||||||
issue = "none"
|
issue = "none"
|
||||||
)]
|
)]
|
||||||
impl const ZeroablePrimitive for $primitive {
|
impl const ZeroablePrimitive for $primitive {}
|
||||||
type NonZero = $NonZero;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,12 +65,23 @@ impl_zeroable_primitive!(NonZeroI64(i64));
|
||||||
impl_zeroable_primitive!(NonZeroI128(i128));
|
impl_zeroable_primitive!(NonZeroI128(i128));
|
||||||
impl_zeroable_primitive!(NonZeroIsize(isize));
|
impl_zeroable_primitive!(NonZeroIsize(isize));
|
||||||
|
|
||||||
#[unstable(
|
/// A value that is known not to equal zero.
|
||||||
feature = "nonzero_internals",
|
///
|
||||||
reason = "implementation detail which may disappear or be replaced at any time",
|
/// This enables some memory layout optimization.
|
||||||
issue = "none"
|
/// For example, `Option<NonZero<u32>>` is the same size as `u32`:
|
||||||
)]
|
///
|
||||||
pub(crate) type NonZero<T> = <T as ZeroablePrimitive>::NonZero;
|
/// ```
|
||||||
|
/// #![feature(generic_nonzero)]
|
||||||
|
/// use core::mem::size_of;
|
||||||
|
///
|
||||||
|
/// assert_eq!(size_of::<Option<core::num::NonZero<u32>>>(), size_of::<u32>());
|
||||||
|
/// ```
|
||||||
|
#[unstable(feature = "generic_nonzero", issue = "120257")]
|
||||||
|
#[repr(transparent)]
|
||||||
|
#[rustc_layout_scalar_valid_range_start(1)]
|
||||||
|
#[rustc_nonnull_optimization_guaranteed]
|
||||||
|
#[rustc_diagnostic_item = "NonZero"]
|
||||||
|
pub struct NonZero<T: ZeroablePrimitive>(T);
|
||||||
|
|
||||||
macro_rules! impl_nonzero_fmt {
|
macro_rules! impl_nonzero_fmt {
|
||||||
( #[$stability: meta] ( $( $Trait: ident ),+ ) for $Ty: ident ) => {
|
( #[$stability: meta] ( $( $Trait: ident ),+ ) for $Ty: ident ) => {
|
||||||
|
@ -131,12 +140,7 @@ macro_rules! nonzero_integer {
|
||||||
///
|
///
|
||||||
/// [null pointer optimization]: crate::option#representation
|
/// [null pointer optimization]: crate::option#representation
|
||||||
#[$stability]
|
#[$stability]
|
||||||
#[derive(Copy, Eq)]
|
pub type $Ty = NonZero<$Int>;
|
||||||
#[repr(transparent)]
|
|
||||||
#[rustc_layout_scalar_valid_range_start(1)]
|
|
||||||
#[rustc_nonnull_optimization_guaranteed]
|
|
||||||
#[rustc_diagnostic_item = stringify!($Ty)]
|
|
||||||
pub struct $Ty($Int);
|
|
||||||
|
|
||||||
impl $Ty {
|
impl $Ty {
|
||||||
/// Creates a non-zero without checking whether the value is non-zero.
|
/// Creates a non-zero without checking whether the value is non-zero.
|
||||||
|
@ -543,6 +547,9 @@ macro_rules! nonzero_integer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[$stability]
|
||||||
|
impl Copy for $Ty {}
|
||||||
|
|
||||||
#[$stability]
|
#[$stability]
|
||||||
impl PartialEq for $Ty {
|
impl PartialEq for $Ty {
|
||||||
#[inline]
|
#[inline]
|
||||||
|
@ -559,6 +566,13 @@ macro_rules! nonzero_integer {
|
||||||
#[unstable(feature = "structural_match", issue = "31434")]
|
#[unstable(feature = "structural_match", issue = "31434")]
|
||||||
impl StructuralPartialEq for $Ty {}
|
impl StructuralPartialEq for $Ty {}
|
||||||
|
|
||||||
|
#[$stability]
|
||||||
|
impl Eq for $Ty {}
|
||||||
|
|
||||||
|
#[unstable(feature = "structural_match", issue = "31434")]
|
||||||
|
#[cfg(bootstrap)]
|
||||||
|
impl StructuralEq for $Ty {}
|
||||||
|
|
||||||
#[$stability]
|
#[$stability]
|
||||||
impl PartialOrd for $Ty {
|
impl PartialOrd for $Ty {
|
||||||
#[inline]
|
#[inline]
|
||||||
|
|
|
@ -323,6 +323,7 @@
|
||||||
#![feature(float_gamma)]
|
#![feature(float_gamma)]
|
||||||
#![feature(float_minimum_maximum)]
|
#![feature(float_minimum_maximum)]
|
||||||
#![feature(float_next_up_down)]
|
#![feature(float_next_up_down)]
|
||||||
|
#![feature(generic_nonzero)]
|
||||||
#![feature(hasher_prefixfree_extras)]
|
#![feature(hasher_prefixfree_extras)]
|
||||||
#![feature(hashmap_internals)]
|
#![feature(hashmap_internals)]
|
||||||
#![feature(hint_assert_unchecked)]
|
#![feature(hint_assert_unchecked)]
|
||||||
|
|
|
@ -16,6 +16,16 @@ pub use core::num::Wrapping;
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
pub use core::num::{FpCategory, ParseFloatError, ParseIntError, TryFromIntError};
|
pub use core::num::{FpCategory, ParseFloatError, ParseIntError, TryFromIntError};
|
||||||
|
|
||||||
|
#[unstable(
|
||||||
|
feature = "nonzero_internals",
|
||||||
|
reason = "implementation detail which may disappear or be replaced at any time",
|
||||||
|
issue = "none"
|
||||||
|
)]
|
||||||
|
pub use core::num::ZeroablePrimitive;
|
||||||
|
|
||||||
|
#[unstable(feature = "generic_nonzero", issue = "120257")]
|
||||||
|
pub use core::num::NonZero;
|
||||||
|
|
||||||
#[stable(feature = "signed_nonzero", since = "1.34.0")]
|
#[stable(feature = "signed_nonzero", since = "1.34.0")]
|
||||||
pub use core::num::{NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize};
|
pub use core::num::{NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize};
|
||||||
#[stable(feature = "nonzero", since = "1.28.0")]
|
#[stable(feature = "nonzero", since = "1.28.0")]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue