1#[cfg(not(feature = "ferrocene_subset"))]
4use super::{IntErrorKind, ParseIntError};
5#[cfg(not(feature = "ferrocene_subset"))]
6use crate::clone::{TrivialClone, UseCloned};
7#[cfg(not(feature = "ferrocene_subset"))]
8use crate::cmp::Ordering;
9#[cfg(not(feature = "ferrocene_subset"))]
10use crate::hash::{Hash, Hasher};
11#[cfg(not(feature = "ferrocene_subset"))]
12use crate::marker::{Destruct, Freeze, StructuralPartialEq};
13#[cfg(not(feature = "ferrocene_subset"))]
14use crate::ops::{BitOr, BitOrAssign, Div, DivAssign, Neg, Rem, RemAssign};
15#[cfg(not(feature = "ferrocene_subset"))]
16use crate::panic::{RefUnwindSafe, UnwindSafe};
17#[cfg(not(feature = "ferrocene_subset"))]
18use crate::str::FromStr;
19#[cfg(not(feature = "ferrocene_subset"))]
20use crate::{fmt, intrinsics, ptr, ub_checks};
21
22#[cfg(feature = "ferrocene_subset")]
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_subset"))]
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_subset"))]
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_subset"))]
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_subset"))]
196impl_nonzero_auto_trait!(unsafe Freeze);
197#[cfg(not(feature = "ferrocene_subset"))]
198impl_nonzero_auto_trait!(RefUnwindSafe);
199#[cfg(not(feature = "ferrocene_subset"))]
200impl_nonzero_auto_trait!(unsafe Send);
201#[cfg(not(feature = "ferrocene_subset"))]
202impl_nonzero_auto_trait!(unsafe Sync);
203#[cfg(not(feature = "ferrocene_subset"))]
204impl_nonzero_auto_trait!(Unpin);
205#[cfg(not(feature = "ferrocene_subset"))]
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_subset"))]
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_subset"))]
229unsafe impl<T> TrivialClone for NonZero<T> where T: ZeroablePrimitive {}
230
231#[stable(feature = "nonzero", since = "1.28.0")]
232#[cfg(not(feature = "ferrocene_subset"))]
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_subset"))]
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_subset"))]
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_subset"))]
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_subset"))]
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_subset"))]
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_subset"))]
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_subset"))]
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_subset"))]
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_subset"))]
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_subset"))]
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_subset"))]
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 #[ferrocene::annotation(
459 "This line cannot be covered as reaching `intrinsics::unreachable` is undefined behavior."
460 )]
461 None => {
462 unsafe {
464 ub_checks::assert_unsafe_precondition!(
465 check_language_ub,
466 "NonZero::new_unchecked requires the argument to be non-zero",
467 () => false,
468 );
469 intrinsics::unreachable()
470 }
471 }
472 }
473 }
474
475 #[unstable(feature = "nonzero_from_mut", issue = "106290")]
478 #[must_use]
479 #[inline]
480 #[cfg(not(feature = "ferrocene_subset"))]
481 pub fn from_mut(n: &mut T) -> Option<&mut Self> {
482 let opt_n = unsafe { &mut *(ptr::from_mut(n).cast::<Option<Self>>()) };
485
486 opt_n.as_mut()
487 }
488
489 #[unstable(feature = "nonzero_from_mut", issue = "106290")]
497 #[must_use]
498 #[inline]
499 #[track_caller]
500 #[cfg(not(feature = "ferrocene_subset"))]
501 pub unsafe fn from_mut_unchecked(n: &mut T) -> &mut Self {
502 match Self::from_mut(n) {
503 Some(n) => n,
504 None => {
505 unsafe {
507 ub_checks::assert_unsafe_precondition!(
508 check_library_ub,
509 "NonZero::from_mut_unchecked requires the argument to dereference as non-zero",
510 () => false,
511 );
512 intrinsics::unreachable()
513 }
514 }
515 }
516 }
517
518 #[stable(feature = "nonzero", since = "1.28.0")]
520 #[rustc_const_stable(feature = "const_nonzero_get", since = "1.34.0")]
521 #[inline]
522 pub const fn get(self) -> T {
523 unsafe { intrinsics::transmute_unchecked(self) }
542 }
543}
544
545macro_rules! nonzero_integer {
546 (
547 #[$stability:meta]
548 Self = $Ty:ident,
549 Primitive = $signedness:ident $Int:ident,
550 SignedPrimitive = $Sint:ty,
551 UnsignedPrimitive = $Uint:ty,
552
553 rot = $rot:literal,
555 rot_op = $rot_op:literal,
556 rot_result = $rot_result:literal,
557 swap_op = $swap_op:literal,
558 swapped = $swapped:literal,
559 reversed = $reversed:literal,
560 leading_zeros_test = $leading_zeros_test:expr,
561 ) => {
562 #[doc = sign_dependent_expr!{
563 $signedness ?
564 if signed {
565 concat!("An [`", stringify!($Int), "`] that is known not to equal zero.")
566 }
567 if unsigned {
568 concat!("A [`", stringify!($Int), "`] that is known not to equal zero.")
569 }
570 }]
571 #[doc = concat!("For example, `Option<", stringify!($Ty), ">` is the same size as `", stringify!($Int), "`:")]
574 #[doc = concat!("assert_eq!(size_of::<Option<core::num::", stringify!($Ty), ">>(), size_of::<", stringify!($Int), ">());")]
577 #[doc = concat!("`", stringify!($Ty), "` is guaranteed to have the same layout and bit validity as `", stringify!($Int), "`")]
582 #[doc = concat!("`Option<", stringify!($Ty), ">` is guaranteed to be compatible with `", stringify!($Int), "`,")]
584 #[doc = concat!("`", stringify!($Ty), "` and `Option<", stringify!($Ty), ">`")]
588 #[doc = concat!("use std::num::", stringify!($Ty), ";")]
592 #[doc = concat!("assert_eq!(size_of::<", stringify!($Ty), ">(), size_of::<Option<", stringify!($Ty), ">>());")]
594 #[doc = concat!("assert_eq!(align_of::<", stringify!($Ty), ">(), align_of::<Option<", stringify!($Ty), ">>());")]
595 #[doc = concat!("`", stringify!($Ty), "`")]
602 #[doc = concat!("use std::num::", stringify!($Ty), ";")]
605 #[doc = concat!("const TEN: ", stringify!($Ty), " = ", stringify!($Ty) , r#"::new(10).expect("ten is non-zero");"#)]
607 #[$stability]
611 pub type $Ty = NonZero<$Int>;
612
613 impl NonZero<$Int> {
614 #[doc = concat!("This value is equal to [`", stringify!($Int), "::BITS`].")]
617 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::BITS, ", stringify!($Int), "::BITS);")]
624 #[stable(feature = "nonzero_bits", since = "1.67.0")]
626 pub const BITS: u32 = <$Int>::BITS;
627
628 #[doc = concat!("let n = NonZero::<", stringify!($Int), ">::new(", $leading_zeros_test, ")?;")]
640 #[stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
646 #[rustc_const_stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
647 #[must_use = "this returns the result of the operation, \
648 without modifying the original"]
649 #[inline]
650 pub const fn leading_zeros(self) -> u32 {
651 unsafe {
653 intrinsics::ctlz_nonzero(self.get() as $Uint)
654 }
655 }
656
657 #[doc = concat!("let n = NonZero::<", stringify!($Int), ">::new(0b0101000)?;")]
670 #[stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
676 #[rustc_const_stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
677 #[must_use = "this returns the result of the operation, \
678 without modifying the original"]
679 #[inline]
680 #[cfg(not(feature = "ferrocene_subset"))]
681 pub const fn trailing_zeros(self) -> u32 {
682 unsafe {
684 intrinsics::cttz_nonzero(self.get() as $Uint)
685 }
686 }
687
688 #[doc = concat!("let a = NonZero::<", stringify!($Int), ">::new(0b_01100100)?;")]
699 #[doc = concat!("let b = NonZero::<", stringify!($Int), ">::new(0b_01000000)?;")]
700 #[unstable(feature = "isolate_most_least_significant_one", issue = "136909")]
706 #[must_use = "this returns the result of the operation, \
707 without modifying the original"]
708 #[inline(always)]
709 #[cfg(not(feature = "ferrocene_subset"))]
710 pub const fn isolate_highest_one(self) -> Self {
711 unsafe {
717 let bit = (((1 as $Uint) << (<$Uint>::BITS - 1)).unchecked_shr(self.leading_zeros()));
718 NonZero::new_unchecked(bit as $Int)
719 }
720 }
721
722 #[doc = concat!("let a = NonZero::<", stringify!($Int), ">::new(0b_01100100)?;")]
733 #[doc = concat!("let b = NonZero::<", stringify!($Int), ">::new(0b_00000100)?;")]
734 #[unstable(feature = "isolate_most_least_significant_one", issue = "136909")]
740 #[must_use = "this returns the result of the operation, \
741 without modifying the original"]
742 #[inline(always)]
743 #[cfg(not(feature = "ferrocene_subset"))]
744 pub const fn isolate_lowest_one(self) -> Self {
745 let n = self.get();
746 let n = n & n.wrapping_neg();
747
748 unsafe { NonZero::new_unchecked(n) }
751 }
752
753 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1)?.highest_one(), 0);")]
764 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1_0000)?.highest_one(), 4);")]
765 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1_1111)?.highest_one(), 4);")]
766 #[unstable(feature = "int_lowest_highest_one", issue = "145203")]
770 #[must_use = "this returns the result of the operation, \
771 without modifying the original"]
772 #[inline(always)]
773 #[cfg(not(feature = "ferrocene_subset"))]
774 pub const fn highest_one(self) -> u32 {
775 Self::BITS - 1 - self.leading_zeros()
776 }
777
778 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1)?.lowest_one(), 0);")]
789 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1_0000)?.lowest_one(), 4);")]
790 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1_1111)?.lowest_one(), 0);")]
791 #[unstable(feature = "int_lowest_highest_one", issue = "145203")]
795 #[must_use = "this returns the result of the operation, \
796 without modifying the original"]
797 #[inline(always)]
798 #[cfg(not(feature = "ferrocene_subset"))]
799 pub const fn lowest_one(self) -> u32 {
800 self.trailing_zeros()
801 }
802
803 #[doc = concat!("let a = NonZero::<", stringify!($Int), ">::new(0b100_0000)?;")]
813 #[doc = concat!("let b = NonZero::<", stringify!($Int), ">::new(0b100_0011)?;")]
814 #[stable(feature = "non_zero_count_ones", since = "1.86.0")]
822 #[rustc_const_stable(feature = "non_zero_count_ones", since = "1.86.0")]
823 #[doc(alias = "popcount")]
824 #[doc(alias = "popcnt")]
825 #[must_use = "this returns the result of the operation, \
826 without modifying the original"]
827 #[inline(always)]
828 #[cfg(not(feature = "ferrocene_subset"))]
829 pub const fn count_ones(self) -> NonZero<u32> {
830 unsafe { NonZero::new_unchecked(self.get().count_ones()) }
834 }
835
836 #[doc = concat!("let n = NonZero::new(", $rot_op, stringify!($Int), ")?;")]
850 #[doc = concat!("let m = NonZero::new(", $rot_result, ")?;")]
851 #[doc = concat!("assert_eq!(n.rotate_left(", $rot, "), m);")]
853 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
857 #[must_use = "this returns the result of the operation, \
858 without modifying the original"]
859 #[inline(always)]
860 #[cfg(not(feature = "ferrocene_subset"))]
861 pub const fn rotate_left(self, n: u32) -> Self {
862 let result = self.get().rotate_left(n);
863 unsafe { Self::new_unchecked(result) }
865 }
866
867 #[doc = concat!("let n = NonZero::new(", $rot_result, stringify!($Int), ")?;")]
882 #[doc = concat!("let m = NonZero::new(", $rot_op, ")?;")]
883 #[doc = concat!("assert_eq!(n.rotate_right(", $rot, "), m);")]
885 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
889 #[must_use = "this returns the result of the operation, \
890 without modifying the original"]
891 #[inline(always)]
892 #[cfg(not(feature = "ferrocene_subset"))]
893 pub const fn rotate_right(self, n: u32) -> Self {
894 let result = self.get().rotate_right(n);
895 unsafe { Self::new_unchecked(result) }
897 }
898
899 #[doc = concat!("let n = NonZero::new(", $swap_op, stringify!($Int), ")?;")]
910 #[doc = concat!("assert_eq!(m, NonZero::new(", $swapped, ")?);")]
913 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
917 #[must_use = "this returns the result of the operation, \
918 without modifying the original"]
919 #[inline(always)]
920 #[cfg(not(feature = "ferrocene_subset"))]
921 pub const fn swap_bytes(self) -> Self {
922 let result = self.get().swap_bytes();
923 unsafe { Self::new_unchecked(result) }
925 }
926
927 #[doc = concat!("let n = NonZero::new(", $swap_op, stringify!($Int), ")?;")]
939 #[doc = concat!("assert_eq!(m, NonZero::new(", $reversed, ")?);")]
942 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
946 #[must_use = "this returns the result of the operation, \
947 without modifying the original"]
948 #[inline(always)]
949 #[cfg(not(feature = "ferrocene_subset"))]
950 pub const fn reverse_bits(self) -> Self {
951 let result = self.get().reverse_bits();
952 unsafe { Self::new_unchecked(result) }
954 }
955
956 #[doc = concat!("use std::num::", stringify!($Ty), ";")]
967 #[doc = concat!("let n = NonZero::new(0x1A", stringify!($Int), ")?;")]
971 #[doc = concat!(" assert_eq!(", stringify!($Ty), "::from_be(n), n)")]
974 #[doc = concat!(" assert_eq!(", stringify!($Ty), "::from_be(n), n.swap_bytes())")]
976 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
981 #[must_use]
982 #[inline(always)]
983 #[cfg(not(feature = "ferrocene_subset"))]
984 pub const fn from_be(x: Self) -> Self {
985 let result = $Int::from_be(x.get());
986 unsafe { Self::new_unchecked(result) }
988 }
989
990 #[doc = concat!("use std::num::", stringify!($Ty), ";")]
1001 #[doc = concat!("let n = NonZero::new(0x1A", stringify!($Int), ")?;")]
1005 #[doc = concat!(" assert_eq!(", stringify!($Ty), "::from_le(n), n)")]
1008 #[doc = concat!(" assert_eq!(", stringify!($Ty), "::from_le(n), n.swap_bytes())")]
1010 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
1015 #[must_use]
1016 #[inline(always)]
1017 #[cfg(not(feature = "ferrocene_subset"))]
1018 pub const fn from_le(x: Self) -> Self {
1019 let result = $Int::from_le(x.get());
1020 unsafe { Self::new_unchecked(result) }
1022 }
1023
1024 #[doc = concat!("let n = NonZero::new(0x1A", stringify!($Int), ")?;")]
1038 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
1048 #[must_use = "this returns the result of the operation, \
1049 without modifying the original"]
1050 #[inline(always)]
1051 #[cfg(not(feature = "ferrocene_subset"))]
1052 pub const fn to_be(self) -> Self {
1053 let result = self.get().to_be();
1054 unsafe { Self::new_unchecked(result) }
1056 }
1057
1058 #[doc = concat!("let n = NonZero::new(0x1A", stringify!($Int), ")?;")]
1072 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
1082 #[must_use = "this returns the result of the operation, \
1083 without modifying the original"]
1084 #[inline(always)]
1085 #[cfg(not(feature = "ferrocene_subset"))]
1086 pub const fn to_le(self) -> Self {
1087 let result = self.get().to_le();
1088 unsafe { Self::new_unchecked(result) }
1090 }
1091
1092 nonzero_integer_signedness_dependent_methods! {
1093 Primitive = $signedness $Int,
1094 SignedPrimitive = $Sint,
1095 UnsignedPrimitive = $Uint,
1096 }
1097
1098 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1110 #[doc = concat!("let four = NonZero::new(4", stringify!($Int), ")?;")]
1111 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
1112 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1119 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1120 #[must_use = "this returns the result of the operation, \
1121 without modifying the original"]
1122 #[inline]
1123 #[cfg(not(feature = "ferrocene_subset"))]
1124 pub const fn checked_mul(self, other: Self) -> Option<Self> {
1125 if let Some(result) = self.get().checked_mul(other.get()) {
1126 Some(unsafe { Self::new_unchecked(result) })
1134 } else {
1135 None
1136 }
1137 }
1138
1139 #[doc = concat!("Return [`NonZero::<", stringify!($Int), ">::MAX`] on overflow.")]
1141 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1150 #[doc = concat!("let four = NonZero::new(4", stringify!($Int), ")?;")]
1151 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
1152 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1159 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1160 #[must_use = "this returns the result of the operation, \
1161 without modifying the original"]
1162 #[inline]
1163 #[cfg(not(feature = "ferrocene_subset"))]
1164 pub const fn saturating_mul(self, other: Self) -> Self {
1165 unsafe { Self::new_unchecked(self.get().saturating_mul(other.get())) }
1174 }
1175
1176 #[doc = sign_dependent_expr!{
1182 $signedness ?
1183 if signed {
1184 concat!("`self * rhs > ", stringify!($Int), "::MAX`, ",
1185 "or `self * rhs < ", stringify!($Int), "::MIN`.")
1186 }
1187 if unsigned {
1188 concat!("`self * rhs > ", stringify!($Int), "::MAX`.")
1189 }
1190 }]
1191 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1202 #[doc = concat!("let four = NonZero::new(4", stringify!($Int), ")?;")]
1203 #[unstable(feature = "nonzero_ops", issue = "84186")]
1209 #[must_use = "this returns the result of the operation, \
1210 without modifying the original"]
1211 #[inline]
1212 #[cfg(not(feature = "ferrocene_subset"))]
1213 pub const unsafe fn unchecked_mul(self, other: Self) -> Self {
1214 unsafe { Self::new_unchecked(self.get().unchecked_mul(other.get())) }
1216 }
1217
1218 #[doc = concat!("let three = NonZero::new(3", stringify!($Int), ")?;")]
1230 #[doc = concat!("let twenty_seven = NonZero::new(27", stringify!($Int), ")?;")]
1231 #[doc = concat!("let half_max = NonZero::new(", stringify!($Int), "::MAX / 2)?;")]
1232 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1239 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1240 #[must_use = "this returns the result of the operation, \
1241 without modifying the original"]
1242 #[inline]
1243 #[cfg(not(feature = "ferrocene_subset"))]
1244 pub const fn checked_pow(self, other: u32) -> Option<Self> {
1245 if let Some(result) = self.get().checked_pow(other) {
1246 Some(unsafe { Self::new_unchecked(result) })
1254 } else {
1255 None
1256 }
1257 }
1258
1259 #[doc = sign_dependent_expr!{
1261 $signedness ?
1262 if signed {
1263 concat!("Return [`NonZero::<", stringify!($Int), ">::MIN`] ",
1264 "or [`NonZero::<", stringify!($Int), ">::MAX`] on overflow.")
1265 }
1266 if unsigned {
1267 concat!("Return [`NonZero::<", stringify!($Int), ">::MAX`] on overflow.")
1268 }
1269 }]
1270 #[doc = concat!("let three = NonZero::new(3", stringify!($Int), ")?;")]
1279 #[doc = concat!("let twenty_seven = NonZero::new(27", stringify!($Int), ")?;")]
1280 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
1281 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1288 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1289 #[must_use = "this returns the result of the operation, \
1290 without modifying the original"]
1291 #[inline]
1292 #[cfg(not(feature = "ferrocene_subset"))]
1293 pub const fn saturating_pow(self, other: u32) -> Self {
1294 unsafe { Self::new_unchecked(self.get().saturating_pow(other)) }
1303 }
1304 }
1305
1306 #[cfg(not(feature = "ferrocene_subset"))]
1307 #[stable(feature = "nonzero_parse", since = "1.35.0")]
1308 impl FromStr for NonZero<$Int> {
1309 type Err = ParseIntError;
1310 fn from_str(src: &str) -> Result<Self, Self::Err> {
1311 Self::new(<$Int>::from_str_radix(src, 10)?)
1312 .ok_or(ParseIntError {
1313 kind: IntErrorKind::Zero
1314 })
1315 }
1316 }
1317
1318 nonzero_integer_signedness_dependent_impls!($signedness $Int);
1319 };
1320
1321 (
1322 Self = $Ty:ident,
1323 Primitive = unsigned $Int:ident,
1324 SignedPrimitive = $Sint:ident,
1325 rot = $rot:literal,
1326 rot_op = $rot_op:literal,
1327 rot_result = $rot_result:literal,
1328 swap_op = $swap_op:literal,
1329 swapped = $swapped:literal,
1330 reversed = $reversed:literal,
1331 $(,)?
1332 ) => {
1333 nonzero_integer! {
1334 #[stable(feature = "nonzero", since = "1.28.0")]
1335 Self = $Ty,
1336 Primitive = unsigned $Int,
1337 SignedPrimitive = $Sint,
1338 UnsignedPrimitive = $Int,
1339 rot = $rot,
1340 rot_op = $rot_op,
1341 rot_result = $rot_result,
1342 swap_op = $swap_op,
1343 swapped = $swapped,
1344 reversed = $reversed,
1345 leading_zeros_test = concat!(stringify!($Int), "::MAX"),
1346 }
1347 };
1348
1349 (
1350 Self = $Ty:ident,
1351 Primitive = signed $Int:ident,
1352 UnsignedPrimitive = $Uint:ident,
1353 rot = $rot:literal,
1354 rot_op = $rot_op:literal,
1355 rot_result = $rot_result:literal,
1356 swap_op = $swap_op:literal,
1357 swapped = $swapped:literal,
1358 reversed = $reversed:literal,
1359 ) => {
1360 nonzero_integer! {
1361 #[stable(feature = "signed_nonzero", since = "1.34.0")]
1362 Self = $Ty,
1363 Primitive = signed $Int,
1364 SignedPrimitive = $Int,
1365 UnsignedPrimitive = $Uint,
1366 rot = $rot,
1367 rot_op = $rot_op,
1368 rot_result = $rot_result,
1369 swap_op = $swap_op,
1370 swapped = $swapped,
1371 reversed = $reversed,
1372 leading_zeros_test = concat!("-1", stringify!($Int)),
1373 }
1374 };
1375}
1376
1377macro_rules! nonzero_integer_signedness_dependent_impls {
1378 (unsigned $Int:ty) => {
1380 #[stable(feature = "nonzero_div", since = "1.51.0")]
1381 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1382 impl const Div<NonZero<$Int>> for $Int {
1383 type Output = $Int;
1384
1385 #[doc(alias = "unchecked_div")]
1391 #[inline]
1392 fn div(self, other: NonZero<$Int>) -> $Int {
1393 unsafe { intrinsics::unchecked_div(self, other.get()) }
1396 }
1397 }
1398
1399 #[cfg(not(feature = "ferrocene_subset"))]
1400 #[stable(feature = "nonzero_div_assign", since = "1.79.0")]
1401 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1402 impl const DivAssign<NonZero<$Int>> for $Int {
1403 #[inline]
1409 fn div_assign(&mut self, other: NonZero<$Int>) {
1410 *self = *self / other;
1411 }
1412 }
1413
1414 #[cfg(not(feature = "ferrocene_subset"))]
1415 #[stable(feature = "nonzero_div", since = "1.51.0")]
1416 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1417 impl const Rem<NonZero<$Int>> for $Int {
1418 type Output = $Int;
1419
1420 #[inline]
1422 fn rem(self, other: NonZero<$Int>) -> $Int {
1423 unsafe { intrinsics::unchecked_rem(self, other.get()) }
1426 }
1427 }
1428
1429 #[cfg(not(feature = "ferrocene_subset"))]
1430 #[stable(feature = "nonzero_div_assign", since = "1.79.0")]
1431 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1432 impl const RemAssign<NonZero<$Int>> for $Int {
1433 #[inline]
1435 fn rem_assign(&mut self, other: NonZero<$Int>) {
1436 *self = *self % other;
1437 }
1438 }
1439
1440 #[cfg(not(feature = "ferrocene_subset"))]
1441 impl NonZero<$Int> {
1442 #[doc = concat!("let one = NonZero::new(1", stringify!($Int), ").unwrap();")]
1451 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX).unwrap();")]
1452 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ").unwrap();")]
1455 #[doc = concat!("let three = NonZero::new(3", stringify!($Int), ").unwrap();")]
1456 #[stable(feature = "unsigned_nonzero_div_ceil", since = "1.92.0")]
1459 #[rustc_const_stable(feature = "unsigned_nonzero_div_ceil", since = "1.92.0")]
1460 #[must_use = "this returns the result of the operation, \
1461 without modifying the original"]
1462 #[inline]
1463 pub const fn div_ceil(self, rhs: Self) -> Self {
1464 let v = self.get().div_ceil(rhs.get());
1465 unsafe { Self::new_unchecked(v) }
1467 }
1468 }
1469 };
1470 (signed $Int:ty) => {
1472 #[cfg(not(feature = "ferrocene_subset"))]
1473 #[stable(feature = "signed_nonzero_neg", since = "1.71.0")]
1474 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1475 impl const Neg for NonZero<$Int> {
1476 type Output = Self;
1477
1478 #[inline]
1479 fn neg(self) -> Self {
1480 unsafe { Self::new_unchecked(self.get().neg()) }
1482 }
1483 }
1484
1485 #[cfg(not(feature = "ferrocene_subset"))]
1486 forward_ref_unop! { impl Neg, neg for NonZero<$Int>,
1487 #[stable(feature = "signed_nonzero_neg", since = "1.71.0")]
1488 #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
1489 };
1490}
1491
1492#[rustfmt::skip] macro_rules! nonzero_integer_signedness_dependent_methods {
1494 (
1496 Primitive = unsigned $Int:ident,
1497 SignedPrimitive = $Sint:ty,
1498 UnsignedPrimitive = $Uint:ty,
1499 ) => {
1500 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::MIN.get(), 1", stringify!($Int), ");")]
1509 #[stable(feature = "nonzero_min_max", since = "1.70.0")]
1511 #[cfg(not(feature = "ferrocene_subset"))]
1512 pub const MIN: Self = Self::new(1).unwrap();
1513
1514 #[doc = concat!("equal to [`", stringify!($Int), "::MAX`].")]
1517 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::MAX.get(), ", stringify!($Int), "::MAX);")]
1524 #[stable(feature = "nonzero_min_max", since = "1.70.0")]
1526 #[cfg(not(feature = "ferrocene_subset"))]
1527 pub const MAX: Self = Self::new(<$Int>::MAX).unwrap();
1528
1529 #[doc = concat!("let one = NonZero::new(1", stringify!($Int), ")?;")]
1542 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1543 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
1544 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1551 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1552 #[must_use = "this returns the result of the operation, \
1553 without modifying the original"]
1554 #[inline]
1555 #[cfg(not(feature = "ferrocene_subset"))]
1556 pub const fn checked_add(self, other: $Int) -> Option<Self> {
1557 if let Some(result) = self.get().checked_add(other) {
1558 Some(unsafe { Self::new_unchecked(result) })
1566 } else {
1567 None
1568 }
1569 }
1570
1571 #[doc = concat!("Return [`NonZero::<", stringify!($Int), ">::MAX`] on overflow.")]
1573 #[doc = concat!("let one = NonZero::new(1", stringify!($Int), ")?;")]
1582 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1583 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
1584 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1591 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1592 #[must_use = "this returns the result of the operation, \
1593 without modifying the original"]
1594 #[inline]
1595 #[cfg(not(feature = "ferrocene_subset"))]
1596 pub const fn saturating_add(self, other: $Int) -> Self {
1597 unsafe { Self::new_unchecked(self.get().saturating_add(other)) }
1605 }
1606
1607 #[doc = concat!("`self + rhs > ", stringify!($Int), "::MAX`.")]
1613 #[doc = concat!("let one = NonZero::new(1", stringify!($Int), ")?;")]
1624 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1625 #[unstable(feature = "nonzero_ops", issue = "84186")]
1631 #[must_use = "this returns the result of the operation, \
1632 without modifying the original"]
1633 #[inline]
1634 #[cfg(not(feature = "ferrocene_subset"))]
1635 pub const unsafe fn unchecked_add(self, other: $Int) -> Self {
1636 unsafe { Self::new_unchecked(self.get().unchecked_add(other)) }
1638 }
1639
1640 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1653 #[doc = concat!("let three = NonZero::new(3", stringify!($Int), ")?;")]
1654 #[doc = concat!("let four = NonZero::new(4", stringify!($Int), ")?;")]
1655 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
1656 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1664 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1665 #[must_use = "this returns the result of the operation, \
1666 without modifying the original"]
1667 #[inline]
1668 #[cfg(not(feature = "ferrocene_subset"))]
1669 pub const fn checked_next_power_of_two(self) -> Option<Self> {
1670 if let Some(nz) = self.get().checked_next_power_of_two() {
1671 Some(unsafe { Self::new_unchecked(nz) })
1674 } else {
1675 None
1676 }
1677 }
1678
1679 #[doc = concat!("[`", stringify!($Int), "::ilog2`],")]
1683 #[doc = concat!("assert_eq!(NonZero::new(7", stringify!($Int), ")?.ilog2(), 2);")]
1694 #[doc = concat!("assert_eq!(NonZero::new(8", stringify!($Int), ")?.ilog2(), 3);")]
1695 #[doc = concat!("assert_eq!(NonZero::new(9", stringify!($Int), ")?.ilog2(), 3);")]
1696 #[stable(feature = "int_log", since = "1.67.0")]
1700 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
1701 #[must_use = "this returns the result of the operation, \
1702 without modifying the original"]
1703 #[inline]
1704 pub const fn ilog2(self) -> u32 {
1705 Self::BITS - 1 - self.leading_zeros()
1706 }
1707
1708 #[doc = concat!("[`", stringify!($Int), "::ilog10`],")]
1712 #[doc = concat!("assert_eq!(NonZero::new(99", stringify!($Int), ")?.ilog10(), 1);")]
1723 #[doc = concat!("assert_eq!(NonZero::new(100", stringify!($Int), ")?.ilog10(), 2);")]
1724 #[doc = concat!("assert_eq!(NonZero::new(101", stringify!($Int), ")?.ilog10(), 2);")]
1725 #[stable(feature = "int_log", since = "1.67.0")]
1729 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
1730 #[must_use = "this returns the result of the operation, \
1731 without modifying the original"]
1732 #[inline]
1733 #[cfg(not(feature = "ferrocene_subset"))]
1734 pub const fn ilog10(self) -> u32 {
1735 super::int_log10::$Int(self)
1736 }
1737
1738 #[doc = concat!("let one = NonZero::new(1", stringify!($Int), ")?;")]
1752 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1753 #[doc = concat!("let four = NonZero::new(4", stringify!($Int), ")?;")]
1754 #[stable(feature = "num_midpoint", since = "1.85.0")]
1761 #[rustc_const_stable(feature = "num_midpoint", since = "1.85.0")]
1762 #[must_use = "this returns the result of the operation, \
1763 without modifying the original"]
1764 #[doc(alias = "average_floor")]
1765 #[doc(alias = "average")]
1766 #[inline]
1767 #[cfg(not(feature = "ferrocene_subset"))]
1768 pub const fn midpoint(self, rhs: Self) -> Self {
1769 unsafe { Self::new_unchecked(self.get().midpoint(rhs.get())) }
1774 }
1775
1776 #[doc = concat!("let eight = NonZero::new(8", stringify!($Int), ")?;")]
1789 #[doc = concat!("let ten = NonZero::new(10", stringify!($Int), ")?;")]
1791 #[must_use]
1796 #[stable(feature = "nonzero_is_power_of_two", since = "1.59.0")]
1797 #[rustc_const_stable(feature = "nonzero_is_power_of_two", since = "1.59.0")]
1798 #[inline]
1799 #[cfg(not(feature = "ferrocene_subset"))]
1800 pub const fn is_power_of_two(self) -> bool {
1801 intrinsics::ctpop(self.get()) < 2
1807 }
1808
1809 #[doc = concat!("let ten = NonZero::new(10", stringify!($Int), ")?;")]
1819 #[doc = concat!("let three = NonZero::new(3", stringify!($Int), ")?;")]
1820 #[stable(feature = "isqrt", since = "1.84.0")]
1826 #[rustc_const_stable(feature = "isqrt", since = "1.84.0")]
1827 #[must_use = "this returns the result of the operation, \
1828 without modifying the original"]
1829 #[inline]
1830 #[cfg(not(feature = "ferrocene_subset"))]
1831 pub const fn isqrt(self) -> Self {
1832 let result = self.get().isqrt();
1833
1834 unsafe { Self::new_unchecked(result) }
1840 }
1841
1842 #[doc = concat!("let n = NonZero::<", stringify!($Int), ">::MAX;")]
1850 #[doc = concat!("assert_eq!(n.cast_signed(), NonZero::new(-1", stringify!($Sint), ").unwrap());")]
1852 #[stable(feature = "integer_sign_cast", since = "1.87.0")]
1854 #[rustc_const_stable(feature = "integer_sign_cast", since = "1.87.0")]
1855 #[must_use = "this returns the result of the operation, \
1856 without modifying the original"]
1857 #[inline(always)]
1858 #[cfg(not(feature = "ferrocene_subset"))]
1859 pub const fn cast_signed(self) -> NonZero<$Sint> {
1860 unsafe { NonZero::new_unchecked(self.get().cast_signed()) }
1862 }
1863
1864 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::MIN.bit_width(), NonZero::new(1)?);")]
1876 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b111)?.bit_width(), NonZero::new(3)?);")]
1877 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1110)?.bit_width(), NonZero::new(4)?);")]
1878 #[unstable(feature = "uint_bit_width", issue = "142326")]
1882 #[must_use = "this returns the result of the operation, \
1883 without modifying the original"]
1884 #[inline(always)]
1885 #[cfg(not(feature = "ferrocene_subset"))]
1886 pub const fn bit_width(self) -> NonZero<u32> {
1887 unsafe { NonZero::new_unchecked(Self::BITS - self.leading_zeros()) }
1890 }
1891 };
1892
1893 (
1895 Primitive = signed $Int:ident,
1896 SignedPrimitive = $Sint:ty,
1897 UnsignedPrimitive = $Uint:ty,
1898 ) => {
1899 #[doc = concat!("equal to [`", stringify!($Int), "::MIN`].")]
1902 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::MIN.get(), ", stringify!($Int), "::MIN);")]
1913 #[stable(feature = "nonzero_min_max", since = "1.70.0")]
1915 #[cfg(not(feature = "ferrocene_subset"))]
1916 pub const MIN: Self = Self::new(<$Int>::MIN).unwrap();
1917
1918 #[doc = concat!("equal to [`", stringify!($Int), "::MAX`].")]
1921 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::MAX.get(), ", stringify!($Int), "::MAX);")]
1932 #[stable(feature = "nonzero_min_max", since = "1.70.0")]
1934 #[cfg(not(feature = "ferrocene_subset"))]
1935 pub const MAX: Self = Self::new(<$Int>::MAX).unwrap();
1936
1937 #[doc = concat!("See [`", stringify!($Int), "::abs`]")]
1939 #[doc = concat!("let pos = NonZero::new(1", stringify!($Int), ")?;")]
1949 #[doc = concat!("let neg = NonZero::new(-1", stringify!($Int), ")?;")]
1950 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1957 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1958 #[must_use = "this returns the result of the operation, \
1959 without modifying the original"]
1960 #[inline]
1961 #[cfg(not(feature = "ferrocene_subset"))]
1962 pub const fn abs(self) -> Self {
1963 unsafe { Self::new_unchecked(self.get().abs()) }
1965 }
1966
1967 #[doc = concat!("`self == NonZero::<", stringify!($Int), ">::MIN`.")]
1970 #[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")]
1989 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1990 #[must_use = "this returns the result of the operation, \
1991 without modifying the original"]
1992 #[inline]
1993 #[cfg(not(feature = "ferrocene_subset"))]
1994 pub const fn checked_abs(self) -> Option<Self> {
1995 if let Some(nz) = self.get().checked_abs() {
1996 Some(unsafe { Self::new_unchecked(nz) })
1998 } else {
1999 None
2000 }
2001 }
2002
2003 #[doc = concat!("[`", stringify!($Int), "::overflowing_abs`].")]
2006 #[doc = concat!("let pos = NonZero::new(1", stringify!($Int), ")?;")]
2015 #[doc = concat!("let neg = NonZero::new(-1", stringify!($Int), ")?;")]
2016 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2017 #[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 #[cfg(not(feature = "ferrocene_subset"))]
2030 pub const fn overflowing_abs(self) -> (Self, bool) {
2031 let (nz, flag) = self.get().overflowing_abs();
2032 (
2033 unsafe { Self::new_unchecked(nz) },
2035 flag,
2036 )
2037 }
2038
2039 #[doc = concat!("[`", stringify!($Int), "::saturating_abs`].")]
2041 #[doc = concat!("let pos = NonZero::new(1", stringify!($Int), ")?;")]
2050 #[doc = concat!("let neg = NonZero::new(-1", stringify!($Int), ")?;")]
2051 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2052 #[doc = concat!("let min_plus = NonZero::new(", stringify!($Int), "::MIN + 1)?;")]
2053 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
2054 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
2063 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
2064 #[must_use = "this returns the result of the operation, \
2065 without modifying the original"]
2066 #[inline]
2067 #[cfg(not(feature = "ferrocene_subset"))]
2068 pub const fn saturating_abs(self) -> Self {
2069 unsafe { Self::new_unchecked(self.get().saturating_abs()) }
2071 }
2072
2073 #[doc = concat!("[`", stringify!($Int), "::wrapping_abs`].")]
2075 #[doc = concat!("let pos = NonZero::new(1", stringify!($Int), ")?;")]
2084 #[doc = concat!("let neg = NonZero::new(-1", stringify!($Int), ")?;")]
2085 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2086 #[doc = concat!("# let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
2087 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
2096 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
2097 #[must_use = "this returns the result of the operation, \
2098 without modifying the original"]
2099 #[inline]
2100 #[cfg(not(feature = "ferrocene_subset"))]
2101 pub const fn wrapping_abs(self) -> Self {
2102 unsafe { Self::new_unchecked(self.get().wrapping_abs()) }
2104 }
2105
2106 #[doc = concat!("let u_pos = NonZero::new(1", stringify!($Uint), ")?;")]
2117 #[doc = concat!("let i_pos = NonZero::new(1", stringify!($Int), ")?;")]
2118 #[doc = concat!("let i_neg = NonZero::new(-1", stringify!($Int), ")?;")]
2119 #[doc = concat!("let i_min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2120 #[doc = concat!("let u_max = NonZero::new(", stringify!($Uint), "::MAX / 2 + 1)?;")]
2121 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
2129 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
2130 #[must_use = "this returns the result of the operation, \
2131 without modifying the original"]
2132 #[inline]
2133 #[cfg(not(feature = "ferrocene_subset"))]
2134 pub const fn unsigned_abs(self) -> NonZero<$Uint> {
2135 unsafe { NonZero::new_unchecked(self.get().unsigned_abs()) }
2137 }
2138
2139 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2150 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2151 #[must_use]
2158 #[inline]
2159 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2160 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2161 #[cfg(not(feature = "ferrocene_subset"))]
2162 pub const fn is_positive(self) -> bool {
2163 self.get().is_positive()
2164 }
2165
2166 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2177 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2178 #[must_use]
2185 #[inline]
2186 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2187 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2188 #[cfg(not(feature = "ferrocene_subset"))]
2189 pub const fn is_negative(self) -> bool {
2190 self.get().is_negative()
2191 }
2192
2193 #[doc = concat!("returning `None` if `self == NonZero::<", stringify!($Int), ">::MIN`.")]
2195 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2204 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2205 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2206 #[inline]
2213 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2214 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2215 #[cfg(not(feature = "ferrocene_subset"))]
2216 pub const fn checked_neg(self) -> Option<Self> {
2217 if let Some(result) = self.get().checked_neg() {
2218 return Some(unsafe { Self::new_unchecked(result) });
2220 }
2221 None
2222 }
2223
2224 #[doc = concat!("See [`", stringify!($Int), "::overflowing_neg`]")]
2227 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2237 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2238 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2239 #[inline]
2246 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2247 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2248 #[cfg(not(feature = "ferrocene_subset"))]
2249 pub const fn overflowing_neg(self) -> (Self, bool) {
2250 let (result, overflow) = self.get().overflowing_neg();
2251 ((unsafe { Self::new_unchecked(result) }), overflow)
2253 }
2254
2255 #[doc = concat!("returning [`NonZero::<", stringify!($Int), ">::MAX`]")]
2257 #[doc = concat!("if `self == NonZero::<", stringify!($Int), ">::MIN`")]
2258 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2268 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2269 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2270 #[doc = concat!("let min_plus_one = NonZero::new(", stringify!($Int), "::MIN + 1)?;")]
2271 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
2272 #[inline]
2280 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2281 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2282 #[cfg(not(feature = "ferrocene_subset"))]
2283 pub const fn saturating_neg(self) -> Self {
2284 if let Some(result) = self.checked_neg() {
2285 return result;
2286 }
2287 Self::MAX
2288 }
2289
2290 #[doc = concat!("See [`", stringify!($Int), "::wrapping_neg`]")]
2294 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2304 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2305 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2306 #[inline]
2313 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2314 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2315 #[cfg(not(feature = "ferrocene_subset"))]
2316 pub const fn wrapping_neg(self) -> Self {
2317 let result = self.get().wrapping_neg();
2318 unsafe { Self::new_unchecked(result) }
2320 }
2321
2322 #[doc = concat!("let n = NonZero::new(-1", stringify!($Int), ").unwrap();")]
2330 #[doc = concat!("assert_eq!(n.cast_unsigned(), NonZero::<", stringify!($Uint), ">::MAX);")]
2332 #[stable(feature = "integer_sign_cast", since = "1.87.0")]
2334 #[rustc_const_stable(feature = "integer_sign_cast", since = "1.87.0")]
2335 #[must_use = "this returns the result of the operation, \
2336 without modifying the original"]
2337 #[inline(always)]
2338 #[cfg(not(feature = "ferrocene_subset"))]
2339 pub const fn cast_unsigned(self) -> NonZero<$Uint> {
2340 unsafe { NonZero::new_unchecked(self.get().cast_unsigned()) }
2342 }
2343
2344 };
2345}
2346
2347nonzero_integer! {
2348 Self = NonZeroU8,
2349 Primitive = unsigned u8,
2350 SignedPrimitive = i8,
2351 rot = 2,
2352 rot_op = "0x82",
2353 rot_result = "0xa",
2354 swap_op = "0x12",
2355 swapped = "0x12",
2356 reversed = "0x48",
2357}
2358
2359nonzero_integer! {
2360 Self = NonZeroU16,
2361 Primitive = unsigned u16,
2362 SignedPrimitive = i16,
2363 rot = 4,
2364 rot_op = "0xa003",
2365 rot_result = "0x3a",
2366 swap_op = "0x1234",
2367 swapped = "0x3412",
2368 reversed = "0x2c48",
2369}
2370
2371nonzero_integer! {
2372 Self = NonZeroU32,
2373 Primitive = unsigned u32,
2374 SignedPrimitive = i32,
2375 rot = 8,
2376 rot_op = "0x10000b3",
2377 rot_result = "0xb301",
2378 swap_op = "0x12345678",
2379 swapped = "0x78563412",
2380 reversed = "0x1e6a2c48",
2381}
2382
2383nonzero_integer! {
2384 Self = NonZeroU64,
2385 Primitive = unsigned u64,
2386 SignedPrimitive = i64,
2387 rot = 12,
2388 rot_op = "0xaa00000000006e1",
2389 rot_result = "0x6e10aa",
2390 swap_op = "0x1234567890123456",
2391 swapped = "0x5634129078563412",
2392 reversed = "0x6a2c48091e6a2c48",
2393}
2394
2395nonzero_integer! {
2396 Self = NonZeroU128,
2397 Primitive = unsigned u128,
2398 SignedPrimitive = i128,
2399 rot = 16,
2400 rot_op = "0x13f40000000000000000000000004f76",
2401 rot_result = "0x4f7613f4",
2402 swap_op = "0x12345678901234567890123456789012",
2403 swapped = "0x12907856341290785634129078563412",
2404 reversed = "0x48091e6a2c48091e6a2c48091e6a2c48",
2405}
2406
2407#[cfg(target_pointer_width = "16")]
2408nonzero_integer! {
2409 Self = NonZeroUsize,
2410 Primitive = unsigned usize,
2411 SignedPrimitive = isize,
2412 rot = 4,
2413 rot_op = "0xa003",
2414 rot_result = "0x3a",
2415 swap_op = "0x1234",
2416 swapped = "0x3412",
2417 reversed = "0x2c48",
2418}
2419
2420#[cfg(target_pointer_width = "32")]
2421nonzero_integer! {
2422 Self = NonZeroUsize,
2423 Primitive = unsigned usize,
2424 SignedPrimitive = isize,
2425 rot = 8,
2426 rot_op = "0x10000b3",
2427 rot_result = "0xb301",
2428 swap_op = "0x12345678",
2429 swapped = "0x78563412",
2430 reversed = "0x1e6a2c48",
2431}
2432
2433#[cfg(target_pointer_width = "64")]
2434nonzero_integer! {
2435 Self = NonZeroUsize,
2436 Primitive = unsigned usize,
2437 SignedPrimitive = isize,
2438 rot = 12,
2439 rot_op = "0xaa00000000006e1",
2440 rot_result = "0x6e10aa",
2441 swap_op = "0x1234567890123456",
2442 swapped = "0x5634129078563412",
2443 reversed = "0x6a2c48091e6a2c48",
2444}
2445
2446#[cfg(not(feature = "ferrocene_subset"))]
2447nonzero_integer! {
2448 Self = NonZeroI8,
2449 Primitive = signed i8,
2450 UnsignedPrimitive = u8,
2451 rot = 2,
2452 rot_op = "-0x7e",
2453 rot_result = "0xa",
2454 swap_op = "0x12",
2455 swapped = "0x12",
2456 reversed = "0x48",
2457}
2458
2459#[cfg(not(feature = "ferrocene_subset"))]
2460nonzero_integer! {
2461 Self = NonZeroI16,
2462 Primitive = signed i16,
2463 UnsignedPrimitive = u16,
2464 rot = 4,
2465 rot_op = "-0x5ffd",
2466 rot_result = "0x3a",
2467 swap_op = "0x1234",
2468 swapped = "0x3412",
2469 reversed = "0x2c48",
2470}
2471
2472#[cfg(not(feature = "ferrocene_subset"))]
2473nonzero_integer! {
2474 Self = NonZeroI32,
2475 Primitive = signed i32,
2476 UnsignedPrimitive = u32,
2477 rot = 8,
2478 rot_op = "0x10000b3",
2479 rot_result = "0xb301",
2480 swap_op = "0x12345678",
2481 swapped = "0x78563412",
2482 reversed = "0x1e6a2c48",
2483}
2484
2485#[cfg(not(feature = "ferrocene_subset"))]
2486nonzero_integer! {
2487 Self = NonZeroI64,
2488 Primitive = signed i64,
2489 UnsignedPrimitive = u64,
2490 rot = 12,
2491 rot_op = "0xaa00000000006e1",
2492 rot_result = "0x6e10aa",
2493 swap_op = "0x1234567890123456",
2494 swapped = "0x5634129078563412",
2495 reversed = "0x6a2c48091e6a2c48",
2496}
2497
2498#[cfg(not(feature = "ferrocene_subset"))]
2499nonzero_integer! {
2500 Self = NonZeroI128,
2501 Primitive = signed i128,
2502 UnsignedPrimitive = u128,
2503 rot = 16,
2504 rot_op = "0x13f40000000000000000000000004f76",
2505 rot_result = "0x4f7613f4",
2506 swap_op = "0x12345678901234567890123456789012",
2507 swapped = "0x12907856341290785634129078563412",
2508 reversed = "0x48091e6a2c48091e6a2c48091e6a2c48",
2509}
2510
2511#[cfg(target_pointer_width = "16")]
2512#[cfg(not(feature = "ferrocene_subset"))]
2513nonzero_integer! {
2514 Self = NonZeroIsize,
2515 Primitive = signed isize,
2516 UnsignedPrimitive = usize,
2517 rot = 4,
2518 rot_op = "-0x5ffd",
2519 rot_result = "0x3a",
2520 swap_op = "0x1234",
2521 swapped = "0x3412",
2522 reversed = "0x2c48",
2523}
2524
2525#[cfg(target_pointer_width = "32")]
2526#[cfg(not(feature = "ferrocene_subset"))]
2527nonzero_integer! {
2528 Self = NonZeroIsize,
2529 Primitive = signed isize,
2530 UnsignedPrimitive = usize,
2531 rot = 8,
2532 rot_op = "0x10000b3",
2533 rot_result = "0xb301",
2534 swap_op = "0x12345678",
2535 swapped = "0x78563412",
2536 reversed = "0x1e6a2c48",
2537}
2538
2539#[cfg(target_pointer_width = "64")]
2540#[cfg(not(feature = "ferrocene_subset"))]
2541nonzero_integer! {
2542 Self = NonZeroIsize,
2543 Primitive = signed isize,
2544 UnsignedPrimitive = usize,
2545 rot = 12,
2546 rot_op = "0xaa00000000006e1",
2547 rot_result = "0x6e10aa",
2548 swap_op = "0x1234567890123456",
2549 swapped = "0x5634129078563412",
2550 reversed = "0x6a2c48091e6a2c48",
2551}