1#[cfg(not(feature = "ferrocene_certified"))]
4use super::{IntErrorKind, ParseIntError};
5#[cfg(not(feature = "ferrocene_certified"))]
6use crate::clone::{TrivialClone, UseCloned};
7#[cfg(not(feature = "ferrocene_certified"))]
8use crate::cmp::Ordering;
9#[cfg(not(feature = "ferrocene_certified"))]
10use crate::hash::{Hash, Hasher};
11#[cfg(not(feature = "ferrocene_certified"))]
12use crate::marker::{Destruct, Freeze, StructuralPartialEq};
13#[cfg(not(feature = "ferrocene_certified"))]
14use crate::ops::{BitOr, BitOrAssign, Div, DivAssign, Neg, Rem, RemAssign};
15#[cfg(not(feature = "ferrocene_certified"))]
16use crate::panic::{RefUnwindSafe, UnwindSafe};
17#[cfg(not(feature = "ferrocene_certified"))]
18use crate::str::FromStr;
19#[cfg(not(feature = "ferrocene_certified"))]
20use crate::{fmt, intrinsics, ptr, ub_checks};
21
22#[cfg(feature = "ferrocene_certified")]
24#[rustfmt::skip]
25use crate::{intrinsics, ub_checks};
26
27#[unstable(
43 feature = "nonzero_internals",
44 reason = "implementation detail which may disappear or be replaced at any time",
45 issue = "none"
46)]
47pub unsafe trait ZeroablePrimitive: Sized + Copy + private::Sealed {
48 #[doc(hidden)]
49 type NonZeroInner: Sized + Copy;
50}
51
52macro_rules! impl_zeroable_primitive {
53 ($($NonZeroInner:ident ( $primitive:ty )),+ $(,)?) => {
54 mod private {
55 #[unstable(
56 feature = "nonzero_internals",
57 reason = "implementation detail which may disappear or be replaced at any time",
58 issue = "none"
59 )]
60 pub trait Sealed {}
61 }
62
63 $(
64 #[unstable(
65 feature = "nonzero_internals",
66 reason = "implementation detail which may disappear or be replaced at any time",
67 issue = "none"
68 )]
69 impl private::Sealed for $primitive {}
70
71 #[unstable(
72 feature = "nonzero_internals",
73 reason = "implementation detail which may disappear or be replaced at any time",
74 issue = "none"
75 )]
76 unsafe impl ZeroablePrimitive for $primitive {
77 type NonZeroInner = super::niche_types::$NonZeroInner;
78 }
79 )+
80 };
81}
82
83impl_zeroable_primitive!(
84 NonZeroU8Inner(u8),
85 NonZeroU16Inner(u16),
86 NonZeroU32Inner(u32),
87 NonZeroU64Inner(u64),
88 NonZeroU128Inner(u128),
89 NonZeroUsizeInner(usize),
90 NonZeroI8Inner(i8),
91 NonZeroI16Inner(i16),
92 NonZeroI32Inner(i32),
93 NonZeroI64Inner(i64),
94 NonZeroI128Inner(i128),
95 NonZeroIsizeInner(isize),
96 NonZeroCharInner(char),
97);
98
99#[stable(feature = "generic_nonzero", since = "1.79.0")]
138#[repr(transparent)]
139#[rustc_nonnull_optimization_guaranteed]
140#[rustc_diagnostic_item = "NonZero"]
141pub struct NonZero<T: ZeroablePrimitive>(T::NonZeroInner);
142
143#[cfg(not(feature = "ferrocene_certified"))]
144macro_rules! impl_nonzero_fmt {
145 ($(#[$Attribute:meta] $Trait:ident)*) => {
146 $(
147 #[$Attribute]
148 impl<T> fmt::$Trait for NonZero<T>
149 where
150 T: ZeroablePrimitive + fmt::$Trait,
151 {
152 #[inline]
153 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
154 self.get().fmt(f)
155 }
156 }
157 )*
158 };
159}
160
161#[cfg(not(feature = "ferrocene_certified"))]
162impl_nonzero_fmt! {
163 #[stable(feature = "nonzero", since = "1.28.0")]
164 Debug
165 #[stable(feature = "nonzero", since = "1.28.0")]
166 Display
167 #[stable(feature = "nonzero", since = "1.28.0")]
168 Binary
169 #[stable(feature = "nonzero", since = "1.28.0")]
170 Octal
171 #[stable(feature = "nonzero", since = "1.28.0")]
172 LowerHex
173 #[stable(feature = "nonzero", since = "1.28.0")]
174 UpperHex
175 #[stable(feature = "nonzero_fmt_exp", since = "1.84.0")]
176 LowerExp
177 #[stable(feature = "nonzero_fmt_exp", since = "1.84.0")]
178 UpperExp
179}
180
181#[cfg(not(feature = "ferrocene_certified"))]
182macro_rules! impl_nonzero_auto_trait {
183 (unsafe $Trait:ident) => {
184 #[stable(feature = "nonzero", since = "1.28.0")]
185 unsafe impl<T> $Trait for NonZero<T> where T: ZeroablePrimitive + $Trait {}
186 };
187 ($Trait:ident) => {
188 #[stable(feature = "nonzero", since = "1.28.0")]
189 impl<T> $Trait for NonZero<T> where T: ZeroablePrimitive + $Trait {}
190 };
191}
192
193#[cfg(not(feature = "ferrocene_certified"))]
196impl_nonzero_auto_trait!(unsafe Freeze);
197#[cfg(not(feature = "ferrocene_certified"))]
198impl_nonzero_auto_trait!(RefUnwindSafe);
199#[cfg(not(feature = "ferrocene_certified"))]
200impl_nonzero_auto_trait!(unsafe Send);
201#[cfg(not(feature = "ferrocene_certified"))]
202impl_nonzero_auto_trait!(unsafe Sync);
203#[cfg(not(feature = "ferrocene_certified"))]
204impl_nonzero_auto_trait!(Unpin);
205#[cfg(not(feature = "ferrocene_certified"))]
206impl_nonzero_auto_trait!(UnwindSafe);
207
208#[stable(feature = "nonzero", since = "1.28.0")]
209impl<T> Clone for NonZero<T>
210where
211 T: ZeroablePrimitive,
212{
213 #[inline]
214 fn clone(&self) -> Self {
215 *self
216 }
217}
218
219#[unstable(feature = "ergonomic_clones", issue = "132290")]
220#[cfg(not(feature = "ferrocene_certified"))]
221impl<T> UseCloned for NonZero<T> where T: ZeroablePrimitive {}
222
223#[stable(feature = "nonzero", since = "1.28.0")]
224impl<T> Copy for NonZero<T> where T: ZeroablePrimitive {}
225
226#[doc(hidden)]
227#[unstable(feature = "trivial_clone", issue = "none")]
228#[cfg(not(feature = "ferrocene_certified"))]
229unsafe impl<T> TrivialClone for NonZero<T> where T: ZeroablePrimitive {}
230
231#[stable(feature = "nonzero", since = "1.28.0")]
232#[cfg(not(feature = "ferrocene_certified"))]
233#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
234impl<T> const PartialEq for NonZero<T>
235where
236 T: ZeroablePrimitive + [const] PartialEq,
237{
238 #[inline]
239 fn eq(&self, other: &Self) -> bool {
240 self.get() == other.get()
241 }
242
243 #[inline]
244 fn ne(&self, other: &Self) -> bool {
245 self.get() != other.get()
246 }
247}
248
249#[unstable(feature = "structural_match", issue = "31434")]
250#[cfg(not(feature = "ferrocene_certified"))]
251impl<T> StructuralPartialEq for NonZero<T> where T: ZeroablePrimitive + StructuralPartialEq {}
252
253#[stable(feature = "nonzero", since = "1.28.0")]
254#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
255#[cfg(not(feature = "ferrocene_certified"))]
256impl<T> const Eq for NonZero<T> where T: ZeroablePrimitive + [const] Eq {}
257
258#[stable(feature = "nonzero", since = "1.28.0")]
259#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
260#[cfg(not(feature = "ferrocene_certified"))]
261impl<T> const PartialOrd for NonZero<T>
262where
263 T: ZeroablePrimitive + [const] PartialOrd,
264{
265 #[inline]
266 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
267 self.get().partial_cmp(&other.get())
268 }
269
270 #[inline]
271 fn lt(&self, other: &Self) -> bool {
272 self.get() < other.get()
273 }
274
275 #[inline]
276 fn le(&self, other: &Self) -> bool {
277 self.get() <= other.get()
278 }
279
280 #[inline]
281 fn gt(&self, other: &Self) -> bool {
282 self.get() > other.get()
283 }
284
285 #[inline]
286 fn ge(&self, other: &Self) -> bool {
287 self.get() >= other.get()
288 }
289}
290
291#[stable(feature = "nonzero", since = "1.28.0")]
292#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
293#[cfg(not(feature = "ferrocene_certified"))]
294impl<T> const Ord for NonZero<T>
295where
296 T: ZeroablePrimitive + [const] Ord + [const] Destruct,
299{
300 #[inline]
301 fn cmp(&self, other: &Self) -> Ordering {
302 self.get().cmp(&other.get())
303 }
304
305 #[inline]
306 fn max(self, other: Self) -> Self {
307 unsafe { Self::new_unchecked(self.get().max(other.get())) }
309 }
310
311 #[inline]
312 fn min(self, other: Self) -> Self {
313 unsafe { Self::new_unchecked(self.get().min(other.get())) }
315 }
316
317 #[inline]
318 fn clamp(self, min: Self, max: Self) -> Self {
319 unsafe { Self::new_unchecked(self.get().clamp(min.get(), max.get())) }
321 }
322}
323
324#[stable(feature = "nonzero", since = "1.28.0")]
325#[cfg(not(feature = "ferrocene_certified"))]
326impl<T> Hash for NonZero<T>
327where
328 T: ZeroablePrimitive + Hash,
329{
330 #[inline]
331 fn hash<H>(&self, state: &mut H)
332 where
333 H: Hasher,
334 {
335 self.get().hash(state)
336 }
337}
338
339#[stable(feature = "from_nonzero", since = "1.31.0")]
340#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
341#[cfg(not(feature = "ferrocene_certified"))]
342impl<T> const From<NonZero<T>> for T
343where
344 T: ZeroablePrimitive,
345{
346 #[inline]
347 fn from(nonzero: NonZero<T>) -> Self {
348 nonzero.get()
350 }
351}
352
353#[stable(feature = "nonzero_bitor", since = "1.45.0")]
354#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
355#[cfg(not(feature = "ferrocene_certified"))]
356impl<T> const BitOr for NonZero<T>
357where
358 T: ZeroablePrimitive + [const] BitOr<Output = T>,
359{
360 type Output = Self;
361
362 #[inline]
363 fn bitor(self, rhs: Self) -> Self::Output {
364 unsafe { Self::new_unchecked(self.get() | rhs.get()) }
366 }
367}
368
369#[stable(feature = "nonzero_bitor", since = "1.45.0")]
370#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
371#[cfg(not(feature = "ferrocene_certified"))]
372impl<T> const BitOr<T> for NonZero<T>
373where
374 T: ZeroablePrimitive + [const] BitOr<Output = T>,
375{
376 type Output = Self;
377
378 #[inline]
379 fn bitor(self, rhs: T) -> Self::Output {
380 unsafe { Self::new_unchecked(self.get() | rhs) }
382 }
383}
384
385#[stable(feature = "nonzero_bitor", since = "1.45.0")]
386#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
387#[cfg(not(feature = "ferrocene_certified"))]
388impl<T> const BitOr<NonZero<T>> for T
389where
390 T: ZeroablePrimitive + [const] BitOr<Output = T>,
391{
392 type Output = NonZero<T>;
393
394 #[inline]
395 fn bitor(self, rhs: NonZero<T>) -> Self::Output {
396 unsafe { NonZero::new_unchecked(self | rhs.get()) }
398 }
399}
400
401#[stable(feature = "nonzero_bitor", since = "1.45.0")]
402#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
403#[cfg(not(feature = "ferrocene_certified"))]
404impl<T> const BitOrAssign for NonZero<T>
405where
406 T: ZeroablePrimitive,
407 Self: [const] BitOr<Output = Self>,
408{
409 #[inline]
410 fn bitor_assign(&mut self, rhs: Self) {
411 *self = *self | rhs;
412 }
413}
414
415#[stable(feature = "nonzero_bitor", since = "1.45.0")]
416#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
417#[cfg(not(feature = "ferrocene_certified"))]
418impl<T> const BitOrAssign<T> for NonZero<T>
419where
420 T: ZeroablePrimitive,
421 Self: [const] BitOr<T, Output = Self>,
422{
423 #[inline]
424 fn bitor_assign(&mut self, rhs: T) {
425 *self = *self | rhs;
426 }
427}
428
429impl<T> NonZero<T>
430where
431 T: ZeroablePrimitive,
432{
433 #[stable(feature = "nonzero", since = "1.28.0")]
435 #[rustc_const_stable(feature = "const_nonzero_int_methods", since = "1.47.0")]
436 #[must_use]
437 #[inline]
438 pub const fn new(n: T) -> Option<Self> {
439 unsafe { intrinsics::transmute_unchecked(n) }
442 }
443
444 #[stable(feature = "nonzero", since = "1.28.0")]
451 #[rustc_const_stable(feature = "nonzero", since = "1.28.0")]
452 #[must_use]
453 #[inline]
454 #[track_caller]
455 pub const unsafe fn new_unchecked(n: T) -> Self {
456 match Self::new(n) {
457 Some(n) => n,
458 None => {
459 unsafe {
461 ub_checks::assert_unsafe_precondition!(
462 check_language_ub,
463 "NonZero::new_unchecked requires the argument to be non-zero",
464 () => false,
465 );
466 intrinsics::unreachable()
467 }
468 }
469 }
470 }
471
472 #[unstable(feature = "nonzero_from_mut", issue = "106290")]
475 #[must_use]
476 #[inline]
477 #[cfg(not(feature = "ferrocene_certified"))]
478 pub fn from_mut(n: &mut T) -> Option<&mut Self> {
479 let opt_n = unsafe { &mut *(ptr::from_mut(n).cast::<Option<Self>>()) };
482
483 opt_n.as_mut()
484 }
485
486 #[unstable(feature = "nonzero_from_mut", issue = "106290")]
494 #[must_use]
495 #[inline]
496 #[track_caller]
497 #[cfg(not(feature = "ferrocene_certified"))]
498 pub unsafe fn from_mut_unchecked(n: &mut T) -> &mut Self {
499 match Self::from_mut(n) {
500 Some(n) => n,
501 None => {
502 unsafe {
504 ub_checks::assert_unsafe_precondition!(
505 check_library_ub,
506 "NonZero::from_mut_unchecked requires the argument to dereference as non-zero",
507 () => false,
508 );
509 intrinsics::unreachable()
510 }
511 }
512 }
513 }
514
515 #[stable(feature = "nonzero", since = "1.28.0")]
517 #[rustc_const_stable(feature = "const_nonzero_get", since = "1.34.0")]
518 #[inline]
519 pub const fn get(self) -> T {
520 unsafe { intrinsics::transmute_unchecked(self) }
539 }
540}
541
542#[cfg(not(feature = "ferrocene_certified"))]
543macro_rules! nonzero_integer {
544 (
545 #[$stability:meta]
546 Self = $Ty:ident,
547 Primitive = $signedness:ident $Int:ident,
548 SignedPrimitive = $Sint:ty,
549 UnsignedPrimitive = $Uint:ty,
550
551 rot = $rot:literal,
553 rot_op = $rot_op:literal,
554 rot_result = $rot_result:literal,
555 swap_op = $swap_op:literal,
556 swapped = $swapped:literal,
557 reversed = $reversed:literal,
558 leading_zeros_test = $leading_zeros_test:expr,
559 ) => {
560 #[doc = sign_dependent_expr!{
561 $signedness ?
562 if signed {
563 concat!("An [`", stringify!($Int), "`] that is known not to equal zero.")
564 }
565 if unsigned {
566 concat!("A [`", stringify!($Int), "`] that is known not to equal zero.")
567 }
568 }]
569 #[doc = concat!("For example, `Option<", stringify!($Ty), ">` is the same size as `", stringify!($Int), "`:")]
572 #[doc = concat!("assert_eq!(size_of::<Option<core::num::", stringify!($Ty), ">>(), size_of::<", stringify!($Int), ">());")]
575 #[doc = concat!("`", stringify!($Ty), "` is guaranteed to have the same layout and bit validity as `", stringify!($Int), "`")]
580 #[doc = concat!("`Option<", stringify!($Ty), ">` is guaranteed to be compatible with `", stringify!($Int), "`,")]
582 #[doc = concat!("`", stringify!($Ty), "` and `Option<", stringify!($Ty), ">`")]
586 #[doc = concat!("use std::num::", stringify!($Ty), ";")]
590 #[doc = concat!("assert_eq!(size_of::<", stringify!($Ty), ">(), size_of::<Option<", stringify!($Ty), ">>());")]
592 #[doc = concat!("assert_eq!(align_of::<", stringify!($Ty), ">(), align_of::<Option<", stringify!($Ty), ">>());")]
593 #[doc = concat!("`", stringify!($Ty), "`")]
600 #[doc = concat!("use std::num::", stringify!($Ty), ";")]
603 #[doc = concat!("const TEN: ", stringify!($Ty), " = ", stringify!($Ty) , r#"::new(10).expect("ten is non-zero");"#)]
605 #[$stability]
609 pub type $Ty = NonZero<$Int>;
610
611 impl NonZero<$Int> {
612 #[doc = concat!("This value is equal to [`", stringify!($Int), "::BITS`].")]
615 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::BITS, ", stringify!($Int), "::BITS);")]
622 #[stable(feature = "nonzero_bits", since = "1.67.0")]
624 pub const BITS: u32 = <$Int>::BITS;
625
626 #[doc = concat!("let n = NonZero::<", stringify!($Int), ">::new(", $leading_zeros_test, ")?;")]
638 #[stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
644 #[rustc_const_stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
645 #[must_use = "this returns the result of the operation, \
646 without modifying the original"]
647 #[inline]
648 pub const fn leading_zeros(self) -> u32 {
649 unsafe {
651 intrinsics::ctlz_nonzero(self.get() as $Uint)
652 }
653 }
654
655 #[doc = concat!("let n = NonZero::<", stringify!($Int), ">::new(0b0101000)?;")]
668 #[stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
674 #[rustc_const_stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
675 #[must_use = "this returns the result of the operation, \
676 without modifying the original"]
677 #[inline]
678 pub const fn trailing_zeros(self) -> u32 {
679 unsafe {
681 intrinsics::cttz_nonzero(self.get() as $Uint)
682 }
683 }
684
685 #[doc = concat!("let a = NonZero::<", stringify!($Int), ">::new(0b_01100100)?;")]
696 #[doc = concat!("let b = NonZero::<", stringify!($Int), ">::new(0b_01000000)?;")]
697 #[unstable(feature = "isolate_most_least_significant_one", issue = "136909")]
703 #[must_use = "this returns the result of the operation, \
704 without modifying the original"]
705 #[inline(always)]
706 pub const fn isolate_highest_one(self) -> Self {
707 unsafe {
713 let bit = (((1 as $Uint) << (<$Uint>::BITS - 1)).unchecked_shr(self.leading_zeros()));
714 NonZero::new_unchecked(bit as $Int)
715 }
716 }
717
718 #[doc = concat!("let a = NonZero::<", stringify!($Int), ">::new(0b_01100100)?;")]
729 #[doc = concat!("let b = NonZero::<", stringify!($Int), ">::new(0b_00000100)?;")]
730 #[unstable(feature = "isolate_most_least_significant_one", issue = "136909")]
736 #[must_use = "this returns the result of the operation, \
737 without modifying the original"]
738 #[inline(always)]
739 pub const fn isolate_lowest_one(self) -> Self {
740 let n = self.get();
741 let n = n & n.wrapping_neg();
742
743 unsafe { NonZero::new_unchecked(n) }
746 }
747
748 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1)?.highest_one(), 0);")]
759 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1_0000)?.highest_one(), 4);")]
760 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1_1111)?.highest_one(), 4);")]
761 #[unstable(feature = "int_lowest_highest_one", issue = "145203")]
765 #[must_use = "this returns the result of the operation, \
766 without modifying the original"]
767 #[inline(always)]
768 pub const fn highest_one(self) -> u32 {
769 Self::BITS - 1 - self.leading_zeros()
770 }
771
772 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1)?.lowest_one(), 0);")]
783 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1_0000)?.lowest_one(), 4);")]
784 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1_1111)?.lowest_one(), 0);")]
785 #[unstable(feature = "int_lowest_highest_one", issue = "145203")]
789 #[must_use = "this returns the result of the operation, \
790 without modifying the original"]
791 #[inline(always)]
792 pub const fn lowest_one(self) -> u32 {
793 self.trailing_zeros()
794 }
795
796 #[doc = concat!("let a = NonZero::<", stringify!($Int), ">::new(0b100_0000)?;")]
806 #[doc = concat!("let b = NonZero::<", stringify!($Int), ">::new(0b100_0011)?;")]
807 #[stable(feature = "non_zero_count_ones", since = "1.86.0")]
815 #[rustc_const_stable(feature = "non_zero_count_ones", since = "1.86.0")]
816 #[doc(alias = "popcount")]
817 #[doc(alias = "popcnt")]
818 #[must_use = "this returns the result of the operation, \
819 without modifying the original"]
820 #[inline(always)]
821 pub const fn count_ones(self) -> NonZero<u32> {
822 unsafe { NonZero::new_unchecked(self.get().count_ones()) }
826 }
827
828 #[doc = concat!("let n = NonZero::new(", $rot_op, stringify!($Int), ")?;")]
842 #[doc = concat!("let m = NonZero::new(", $rot_result, ")?;")]
843 #[doc = concat!("assert_eq!(n.rotate_left(", $rot, "), m);")]
845 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
849 #[must_use = "this returns the result of the operation, \
850 without modifying the original"]
851 #[inline(always)]
852 pub const fn rotate_left(self, n: u32) -> Self {
853 let result = self.get().rotate_left(n);
854 unsafe { Self::new_unchecked(result) }
856 }
857
858 #[doc = concat!("let n = NonZero::new(", $rot_result, stringify!($Int), ")?;")]
873 #[doc = concat!("let m = NonZero::new(", $rot_op, ")?;")]
874 #[doc = concat!("assert_eq!(n.rotate_right(", $rot, "), m);")]
876 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
880 #[must_use = "this returns the result of the operation, \
881 without modifying the original"]
882 #[inline(always)]
883 pub const fn rotate_right(self, n: u32) -> Self {
884 let result = self.get().rotate_right(n);
885 unsafe { Self::new_unchecked(result) }
887 }
888
889 #[doc = concat!("let n = NonZero::new(", $swap_op, stringify!($Int), ")?;")]
900 #[doc = concat!("assert_eq!(m, NonZero::new(", $swapped, ")?);")]
903 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
907 #[must_use = "this returns the result of the operation, \
908 without modifying the original"]
909 #[inline(always)]
910 pub const fn swap_bytes(self) -> Self {
911 let result = self.get().swap_bytes();
912 unsafe { Self::new_unchecked(result) }
914 }
915
916 #[doc = concat!("let n = NonZero::new(", $swap_op, stringify!($Int), ")?;")]
928 #[doc = concat!("assert_eq!(m, NonZero::new(", $reversed, ")?);")]
931 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
935 #[must_use = "this returns the result of the operation, \
936 without modifying the original"]
937 #[inline(always)]
938 pub const fn reverse_bits(self) -> Self {
939 let result = self.get().reverse_bits();
940 unsafe { Self::new_unchecked(result) }
942 }
943
944 #[doc = concat!("use std::num::", stringify!($Ty), ";")]
955 #[doc = concat!("let n = NonZero::new(0x1A", stringify!($Int), ")?;")]
959 #[doc = concat!(" assert_eq!(", stringify!($Ty), "::from_be(n), n)")]
962 #[doc = concat!(" assert_eq!(", stringify!($Ty), "::from_be(n), n.swap_bytes())")]
964 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
969 #[must_use]
970 #[inline(always)]
971 pub const fn from_be(x: Self) -> Self {
972 let result = $Int::from_be(x.get());
973 unsafe { Self::new_unchecked(result) }
975 }
976
977 #[doc = concat!("use std::num::", stringify!($Ty), ";")]
988 #[doc = concat!("let n = NonZero::new(0x1A", stringify!($Int), ")?;")]
992 #[doc = concat!(" assert_eq!(", stringify!($Ty), "::from_le(n), n)")]
995 #[doc = concat!(" assert_eq!(", stringify!($Ty), "::from_le(n), n.swap_bytes())")]
997 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
1002 #[must_use]
1003 #[inline(always)]
1004 pub const fn from_le(x: Self) -> Self {
1005 let result = $Int::from_le(x.get());
1006 unsafe { Self::new_unchecked(result) }
1008 }
1009
1010 #[doc = concat!("let n = NonZero::new(0x1A", stringify!($Int), ")?;")]
1024 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
1034 #[must_use = "this returns the result of the operation, \
1035 without modifying the original"]
1036 #[inline(always)]
1037 pub const fn to_be(self) -> Self {
1038 let result = self.get().to_be();
1039 unsafe { Self::new_unchecked(result) }
1041 }
1042
1043 #[doc = concat!("let n = NonZero::new(0x1A", stringify!($Int), ")?;")]
1057 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
1067 #[must_use = "this returns the result of the operation, \
1068 without modifying the original"]
1069 #[inline(always)]
1070 pub const fn to_le(self) -> Self {
1071 let result = self.get().to_le();
1072 unsafe { Self::new_unchecked(result) }
1074 }
1075
1076 nonzero_integer_signedness_dependent_methods! {
1077 Primitive = $signedness $Int,
1078 SignedPrimitive = $Sint,
1079 UnsignedPrimitive = $Uint,
1080 }
1081
1082 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1094 #[doc = concat!("let four = NonZero::new(4", stringify!($Int), ")?;")]
1095 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
1096 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1103 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1104 #[must_use = "this returns the result of the operation, \
1105 without modifying the original"]
1106 #[inline]
1107 pub const fn checked_mul(self, other: Self) -> Option<Self> {
1108 if let Some(result) = self.get().checked_mul(other.get()) {
1109 Some(unsafe { Self::new_unchecked(result) })
1117 } else {
1118 None
1119 }
1120 }
1121
1122 #[doc = concat!("Return [`NonZero::<", stringify!($Int), ">::MAX`] on overflow.")]
1124 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1133 #[doc = concat!("let four = NonZero::new(4", stringify!($Int), ")?;")]
1134 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
1135 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1142 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1143 #[must_use = "this returns the result of the operation, \
1144 without modifying the original"]
1145 #[inline]
1146 pub const fn saturating_mul(self, other: Self) -> Self {
1147 unsafe { Self::new_unchecked(self.get().saturating_mul(other.get())) }
1156 }
1157
1158 #[doc = sign_dependent_expr!{
1164 $signedness ?
1165 if signed {
1166 concat!("`self * rhs > ", stringify!($Int), "::MAX`, ",
1167 "or `self * rhs < ", stringify!($Int), "::MIN`.")
1168 }
1169 if unsigned {
1170 concat!("`self * rhs > ", stringify!($Int), "::MAX`.")
1171 }
1172 }]
1173 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1184 #[doc = concat!("let four = NonZero::new(4", stringify!($Int), ")?;")]
1185 #[unstable(feature = "nonzero_ops", issue = "84186")]
1191 #[must_use = "this returns the result of the operation, \
1192 without modifying the original"]
1193 #[inline]
1194 pub const unsafe fn unchecked_mul(self, other: Self) -> Self {
1195 unsafe { Self::new_unchecked(self.get().unchecked_mul(other.get())) }
1197 }
1198
1199 #[doc = concat!("let three = NonZero::new(3", stringify!($Int), ")?;")]
1211 #[doc = concat!("let twenty_seven = NonZero::new(27", stringify!($Int), ")?;")]
1212 #[doc = concat!("let half_max = NonZero::new(", stringify!($Int), "::MAX / 2)?;")]
1213 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1220 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1221 #[must_use = "this returns the result of the operation, \
1222 without modifying the original"]
1223 #[inline]
1224 pub const fn checked_pow(self, other: u32) -> Option<Self> {
1225 if let Some(result) = self.get().checked_pow(other) {
1226 Some(unsafe { Self::new_unchecked(result) })
1234 } else {
1235 None
1236 }
1237 }
1238
1239 #[doc = sign_dependent_expr!{
1241 $signedness ?
1242 if signed {
1243 concat!("Return [`NonZero::<", stringify!($Int), ">::MIN`] ",
1244 "or [`NonZero::<", stringify!($Int), ">::MAX`] on overflow.")
1245 }
1246 if unsigned {
1247 concat!("Return [`NonZero::<", stringify!($Int), ">::MAX`] on overflow.")
1248 }
1249 }]
1250 #[doc = concat!("let three = NonZero::new(3", stringify!($Int), ")?;")]
1259 #[doc = concat!("let twenty_seven = NonZero::new(27", stringify!($Int), ")?;")]
1260 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
1261 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1268 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1269 #[must_use = "this returns the result of the operation, \
1270 without modifying the original"]
1271 #[inline]
1272 pub const fn saturating_pow(self, other: u32) -> Self {
1273 unsafe { Self::new_unchecked(self.get().saturating_pow(other)) }
1282 }
1283 }
1284
1285 #[stable(feature = "nonzero_parse", since = "1.35.0")]
1286 impl FromStr for NonZero<$Int> {
1287 type Err = ParseIntError;
1288 fn from_str(src: &str) -> Result<Self, Self::Err> {
1289 Self::new(<$Int>::from_str_radix(src, 10)?)
1290 .ok_or(ParseIntError {
1291 kind: IntErrorKind::Zero
1292 })
1293 }
1294 }
1295
1296 nonzero_integer_signedness_dependent_impls!($signedness $Int);
1297 };
1298
1299 (
1300 Self = $Ty:ident,
1301 Primitive = unsigned $Int:ident,
1302 SignedPrimitive = $Sint:ident,
1303 rot = $rot:literal,
1304 rot_op = $rot_op:literal,
1305 rot_result = $rot_result:literal,
1306 swap_op = $swap_op:literal,
1307 swapped = $swapped:literal,
1308 reversed = $reversed:literal,
1309 $(,)?
1310 ) => {
1311 nonzero_integer! {
1312 #[stable(feature = "nonzero", since = "1.28.0")]
1313 Self = $Ty,
1314 Primitive = unsigned $Int,
1315 SignedPrimitive = $Sint,
1316 UnsignedPrimitive = $Int,
1317 rot = $rot,
1318 rot_op = $rot_op,
1319 rot_result = $rot_result,
1320 swap_op = $swap_op,
1321 swapped = $swapped,
1322 reversed = $reversed,
1323 leading_zeros_test = concat!(stringify!($Int), "::MAX"),
1324 }
1325 };
1326
1327 (
1328 Self = $Ty:ident,
1329 Primitive = signed $Int:ident,
1330 UnsignedPrimitive = $Uint:ident,
1331 rot = $rot:literal,
1332 rot_op = $rot_op:literal,
1333 rot_result = $rot_result:literal,
1334 swap_op = $swap_op:literal,
1335 swapped = $swapped:literal,
1336 reversed = $reversed:literal,
1337 ) => {
1338 nonzero_integer! {
1339 #[stable(feature = "signed_nonzero", since = "1.34.0")]
1340 Self = $Ty,
1341 Primitive = signed $Int,
1342 SignedPrimitive = $Int,
1343 UnsignedPrimitive = $Uint,
1344 rot = $rot,
1345 rot_op = $rot_op,
1346 rot_result = $rot_result,
1347 swap_op = $swap_op,
1348 swapped = $swapped,
1349 reversed = $reversed,
1350 leading_zeros_test = concat!("-1", stringify!($Int)),
1351 }
1352 };
1353}
1354
1355#[cfg(not(feature = "ferrocene_certified"))]
1356macro_rules! nonzero_integer_signedness_dependent_impls {
1357 (unsigned $Int:ty) => {
1359 #[stable(feature = "nonzero_div", since = "1.51.0")]
1360 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1361 impl const Div<NonZero<$Int>> for $Int {
1362 type Output = $Int;
1363
1364 #[doc(alias = "unchecked_div")]
1370 #[inline]
1371 fn div(self, other: NonZero<$Int>) -> $Int {
1372 unsafe { intrinsics::unchecked_div(self, other.get()) }
1375 }
1376 }
1377
1378 #[stable(feature = "nonzero_div_assign", since = "1.79.0")]
1379 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1380 impl const DivAssign<NonZero<$Int>> for $Int {
1381 #[inline]
1387 fn div_assign(&mut self, other: NonZero<$Int>) {
1388 *self = *self / other;
1389 }
1390 }
1391
1392 #[stable(feature = "nonzero_div", since = "1.51.0")]
1393 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1394 impl const Rem<NonZero<$Int>> for $Int {
1395 type Output = $Int;
1396
1397 #[inline]
1399 fn rem(self, other: NonZero<$Int>) -> $Int {
1400 unsafe { intrinsics::unchecked_rem(self, other.get()) }
1403 }
1404 }
1405
1406 #[stable(feature = "nonzero_div_assign", since = "1.79.0")]
1407 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1408 impl const RemAssign<NonZero<$Int>> for $Int {
1409 #[inline]
1411 fn rem_assign(&mut self, other: NonZero<$Int>) {
1412 *self = *self % other;
1413 }
1414 }
1415
1416 impl NonZero<$Int> {
1417 #[doc = concat!("let one = NonZero::new(1", stringify!($Int), ").unwrap();")]
1426 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX).unwrap();")]
1427 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ").unwrap();")]
1430 #[doc = concat!("let three = NonZero::new(3", stringify!($Int), ").unwrap();")]
1431 #[stable(feature = "unsigned_nonzero_div_ceil", since = "1.92.0")]
1434 #[rustc_const_stable(feature = "unsigned_nonzero_div_ceil", since = "1.92.0")]
1435 #[must_use = "this returns the result of the operation, \
1436 without modifying the original"]
1437 #[inline]
1438 pub const fn div_ceil(self, rhs: Self) -> Self {
1439 let v = self.get().div_ceil(rhs.get());
1440 unsafe { Self::new_unchecked(v) }
1442 }
1443 }
1444 };
1445 (signed $Int:ty) => {
1447 #[stable(feature = "signed_nonzero_neg", since = "1.71.0")]
1448 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1449 impl const Neg for NonZero<$Int> {
1450 type Output = Self;
1451
1452 #[inline]
1453 fn neg(self) -> Self {
1454 unsafe { Self::new_unchecked(self.get().neg()) }
1456 }
1457 }
1458
1459 forward_ref_unop! { impl Neg, neg for NonZero<$Int>,
1460 #[stable(feature = "signed_nonzero_neg", since = "1.71.0")]
1461 #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
1462 };
1463}
1464
1465#[rustfmt::skip] #[cfg(not(feature = "ferrocene_certified"))]
1467macro_rules! nonzero_integer_signedness_dependent_methods {
1468 (
1470 Primitive = unsigned $Int:ident,
1471 SignedPrimitive = $Sint:ty,
1472 UnsignedPrimitive = $Uint:ty,
1473 ) => {
1474 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::MIN.get(), 1", stringify!($Int), ");")]
1483 #[stable(feature = "nonzero_min_max", since = "1.70.0")]
1485 pub const MIN: Self = Self::new(1).unwrap();
1486
1487 #[doc = concat!("equal to [`", stringify!($Int), "::MAX`].")]
1490 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::MAX.get(), ", stringify!($Int), "::MAX);")]
1497 #[stable(feature = "nonzero_min_max", since = "1.70.0")]
1499 pub const MAX: Self = Self::new(<$Int>::MAX).unwrap();
1500
1501 #[doc = concat!("let one = NonZero::new(1", stringify!($Int), ")?;")]
1514 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1515 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
1516 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1523 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1524 #[must_use = "this returns the result of the operation, \
1525 without modifying the original"]
1526 #[inline]
1527 pub const fn checked_add(self, other: $Int) -> Option<Self> {
1528 if let Some(result) = self.get().checked_add(other) {
1529 Some(unsafe { Self::new_unchecked(result) })
1537 } else {
1538 None
1539 }
1540 }
1541
1542 #[doc = concat!("Return [`NonZero::<", stringify!($Int), ">::MAX`] on overflow.")]
1544 #[doc = concat!("let one = NonZero::new(1", stringify!($Int), ")?;")]
1553 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1554 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
1555 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1562 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1563 #[must_use = "this returns the result of the operation, \
1564 without modifying the original"]
1565 #[inline]
1566 pub const fn saturating_add(self, other: $Int) -> Self {
1567 unsafe { Self::new_unchecked(self.get().saturating_add(other)) }
1575 }
1576
1577 #[doc = concat!("`self + rhs > ", stringify!($Int), "::MAX`.")]
1583 #[doc = concat!("let one = NonZero::new(1", stringify!($Int), ")?;")]
1594 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1595 #[unstable(feature = "nonzero_ops", issue = "84186")]
1601 #[must_use = "this returns the result of the operation, \
1602 without modifying the original"]
1603 #[inline]
1604 pub const unsafe fn unchecked_add(self, other: $Int) -> Self {
1605 unsafe { Self::new_unchecked(self.get().unchecked_add(other)) }
1607 }
1608
1609 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1622 #[doc = concat!("let three = NonZero::new(3", stringify!($Int), ")?;")]
1623 #[doc = concat!("let four = NonZero::new(4", stringify!($Int), ")?;")]
1624 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
1625 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1633 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1634 #[must_use = "this returns the result of the operation, \
1635 without modifying the original"]
1636 #[inline]
1637 pub const fn checked_next_power_of_two(self) -> Option<Self> {
1638 if let Some(nz) = self.get().checked_next_power_of_two() {
1639 Some(unsafe { Self::new_unchecked(nz) })
1642 } else {
1643 None
1644 }
1645 }
1646
1647 #[doc = concat!("[`", stringify!($Int), "::ilog2`],")]
1651 #[doc = concat!("assert_eq!(NonZero::new(7", stringify!($Int), ")?.ilog2(), 2);")]
1662 #[doc = concat!("assert_eq!(NonZero::new(8", stringify!($Int), ")?.ilog2(), 3);")]
1663 #[doc = concat!("assert_eq!(NonZero::new(9", stringify!($Int), ")?.ilog2(), 3);")]
1664 #[stable(feature = "int_log", since = "1.67.0")]
1668 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
1669 #[must_use = "this returns the result of the operation, \
1670 without modifying the original"]
1671 #[inline]
1672 pub const fn ilog2(self) -> u32 {
1673 Self::BITS - 1 - self.leading_zeros()
1674 }
1675
1676 #[doc = concat!("[`", stringify!($Int), "::ilog10`],")]
1680 #[doc = concat!("assert_eq!(NonZero::new(99", stringify!($Int), ")?.ilog10(), 1);")]
1691 #[doc = concat!("assert_eq!(NonZero::new(100", stringify!($Int), ")?.ilog10(), 2);")]
1692 #[doc = concat!("assert_eq!(NonZero::new(101", stringify!($Int), ")?.ilog10(), 2);")]
1693 #[stable(feature = "int_log", since = "1.67.0")]
1697 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
1698 #[must_use = "this returns the result of the operation, \
1699 without modifying the original"]
1700 #[inline]
1701 pub const fn ilog10(self) -> u32 {
1702 super::int_log10::$Int(self.get())
1703 }
1704
1705 #[doc = concat!("let one = NonZero::new(1", stringify!($Int), ")?;")]
1719 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1720 #[doc = concat!("let four = NonZero::new(4", stringify!($Int), ")?;")]
1721 #[stable(feature = "num_midpoint", since = "1.85.0")]
1728 #[rustc_const_stable(feature = "num_midpoint", since = "1.85.0")]
1729 #[must_use = "this returns the result of the operation, \
1730 without modifying the original"]
1731 #[doc(alias = "average_floor")]
1732 #[doc(alias = "average")]
1733 #[inline]
1734 pub const fn midpoint(self, rhs: Self) -> Self {
1735 unsafe { Self::new_unchecked(self.get().midpoint(rhs.get())) }
1740 }
1741
1742 #[doc = concat!("let eight = NonZero::new(8", stringify!($Int), ")?;")]
1755 #[doc = concat!("let ten = NonZero::new(10", stringify!($Int), ")?;")]
1757 #[must_use]
1762 #[stable(feature = "nonzero_is_power_of_two", since = "1.59.0")]
1763 #[rustc_const_stable(feature = "nonzero_is_power_of_two", since = "1.59.0")]
1764 #[inline]
1765 pub const fn is_power_of_two(self) -> bool {
1766 intrinsics::ctpop(self.get()) < 2
1772 }
1773
1774 #[doc = concat!("let ten = NonZero::new(10", stringify!($Int), ")?;")]
1784 #[doc = concat!("let three = NonZero::new(3", stringify!($Int), ")?;")]
1785 #[stable(feature = "isqrt", since = "1.84.0")]
1791 #[rustc_const_stable(feature = "isqrt", since = "1.84.0")]
1792 #[must_use = "this returns the result of the operation, \
1793 without modifying the original"]
1794 #[inline]
1795 pub const fn isqrt(self) -> Self {
1796 let result = self.get().isqrt();
1797
1798 unsafe { Self::new_unchecked(result) }
1804 }
1805
1806 #[doc = concat!("let n = NonZero::<", stringify!($Int), ">::MAX;")]
1814 #[doc = concat!("assert_eq!(n.cast_signed(), NonZero::new(-1", stringify!($Sint), ").unwrap());")]
1816 #[stable(feature = "integer_sign_cast", since = "1.87.0")]
1818 #[rustc_const_stable(feature = "integer_sign_cast", since = "1.87.0")]
1819 #[must_use = "this returns the result of the operation, \
1820 without modifying the original"]
1821 #[inline(always)]
1822 pub const fn cast_signed(self) -> NonZero<$Sint> {
1823 unsafe { NonZero::new_unchecked(self.get().cast_signed()) }
1825 }
1826 };
1827
1828 (
1830 Primitive = signed $Int:ident,
1831 SignedPrimitive = $Sint:ty,
1832 UnsignedPrimitive = $Uint:ty,
1833 ) => {
1834 #[doc = concat!("equal to [`", stringify!($Int), "::MIN`].")]
1837 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::MIN.get(), ", stringify!($Int), "::MIN);")]
1848 #[stable(feature = "nonzero_min_max", since = "1.70.0")]
1850 pub const MIN: Self = Self::new(<$Int>::MIN).unwrap();
1851
1852 #[doc = concat!("equal to [`", stringify!($Int), "::MAX`].")]
1855 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::MAX.get(), ", stringify!($Int), "::MAX);")]
1866 #[stable(feature = "nonzero_min_max", since = "1.70.0")]
1868 pub const MAX: Self = Self::new(<$Int>::MAX).unwrap();
1869
1870 #[doc = concat!("See [`", stringify!($Int), "::abs`]")]
1872 #[doc = concat!("let pos = NonZero::new(1", stringify!($Int), ")?;")]
1882 #[doc = concat!("let neg = NonZero::new(-1", stringify!($Int), ")?;")]
1883 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1890 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1891 #[must_use = "this returns the result of the operation, \
1892 without modifying the original"]
1893 #[inline]
1894 pub const fn abs(self) -> Self {
1895 unsafe { Self::new_unchecked(self.get().abs()) }
1897 }
1898
1899 #[doc = concat!("`self == NonZero::<", stringify!($Int), ">::MIN`.")]
1902 #[doc = concat!("let pos = NonZero::new(1", stringify!($Int), ")?;")]
1912 #[doc = concat!("let neg = NonZero::new(-1", stringify!($Int), ")?;")]
1913 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
1914 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1921 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1922 #[must_use = "this returns the result of the operation, \
1923 without modifying the original"]
1924 #[inline]
1925 pub const fn checked_abs(self) -> Option<Self> {
1926 if let Some(nz) = self.get().checked_abs() {
1927 Some(unsafe { Self::new_unchecked(nz) })
1929 } else {
1930 None
1931 }
1932 }
1933
1934 #[doc = concat!("[`", stringify!($Int), "::overflowing_abs`].")]
1937 #[doc = concat!("let pos = NonZero::new(1", stringify!($Int), ")?;")]
1946 #[doc = concat!("let neg = NonZero::new(-1", stringify!($Int), ")?;")]
1947 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
1948 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1956 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1957 #[must_use = "this returns the result of the operation, \
1958 without modifying the original"]
1959 #[inline]
1960 pub const fn overflowing_abs(self) -> (Self, bool) {
1961 let (nz, flag) = self.get().overflowing_abs();
1962 (
1963 unsafe { Self::new_unchecked(nz) },
1965 flag,
1966 )
1967 }
1968
1969 #[doc = concat!("[`", stringify!($Int), "::saturating_abs`].")]
1971 #[doc = concat!("let pos = NonZero::new(1", stringify!($Int), ")?;")]
1980 #[doc = concat!("let neg = NonZero::new(-1", stringify!($Int), ")?;")]
1981 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
1982 #[doc = concat!("let min_plus = NonZero::new(", stringify!($Int), "::MIN + 1)?;")]
1983 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
1984 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1993 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1994 #[must_use = "this returns the result of the operation, \
1995 without modifying the original"]
1996 #[inline]
1997 pub const fn saturating_abs(self) -> Self {
1998 unsafe { Self::new_unchecked(self.get().saturating_abs()) }
2000 }
2001
2002 #[doc = concat!("[`", stringify!($Int), "::wrapping_abs`].")]
2004 #[doc = concat!("let pos = NonZero::new(1", stringify!($Int), ")?;")]
2013 #[doc = concat!("let neg = NonZero::new(-1", stringify!($Int), ")?;")]
2014 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2015 #[doc = concat!("# let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
2016 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
2025 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
2026 #[must_use = "this returns the result of the operation, \
2027 without modifying the original"]
2028 #[inline]
2029 pub const fn wrapping_abs(self) -> Self {
2030 unsafe { Self::new_unchecked(self.get().wrapping_abs()) }
2032 }
2033
2034 #[doc = concat!("let u_pos = NonZero::new(1", stringify!($Uint), ")?;")]
2045 #[doc = concat!("let i_pos = NonZero::new(1", stringify!($Int), ")?;")]
2046 #[doc = concat!("let i_neg = NonZero::new(-1", stringify!($Int), ")?;")]
2047 #[doc = concat!("let i_min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2048 #[doc = concat!("let u_max = NonZero::new(", stringify!($Uint), "::MAX / 2 + 1)?;")]
2049 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
2057 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
2058 #[must_use = "this returns the result of the operation, \
2059 without modifying the original"]
2060 #[inline]
2061 pub const fn unsigned_abs(self) -> NonZero<$Uint> {
2062 unsafe { NonZero::new_unchecked(self.get().unsigned_abs()) }
2064 }
2065
2066 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2077 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2078 #[must_use]
2085 #[inline]
2086 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2087 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2088 pub const fn is_positive(self) -> bool {
2089 self.get().is_positive()
2090 }
2091
2092 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2103 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2104 #[must_use]
2111 #[inline]
2112 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2113 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2114 pub const fn is_negative(self) -> bool {
2115 self.get().is_negative()
2116 }
2117
2118 #[doc = concat!("returning `None` if `self == NonZero::<", stringify!($Int), ">::MIN`.")]
2120 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2129 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2130 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2131 #[inline]
2138 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2139 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2140 pub const fn checked_neg(self) -> Option<Self> {
2141 if let Some(result) = self.get().checked_neg() {
2142 return Some(unsafe { Self::new_unchecked(result) });
2144 }
2145 None
2146 }
2147
2148 #[doc = concat!("See [`", stringify!($Int), "::overflowing_neg`]")]
2151 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2161 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2162 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2163 #[inline]
2170 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2171 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2172 pub const fn overflowing_neg(self) -> (Self, bool) {
2173 let (result, overflow) = self.get().overflowing_neg();
2174 ((unsafe { Self::new_unchecked(result) }), overflow)
2176 }
2177
2178 #[doc = concat!("returning [`NonZero::<", stringify!($Int), ">::MAX`]")]
2180 #[doc = concat!("if `self == NonZero::<", stringify!($Int), ">::MIN`")]
2181 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2191 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2192 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2193 #[doc = concat!("let min_plus_one = NonZero::new(", stringify!($Int), "::MIN + 1)?;")]
2194 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
2195 #[inline]
2203 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2204 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2205 pub const fn saturating_neg(self) -> Self {
2206 if let Some(result) = self.checked_neg() {
2207 return result;
2208 }
2209 Self::MAX
2210 }
2211
2212 #[doc = concat!("See [`", stringify!($Int), "::wrapping_neg`]")]
2216 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2226 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2227 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2228 #[inline]
2235 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2236 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2237 pub const fn wrapping_neg(self) -> Self {
2238 let result = self.get().wrapping_neg();
2239 unsafe { Self::new_unchecked(result) }
2241 }
2242
2243 #[doc = concat!("let n = NonZero::new(-1", stringify!($Int), ").unwrap();")]
2251 #[doc = concat!("assert_eq!(n.cast_unsigned(), NonZero::<", stringify!($Uint), ">::MAX);")]
2253 #[stable(feature = "integer_sign_cast", since = "1.87.0")]
2255 #[rustc_const_stable(feature = "integer_sign_cast", since = "1.87.0")]
2256 #[must_use = "this returns the result of the operation, \
2257 without modifying the original"]
2258 #[inline(always)]
2259 pub const fn cast_unsigned(self) -> NonZero<$Uint> {
2260 unsafe { NonZero::new_unchecked(self.get().cast_unsigned()) }
2262 }
2263
2264 };
2265}
2266
2267#[cfg(not(feature = "ferrocene_certified"))]
2268nonzero_integer! {
2269 Self = NonZeroU8,
2270 Primitive = unsigned u8,
2271 SignedPrimitive = i8,
2272 rot = 2,
2273 rot_op = "0x82",
2274 rot_result = "0xa",
2275 swap_op = "0x12",
2276 swapped = "0x12",
2277 reversed = "0x48",
2278}
2279
2280#[cfg(not(feature = "ferrocene_certified"))]
2281nonzero_integer! {
2282 Self = NonZeroU16,
2283 Primitive = unsigned u16,
2284 SignedPrimitive = i16,
2285 rot = 4,
2286 rot_op = "0xa003",
2287 rot_result = "0x3a",
2288 swap_op = "0x1234",
2289 swapped = "0x3412",
2290 reversed = "0x2c48",
2291}
2292
2293#[cfg(not(feature = "ferrocene_certified"))]
2294nonzero_integer! {
2295 Self = NonZeroU32,
2296 Primitive = unsigned u32,
2297 SignedPrimitive = i32,
2298 rot = 8,
2299 rot_op = "0x10000b3",
2300 rot_result = "0xb301",
2301 swap_op = "0x12345678",
2302 swapped = "0x78563412",
2303 reversed = "0x1e6a2c48",
2304}
2305
2306#[cfg(not(feature = "ferrocene_certified"))]
2307nonzero_integer! {
2308 Self = NonZeroU64,
2309 Primitive = unsigned u64,
2310 SignedPrimitive = i64,
2311 rot = 12,
2312 rot_op = "0xaa00000000006e1",
2313 rot_result = "0x6e10aa",
2314 swap_op = "0x1234567890123456",
2315 swapped = "0x5634129078563412",
2316 reversed = "0x6a2c48091e6a2c48",
2317}
2318
2319#[cfg(not(feature = "ferrocene_certified"))]
2320nonzero_integer! {
2321 Self = NonZeroU128,
2322 Primitive = unsigned u128,
2323 SignedPrimitive = i128,
2324 rot = 16,
2325 rot_op = "0x13f40000000000000000000000004f76",
2326 rot_result = "0x4f7613f4",
2327 swap_op = "0x12345678901234567890123456789012",
2328 swapped = "0x12907856341290785634129078563412",
2329 reversed = "0x48091e6a2c48091e6a2c48091e6a2c48",
2330}
2331
2332#[cfg(target_pointer_width = "16")]
2333#[cfg(not(feature = "ferrocene_certified"))]
2334nonzero_integer! {
2335 Self = NonZeroUsize,
2336 Primitive = unsigned usize,
2337 SignedPrimitive = isize,
2338 rot = 4,
2339 rot_op = "0xa003",
2340 rot_result = "0x3a",
2341 swap_op = "0x1234",
2342 swapped = "0x3412",
2343 reversed = "0x2c48",
2344}
2345
2346#[cfg(target_pointer_width = "32")]
2347#[cfg(not(feature = "ferrocene_certified"))]
2348nonzero_integer! {
2349 Self = NonZeroUsize,
2350 Primitive = unsigned usize,
2351 SignedPrimitive = isize,
2352 rot = 8,
2353 rot_op = "0x10000b3",
2354 rot_result = "0xb301",
2355 swap_op = "0x12345678",
2356 swapped = "0x78563412",
2357 reversed = "0x1e6a2c48",
2358}
2359
2360#[cfg(target_pointer_width = "64")]
2361#[cfg(not(feature = "ferrocene_certified"))]
2362nonzero_integer! {
2363 Self = NonZeroUsize,
2364 Primitive = unsigned usize,
2365 SignedPrimitive = isize,
2366 rot = 12,
2367 rot_op = "0xaa00000000006e1",
2368 rot_result = "0x6e10aa",
2369 swap_op = "0x1234567890123456",
2370 swapped = "0x5634129078563412",
2371 reversed = "0x6a2c48091e6a2c48",
2372}
2373
2374#[cfg(not(feature = "ferrocene_certified"))]
2375nonzero_integer! {
2376 Self = NonZeroI8,
2377 Primitive = signed i8,
2378 UnsignedPrimitive = u8,
2379 rot = 2,
2380 rot_op = "-0x7e",
2381 rot_result = "0xa",
2382 swap_op = "0x12",
2383 swapped = "0x12",
2384 reversed = "0x48",
2385}
2386
2387#[cfg(not(feature = "ferrocene_certified"))]
2388nonzero_integer! {
2389 Self = NonZeroI16,
2390 Primitive = signed i16,
2391 UnsignedPrimitive = u16,
2392 rot = 4,
2393 rot_op = "-0x5ffd",
2394 rot_result = "0x3a",
2395 swap_op = "0x1234",
2396 swapped = "0x3412",
2397 reversed = "0x2c48",
2398}
2399
2400#[cfg(not(feature = "ferrocene_certified"))]
2401nonzero_integer! {
2402 Self = NonZeroI32,
2403 Primitive = signed i32,
2404 UnsignedPrimitive = u32,
2405 rot = 8,
2406 rot_op = "0x10000b3",
2407 rot_result = "0xb301",
2408 swap_op = "0x12345678",
2409 swapped = "0x78563412",
2410 reversed = "0x1e6a2c48",
2411}
2412
2413#[cfg(not(feature = "ferrocene_certified"))]
2414nonzero_integer! {
2415 Self = NonZeroI64,
2416 Primitive = signed i64,
2417 UnsignedPrimitive = u64,
2418 rot = 12,
2419 rot_op = "0xaa00000000006e1",
2420 rot_result = "0x6e10aa",
2421 swap_op = "0x1234567890123456",
2422 swapped = "0x5634129078563412",
2423 reversed = "0x6a2c48091e6a2c48",
2424}
2425
2426#[cfg(not(feature = "ferrocene_certified"))]
2427nonzero_integer! {
2428 Self = NonZeroI128,
2429 Primitive = signed i128,
2430 UnsignedPrimitive = u128,
2431 rot = 16,
2432 rot_op = "0x13f40000000000000000000000004f76",
2433 rot_result = "0x4f7613f4",
2434 swap_op = "0x12345678901234567890123456789012",
2435 swapped = "0x12907856341290785634129078563412",
2436 reversed = "0x48091e6a2c48091e6a2c48091e6a2c48",
2437}
2438
2439#[cfg(target_pointer_width = "16")]
2440#[cfg(not(feature = "ferrocene_certified"))]
2441nonzero_integer! {
2442 Self = NonZeroIsize,
2443 Primitive = signed isize,
2444 UnsignedPrimitive = usize,
2445 rot = 4,
2446 rot_op = "-0x5ffd",
2447 rot_result = "0x3a",
2448 swap_op = "0x1234",
2449 swapped = "0x3412",
2450 reversed = "0x2c48",
2451}
2452
2453#[cfg(target_pointer_width = "32")]
2454#[cfg(not(feature = "ferrocene_certified"))]
2455nonzero_integer! {
2456 Self = NonZeroIsize,
2457 Primitive = signed isize,
2458 UnsignedPrimitive = usize,
2459 rot = 8,
2460 rot_op = "0x10000b3",
2461 rot_result = "0xb301",
2462 swap_op = "0x12345678",
2463 swapped = "0x78563412",
2464 reversed = "0x1e6a2c48",
2465}
2466
2467#[cfg(target_pointer_width = "64")]
2468#[cfg(not(feature = "ferrocene_certified"))]
2469nonzero_integer! {
2470 Self = NonZeroIsize,
2471 Primitive = signed isize,
2472 UnsignedPrimitive = usize,
2473 rot = 12,
2474 rot_op = "0xaa00000000006e1",
2475 rot_result = "0x6e10aa",
2476 swap_op = "0x1234567890123456",
2477 swapped = "0x5634129078563412",
2478 reversed = "0x6a2c48091e6a2c48",
2479}