@@ -8,7 +8,6 @@ use crate::convert::Infallible;
88use crate :: fmt;
99use crate :: intrinsics;
1010use crate :: mem;
11- use crate :: ops:: { BitOr , BitOrAssign } ;
1211use crate :: str:: FromStr ;
1312
1413// Used because the `?` operator is not allowed in a const context.
@@ -28,188 +27,13 @@ macro_rules! unlikely {
2827 } ;
2928}
3029
31- macro_rules! impl_nonzero_fmt {
32- ( #[ $stability: meta] ( $( $Trait: ident ) ,+ ) for $Ty: ident ) => {
33- $(
34- #[ $stability]
35- impl fmt:: $Trait for $Ty {
36- #[ inline]
37- fn fmt( & self , f: & mut fmt:: Formatter <' _>) -> fmt:: Result {
38- self . get( ) . fmt( f)
39- }
40- }
41- ) +
42- }
43- }
44-
4530macro_rules! doc_comment {
4631 ( $x: expr, $( $tt: tt) * ) => {
4732 #[ doc = $x]
4833 $( $tt) *
4934 } ;
5035}
5136
52- macro_rules! nonzero_integers {
53- ( $( #[ $stability: meta] $Ty: ident( $Int: ty) ; ) + ) => {
54- $(
55- doc_comment! {
56- concat!( "An integer that is known not to equal zero.
57-
58- This enables some memory layout optimization.
59- For example, `Option<" , stringify!( $Ty) , ">` is the same size as `" , stringify!( $Int) , "`:
60-
61- ```rust
62- use std::mem::size_of;
63- assert_eq!(size_of::<Option<core::num::" , stringify!( $Ty) , ">>(), size_of::<" , stringify!( $Int) ,
64- ">());
65- ```" ) ,
66- #[ $stability]
67- #[ derive( Copy , Clone , Eq , PartialEq , Ord , PartialOrd , Hash ) ]
68- #[ repr( transparent) ]
69- #[ rustc_layout_scalar_valid_range_start( 1 ) ]
70- #[ rustc_nonnull_optimization_guaranteed]
71- pub struct $Ty( $Int) ;
72- }
73-
74- impl $Ty {
75- /// Creates a non-zero without checking the value.
76- ///
77- /// # Safety
78- ///
79- /// The value must not be zero.
80- #[ $stability]
81- #[ rustc_const_stable( feature = "nonzero" , since = "1.34.0" ) ]
82- #[ inline]
83- pub const unsafe fn new_unchecked( n: $Int) -> Self {
84- // SAFETY: this is guaranteed to be safe by the caller.
85- unsafe { Self ( n) }
86- }
87-
88- /// Creates a non-zero if the given value is not zero.
89- #[ $stability]
90- #[ rustc_const_stable( feature = "const_nonzero_int_methods" , since = "1.47.0" ) ]
91- #[ inline]
92- pub const fn new( n: $Int) -> Option <Self > {
93- if n != 0 {
94- // SAFETY: we just checked that there's no `0`
95- Some ( unsafe { Self ( n) } )
96- } else {
97- None
98- }
99- }
100-
101- /// Returns the value as a primitive type.
102- #[ $stability]
103- #[ inline]
104- #[ rustc_const_stable( feature = "nonzero" , since = "1.34.0" ) ]
105- pub const fn get( self ) -> $Int {
106- self . 0
107- }
108-
109- }
110-
111- #[ stable( feature = "from_nonzero" , since = "1.31.0" ) ]
112- impl From <$Ty> for $Int {
113- doc_comment! {
114- concat!(
115- "Converts a `" , stringify!( $Ty) , "` into an `" , stringify!( $Int) , "`" ) ,
116- fn from( nonzero: $Ty) -> Self {
117- nonzero. 0
118- }
119- }
120- }
121-
122- #[ stable( feature = "nonzero_bitor" , since = "1.45.0" ) ]
123- impl BitOr for $Ty {
124- type Output = Self ;
125- #[ inline]
126- fn bitor( self , rhs: Self ) -> Self :: Output {
127- // SAFETY: since `self` and `rhs` are both nonzero, the
128- // result of the bitwise-or will be nonzero.
129- unsafe { $Ty:: new_unchecked( self . get( ) | rhs. get( ) ) }
130- }
131- }
132-
133- #[ stable( feature = "nonzero_bitor" , since = "1.45.0" ) ]
134- impl BitOr <$Int> for $Ty {
135- type Output = Self ;
136- #[ inline]
137- fn bitor( self , rhs: $Int) -> Self :: Output {
138- // SAFETY: since `self` is nonzero, the result of the
139- // bitwise-or will be nonzero regardless of the value of
140- // `rhs`.
141- unsafe { $Ty:: new_unchecked( self . get( ) | rhs) }
142- }
143- }
144-
145- #[ stable( feature = "nonzero_bitor" , since = "1.45.0" ) ]
146- impl BitOr <$Ty> for $Int {
147- type Output = $Ty;
148- #[ inline]
149- fn bitor( self , rhs: $Ty) -> Self :: Output {
150- // SAFETY: since `rhs` is nonzero, the result of the
151- // bitwise-or will be nonzero regardless of the value of
152- // `self`.
153- unsafe { $Ty:: new_unchecked( self | rhs. get( ) ) }
154- }
155- }
156-
157- #[ stable( feature = "nonzero_bitor" , since = "1.45.0" ) ]
158- impl BitOrAssign for $Ty {
159- #[ inline]
160- fn bitor_assign( & mut self , rhs: Self ) {
161- * self = * self | rhs;
162- }
163- }
164-
165- #[ stable( feature = "nonzero_bitor" , since = "1.45.0" ) ]
166- impl BitOrAssign <$Int> for $Ty {
167- #[ inline]
168- fn bitor_assign( & mut self , rhs: $Int) {
169- * self = * self | rhs;
170- }
171- }
172-
173- impl_nonzero_fmt! {
174- #[ $stability] ( Debug , Display , Binary , Octal , LowerHex , UpperHex ) for $Ty
175- }
176- ) +
177- }
178- }
179-
180- nonzero_integers ! {
181- #[ stable( feature = "nonzero" , since = "1.28.0" ) ] NonZeroU8 ( u8 ) ;
182- #[ stable( feature = "nonzero" , since = "1.28.0" ) ] NonZeroU16 ( u16 ) ;
183- #[ stable( feature = "nonzero" , since = "1.28.0" ) ] NonZeroU32 ( u32 ) ;
184- #[ stable( feature = "nonzero" , since = "1.28.0" ) ] NonZeroU64 ( u64 ) ;
185- #[ stable( feature = "nonzero" , since = "1.28.0" ) ] NonZeroU128 ( u128 ) ;
186- #[ stable( feature = "nonzero" , since = "1.28.0" ) ] NonZeroUsize ( usize ) ;
187- #[ stable( feature = "signed_nonzero" , since = "1.34.0" ) ] NonZeroI8 ( i8 ) ;
188- #[ stable( feature = "signed_nonzero" , since = "1.34.0" ) ] NonZeroI16 ( i16 ) ;
189- #[ stable( feature = "signed_nonzero" , since = "1.34.0" ) ] NonZeroI32 ( i32 ) ;
190- #[ stable( feature = "signed_nonzero" , since = "1.34.0" ) ] NonZeroI64 ( i64 ) ;
191- #[ stable( feature = "signed_nonzero" , since = "1.34.0" ) ] NonZeroI128 ( i128 ) ;
192- #[ stable( feature = "signed_nonzero" , since = "1.34.0" ) ] NonZeroIsize ( isize ) ;
193- }
194-
195- macro_rules! from_str_radix_nzint_impl {
196- ( $( $t: ty) * ) => { $(
197- #[ stable( feature = "nonzero_parse" , since = "1.35.0" ) ]
198- impl FromStr for $t {
199- type Err = ParseIntError ;
200- fn from_str( src: & str ) -> Result <Self , Self :: Err > {
201- Self :: new( from_str_radix( src, 10 ) ?)
202- . ok_or( ParseIntError {
203- kind: IntErrorKind :: Zero
204- } )
205- }
206- }
207- ) * }
208- }
209-
210- from_str_radix_nzint_impl ! { NonZeroU8 NonZeroU16 NonZeroU32 NonZeroU64 NonZeroU128 NonZeroUsize
211- NonZeroI8 NonZeroI16 NonZeroI32 NonZeroI64 NonZeroI128 NonZeroIsize }
212-
21337/// Provides intentionally-wrapped arithmetic on `T`.
21438///
21539/// Operations like `+` on `u32` values are intended to never overflow,
@@ -289,8 +113,15 @@ pub mod dec2flt;
289113pub mod diy_float;
290114pub mod flt2dec;
291115
116+ mod nonzero;
292117mod wrapping;
293118
119+ #[ stable( feature = "nonzero" , since = "1.28.0" ) ]
120+ pub use nonzero:: { NonZeroU128 , NonZeroU16 , NonZeroU32 , NonZeroU64 , NonZeroU8 , NonZeroUsize } ;
121+
122+ #[ stable( feature = "signed_nonzero" , since = "1.34.0" ) ]
123+ pub use nonzero:: { NonZeroI128 , NonZeroI16 , NonZeroI32 , NonZeroI64 , NonZeroI8 , NonZeroIsize } ;
124+
294125macro_rules! usize_isize_to_xe_bytes_doc {
295126 ( ) => {
296127 "
0 commit comments