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, ops::Div, 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
542macro_rules! nonzero_integer {
543 (
544 #[$stability:meta]
545 Self = $Ty:ident,
546 Primitive = $signedness:ident $Int:ident,
547 SignedPrimitive = $Sint:ty,
548 UnsignedPrimitive = $Uint:ty,
549
550 rot = $rot:literal,
552 rot_op = $rot_op:literal,
553 rot_result = $rot_result:literal,
554 swap_op = $swap_op:literal,
555 swapped = $swapped:literal,
556 reversed = $reversed:literal,
557 leading_zeros_test = $leading_zeros_test:expr,
558 ) => {
559 #[doc = sign_dependent_expr!{
560 $signedness ?
561 if signed {
562 concat!("An [`", stringify!($Int), "`] that is known not to equal zero.")
563 }
564 if unsigned {
565 concat!("A [`", stringify!($Int), "`] that is known not to equal zero.")
566 }
567 }]
568 #[doc = concat!("For example, `Option<", stringify!($Ty), ">` is the same size as `", stringify!($Int), "`:")]
571 #[doc = concat!("assert_eq!(size_of::<Option<core::num::", stringify!($Ty), ">>(), size_of::<", stringify!($Int), ">());")]
574 #[doc = concat!("`", stringify!($Ty), "` is guaranteed to have the same layout and bit validity as `", stringify!($Int), "`")]
579 #[doc = concat!("`Option<", stringify!($Ty), ">` is guaranteed to be compatible with `", stringify!($Int), "`,")]
581 #[doc = concat!("`", stringify!($Ty), "` and `Option<", stringify!($Ty), ">`")]
585 #[doc = concat!("use std::num::", stringify!($Ty), ";")]
589 #[doc = concat!("assert_eq!(size_of::<", stringify!($Ty), ">(), size_of::<Option<", stringify!($Ty), ">>());")]
591 #[doc = concat!("assert_eq!(align_of::<", stringify!($Ty), ">(), align_of::<Option<", stringify!($Ty), ">>());")]
592 #[doc = concat!("`", stringify!($Ty), "`")]
599 #[doc = concat!("use std::num::", stringify!($Ty), ";")]
602 #[doc = concat!("const TEN: ", stringify!($Ty), " = ", stringify!($Ty) , r#"::new(10).expect("ten is non-zero");"#)]
604 #[$stability]
608 #[cfg(not(feature = "ferrocene_certified"))]
609 pub type $Ty = NonZero<$Int>;
610
611 #[cfg(not(feature = "ferrocene_certified"))]
612 impl NonZero<$Int> {
613 #[doc = concat!("This value is equal to [`", stringify!($Int), "::BITS`].")]
616 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::BITS, ", stringify!($Int), "::BITS);")]
623 #[stable(feature = "nonzero_bits", since = "1.67.0")]
625 pub const BITS: u32 = <$Int>::BITS;
626
627 #[doc = concat!("let n = NonZero::<", stringify!($Int), ">::new(", $leading_zeros_test, ")?;")]
639 #[stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
645 #[rustc_const_stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
646 #[must_use = "this returns the result of the operation, \
647 without modifying the original"]
648 #[inline]
649 pub const fn leading_zeros(self) -> u32 {
650 unsafe {
652 intrinsics::ctlz_nonzero(self.get() as $Uint)
653 }
654 }
655
656 #[doc = concat!("let n = NonZero::<", stringify!($Int), ">::new(0b0101000)?;")]
669 #[stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
675 #[rustc_const_stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
676 #[must_use = "this returns the result of the operation, \
677 without modifying the original"]
678 #[inline]
679 pub const fn trailing_zeros(self) -> u32 {
680 unsafe {
682 intrinsics::cttz_nonzero(self.get() as $Uint)
683 }
684 }
685
686 #[doc = concat!("let a = NonZero::<", stringify!($Int), ">::new(0b_01100100)?;")]
697 #[doc = concat!("let b = NonZero::<", stringify!($Int), ">::new(0b_01000000)?;")]
698 #[unstable(feature = "isolate_most_least_significant_one", issue = "136909")]
704 #[must_use = "this returns the result of the operation, \
705 without modifying the original"]
706 #[inline(always)]
707 pub const fn isolate_highest_one(self) -> Self {
708 unsafe {
714 let bit = (((1 as $Uint) << (<$Uint>::BITS - 1)).unchecked_shr(self.leading_zeros()));
715 NonZero::new_unchecked(bit as $Int)
716 }
717 }
718
719 #[doc = concat!("let a = NonZero::<", stringify!($Int), ">::new(0b_01100100)?;")]
730 #[doc = concat!("let b = NonZero::<", stringify!($Int), ">::new(0b_00000100)?;")]
731 #[unstable(feature = "isolate_most_least_significant_one", issue = "136909")]
737 #[must_use = "this returns the result of the operation, \
738 without modifying the original"]
739 #[inline(always)]
740 pub const fn isolate_lowest_one(self) -> Self {
741 let n = self.get();
742 let n = n & n.wrapping_neg();
743
744 unsafe { NonZero::new_unchecked(n) }
747 }
748
749 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1)?.highest_one(), 0);")]
760 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1_0000)?.highest_one(), 4);")]
761 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1_1111)?.highest_one(), 4);")]
762 #[unstable(feature = "int_lowest_highest_one", issue = "145203")]
766 #[must_use = "this returns the result of the operation, \
767 without modifying the original"]
768 #[inline(always)]
769 pub const fn highest_one(self) -> u32 {
770 Self::BITS - 1 - self.leading_zeros()
771 }
772
773 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1)?.lowest_one(), 0);")]
784 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1_0000)?.lowest_one(), 4);")]
785 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1_1111)?.lowest_one(), 0);")]
786 #[unstable(feature = "int_lowest_highest_one", issue = "145203")]
790 #[must_use = "this returns the result of the operation, \
791 without modifying the original"]
792 #[inline(always)]
793 pub const fn lowest_one(self) -> u32 {
794 self.trailing_zeros()
795 }
796
797 #[doc = concat!("let a = NonZero::<", stringify!($Int), ">::new(0b100_0000)?;")]
807 #[doc = concat!("let b = NonZero::<", stringify!($Int), ">::new(0b100_0011)?;")]
808 #[stable(feature = "non_zero_count_ones", since = "1.86.0")]
816 #[rustc_const_stable(feature = "non_zero_count_ones", since = "1.86.0")]
817 #[doc(alias = "popcount")]
818 #[doc(alias = "popcnt")]
819 #[must_use = "this returns the result of the operation, \
820 without modifying the original"]
821 #[inline(always)]
822 pub const fn count_ones(self) -> NonZero<u32> {
823 unsafe { NonZero::new_unchecked(self.get().count_ones()) }
827 }
828
829 #[doc = concat!("let n = NonZero::new(", $rot_op, stringify!($Int), ")?;")]
843 #[doc = concat!("let m = NonZero::new(", $rot_result, ")?;")]
844 #[doc = concat!("assert_eq!(n.rotate_left(", $rot, "), m);")]
846 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
850 #[must_use = "this returns the result of the operation, \
851 without modifying the original"]
852 #[inline(always)]
853 pub const fn rotate_left(self, n: u32) -> Self {
854 let result = self.get().rotate_left(n);
855 unsafe { Self::new_unchecked(result) }
857 }
858
859 #[doc = concat!("let n = NonZero::new(", $rot_result, stringify!($Int), ")?;")]
874 #[doc = concat!("let m = NonZero::new(", $rot_op, ")?;")]
875 #[doc = concat!("assert_eq!(n.rotate_right(", $rot, "), m);")]
877 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
881 #[must_use = "this returns the result of the operation, \
882 without modifying the original"]
883 #[inline(always)]
884 pub const fn rotate_right(self, n: u32) -> Self {
885 let result = self.get().rotate_right(n);
886 unsafe { Self::new_unchecked(result) }
888 }
889
890 #[doc = concat!("let n = NonZero::new(", $swap_op, stringify!($Int), ")?;")]
901 #[doc = concat!("assert_eq!(m, NonZero::new(", $swapped, ")?);")]
904 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
908 #[must_use = "this returns the result of the operation, \
909 without modifying the original"]
910 #[inline(always)]
911 pub const fn swap_bytes(self) -> Self {
912 let result = self.get().swap_bytes();
913 unsafe { Self::new_unchecked(result) }
915 }
916
917 #[doc = concat!("let n = NonZero::new(", $swap_op, stringify!($Int), ")?;")]
929 #[doc = concat!("assert_eq!(m, NonZero::new(", $reversed, ")?);")]
932 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
936 #[must_use = "this returns the result of the operation, \
937 without modifying the original"]
938 #[inline(always)]
939 pub const fn reverse_bits(self) -> Self {
940 let result = self.get().reverse_bits();
941 unsafe { Self::new_unchecked(result) }
943 }
944
945 #[doc = concat!("use std::num::", stringify!($Ty), ";")]
956 #[doc = concat!("let n = NonZero::new(0x1A", stringify!($Int), ")?;")]
960 #[doc = concat!(" assert_eq!(", stringify!($Ty), "::from_be(n), n)")]
963 #[doc = concat!(" assert_eq!(", stringify!($Ty), "::from_be(n), n.swap_bytes())")]
965 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
970 #[must_use]
971 #[inline(always)]
972 pub const fn from_be(x: Self) -> Self {
973 let result = $Int::from_be(x.get());
974 unsafe { Self::new_unchecked(result) }
976 }
977
978 #[doc = concat!("use std::num::", stringify!($Ty), ";")]
989 #[doc = concat!("let n = NonZero::new(0x1A", stringify!($Int), ")?;")]
993 #[doc = concat!(" assert_eq!(", stringify!($Ty), "::from_le(n), n)")]
996 #[doc = concat!(" assert_eq!(", stringify!($Ty), "::from_le(n), n.swap_bytes())")]
998 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
1003 #[must_use]
1004 #[inline(always)]
1005 pub const fn from_le(x: Self) -> Self {
1006 let result = $Int::from_le(x.get());
1007 unsafe { Self::new_unchecked(result) }
1009 }
1010
1011 #[doc = concat!("let n = NonZero::new(0x1A", stringify!($Int), ")?;")]
1025 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
1035 #[must_use = "this returns the result of the operation, \
1036 without modifying the original"]
1037 #[inline(always)]
1038 pub const fn to_be(self) -> Self {
1039 let result = self.get().to_be();
1040 unsafe { Self::new_unchecked(result) }
1042 }
1043
1044 #[doc = concat!("let n = NonZero::new(0x1A", stringify!($Int), ")?;")]
1058 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
1068 #[must_use = "this returns the result of the operation, \
1069 without modifying the original"]
1070 #[inline(always)]
1071 pub const fn to_le(self) -> Self {
1072 let result = self.get().to_le();
1073 unsafe { Self::new_unchecked(result) }
1075 }
1076
1077 nonzero_integer_signedness_dependent_methods! {
1078 Primitive = $signedness $Int,
1079 SignedPrimitive = $Sint,
1080 UnsignedPrimitive = $Uint,
1081 }
1082
1083 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1095 #[doc = concat!("let four = NonZero::new(4", stringify!($Int), ")?;")]
1096 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
1097 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1104 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1105 #[must_use = "this returns the result of the operation, \
1106 without modifying the original"]
1107 #[inline]
1108 pub const fn checked_mul(self, other: Self) -> Option<Self> {
1109 if let Some(result) = self.get().checked_mul(other.get()) {
1110 Some(unsafe { Self::new_unchecked(result) })
1118 } else {
1119 None
1120 }
1121 }
1122
1123 #[doc = concat!("Return [`NonZero::<", stringify!($Int), ">::MAX`] on overflow.")]
1125 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1134 #[doc = concat!("let four = NonZero::new(4", stringify!($Int), ")?;")]
1135 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
1136 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1143 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1144 #[must_use = "this returns the result of the operation, \
1145 without modifying the original"]
1146 #[inline]
1147 pub const fn saturating_mul(self, other: Self) -> Self {
1148 unsafe { Self::new_unchecked(self.get().saturating_mul(other.get())) }
1157 }
1158
1159 #[doc = sign_dependent_expr!{
1165 $signedness ?
1166 if signed {
1167 concat!("`self * rhs > ", stringify!($Int), "::MAX`, ",
1168 "or `self * rhs < ", stringify!($Int), "::MIN`.")
1169 }
1170 if unsigned {
1171 concat!("`self * rhs > ", stringify!($Int), "::MAX`.")
1172 }
1173 }]
1174 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1185 #[doc = concat!("let four = NonZero::new(4", stringify!($Int), ")?;")]
1186 #[unstable(feature = "nonzero_ops", issue = "84186")]
1192 #[must_use = "this returns the result of the operation, \
1193 without modifying the original"]
1194 #[inline]
1195 pub const unsafe fn unchecked_mul(self, other: Self) -> Self {
1196 unsafe { Self::new_unchecked(self.get().unchecked_mul(other.get())) }
1198 }
1199
1200 #[doc = concat!("let three = NonZero::new(3", stringify!($Int), ")?;")]
1212 #[doc = concat!("let twenty_seven = NonZero::new(27", stringify!($Int), ")?;")]
1213 #[doc = concat!("let half_max = NonZero::new(", stringify!($Int), "::MAX / 2)?;")]
1214 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1221 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1222 #[must_use = "this returns the result of the operation, \
1223 without modifying the original"]
1224 #[inline]
1225 pub const fn checked_pow(self, other: u32) -> Option<Self> {
1226 if let Some(result) = self.get().checked_pow(other) {
1227 Some(unsafe { Self::new_unchecked(result) })
1235 } else {
1236 None
1237 }
1238 }
1239
1240 #[doc = sign_dependent_expr!{
1242 $signedness ?
1243 if signed {
1244 concat!("Return [`NonZero::<", stringify!($Int), ">::MIN`] ",
1245 "or [`NonZero::<", stringify!($Int), ">::MAX`] on overflow.")
1246 }
1247 if unsigned {
1248 concat!("Return [`NonZero::<", stringify!($Int), ">::MAX`] on overflow.")
1249 }
1250 }]
1251 #[doc = concat!("let three = NonZero::new(3", stringify!($Int), ")?;")]
1260 #[doc = concat!("let twenty_seven = NonZero::new(27", stringify!($Int), ")?;")]
1261 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
1262 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1269 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1270 #[must_use = "this returns the result of the operation, \
1271 without modifying the original"]
1272 #[inline]
1273 pub const fn saturating_pow(self, other: u32) -> Self {
1274 unsafe { Self::new_unchecked(self.get().saturating_pow(other)) }
1283 }
1284 }
1285
1286 #[cfg(not(feature = "ferrocene_certified"))]
1287 #[stable(feature = "nonzero_parse", since = "1.35.0")]
1288 impl FromStr for NonZero<$Int> {
1289 type Err = ParseIntError;
1290 fn from_str(src: &str) -> Result<Self, Self::Err> {
1291 Self::new(<$Int>::from_str_radix(src, 10)?)
1292 .ok_or(ParseIntError {
1293 kind: IntErrorKind::Zero
1294 })
1295 }
1296 }
1297
1298 nonzero_integer_signedness_dependent_impls!($signedness $Int);
1299 };
1300
1301 (
1302 Self = $Ty:ident,
1303 Primitive = unsigned $Int:ident,
1304 SignedPrimitive = $Sint:ident,
1305 rot = $rot:literal,
1306 rot_op = $rot_op:literal,
1307 rot_result = $rot_result:literal,
1308 swap_op = $swap_op:literal,
1309 swapped = $swapped:literal,
1310 reversed = $reversed:literal,
1311 $(,)?
1312 ) => {
1313 nonzero_integer! {
1314 #[stable(feature = "nonzero", since = "1.28.0")]
1315 Self = $Ty,
1316 Primitive = unsigned $Int,
1317 SignedPrimitive = $Sint,
1318 UnsignedPrimitive = $Int,
1319 rot = $rot,
1320 rot_op = $rot_op,
1321 rot_result = $rot_result,
1322 swap_op = $swap_op,
1323 swapped = $swapped,
1324 reversed = $reversed,
1325 leading_zeros_test = concat!(stringify!($Int), "::MAX"),
1326 }
1327 };
1328
1329 (
1330 Self = $Ty:ident,
1331 Primitive = signed $Int:ident,
1332 UnsignedPrimitive = $Uint:ident,
1333 rot = $rot:literal,
1334 rot_op = $rot_op:literal,
1335 rot_result = $rot_result:literal,
1336 swap_op = $swap_op:literal,
1337 swapped = $swapped:literal,
1338 reversed = $reversed:literal,
1339 ) => {
1340 nonzero_integer! {
1341 #[stable(feature = "signed_nonzero", since = "1.34.0")]
1342 Self = $Ty,
1343 Primitive = signed $Int,
1344 SignedPrimitive = $Int,
1345 UnsignedPrimitive = $Uint,
1346 rot = $rot,
1347 rot_op = $rot_op,
1348 rot_result = $rot_result,
1349 swap_op = $swap_op,
1350 swapped = $swapped,
1351 reversed = $reversed,
1352 leading_zeros_test = concat!("-1", stringify!($Int)),
1353 }
1354 };
1355}
1356
1357macro_rules! nonzero_integer_signedness_dependent_impls {
1358 (unsigned $Int:ty) => {
1360 #[stable(feature = "nonzero_div", since = "1.51.0")]
1361 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1362 impl const Div<NonZero<$Int>> for $Int {
1363 type Output = $Int;
1364
1365 #[doc(alias = "unchecked_div")]
1371 #[inline]
1372 fn div(self, other: NonZero<$Int>) -> $Int {
1373 unsafe { intrinsics::unchecked_div(self, other.get()) }
1376 }
1377 }
1378
1379 #[cfg(not(feature = "ferrocene_certified"))]
1380 #[stable(feature = "nonzero_div_assign", since = "1.79.0")]
1381 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1382 impl const DivAssign<NonZero<$Int>> for $Int {
1383 #[inline]
1389 fn div_assign(&mut self, other: NonZero<$Int>) {
1390 *self = *self / other;
1391 }
1392 }
1393
1394 #[cfg(not(feature = "ferrocene_certified"))]
1395 #[stable(feature = "nonzero_div", since = "1.51.0")]
1396 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1397 impl const Rem<NonZero<$Int>> for $Int {
1398 type Output = $Int;
1399
1400 #[inline]
1402 fn rem(self, other: NonZero<$Int>) -> $Int {
1403 unsafe { intrinsics::unchecked_rem(self, other.get()) }
1406 }
1407 }
1408
1409 #[cfg(not(feature = "ferrocene_certified"))]
1410 #[stable(feature = "nonzero_div_assign", since = "1.79.0")]
1411 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1412 impl const RemAssign<NonZero<$Int>> for $Int {
1413 #[inline]
1415 fn rem_assign(&mut self, other: NonZero<$Int>) {
1416 *self = *self % other;
1417 }
1418 }
1419
1420 #[cfg(not(feature = "ferrocene_certified"))]
1421 impl NonZero<$Int> {
1422 #[doc = concat!("let one = NonZero::new(1", stringify!($Int), ").unwrap();")]
1431 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX).unwrap();")]
1432 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ").unwrap();")]
1435 #[doc = concat!("let three = NonZero::new(3", stringify!($Int), ").unwrap();")]
1436 #[stable(feature = "unsigned_nonzero_div_ceil", since = "1.92.0")]
1439 #[rustc_const_stable(feature = "unsigned_nonzero_div_ceil", since = "1.92.0")]
1440 #[must_use = "this returns the result of the operation, \
1441 without modifying the original"]
1442 #[inline]
1443 pub const fn div_ceil(self, rhs: Self) -> Self {
1444 let v = self.get().div_ceil(rhs.get());
1445 unsafe { Self::new_unchecked(v) }
1447 }
1448 }
1449 };
1450 (signed $Int:ty) => {
1452 #[cfg(not(feature = "ferrocene_certified"))]
1453 #[stable(feature = "signed_nonzero_neg", since = "1.71.0")]
1454 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1455 impl const Neg for NonZero<$Int> {
1456 type Output = Self;
1457
1458 #[inline]
1459 fn neg(self) -> Self {
1460 unsafe { Self::new_unchecked(self.get().neg()) }
1462 }
1463 }
1464
1465 #[cfg(not(feature = "ferrocene_certified"))]
1466 forward_ref_unop! { impl Neg, neg for NonZero<$Int>,
1467 #[stable(feature = "signed_nonzero_neg", since = "1.71.0")]
1468 #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
1469 };
1470}
1471
1472#[rustfmt::skip] #[cfg(not(feature = "ferrocene_certified"))]
1474macro_rules! nonzero_integer_signedness_dependent_methods {
1475 (
1477 Primitive = unsigned $Int:ident,
1478 SignedPrimitive = $Sint:ty,
1479 UnsignedPrimitive = $Uint:ty,
1480 ) => {
1481 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::MIN.get(), 1", stringify!($Int), ");")]
1490 #[stable(feature = "nonzero_min_max", since = "1.70.0")]
1492 pub const MIN: Self = Self::new(1).unwrap();
1493
1494 #[doc = concat!("equal to [`", stringify!($Int), "::MAX`].")]
1497 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::MAX.get(), ", stringify!($Int), "::MAX);")]
1504 #[stable(feature = "nonzero_min_max", since = "1.70.0")]
1506 pub const MAX: Self = Self::new(<$Int>::MAX).unwrap();
1507
1508 #[doc = concat!("let one = NonZero::new(1", stringify!($Int), ")?;")]
1521 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1522 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
1523 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1530 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1531 #[must_use = "this returns the result of the operation, \
1532 without modifying the original"]
1533 #[inline]
1534 pub const fn checked_add(self, other: $Int) -> Option<Self> {
1535 if let Some(result) = self.get().checked_add(other) {
1536 Some(unsafe { Self::new_unchecked(result) })
1544 } else {
1545 None
1546 }
1547 }
1548
1549 #[doc = concat!("Return [`NonZero::<", stringify!($Int), ">::MAX`] on overflow.")]
1551 #[doc = concat!("let one = NonZero::new(1", stringify!($Int), ")?;")]
1560 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1561 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
1562 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1569 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1570 #[must_use = "this returns the result of the operation, \
1571 without modifying the original"]
1572 #[inline]
1573 pub const fn saturating_add(self, other: $Int) -> Self {
1574 unsafe { Self::new_unchecked(self.get().saturating_add(other)) }
1582 }
1583
1584 #[doc = concat!("`self + rhs > ", stringify!($Int), "::MAX`.")]
1590 #[doc = concat!("let one = NonZero::new(1", stringify!($Int), ")?;")]
1601 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1602 #[unstable(feature = "nonzero_ops", issue = "84186")]
1608 #[must_use = "this returns the result of the operation, \
1609 without modifying the original"]
1610 #[inline]
1611 pub const unsafe fn unchecked_add(self, other: $Int) -> Self {
1612 unsafe { Self::new_unchecked(self.get().unchecked_add(other)) }
1614 }
1615
1616 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1629 #[doc = concat!("let three = NonZero::new(3", stringify!($Int), ")?;")]
1630 #[doc = concat!("let four = NonZero::new(4", stringify!($Int), ")?;")]
1631 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
1632 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1640 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1641 #[must_use = "this returns the result of the operation, \
1642 without modifying the original"]
1643 #[inline]
1644 pub const fn checked_next_power_of_two(self) -> Option<Self> {
1645 if let Some(nz) = self.get().checked_next_power_of_two() {
1646 Some(unsafe { Self::new_unchecked(nz) })
1649 } else {
1650 None
1651 }
1652 }
1653
1654 #[doc = concat!("[`", stringify!($Int), "::ilog2`],")]
1658 #[doc = concat!("assert_eq!(NonZero::new(7", stringify!($Int), ")?.ilog2(), 2);")]
1669 #[doc = concat!("assert_eq!(NonZero::new(8", stringify!($Int), ")?.ilog2(), 3);")]
1670 #[doc = concat!("assert_eq!(NonZero::new(9", stringify!($Int), ")?.ilog2(), 3);")]
1671 #[stable(feature = "int_log", since = "1.67.0")]
1675 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
1676 #[must_use = "this returns the result of the operation, \
1677 without modifying the original"]
1678 #[inline]
1679 pub const fn ilog2(self) -> u32 {
1680 Self::BITS - 1 - self.leading_zeros()
1681 }
1682
1683 #[doc = concat!("[`", stringify!($Int), "::ilog10`],")]
1687 #[doc = concat!("assert_eq!(NonZero::new(99", stringify!($Int), ")?.ilog10(), 1);")]
1698 #[doc = concat!("assert_eq!(NonZero::new(100", stringify!($Int), ")?.ilog10(), 2);")]
1699 #[doc = concat!("assert_eq!(NonZero::new(101", stringify!($Int), ")?.ilog10(), 2);")]
1700 #[stable(feature = "int_log", since = "1.67.0")]
1704 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
1705 #[must_use = "this returns the result of the operation, \
1706 without modifying the original"]
1707 #[inline]
1708 pub const fn ilog10(self) -> u32 {
1709 super::int_log10::$Int(self.get())
1710 }
1711
1712 #[doc = concat!("let one = NonZero::new(1", stringify!($Int), ")?;")]
1726 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1727 #[doc = concat!("let four = NonZero::new(4", stringify!($Int), ")?;")]
1728 #[stable(feature = "num_midpoint", since = "1.85.0")]
1735 #[rustc_const_stable(feature = "num_midpoint", since = "1.85.0")]
1736 #[must_use = "this returns the result of the operation, \
1737 without modifying the original"]
1738 #[doc(alias = "average_floor")]
1739 #[doc(alias = "average")]
1740 #[inline]
1741 pub const fn midpoint(self, rhs: Self) -> Self {
1742 unsafe { Self::new_unchecked(self.get().midpoint(rhs.get())) }
1747 }
1748
1749 #[doc = concat!("let eight = NonZero::new(8", stringify!($Int), ")?;")]
1762 #[doc = concat!("let ten = NonZero::new(10", stringify!($Int), ")?;")]
1764 #[must_use]
1769 #[stable(feature = "nonzero_is_power_of_two", since = "1.59.0")]
1770 #[rustc_const_stable(feature = "nonzero_is_power_of_two", since = "1.59.0")]
1771 #[inline]
1772 pub const fn is_power_of_two(self) -> bool {
1773 intrinsics::ctpop(self.get()) < 2
1779 }
1780
1781 #[doc = concat!("let ten = NonZero::new(10", stringify!($Int), ")?;")]
1791 #[doc = concat!("let three = NonZero::new(3", stringify!($Int), ")?;")]
1792 #[stable(feature = "isqrt", since = "1.84.0")]
1798 #[rustc_const_stable(feature = "isqrt", since = "1.84.0")]
1799 #[must_use = "this returns the result of the operation, \
1800 without modifying the original"]
1801 #[inline]
1802 pub const fn isqrt(self) -> Self {
1803 let result = self.get().isqrt();
1804
1805 unsafe { Self::new_unchecked(result) }
1811 }
1812
1813 #[doc = concat!("let n = NonZero::<", stringify!($Int), ">::MAX;")]
1821 #[doc = concat!("assert_eq!(n.cast_signed(), NonZero::new(-1", stringify!($Sint), ").unwrap());")]
1823 #[stable(feature = "integer_sign_cast", since = "1.87.0")]
1825 #[rustc_const_stable(feature = "integer_sign_cast", since = "1.87.0")]
1826 #[must_use = "this returns the result of the operation, \
1827 without modifying the original"]
1828 #[inline(always)]
1829 pub const fn cast_signed(self) -> NonZero<$Sint> {
1830 unsafe { NonZero::new_unchecked(self.get().cast_signed()) }
1832 }
1833
1834 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::MIN.bit_width(), NonZero::new(1)?);")]
1846 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b111)?.bit_width(), NonZero::new(3)?);")]
1847 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1110)?.bit_width(), NonZero::new(4)?);")]
1848 #[unstable(feature = "uint_bit_width", issue = "142326")]
1852 #[must_use = "this returns the result of the operation, \
1853 without modifying the original"]
1854 #[inline(always)]
1855 pub const fn bit_width(self) -> NonZero<u32> {
1856 unsafe { NonZero::new_unchecked(Self::BITS - self.leading_zeros()) }
1859 }
1860 };
1861
1862 (
1864 Primitive = signed $Int:ident,
1865 SignedPrimitive = $Sint:ty,
1866 UnsignedPrimitive = $Uint:ty,
1867 ) => {
1868 #[doc = concat!("equal to [`", stringify!($Int), "::MIN`].")]
1871 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::MIN.get(), ", stringify!($Int), "::MIN);")]
1882 #[stable(feature = "nonzero_min_max", since = "1.70.0")]
1884 pub const MIN: Self = Self::new(<$Int>::MIN).unwrap();
1885
1886 #[doc = concat!("equal to [`", stringify!($Int), "::MAX`].")]
1889 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::MAX.get(), ", stringify!($Int), "::MAX);")]
1900 #[stable(feature = "nonzero_min_max", since = "1.70.0")]
1902 pub const MAX: Self = Self::new(<$Int>::MAX).unwrap();
1903
1904 #[doc = concat!("See [`", stringify!($Int), "::abs`]")]
1906 #[doc = concat!("let pos = NonZero::new(1", stringify!($Int), ")?;")]
1916 #[doc = concat!("let neg = NonZero::new(-1", stringify!($Int), ")?;")]
1917 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1924 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1925 #[must_use = "this returns the result of the operation, \
1926 without modifying the original"]
1927 #[inline]
1928 pub const fn abs(self) -> Self {
1929 unsafe { Self::new_unchecked(self.get().abs()) }
1931 }
1932
1933 #[doc = concat!("`self == NonZero::<", stringify!($Int), ">::MIN`.")]
1936 #[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")]
1955 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1956 #[must_use = "this returns the result of the operation, \
1957 without modifying the original"]
1958 #[inline]
1959 pub const fn checked_abs(self) -> Option<Self> {
1960 if let Some(nz) = self.get().checked_abs() {
1961 Some(unsafe { Self::new_unchecked(nz) })
1963 } else {
1964 None
1965 }
1966 }
1967
1968 #[doc = concat!("[`", stringify!($Int), "::overflowing_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 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1990 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1991 #[must_use = "this returns the result of the operation, \
1992 without modifying the original"]
1993 #[inline]
1994 pub const fn overflowing_abs(self) -> (Self, bool) {
1995 let (nz, flag) = self.get().overflowing_abs();
1996 (
1997 unsafe { Self::new_unchecked(nz) },
1999 flag,
2000 )
2001 }
2002
2003 #[doc = concat!("[`", stringify!($Int), "::saturating_abs`].")]
2005 #[doc = concat!("let pos = NonZero::new(1", stringify!($Int), ")?;")]
2014 #[doc = concat!("let neg = NonZero::new(-1", stringify!($Int), ")?;")]
2015 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2016 #[doc = concat!("let min_plus = NonZero::new(", stringify!($Int), "::MIN + 1)?;")]
2017 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
2018 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
2027 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
2028 #[must_use = "this returns the result of the operation, \
2029 without modifying the original"]
2030 #[inline]
2031 pub const fn saturating_abs(self) -> Self {
2032 unsafe { Self::new_unchecked(self.get().saturating_abs()) }
2034 }
2035
2036 #[doc = concat!("[`", stringify!($Int), "::wrapping_abs`].")]
2038 #[doc = concat!("let pos = NonZero::new(1", stringify!($Int), ")?;")]
2047 #[doc = concat!("let neg = NonZero::new(-1", stringify!($Int), ")?;")]
2048 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2049 #[doc = concat!("# let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
2050 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
2059 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
2060 #[must_use = "this returns the result of the operation, \
2061 without modifying the original"]
2062 #[inline]
2063 pub const fn wrapping_abs(self) -> Self {
2064 unsafe { Self::new_unchecked(self.get().wrapping_abs()) }
2066 }
2067
2068 #[doc = concat!("let u_pos = NonZero::new(1", stringify!($Uint), ")?;")]
2079 #[doc = concat!("let i_pos = NonZero::new(1", stringify!($Int), ")?;")]
2080 #[doc = concat!("let i_neg = NonZero::new(-1", stringify!($Int), ")?;")]
2081 #[doc = concat!("let i_min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2082 #[doc = concat!("let u_max = NonZero::new(", stringify!($Uint), "::MAX / 2 + 1)?;")]
2083 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
2091 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
2092 #[must_use = "this returns the result of the operation, \
2093 without modifying the original"]
2094 #[inline]
2095 pub const fn unsigned_abs(self) -> NonZero<$Uint> {
2096 unsafe { NonZero::new_unchecked(self.get().unsigned_abs()) }
2098 }
2099
2100 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2111 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2112 #[must_use]
2119 #[inline]
2120 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2121 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2122 pub const fn is_positive(self) -> bool {
2123 self.get().is_positive()
2124 }
2125
2126 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2137 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2138 #[must_use]
2145 #[inline]
2146 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2147 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2148 pub const fn is_negative(self) -> bool {
2149 self.get().is_negative()
2150 }
2151
2152 #[doc = concat!("returning `None` if `self == NonZero::<", stringify!($Int), ">::MIN`.")]
2154 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2163 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2164 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2165 #[inline]
2172 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2173 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2174 pub const fn checked_neg(self) -> Option<Self> {
2175 if let Some(result) = self.get().checked_neg() {
2176 return Some(unsafe { Self::new_unchecked(result) });
2178 }
2179 None
2180 }
2181
2182 #[doc = concat!("See [`", stringify!($Int), "::overflowing_neg`]")]
2185 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2195 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2196 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2197 #[inline]
2204 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2205 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2206 pub const fn overflowing_neg(self) -> (Self, bool) {
2207 let (result, overflow) = self.get().overflowing_neg();
2208 ((unsafe { Self::new_unchecked(result) }), overflow)
2210 }
2211
2212 #[doc = concat!("returning [`NonZero::<", stringify!($Int), ">::MAX`]")]
2214 #[doc = concat!("if `self == NonZero::<", stringify!($Int), ">::MIN`")]
2215 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2225 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2226 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2227 #[doc = concat!("let min_plus_one = NonZero::new(", stringify!($Int), "::MIN + 1)?;")]
2228 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
2229 #[inline]
2237 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2238 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2239 pub const fn saturating_neg(self) -> Self {
2240 if let Some(result) = self.checked_neg() {
2241 return result;
2242 }
2243 Self::MAX
2244 }
2245
2246 #[doc = concat!("See [`", stringify!($Int), "::wrapping_neg`]")]
2250 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2260 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2261 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2262 #[inline]
2269 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2270 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2271 pub const fn wrapping_neg(self) -> Self {
2272 let result = self.get().wrapping_neg();
2273 unsafe { Self::new_unchecked(result) }
2275 }
2276
2277 #[doc = concat!("let n = NonZero::new(-1", stringify!($Int), ").unwrap();")]
2285 #[doc = concat!("assert_eq!(n.cast_unsigned(), NonZero::<", stringify!($Uint), ">::MAX);")]
2287 #[stable(feature = "integer_sign_cast", since = "1.87.0")]
2289 #[rustc_const_stable(feature = "integer_sign_cast", since = "1.87.0")]
2290 #[must_use = "this returns the result of the operation, \
2291 without modifying the original"]
2292 #[inline(always)]
2293 pub const fn cast_unsigned(self) -> NonZero<$Uint> {
2294 unsafe { NonZero::new_unchecked(self.get().cast_unsigned()) }
2296 }
2297
2298 };
2299}
2300
2301#[cfg(not(feature = "ferrocene_certified"))]
2302nonzero_integer! {
2303 Self = NonZeroU8,
2304 Primitive = unsigned u8,
2305 SignedPrimitive = i8,
2306 rot = 2,
2307 rot_op = "0x82",
2308 rot_result = "0xa",
2309 swap_op = "0x12",
2310 swapped = "0x12",
2311 reversed = "0x48",
2312}
2313
2314#[cfg(not(feature = "ferrocene_certified"))]
2315nonzero_integer! {
2316 Self = NonZeroU16,
2317 Primitive = unsigned u16,
2318 SignedPrimitive = i16,
2319 rot = 4,
2320 rot_op = "0xa003",
2321 rot_result = "0x3a",
2322 swap_op = "0x1234",
2323 swapped = "0x3412",
2324 reversed = "0x2c48",
2325}
2326
2327#[cfg(not(feature = "ferrocene_certified"))]
2328nonzero_integer! {
2329 Self = NonZeroU32,
2330 Primitive = unsigned u32,
2331 SignedPrimitive = i32,
2332 rot = 8,
2333 rot_op = "0x10000b3",
2334 rot_result = "0xb301",
2335 swap_op = "0x12345678",
2336 swapped = "0x78563412",
2337 reversed = "0x1e6a2c48",
2338}
2339
2340#[cfg(not(feature = "ferrocene_certified"))]
2341nonzero_integer! {
2342 Self = NonZeroU64,
2343 Primitive = unsigned u64,
2344 SignedPrimitive = i64,
2345 rot = 12,
2346 rot_op = "0xaa00000000006e1",
2347 rot_result = "0x6e10aa",
2348 swap_op = "0x1234567890123456",
2349 swapped = "0x5634129078563412",
2350 reversed = "0x6a2c48091e6a2c48",
2351}
2352
2353#[cfg(not(feature = "ferrocene_certified"))]
2354nonzero_integer! {
2355 Self = NonZeroU128,
2356 Primitive = unsigned u128,
2357 SignedPrimitive = i128,
2358 rot = 16,
2359 rot_op = "0x13f40000000000000000000000004f76",
2360 rot_result = "0x4f7613f4",
2361 swap_op = "0x12345678901234567890123456789012",
2362 swapped = "0x12907856341290785634129078563412",
2363 reversed = "0x48091e6a2c48091e6a2c48091e6a2c48",
2364}
2365
2366#[cfg(target_pointer_width = "16")]
2367nonzero_integer! {
2368 Self = NonZeroUsize,
2369 Primitive = unsigned usize,
2370 SignedPrimitive = isize,
2371 rot = 4,
2372 rot_op = "0xa003",
2373 rot_result = "0x3a",
2374 swap_op = "0x1234",
2375 swapped = "0x3412",
2376 reversed = "0x2c48",
2377}
2378
2379#[cfg(target_pointer_width = "32")]
2380nonzero_integer! {
2381 Self = NonZeroUsize,
2382 Primitive = unsigned usize,
2383 SignedPrimitive = isize,
2384 rot = 8,
2385 rot_op = "0x10000b3",
2386 rot_result = "0xb301",
2387 swap_op = "0x12345678",
2388 swapped = "0x78563412",
2389 reversed = "0x1e6a2c48",
2390}
2391
2392#[cfg(target_pointer_width = "64")]
2393nonzero_integer! {
2394 Self = NonZeroUsize,
2395 Primitive = unsigned usize,
2396 SignedPrimitive = isize,
2397 rot = 12,
2398 rot_op = "0xaa00000000006e1",
2399 rot_result = "0x6e10aa",
2400 swap_op = "0x1234567890123456",
2401 swapped = "0x5634129078563412",
2402 reversed = "0x6a2c48091e6a2c48",
2403}
2404
2405#[cfg(not(feature = "ferrocene_certified"))]
2406nonzero_integer! {
2407 Self = NonZeroI8,
2408 Primitive = signed i8,
2409 UnsignedPrimitive = u8,
2410 rot = 2,
2411 rot_op = "-0x7e",
2412 rot_result = "0xa",
2413 swap_op = "0x12",
2414 swapped = "0x12",
2415 reversed = "0x48",
2416}
2417
2418#[cfg(not(feature = "ferrocene_certified"))]
2419nonzero_integer! {
2420 Self = NonZeroI16,
2421 Primitive = signed i16,
2422 UnsignedPrimitive = u16,
2423 rot = 4,
2424 rot_op = "-0x5ffd",
2425 rot_result = "0x3a",
2426 swap_op = "0x1234",
2427 swapped = "0x3412",
2428 reversed = "0x2c48",
2429}
2430
2431#[cfg(not(feature = "ferrocene_certified"))]
2432nonzero_integer! {
2433 Self = NonZeroI32,
2434 Primitive = signed i32,
2435 UnsignedPrimitive = u32,
2436 rot = 8,
2437 rot_op = "0x10000b3",
2438 rot_result = "0xb301",
2439 swap_op = "0x12345678",
2440 swapped = "0x78563412",
2441 reversed = "0x1e6a2c48",
2442}
2443
2444#[cfg(not(feature = "ferrocene_certified"))]
2445nonzero_integer! {
2446 Self = NonZeroI64,
2447 Primitive = signed i64,
2448 UnsignedPrimitive = u64,
2449 rot = 12,
2450 rot_op = "0xaa00000000006e1",
2451 rot_result = "0x6e10aa",
2452 swap_op = "0x1234567890123456",
2453 swapped = "0x5634129078563412",
2454 reversed = "0x6a2c48091e6a2c48",
2455}
2456
2457#[cfg(not(feature = "ferrocene_certified"))]
2458nonzero_integer! {
2459 Self = NonZeroI128,
2460 Primitive = signed i128,
2461 UnsignedPrimitive = u128,
2462 rot = 16,
2463 rot_op = "0x13f40000000000000000000000004f76",
2464 rot_result = "0x4f7613f4",
2465 swap_op = "0x12345678901234567890123456789012",
2466 swapped = "0x12907856341290785634129078563412",
2467 reversed = "0x48091e6a2c48091e6a2c48091e6a2c48",
2468}
2469
2470#[cfg(target_pointer_width = "16")]
2471#[cfg(not(feature = "ferrocene_certified"))]
2472nonzero_integer! {
2473 Self = NonZeroIsize,
2474 Primitive = signed isize,
2475 UnsignedPrimitive = usize,
2476 rot = 4,
2477 rot_op = "-0x5ffd",
2478 rot_result = "0x3a",
2479 swap_op = "0x1234",
2480 swapped = "0x3412",
2481 reversed = "0x2c48",
2482}
2483
2484#[cfg(target_pointer_width = "32")]
2485#[cfg(not(feature = "ferrocene_certified"))]
2486nonzero_integer! {
2487 Self = NonZeroIsize,
2488 Primitive = signed isize,
2489 UnsignedPrimitive = usize,
2490 rot = 8,
2491 rot_op = "0x10000b3",
2492 rot_result = "0xb301",
2493 swap_op = "0x12345678",
2494 swapped = "0x78563412",
2495 reversed = "0x1e6a2c48",
2496}
2497
2498#[cfg(target_pointer_width = "64")]
2499#[cfg(not(feature = "ferrocene_certified"))]
2500nonzero_integer! {
2501 Self = NonZeroIsize,
2502 Primitive = signed isize,
2503 UnsignedPrimitive = usize,
2504 rot = 12,
2505 rot_op = "0xaa00000000006e1",
2506 rot_result = "0x6e10aa",
2507 swap_op = "0x1234567890123456",
2508 swapped = "0x5634129078563412",
2509 reversed = "0x6a2c48091e6a2c48",
2510}