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;
9use crate::hash::{Hash, Hasher};
10#[cfg(not(feature = "ferrocene_subset"))]
11use crate::marker::{Destruct, Freeze, StructuralPartialEq};
12#[cfg(not(feature = "ferrocene_subset"))]
13use crate::ops::{BitOr, BitOrAssign, Div, DivAssign, Neg, Rem, RemAssign};
14#[cfg(not(feature = "ferrocene_subset"))]
15use crate::panic::{RefUnwindSafe, UnwindSafe};
16#[cfg(not(feature = "ferrocene_subset"))]
17use crate::str::FromStr;
18#[cfg(not(feature = "ferrocene_subset"))]
19use crate::{fmt, intrinsics, ptr, ub_checks};
20
21#[cfg(feature = "ferrocene_subset")]
23#[rustfmt::skip]
24use crate::{fmt, intrinsics, ops::Div, ub_checks};
25
26#[unstable(
42 feature = "nonzero_internals",
43 reason = "implementation detail which may disappear or be replaced at any time",
44 issue = "none"
45)]
46pub unsafe trait ZeroablePrimitive: Sized + Copy + private::Sealed {
47 #[doc(hidden)]
48 type NonZeroInner: Sized + Copy;
49}
50
51macro_rules! impl_zeroable_primitive {
52 ($($NonZeroInner:ident ( $primitive:ty )),+ $(,)?) => {
53 mod private {
54 #[unstable(
55 feature = "nonzero_internals",
56 reason = "implementation detail which may disappear or be replaced at any time",
57 issue = "none"
58 )]
59 pub trait Sealed {}
60 }
61
62 $(
63 #[unstable(
64 feature = "nonzero_internals",
65 reason = "implementation detail which may disappear or be replaced at any time",
66 issue = "none"
67 )]
68 impl private::Sealed for $primitive {}
69
70 #[unstable(
71 feature = "nonzero_internals",
72 reason = "implementation detail which may disappear or be replaced at any time",
73 issue = "none"
74 )]
75 unsafe impl ZeroablePrimitive for $primitive {
76 type NonZeroInner = super::niche_types::$NonZeroInner;
77 }
78 )+
79 };
80}
81
82impl_zeroable_primitive!(
83 NonZeroU8Inner(u8),
84 NonZeroU16Inner(u16),
85 NonZeroU32Inner(u32),
86 NonZeroU64Inner(u64),
87 NonZeroU128Inner(u128),
88 NonZeroUsizeInner(usize),
89 NonZeroI8Inner(i8),
90 NonZeroI16Inner(i16),
91 NonZeroI32Inner(i32),
92 NonZeroI64Inner(i64),
93 NonZeroI128Inner(i128),
94 NonZeroIsizeInner(isize),
95 NonZeroCharInner(char),
96);
97
98#[stable(feature = "generic_nonzero", since = "1.79.0")]
137#[repr(transparent)]
138#[rustc_nonnull_optimization_guaranteed]
139#[rustc_diagnostic_item = "NonZero"]
140pub struct NonZero<T: ZeroablePrimitive>(T::NonZeroInner);
141
142macro_rules! impl_nonzero_fmt {
143 ($(#[$Attribute:meta] $Trait:ident)*) => {
144 $(
145 #[$Attribute]
146 impl<T> fmt::$Trait for NonZero<T>
147 where
148 T: ZeroablePrimitive + fmt::$Trait,
149 {
150 #[inline]
151 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
152 self.get().fmt(f)
153 }
154 }
155 )*
156 };
157}
158
159impl_nonzero_fmt! {
160 #[stable(feature = "nonzero", since = "1.28.0")]
161 Debug
162 #[stable(feature = "nonzero", since = "1.28.0")]
163 Display
164 #[stable(feature = "nonzero", since = "1.28.0")]
165 Binary
166 #[stable(feature = "nonzero", since = "1.28.0")]
167 Octal
168 #[stable(feature = "nonzero", since = "1.28.0")]
169 LowerHex
170 #[stable(feature = "nonzero", since = "1.28.0")]
171 UpperHex
172 #[stable(feature = "nonzero_fmt_exp", since = "1.84.0")]
173 LowerExp
174 #[stable(feature = "nonzero_fmt_exp", since = "1.84.0")]
175 UpperExp
176}
177
178#[cfg(not(feature = "ferrocene_subset"))]
179macro_rules! impl_nonzero_auto_trait {
180 (unsafe $Trait:ident) => {
181 #[stable(feature = "nonzero", since = "1.28.0")]
182 unsafe impl<T> $Trait for NonZero<T> where T: ZeroablePrimitive + $Trait {}
183 };
184 ($Trait:ident) => {
185 #[stable(feature = "nonzero", since = "1.28.0")]
186 impl<T> $Trait for NonZero<T> where T: ZeroablePrimitive + $Trait {}
187 };
188}
189
190#[cfg(not(feature = "ferrocene_subset"))]
193impl_nonzero_auto_trait!(unsafe Freeze);
194#[cfg(not(feature = "ferrocene_subset"))]
195impl_nonzero_auto_trait!(RefUnwindSafe);
196#[cfg(not(feature = "ferrocene_subset"))]
197impl_nonzero_auto_trait!(unsafe Send);
198#[cfg(not(feature = "ferrocene_subset"))]
199impl_nonzero_auto_trait!(unsafe Sync);
200#[cfg(not(feature = "ferrocene_subset"))]
201impl_nonzero_auto_trait!(Unpin);
202#[cfg(not(feature = "ferrocene_subset"))]
203impl_nonzero_auto_trait!(UnwindSafe);
204
205#[stable(feature = "nonzero", since = "1.28.0")]
206impl<T> Clone for NonZero<T>
207where
208 T: ZeroablePrimitive,
209{
210 #[inline]
211 fn clone(&self) -> Self {
212 *self
213 }
214}
215
216#[unstable(feature = "ergonomic_clones", issue = "132290")]
217#[cfg(not(feature = "ferrocene_subset"))]
218impl<T> UseCloned for NonZero<T> where T: ZeroablePrimitive {}
219
220#[stable(feature = "nonzero", since = "1.28.0")]
221impl<T> Copy for NonZero<T> where T: ZeroablePrimitive {}
222
223#[doc(hidden)]
224#[unstable(feature = "trivial_clone", issue = "none")]
225#[cfg(not(feature = "ferrocene_subset"))]
226unsafe impl<T> TrivialClone for NonZero<T> where T: ZeroablePrimitive {}
227
228#[stable(feature = "nonzero", since = "1.28.0")]
229#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
230impl<T> const PartialEq for NonZero<T>
231where
232 T: ZeroablePrimitive + [const] PartialEq,
233{
234 #[inline]
235 fn eq(&self, other: &Self) -> bool {
236 self.get() == other.get()
237 }
238
239 #[inline]
240 fn ne(&self, other: &Self) -> bool {
241 self.get() != other.get()
242 }
243}
244
245#[unstable(feature = "structural_match", issue = "31434")]
246#[cfg(not(feature = "ferrocene_subset"))]
247impl<T> StructuralPartialEq for NonZero<T> where T: ZeroablePrimitive + StructuralPartialEq {}
248
249#[stable(feature = "nonzero", since = "1.28.0")]
250#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
251#[cfg(not(feature = "ferrocene_subset"))]
252impl<T> const Eq for NonZero<T> where T: ZeroablePrimitive + [const] Eq {}
253
254#[stable(feature = "nonzero", since = "1.28.0")]
255#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
256#[cfg(not(feature = "ferrocene_subset"))]
257impl<T> const PartialOrd for NonZero<T>
258where
259 T: ZeroablePrimitive + [const] PartialOrd,
260{
261 #[inline]
262 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
263 self.get().partial_cmp(&other.get())
264 }
265
266 #[inline]
267 fn lt(&self, other: &Self) -> bool {
268 self.get() < other.get()
269 }
270
271 #[inline]
272 fn le(&self, other: &Self) -> bool {
273 self.get() <= other.get()
274 }
275
276 #[inline]
277 fn gt(&self, other: &Self) -> bool {
278 self.get() > other.get()
279 }
280
281 #[inline]
282 fn ge(&self, other: &Self) -> bool {
283 self.get() >= other.get()
284 }
285}
286
287#[stable(feature = "nonzero", since = "1.28.0")]
288#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
289#[cfg(not(feature = "ferrocene_subset"))]
290impl<T> const Ord for NonZero<T>
291where
292 T: ZeroablePrimitive + [const] Ord + [const] Destruct,
295{
296 #[inline]
297 fn cmp(&self, other: &Self) -> Ordering {
298 self.get().cmp(&other.get())
299 }
300
301 #[inline]
302 fn max(self, other: Self) -> Self {
303 unsafe { Self::new_unchecked(self.get().max(other.get())) }
305 }
306
307 #[inline]
308 fn min(self, other: Self) -> Self {
309 unsafe { Self::new_unchecked(self.get().min(other.get())) }
311 }
312
313 #[inline]
314 fn clamp(self, min: Self, max: Self) -> Self {
315 unsafe { Self::new_unchecked(self.get().clamp(min.get(), max.get())) }
317 }
318}
319
320#[stable(feature = "nonzero", since = "1.28.0")]
321impl<T> Hash for NonZero<T>
322where
323 T: ZeroablePrimitive + Hash,
324{
325 #[inline]
326 fn hash<H>(&self, state: &mut H)
327 where
328 H: Hasher,
329 {
330 self.get().hash(state)
331 }
332}
333
334#[stable(feature = "from_nonzero", since = "1.31.0")]
335#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
336#[cfg(not(feature = "ferrocene_subset"))]
337impl<T> const From<NonZero<T>> for T
338where
339 T: ZeroablePrimitive,
340{
341 #[inline]
342 fn from(nonzero: NonZero<T>) -> Self {
343 nonzero.get()
345 }
346}
347
348#[stable(feature = "nonzero_bitor", since = "1.45.0")]
349#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
350#[cfg(not(feature = "ferrocene_subset"))]
351impl<T> const BitOr for NonZero<T>
352where
353 T: ZeroablePrimitive + [const] BitOr<Output = T>,
354{
355 type Output = Self;
356
357 #[inline]
358 fn bitor(self, rhs: Self) -> Self::Output {
359 unsafe { Self::new_unchecked(self.get() | rhs.get()) }
361 }
362}
363
364#[stable(feature = "nonzero_bitor", since = "1.45.0")]
365#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
366#[cfg(not(feature = "ferrocene_subset"))]
367impl<T> const BitOr<T> for NonZero<T>
368where
369 T: ZeroablePrimitive + [const] BitOr<Output = T>,
370{
371 type Output = Self;
372
373 #[inline]
374 fn bitor(self, rhs: T) -> Self::Output {
375 unsafe { Self::new_unchecked(self.get() | rhs) }
377 }
378}
379
380#[stable(feature = "nonzero_bitor", since = "1.45.0")]
381#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
382#[cfg(not(feature = "ferrocene_subset"))]
383impl<T> const BitOr<NonZero<T>> for T
384where
385 T: ZeroablePrimitive + [const] BitOr<Output = T>,
386{
387 type Output = NonZero<T>;
388
389 #[inline]
390 fn bitor(self, rhs: NonZero<T>) -> Self::Output {
391 unsafe { NonZero::new_unchecked(self | rhs.get()) }
393 }
394}
395
396#[stable(feature = "nonzero_bitor", since = "1.45.0")]
397#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
398#[cfg(not(feature = "ferrocene_subset"))]
399impl<T> const BitOrAssign for NonZero<T>
400where
401 T: ZeroablePrimitive,
402 Self: [const] BitOr<Output = Self>,
403{
404 #[inline]
405 fn bitor_assign(&mut self, rhs: Self) {
406 *self = *self | rhs;
407 }
408}
409
410#[stable(feature = "nonzero_bitor", since = "1.45.0")]
411#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
412#[cfg(not(feature = "ferrocene_subset"))]
413impl<T> const BitOrAssign<T> for NonZero<T>
414where
415 T: ZeroablePrimitive,
416 Self: [const] BitOr<T, Output = Self>,
417{
418 #[inline]
419 fn bitor_assign(&mut self, rhs: T) {
420 *self = *self | rhs;
421 }
422}
423
424impl<T> NonZero<T>
425where
426 T: ZeroablePrimitive,
427{
428 #[stable(feature = "nonzero", since = "1.28.0")]
430 #[rustc_const_stable(feature = "const_nonzero_int_methods", since = "1.47.0")]
431 #[must_use]
432 #[inline]
433 pub const fn new(n: T) -> Option<Self> {
434 unsafe { intrinsics::transmute_unchecked(n) }
437 }
438
439 #[stable(feature = "nonzero", since = "1.28.0")]
446 #[rustc_const_stable(feature = "nonzero", since = "1.28.0")]
447 #[must_use]
448 #[inline]
449 #[track_caller]
450 pub const unsafe fn new_unchecked(n: T) -> Self {
451 match Self::new(n) {
452 Some(n) => n,
453 #[ferrocene::annotation(
454 "This line cannot be covered as reaching `intrinsics::unreachable` is undefined behavior."
455 )]
456 None => {
457 unsafe {
459 ub_checks::assert_unsafe_precondition!(
460 check_language_ub,
461 "NonZero::new_unchecked requires the argument to be non-zero",
462 () => false,
463 );
464 intrinsics::unreachable()
465 }
466 }
467 }
468 }
469
470 #[unstable(feature = "nonzero_from_mut", issue = "106290")]
473 #[must_use]
474 #[inline]
475 #[cfg(not(feature = "ferrocene_subset"))]
476 pub fn from_mut(n: &mut T) -> Option<&mut Self> {
477 let opt_n = unsafe { &mut *(ptr::from_mut(n).cast::<Option<Self>>()) };
480
481 opt_n.as_mut()
482 }
483
484 #[unstable(feature = "nonzero_from_mut", issue = "106290")]
492 #[must_use]
493 #[inline]
494 #[track_caller]
495 #[cfg(not(feature = "ferrocene_subset"))]
496 pub unsafe fn from_mut_unchecked(n: &mut T) -> &mut Self {
497 match Self::from_mut(n) {
498 Some(n) => n,
499 None => {
500 unsafe {
502 ub_checks::assert_unsafe_precondition!(
503 check_library_ub,
504 "NonZero::from_mut_unchecked requires the argument to dereference as non-zero",
505 () => false,
506 );
507 intrinsics::unreachable()
508 }
509 }
510 }
511 }
512
513 #[stable(feature = "nonzero", since = "1.28.0")]
515 #[rustc_const_stable(feature = "const_nonzero_get", since = "1.34.0")]
516 #[inline]
517 pub const fn get(self) -> T {
518 unsafe { intrinsics::transmute_unchecked(self) }
537 }
538}
539
540macro_rules! nonzero_integer {
541 (
542 #[$stability:meta]
543 Self = $Ty:ident,
544 Primitive = $signedness:ident $Int:ident,
545 SignedPrimitive = $Sint:ty,
546 UnsignedPrimitive = $Uint:ty,
547
548 rot = $rot:literal,
550 rot_op = $rot_op:literal,
551 rot_result = $rot_result:literal,
552 swap_op = $swap_op:literal,
553 swapped = $swapped:literal,
554 reversed = $reversed:literal,
555 leading_zeros_test = $leading_zeros_test:expr,
556 ) => {
557 #[doc = sign_dependent_expr!{
558 $signedness ?
559 if signed {
560 concat!("An [`", stringify!($Int), "`] that is known not to equal zero.")
561 }
562 if unsigned {
563 concat!("A [`", stringify!($Int), "`] that is known not to equal zero.")
564 }
565 }]
566 #[doc = concat!("For example, `Option<", stringify!($Ty), ">` is the same size as `", stringify!($Int), "`:")]
569 #[doc = concat!("assert_eq!(size_of::<Option<core::num::", stringify!($Ty), ">>(), size_of::<", stringify!($Int), ">());")]
572 #[doc = concat!("`", stringify!($Ty), "` is guaranteed to have the same layout and bit validity as `", stringify!($Int), "`")]
577 #[doc = concat!("`Option<", stringify!($Ty), ">` is guaranteed to be compatible with `", stringify!($Int), "`,")]
579 #[doc = concat!("`", stringify!($Ty), "` and `Option<", stringify!($Ty), ">`")]
583 #[doc = concat!("use std::num::", stringify!($Ty), ";")]
587 #[doc = concat!("assert_eq!(size_of::<", stringify!($Ty), ">(), size_of::<Option<", stringify!($Ty), ">>());")]
589 #[doc = concat!("assert_eq!(align_of::<", stringify!($Ty), ">(), align_of::<Option<", stringify!($Ty), ">>());")]
590 #[doc = concat!("`", stringify!($Ty), "`")]
597 #[doc = concat!("use std::num::", stringify!($Ty), ";")]
600 #[doc = concat!("const TEN: ", stringify!($Ty), " = ", stringify!($Ty) , r#"::new(10).expect("ten is non-zero");"#)]
602 #[$stability]
606 pub type $Ty = NonZero<$Int>;
607
608 impl NonZero<$Int> {
609 #[doc = concat!("This value is equal to [`", stringify!($Int), "::BITS`].")]
612 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::BITS, ", stringify!($Int), "::BITS);")]
619 #[stable(feature = "nonzero_bits", since = "1.67.0")]
621 pub const BITS: u32 = <$Int>::BITS;
622
623 #[doc = concat!("let n = NonZero::<", stringify!($Int), ">::new(", $leading_zeros_test, ")?;")]
635 #[stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
641 #[rustc_const_stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
642 #[must_use = "this returns the result of the operation, \
643 without modifying the original"]
644 #[inline]
645 pub const fn leading_zeros(self) -> u32 {
646 unsafe {
648 intrinsics::ctlz_nonzero(self.get() as $Uint)
649 }
650 }
651
652 #[doc = concat!("let n = NonZero::<", stringify!($Int), ">::new(0b0101000)?;")]
665 #[stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
671 #[rustc_const_stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
672 #[must_use = "this returns the result of the operation, \
673 without modifying the original"]
674 #[inline]
675 pub const fn trailing_zeros(self) -> u32 {
676 unsafe {
678 intrinsics::cttz_nonzero(self.get() as $Uint)
679 }
680 }
681
682 #[doc = concat!("let a = NonZero::<", stringify!($Int), ">::new(0b_01100100)?;")]
693 #[doc = concat!("let b = NonZero::<", stringify!($Int), ">::new(0b_01000000)?;")]
694 #[unstable(feature = "isolate_most_least_significant_one", issue = "136909")]
700 #[must_use = "this returns the result of the operation, \
701 without modifying the original"]
702 #[inline(always)]
703 #[cfg(not(feature = "ferrocene_subset"))]
704 pub const fn isolate_highest_one(self) -> Self {
705 unsafe {
711 let bit = (((1 as $Uint) << (<$Uint>::BITS - 1)).unchecked_shr(self.leading_zeros()));
712 NonZero::new_unchecked(bit as $Int)
713 }
714 }
715
716 #[doc = concat!("let a = NonZero::<", stringify!($Int), ">::new(0b_01100100)?;")]
727 #[doc = concat!("let b = NonZero::<", stringify!($Int), ">::new(0b_00000100)?;")]
728 #[unstable(feature = "isolate_most_least_significant_one", issue = "136909")]
734 #[must_use = "this returns the result of the operation, \
735 without modifying the original"]
736 #[inline(always)]
737 #[cfg(not(feature = "ferrocene_subset"))]
738 pub const fn isolate_lowest_one(self) -> Self {
739 let n = self.get();
740 let n = n & n.wrapping_neg();
741
742 unsafe { NonZero::new_unchecked(n) }
745 }
746
747 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1)?.highest_one(), 0);")]
758 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1_0000)?.highest_one(), 4);")]
759 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1_1111)?.highest_one(), 4);")]
760 #[unstable(feature = "int_lowest_highest_one", issue = "145203")]
764 #[must_use = "this returns the result of the operation, \
765 without modifying the original"]
766 #[inline(always)]
767 #[cfg(not(feature = "ferrocene_subset"))]
768 pub const fn highest_one(self) -> u32 {
769 Self::BITS - 1 - self.leading_zeros()
770 }
771
772 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1)?.lowest_one(), 0);")]
783 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1_0000)?.lowest_one(), 4);")]
784 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1_1111)?.lowest_one(), 0);")]
785 #[unstable(feature = "int_lowest_highest_one", issue = "145203")]
789 #[must_use = "this returns the result of the operation, \
790 without modifying the original"]
791 #[inline(always)]
792 #[cfg(not(feature = "ferrocene_subset"))]
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 #[cfg(not(feature = "ferrocene_subset"))]
823 pub const fn count_ones(self) -> NonZero<u32> {
824 unsafe { NonZero::new_unchecked(self.get().count_ones()) }
828 }
829
830 #[doc = concat!("let n = NonZero::new(", $rot_op, stringify!($Int), ")?;")]
844 #[doc = concat!("let m = NonZero::new(", $rot_result, ")?;")]
845 #[doc = concat!("assert_eq!(n.rotate_left(", $rot, "), m);")]
847 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
851 #[must_use = "this returns the result of the operation, \
852 without modifying the original"]
853 #[inline(always)]
854 #[cfg(not(feature = "ferrocene_subset"))]
855 pub const fn rotate_left(self, n: u32) -> Self {
856 let result = self.get().rotate_left(n);
857 unsafe { Self::new_unchecked(result) }
859 }
860
861 #[doc = concat!("let n = NonZero::new(", $rot_result, stringify!($Int), ")?;")]
876 #[doc = concat!("let m = NonZero::new(", $rot_op, ")?;")]
877 #[doc = concat!("assert_eq!(n.rotate_right(", $rot, "), m);")]
879 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
883 #[must_use = "this returns the result of the operation, \
884 without modifying the original"]
885 #[inline(always)]
886 #[cfg(not(feature = "ferrocene_subset"))]
887 pub const fn rotate_right(self, n: u32) -> Self {
888 let result = self.get().rotate_right(n);
889 unsafe { Self::new_unchecked(result) }
891 }
892
893 #[doc = concat!("let n = NonZero::new(", $swap_op, stringify!($Int), ")?;")]
904 #[doc = concat!("assert_eq!(m, NonZero::new(", $swapped, ")?);")]
907 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
911 #[must_use = "this returns the result of the operation, \
912 without modifying the original"]
913 #[inline(always)]
914 #[cfg(not(feature = "ferrocene_subset"))]
915 pub const fn swap_bytes(self) -> Self {
916 let result = self.get().swap_bytes();
917 unsafe { Self::new_unchecked(result) }
919 }
920
921 #[doc = concat!("let n = NonZero::new(", $swap_op, stringify!($Int), ")?;")]
933 #[doc = concat!("assert_eq!(m, NonZero::new(", $reversed, ")?);")]
936 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
940 #[must_use = "this returns the result of the operation, \
941 without modifying the original"]
942 #[inline(always)]
943 #[cfg(not(feature = "ferrocene_subset"))]
944 pub const fn reverse_bits(self) -> Self {
945 let result = self.get().reverse_bits();
946 unsafe { Self::new_unchecked(result) }
948 }
949
950 #[doc = concat!("use std::num::", stringify!($Ty), ";")]
961 #[doc = concat!("let n = NonZero::new(0x1A", stringify!($Int), ")?;")]
965 #[doc = concat!(" assert_eq!(", stringify!($Ty), "::from_be(n), n)")]
968 #[doc = concat!(" assert_eq!(", stringify!($Ty), "::from_be(n), n.swap_bytes())")]
970 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
975 #[must_use]
976 #[inline(always)]
977 #[cfg(not(feature = "ferrocene_subset"))]
978 pub const fn from_be(x: Self) -> Self {
979 let result = $Int::from_be(x.get());
980 unsafe { Self::new_unchecked(result) }
982 }
983
984 #[doc = concat!("use std::num::", stringify!($Ty), ";")]
995 #[doc = concat!("let n = NonZero::new(0x1A", stringify!($Int), ")?;")]
999 #[doc = concat!(" assert_eq!(", stringify!($Ty), "::from_le(n), n)")]
1002 #[doc = concat!(" assert_eq!(", stringify!($Ty), "::from_le(n), n.swap_bytes())")]
1004 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
1009 #[must_use]
1010 #[inline(always)]
1011 #[cfg(not(feature = "ferrocene_subset"))]
1012 pub const fn from_le(x: Self) -> Self {
1013 let result = $Int::from_le(x.get());
1014 unsafe { Self::new_unchecked(result) }
1016 }
1017
1018 #[doc = concat!("let n = NonZero::new(0x1A", stringify!($Int), ")?;")]
1032 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
1042 #[must_use = "this returns the result of the operation, \
1043 without modifying the original"]
1044 #[inline(always)]
1045 #[cfg(not(feature = "ferrocene_subset"))]
1046 pub const fn to_be(self) -> Self {
1047 let result = self.get().to_be();
1048 unsafe { Self::new_unchecked(result) }
1050 }
1051
1052 #[doc = concat!("let n = NonZero::new(0x1A", stringify!($Int), ")?;")]
1066 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
1076 #[must_use = "this returns the result of the operation, \
1077 without modifying the original"]
1078 #[inline(always)]
1079 #[cfg(not(feature = "ferrocene_subset"))]
1080 pub const fn to_le(self) -> Self {
1081 let result = self.get().to_le();
1082 unsafe { Self::new_unchecked(result) }
1084 }
1085
1086 nonzero_integer_signedness_dependent_methods! {
1087 Primitive = $signedness $Int,
1088 SignedPrimitive = $Sint,
1089 UnsignedPrimitive = $Uint,
1090 }
1091
1092 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1104 #[doc = concat!("let four = NonZero::new(4", stringify!($Int), ")?;")]
1105 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
1106 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1113 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1114 #[must_use = "this returns the result of the operation, \
1115 without modifying the original"]
1116 #[inline]
1117 #[cfg(not(feature = "ferrocene_subset"))]
1118 pub const fn checked_mul(self, other: Self) -> Option<Self> {
1119 if let Some(result) = self.get().checked_mul(other.get()) {
1120 Some(unsafe { Self::new_unchecked(result) })
1128 } else {
1129 None
1130 }
1131 }
1132
1133 #[doc = concat!("Return [`NonZero::<", stringify!($Int), ">::MAX`] on overflow.")]
1135 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1144 #[doc = concat!("let four = NonZero::new(4", stringify!($Int), ")?;")]
1145 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
1146 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1153 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1154 #[must_use = "this returns the result of the operation, \
1155 without modifying the original"]
1156 #[inline]
1157 #[cfg(not(feature = "ferrocene_subset"))]
1158 pub const fn saturating_mul(self, other: Self) -> Self {
1159 unsafe { Self::new_unchecked(self.get().saturating_mul(other.get())) }
1168 }
1169
1170 #[doc = sign_dependent_expr!{
1176 $signedness ?
1177 if signed {
1178 concat!("`self * rhs > ", stringify!($Int), "::MAX`, ",
1179 "or `self * rhs < ", stringify!($Int), "::MIN`.")
1180 }
1181 if unsigned {
1182 concat!("`self * rhs > ", stringify!($Int), "::MAX`.")
1183 }
1184 }]
1185 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1196 #[doc = concat!("let four = NonZero::new(4", stringify!($Int), ")?;")]
1197 #[unstable(feature = "nonzero_ops", issue = "84186")]
1203 #[must_use = "this returns the result of the operation, \
1204 without modifying the original"]
1205 #[inline]
1206 #[cfg(not(feature = "ferrocene_subset"))]
1207 pub const unsafe fn unchecked_mul(self, other: Self) -> Self {
1208 unsafe { Self::new_unchecked(self.get().unchecked_mul(other.get())) }
1210 }
1211
1212 #[doc = concat!("let three = NonZero::new(3", stringify!($Int), ")?;")]
1224 #[doc = concat!("let twenty_seven = NonZero::new(27", stringify!($Int), ")?;")]
1225 #[doc = concat!("let half_max = NonZero::new(", stringify!($Int), "::MAX / 2)?;")]
1226 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1233 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1234 #[must_use = "this returns the result of the operation, \
1235 without modifying the original"]
1236 #[inline]
1237 #[cfg(not(feature = "ferrocene_subset"))]
1238 pub const fn checked_pow(self, other: u32) -> Option<Self> {
1239 if let Some(result) = self.get().checked_pow(other) {
1240 Some(unsafe { Self::new_unchecked(result) })
1248 } else {
1249 None
1250 }
1251 }
1252
1253 #[doc = sign_dependent_expr!{
1255 $signedness ?
1256 if signed {
1257 concat!("Return [`NonZero::<", stringify!($Int), ">::MIN`] ",
1258 "or [`NonZero::<", stringify!($Int), ">::MAX`] on overflow.")
1259 }
1260 if unsigned {
1261 concat!("Return [`NonZero::<", stringify!($Int), ">::MAX`] on overflow.")
1262 }
1263 }]
1264 #[doc = concat!("let three = NonZero::new(3", stringify!($Int), ")?;")]
1273 #[doc = concat!("let twenty_seven = NonZero::new(27", stringify!($Int), ")?;")]
1274 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
1275 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1282 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1283 #[must_use = "this returns the result of the operation, \
1284 without modifying the original"]
1285 #[inline]
1286 #[cfg(not(feature = "ferrocene_subset"))]
1287 pub const fn saturating_pow(self, other: u32) -> Self {
1288 unsafe { Self::new_unchecked(self.get().saturating_pow(other)) }
1297 }
1298 }
1299
1300 #[cfg(not(feature = "ferrocene_subset"))]
1301 #[stable(feature = "nonzero_parse", since = "1.35.0")]
1302 impl FromStr for NonZero<$Int> {
1303 type Err = ParseIntError;
1304 fn from_str(src: &str) -> Result<Self, Self::Err> {
1305 Self::new(<$Int>::from_str_radix(src, 10)?)
1306 .ok_or(ParseIntError {
1307 kind: IntErrorKind::Zero
1308 })
1309 }
1310 }
1311
1312 nonzero_integer_signedness_dependent_impls!($signedness $Int);
1313 };
1314
1315 (
1316 Self = $Ty:ident,
1317 Primitive = unsigned $Int:ident,
1318 SignedPrimitive = $Sint:ident,
1319 rot = $rot:literal,
1320 rot_op = $rot_op:literal,
1321 rot_result = $rot_result:literal,
1322 swap_op = $swap_op:literal,
1323 swapped = $swapped:literal,
1324 reversed = $reversed:literal,
1325 $(,)?
1326 ) => {
1327 nonzero_integer! {
1328 #[stable(feature = "nonzero", since = "1.28.0")]
1329 Self = $Ty,
1330 Primitive = unsigned $Int,
1331 SignedPrimitive = $Sint,
1332 UnsignedPrimitive = $Int,
1333 rot = $rot,
1334 rot_op = $rot_op,
1335 rot_result = $rot_result,
1336 swap_op = $swap_op,
1337 swapped = $swapped,
1338 reversed = $reversed,
1339 leading_zeros_test = concat!(stringify!($Int), "::MAX"),
1340 }
1341 };
1342
1343 (
1344 Self = $Ty:ident,
1345 Primitive = signed $Int:ident,
1346 UnsignedPrimitive = $Uint:ident,
1347 rot = $rot:literal,
1348 rot_op = $rot_op:literal,
1349 rot_result = $rot_result:literal,
1350 swap_op = $swap_op:literal,
1351 swapped = $swapped:literal,
1352 reversed = $reversed:literal,
1353 ) => {
1354 nonzero_integer! {
1355 #[stable(feature = "signed_nonzero", since = "1.34.0")]
1356 Self = $Ty,
1357 Primitive = signed $Int,
1358 SignedPrimitive = $Int,
1359 UnsignedPrimitive = $Uint,
1360 rot = $rot,
1361 rot_op = $rot_op,
1362 rot_result = $rot_result,
1363 swap_op = $swap_op,
1364 swapped = $swapped,
1365 reversed = $reversed,
1366 leading_zeros_test = concat!("-1", stringify!($Int)),
1367 }
1368 };
1369}
1370
1371macro_rules! nonzero_integer_signedness_dependent_impls {
1372 (unsigned $Int:ty) => {
1374 #[stable(feature = "nonzero_div", since = "1.51.0")]
1375 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1376 impl const Div<NonZero<$Int>> for $Int {
1377 type Output = $Int;
1378
1379 #[doc(alias = "unchecked_div")]
1385 #[inline]
1386 fn div(self, other: NonZero<$Int>) -> $Int {
1387 unsafe { intrinsics::unchecked_div(self, other.get()) }
1390 }
1391 }
1392
1393 #[cfg(not(feature = "ferrocene_subset"))]
1394 #[stable(feature = "nonzero_div_assign", since = "1.79.0")]
1395 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1396 impl const DivAssign<NonZero<$Int>> for $Int {
1397 #[inline]
1403 fn div_assign(&mut self, other: NonZero<$Int>) {
1404 *self = *self / other;
1405 }
1406 }
1407
1408 #[cfg(not(feature = "ferrocene_subset"))]
1409 #[stable(feature = "nonzero_div", since = "1.51.0")]
1410 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1411 impl const Rem<NonZero<$Int>> for $Int {
1412 type Output = $Int;
1413
1414 #[inline]
1416 fn rem(self, other: NonZero<$Int>) -> $Int {
1417 unsafe { intrinsics::unchecked_rem(self, other.get()) }
1420 }
1421 }
1422
1423 #[cfg(not(feature = "ferrocene_subset"))]
1424 #[stable(feature = "nonzero_div_assign", since = "1.79.0")]
1425 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1426 impl const RemAssign<NonZero<$Int>> for $Int {
1427 #[inline]
1429 fn rem_assign(&mut self, other: NonZero<$Int>) {
1430 *self = *self % other;
1431 }
1432 }
1433
1434 #[cfg(not(feature = "ferrocene_subset"))]
1435 impl NonZero<$Int> {
1436 #[doc = concat!("let one = NonZero::new(1", stringify!($Int), ").unwrap();")]
1445 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX).unwrap();")]
1446 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ").unwrap();")]
1449 #[doc = concat!("let three = NonZero::new(3", stringify!($Int), ").unwrap();")]
1450 #[stable(feature = "unsigned_nonzero_div_ceil", since = "1.92.0")]
1453 #[rustc_const_stable(feature = "unsigned_nonzero_div_ceil", since = "1.92.0")]
1454 #[must_use = "this returns the result of the operation, \
1455 without modifying the original"]
1456 #[inline]
1457 pub const fn div_ceil(self, rhs: Self) -> Self {
1458 let v = self.get().div_ceil(rhs.get());
1459 unsafe { Self::new_unchecked(v) }
1461 }
1462 }
1463 };
1464 (signed $Int:ty) => {
1466 #[cfg(not(feature = "ferrocene_subset"))]
1467 #[stable(feature = "signed_nonzero_neg", since = "1.71.0")]
1468 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1469 impl const Neg for NonZero<$Int> {
1470 type Output = Self;
1471
1472 #[inline]
1473 fn neg(self) -> Self {
1474 unsafe { Self::new_unchecked(self.get().neg()) }
1476 }
1477 }
1478
1479 #[cfg(not(feature = "ferrocene_subset"))]
1480 forward_ref_unop! { impl Neg, neg for NonZero<$Int>,
1481 #[stable(feature = "signed_nonzero_neg", since = "1.71.0")]
1482 #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
1483 };
1484}
1485
1486#[rustfmt::skip] macro_rules! nonzero_integer_signedness_dependent_methods {
1488 (
1490 Primitive = unsigned $Int:ident,
1491 SignedPrimitive = $Sint:ty,
1492 UnsignedPrimitive = $Uint:ty,
1493 ) => {
1494 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::MIN.get(), 1", stringify!($Int), ");")]
1503 #[stable(feature = "nonzero_min_max", since = "1.70.0")]
1505 #[cfg(not(feature = "ferrocene_subset"))]
1506 pub const MIN: Self = Self::new(1).unwrap();
1507
1508 #[doc = concat!("equal to [`", stringify!($Int), "::MAX`].")]
1511 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::MAX.get(), ", stringify!($Int), "::MAX);")]
1518 #[stable(feature = "nonzero_min_max", since = "1.70.0")]
1520 #[cfg(not(feature = "ferrocene_subset"))]
1521 pub const MAX: Self = Self::new(<$Int>::MAX).unwrap();
1522
1523 #[doc = concat!("let one = NonZero::new(1", stringify!($Int), ")?;")]
1536 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1537 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
1538 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1545 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1546 #[must_use = "this returns the result of the operation, \
1547 without modifying the original"]
1548 #[inline]
1549 #[cfg(not(feature = "ferrocene_subset"))]
1550 pub const fn checked_add(self, other: $Int) -> Option<Self> {
1551 if let Some(result) = self.get().checked_add(other) {
1552 Some(unsafe { Self::new_unchecked(result) })
1560 } else {
1561 None
1562 }
1563 }
1564
1565 #[doc = concat!("Return [`NonZero::<", stringify!($Int), ">::MAX`] on overflow.")]
1567 #[doc = concat!("let one = NonZero::new(1", stringify!($Int), ")?;")]
1576 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1577 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
1578 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1585 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1586 #[must_use = "this returns the result of the operation, \
1587 without modifying the original"]
1588 #[inline]
1589 #[cfg(not(feature = "ferrocene_subset"))]
1590 pub const fn saturating_add(self, other: $Int) -> Self {
1591 unsafe { Self::new_unchecked(self.get().saturating_add(other)) }
1599 }
1600
1601 #[doc = concat!("`self + rhs > ", stringify!($Int), "::MAX`.")]
1607 #[doc = concat!("let one = NonZero::new(1", stringify!($Int), ")?;")]
1618 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1619 #[unstable(feature = "nonzero_ops", issue = "84186")]
1625 #[must_use = "this returns the result of the operation, \
1626 without modifying the original"]
1627 #[inline]
1628 #[cfg(not(feature = "ferrocene_subset"))]
1629 pub const unsafe fn unchecked_add(self, other: $Int) -> Self {
1630 unsafe { Self::new_unchecked(self.get().unchecked_add(other)) }
1632 }
1633
1634 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1647 #[doc = concat!("let three = NonZero::new(3", stringify!($Int), ")?;")]
1648 #[doc = concat!("let four = NonZero::new(4", stringify!($Int), ")?;")]
1649 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
1650 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1658 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1659 #[must_use = "this returns the result of the operation, \
1660 without modifying the original"]
1661 #[inline]
1662 #[cfg(not(feature = "ferrocene_subset"))]
1663 pub const fn checked_next_power_of_two(self) -> Option<Self> {
1664 if let Some(nz) = self.get().checked_next_power_of_two() {
1665 Some(unsafe { Self::new_unchecked(nz) })
1668 } else {
1669 None
1670 }
1671 }
1672
1673 #[doc = concat!("[`", stringify!($Int), "::ilog2`],")]
1677 #[doc = concat!("assert_eq!(NonZero::new(7", stringify!($Int), ")?.ilog2(), 2);")]
1688 #[doc = concat!("assert_eq!(NonZero::new(8", stringify!($Int), ")?.ilog2(), 3);")]
1689 #[doc = concat!("assert_eq!(NonZero::new(9", stringify!($Int), ")?.ilog2(), 3);")]
1690 #[stable(feature = "int_log", since = "1.67.0")]
1694 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
1695 #[must_use = "this returns the result of the operation, \
1696 without modifying the original"]
1697 #[inline]
1698 pub const fn ilog2(self) -> u32 {
1699 Self::BITS - 1 - self.leading_zeros()
1700 }
1701
1702 #[doc = concat!("[`", stringify!($Int), "::ilog10`],")]
1706 #[doc = concat!("assert_eq!(NonZero::new(99", stringify!($Int), ")?.ilog10(), 1);")]
1717 #[doc = concat!("assert_eq!(NonZero::new(100", stringify!($Int), ")?.ilog10(), 2);")]
1718 #[doc = concat!("assert_eq!(NonZero::new(101", stringify!($Int), ")?.ilog10(), 2);")]
1719 #[stable(feature = "int_log", since = "1.67.0")]
1723 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
1724 #[must_use = "this returns the result of the operation, \
1725 without modifying the original"]
1726 #[inline]
1727 pub const fn ilog10(self) -> u32 {
1728 super::int_log10::$Int(self)
1729 }
1730
1731 #[doc = concat!("let one = NonZero::new(1", stringify!($Int), ")?;")]
1745 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1746 #[doc = concat!("let four = NonZero::new(4", stringify!($Int), ")?;")]
1747 #[stable(feature = "num_midpoint", since = "1.85.0")]
1754 #[rustc_const_stable(feature = "num_midpoint", since = "1.85.0")]
1755 #[must_use = "this returns the result of the operation, \
1756 without modifying the original"]
1757 #[doc(alias = "average_floor")]
1758 #[doc(alias = "average")]
1759 #[inline]
1760 #[cfg(not(feature = "ferrocene_subset"))]
1761 pub const fn midpoint(self, rhs: Self) -> Self {
1762 unsafe { Self::new_unchecked(self.get().midpoint(rhs.get())) }
1767 }
1768
1769 #[doc = concat!("let eight = NonZero::new(8", stringify!($Int), ")?;")]
1782 #[doc = concat!("let ten = NonZero::new(10", stringify!($Int), ")?;")]
1784 #[must_use]
1789 #[stable(feature = "nonzero_is_power_of_two", since = "1.59.0")]
1790 #[rustc_const_stable(feature = "nonzero_is_power_of_two", since = "1.59.0")]
1791 #[inline]
1792 #[cfg(not(feature = "ferrocene_subset"))]
1793 pub const fn is_power_of_two(self) -> bool {
1794 intrinsics::ctpop(self.get()) < 2
1800 }
1801
1802 #[doc = concat!("let ten = NonZero::new(10", stringify!($Int), ")?;")]
1812 #[doc = concat!("let three = NonZero::new(3", stringify!($Int), ")?;")]
1813 #[stable(feature = "isqrt", since = "1.84.0")]
1819 #[rustc_const_stable(feature = "isqrt", since = "1.84.0")]
1820 #[must_use = "this returns the result of the operation, \
1821 without modifying the original"]
1822 #[inline]
1823 #[cfg(not(feature = "ferrocene_subset"))]
1824 pub const fn isqrt(self) -> Self {
1825 let result = self.get().isqrt();
1826
1827 unsafe { Self::new_unchecked(result) }
1833 }
1834
1835 #[doc = concat!("let n = NonZero::<", stringify!($Int), ">::MAX;")]
1843 #[doc = concat!("assert_eq!(n.cast_signed(), NonZero::new(-1", stringify!($Sint), ").unwrap());")]
1845 #[stable(feature = "integer_sign_cast", since = "1.87.0")]
1847 #[rustc_const_stable(feature = "integer_sign_cast", since = "1.87.0")]
1848 #[must_use = "this returns the result of the operation, \
1849 without modifying the original"]
1850 #[inline(always)]
1851 #[cfg(not(feature = "ferrocene_subset"))]
1852 pub const fn cast_signed(self) -> NonZero<$Sint> {
1853 unsafe { NonZero::new_unchecked(self.get().cast_signed()) }
1855 }
1856
1857 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::MIN.bit_width(), NonZero::new(1)?);")]
1869 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b111)?.bit_width(), NonZero::new(3)?);")]
1870 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1110)?.bit_width(), NonZero::new(4)?);")]
1871 #[unstable(feature = "uint_bit_width", issue = "142326")]
1875 #[must_use = "this returns the result of the operation, \
1876 without modifying the original"]
1877 #[inline(always)]
1878 #[cfg(not(feature = "ferrocene_subset"))]
1879 pub const fn bit_width(self) -> NonZero<u32> {
1880 unsafe { NonZero::new_unchecked(Self::BITS - self.leading_zeros()) }
1883 }
1884 };
1885
1886 (
1888 Primitive = signed $Int:ident,
1889 SignedPrimitive = $Sint:ty,
1890 UnsignedPrimitive = $Uint:ty,
1891 ) => {
1892 #[doc = concat!("equal to [`", stringify!($Int), "::MIN`].")]
1895 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::MIN.get(), ", stringify!($Int), "::MIN);")]
1906 #[stable(feature = "nonzero_min_max", since = "1.70.0")]
1908 #[cfg(not(feature = "ferrocene_subset"))]
1909 pub const MIN: Self = Self::new(<$Int>::MIN).unwrap();
1910
1911 #[doc = concat!("equal to [`", stringify!($Int), "::MAX`].")]
1914 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::MAX.get(), ", stringify!($Int), "::MAX);")]
1925 #[stable(feature = "nonzero_min_max", since = "1.70.0")]
1927 #[cfg(not(feature = "ferrocene_subset"))]
1928 pub const MAX: Self = Self::new(<$Int>::MAX).unwrap();
1929
1930 #[doc = concat!("See [`", stringify!($Int), "::abs`]")]
1932 #[doc = concat!("let pos = NonZero::new(1", stringify!($Int), ")?;")]
1942 #[doc = concat!("let neg = NonZero::new(-1", stringify!($Int), ")?;")]
1943 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1950 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1951 #[must_use = "this returns the result of the operation, \
1952 without modifying the original"]
1953 #[inline]
1954 #[cfg(not(feature = "ferrocene_subset"))]
1955 pub const fn abs(self) -> Self {
1956 unsafe { Self::new_unchecked(self.get().abs()) }
1958 }
1959
1960 #[doc = concat!("`self == NonZero::<", stringify!($Int), ">::MIN`.")]
1963 #[doc = concat!("let pos = NonZero::new(1", stringify!($Int), ")?;")]
1973 #[doc = concat!("let neg = NonZero::new(-1", stringify!($Int), ")?;")]
1974 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
1975 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1982 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1983 #[must_use = "this returns the result of the operation, \
1984 without modifying the original"]
1985 #[inline]
1986 #[cfg(not(feature = "ferrocene_subset"))]
1987 pub const fn checked_abs(self) -> Option<Self> {
1988 if let Some(nz) = self.get().checked_abs() {
1989 Some(unsafe { Self::new_unchecked(nz) })
1991 } else {
1992 None
1993 }
1994 }
1995
1996 #[doc = concat!("[`", stringify!($Int), "::overflowing_abs`].")]
1999 #[doc = concat!("let pos = NonZero::new(1", stringify!($Int), ")?;")]
2008 #[doc = concat!("let neg = NonZero::new(-1", stringify!($Int), ")?;")]
2009 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2010 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
2018 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
2019 #[must_use = "this returns the result of the operation, \
2020 without modifying the original"]
2021 #[inline]
2022 #[cfg(not(feature = "ferrocene_subset"))]
2023 pub const fn overflowing_abs(self) -> (Self, bool) {
2024 let (nz, flag) = self.get().overflowing_abs();
2025 (
2026 unsafe { Self::new_unchecked(nz) },
2028 flag,
2029 )
2030 }
2031
2032 #[doc = concat!("[`", stringify!($Int), "::saturating_abs`].")]
2034 #[doc = concat!("let pos = NonZero::new(1", stringify!($Int), ")?;")]
2043 #[doc = concat!("let neg = NonZero::new(-1", stringify!($Int), ")?;")]
2044 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2045 #[doc = concat!("let min_plus = NonZero::new(", stringify!($Int), "::MIN + 1)?;")]
2046 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
2047 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
2056 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
2057 #[must_use = "this returns the result of the operation, \
2058 without modifying the original"]
2059 #[inline]
2060 #[cfg(not(feature = "ferrocene_subset"))]
2061 pub const fn saturating_abs(self) -> Self {
2062 unsafe { Self::new_unchecked(self.get().saturating_abs()) }
2064 }
2065
2066 #[doc = concat!("[`", stringify!($Int), "::wrapping_abs`].")]
2068 #[doc = concat!("let pos = NonZero::new(1", stringify!($Int), ")?;")]
2077 #[doc = concat!("let neg = NonZero::new(-1", stringify!($Int), ")?;")]
2078 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2079 #[doc = concat!("# let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
2080 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
2089 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
2090 #[must_use = "this returns the result of the operation, \
2091 without modifying the original"]
2092 #[inline]
2093 #[cfg(not(feature = "ferrocene_subset"))]
2094 pub const fn wrapping_abs(self) -> Self {
2095 unsafe { Self::new_unchecked(self.get().wrapping_abs()) }
2097 }
2098
2099 #[doc = concat!("let u_pos = NonZero::new(1", stringify!($Uint), ")?;")]
2110 #[doc = concat!("let i_pos = NonZero::new(1", stringify!($Int), ")?;")]
2111 #[doc = concat!("let i_neg = NonZero::new(-1", stringify!($Int), ")?;")]
2112 #[doc = concat!("let i_min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2113 #[doc = concat!("let u_max = NonZero::new(", stringify!($Uint), "::MAX / 2 + 1)?;")]
2114 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
2122 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
2123 #[must_use = "this returns the result of the operation, \
2124 without modifying the original"]
2125 #[inline]
2126 #[cfg(not(feature = "ferrocene_subset"))]
2127 pub const fn unsigned_abs(self) -> NonZero<$Uint> {
2128 unsafe { NonZero::new_unchecked(self.get().unsigned_abs()) }
2130 }
2131
2132 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2143 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2144 #[must_use]
2151 #[inline]
2152 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2153 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2154 #[cfg(not(feature = "ferrocene_subset"))]
2155 pub const fn is_positive(self) -> bool {
2156 self.get().is_positive()
2157 }
2158
2159 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2170 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2171 #[must_use]
2178 #[inline]
2179 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2180 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2181 #[cfg(not(feature = "ferrocene_subset"))]
2182 pub const fn is_negative(self) -> bool {
2183 self.get().is_negative()
2184 }
2185
2186 #[doc = concat!("returning `None` if `self == NonZero::<", stringify!($Int), ">::MIN`.")]
2188 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2197 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2198 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2199 #[inline]
2206 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2207 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2208 #[cfg(not(feature = "ferrocene_subset"))]
2209 pub const fn checked_neg(self) -> Option<Self> {
2210 if let Some(result) = self.get().checked_neg() {
2211 return Some(unsafe { Self::new_unchecked(result) });
2213 }
2214 None
2215 }
2216
2217 #[doc = concat!("See [`", stringify!($Int), "::overflowing_neg`]")]
2220 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2230 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2231 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2232 #[inline]
2239 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2240 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2241 #[cfg(not(feature = "ferrocene_subset"))]
2242 pub const fn overflowing_neg(self) -> (Self, bool) {
2243 let (result, overflow) = self.get().overflowing_neg();
2244 ((unsafe { Self::new_unchecked(result) }), overflow)
2246 }
2247
2248 #[doc = concat!("returning [`NonZero::<", stringify!($Int), ">::MAX`]")]
2250 #[doc = concat!("if `self == NonZero::<", stringify!($Int), ">::MIN`")]
2251 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2261 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2262 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2263 #[doc = concat!("let min_plus_one = NonZero::new(", stringify!($Int), "::MIN + 1)?;")]
2264 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
2265 #[inline]
2273 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2274 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2275 #[cfg(not(feature = "ferrocene_subset"))]
2276 pub const fn saturating_neg(self) -> Self {
2277 if let Some(result) = self.checked_neg() {
2278 return result;
2279 }
2280 Self::MAX
2281 }
2282
2283 #[doc = concat!("See [`", stringify!($Int), "::wrapping_neg`]")]
2287 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2297 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2298 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2299 #[inline]
2306 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2307 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2308 #[cfg(not(feature = "ferrocene_subset"))]
2309 pub const fn wrapping_neg(self) -> Self {
2310 let result = self.get().wrapping_neg();
2311 unsafe { Self::new_unchecked(result) }
2313 }
2314
2315 #[doc = concat!("let n = NonZero::new(-1", stringify!($Int), ").unwrap();")]
2323 #[doc = concat!("assert_eq!(n.cast_unsigned(), NonZero::<", stringify!($Uint), ">::MAX);")]
2325 #[stable(feature = "integer_sign_cast", since = "1.87.0")]
2327 #[rustc_const_stable(feature = "integer_sign_cast", since = "1.87.0")]
2328 #[must_use = "this returns the result of the operation, \
2329 without modifying the original"]
2330 #[inline(always)]
2331 #[cfg(not(feature = "ferrocene_subset"))]
2332 pub const fn cast_unsigned(self) -> NonZero<$Uint> {
2333 unsafe { NonZero::new_unchecked(self.get().cast_unsigned()) }
2335 }
2336
2337 };
2338}
2339
2340nonzero_integer! {
2341 Self = NonZeroU8,
2342 Primitive = unsigned u8,
2343 SignedPrimitive = i8,
2344 rot = 2,
2345 rot_op = "0x82",
2346 rot_result = "0xa",
2347 swap_op = "0x12",
2348 swapped = "0x12",
2349 reversed = "0x48",
2350}
2351
2352nonzero_integer! {
2353 Self = NonZeroU16,
2354 Primitive = unsigned u16,
2355 SignedPrimitive = i16,
2356 rot = 4,
2357 rot_op = "0xa003",
2358 rot_result = "0x3a",
2359 swap_op = "0x1234",
2360 swapped = "0x3412",
2361 reversed = "0x2c48",
2362}
2363
2364nonzero_integer! {
2365 Self = NonZeroU32,
2366 Primitive = unsigned u32,
2367 SignedPrimitive = i32,
2368 rot = 8,
2369 rot_op = "0x10000b3",
2370 rot_result = "0xb301",
2371 swap_op = "0x12345678",
2372 swapped = "0x78563412",
2373 reversed = "0x1e6a2c48",
2374}
2375
2376nonzero_integer! {
2377 Self = NonZeroU64,
2378 Primitive = unsigned u64,
2379 SignedPrimitive = i64,
2380 rot = 12,
2381 rot_op = "0xaa00000000006e1",
2382 rot_result = "0x6e10aa",
2383 swap_op = "0x1234567890123456",
2384 swapped = "0x5634129078563412",
2385 reversed = "0x6a2c48091e6a2c48",
2386}
2387
2388nonzero_integer! {
2389 Self = NonZeroU128,
2390 Primitive = unsigned u128,
2391 SignedPrimitive = i128,
2392 rot = 16,
2393 rot_op = "0x13f40000000000000000000000004f76",
2394 rot_result = "0x4f7613f4",
2395 swap_op = "0x12345678901234567890123456789012",
2396 swapped = "0x12907856341290785634129078563412",
2397 reversed = "0x48091e6a2c48091e6a2c48091e6a2c48",
2398}
2399
2400#[cfg(target_pointer_width = "16")]
2401nonzero_integer! {
2402 Self = NonZeroUsize,
2403 Primitive = unsigned usize,
2404 SignedPrimitive = isize,
2405 rot = 4,
2406 rot_op = "0xa003",
2407 rot_result = "0x3a",
2408 swap_op = "0x1234",
2409 swapped = "0x3412",
2410 reversed = "0x2c48",
2411}
2412
2413#[cfg(target_pointer_width = "32")]
2414nonzero_integer! {
2415 Self = NonZeroUsize,
2416 Primitive = unsigned usize,
2417 SignedPrimitive = isize,
2418 rot = 8,
2419 rot_op = "0x10000b3",
2420 rot_result = "0xb301",
2421 swap_op = "0x12345678",
2422 swapped = "0x78563412",
2423 reversed = "0x1e6a2c48",
2424}
2425
2426#[cfg(target_pointer_width = "64")]
2427nonzero_integer! {
2428 Self = NonZeroUsize,
2429 Primitive = unsigned usize,
2430 SignedPrimitive = isize,
2431 rot = 12,
2432 rot_op = "0xaa00000000006e1",
2433 rot_result = "0x6e10aa",
2434 swap_op = "0x1234567890123456",
2435 swapped = "0x5634129078563412",
2436 reversed = "0x6a2c48091e6a2c48",
2437}
2438
2439#[cfg(not(feature = "ferrocene_subset"))]
2440nonzero_integer! {
2441 Self = NonZeroI8,
2442 Primitive = signed i8,
2443 UnsignedPrimitive = u8,
2444 rot = 2,
2445 rot_op = "-0x7e",
2446 rot_result = "0xa",
2447 swap_op = "0x12",
2448 swapped = "0x12",
2449 reversed = "0x48",
2450}
2451
2452#[cfg(not(feature = "ferrocene_subset"))]
2453nonzero_integer! {
2454 Self = NonZeroI16,
2455 Primitive = signed i16,
2456 UnsignedPrimitive = u16,
2457 rot = 4,
2458 rot_op = "-0x5ffd",
2459 rot_result = "0x3a",
2460 swap_op = "0x1234",
2461 swapped = "0x3412",
2462 reversed = "0x2c48",
2463}
2464
2465#[cfg(not(feature = "ferrocene_subset"))]
2466nonzero_integer! {
2467 Self = NonZeroI32,
2468 Primitive = signed i32,
2469 UnsignedPrimitive = u32,
2470 rot = 8,
2471 rot_op = "0x10000b3",
2472 rot_result = "0xb301",
2473 swap_op = "0x12345678",
2474 swapped = "0x78563412",
2475 reversed = "0x1e6a2c48",
2476}
2477
2478#[cfg(not(feature = "ferrocene_subset"))]
2479nonzero_integer! {
2480 Self = NonZeroI64,
2481 Primitive = signed i64,
2482 UnsignedPrimitive = u64,
2483 rot = 12,
2484 rot_op = "0xaa00000000006e1",
2485 rot_result = "0x6e10aa",
2486 swap_op = "0x1234567890123456",
2487 swapped = "0x5634129078563412",
2488 reversed = "0x6a2c48091e6a2c48",
2489}
2490
2491#[cfg(not(feature = "ferrocene_subset"))]
2492nonzero_integer! {
2493 Self = NonZeroI128,
2494 Primitive = signed i128,
2495 UnsignedPrimitive = u128,
2496 rot = 16,
2497 rot_op = "0x13f40000000000000000000000004f76",
2498 rot_result = "0x4f7613f4",
2499 swap_op = "0x12345678901234567890123456789012",
2500 swapped = "0x12907856341290785634129078563412",
2501 reversed = "0x48091e6a2c48091e6a2c48091e6a2c48",
2502}
2503
2504#[cfg(target_pointer_width = "16")]
2505#[cfg(not(feature = "ferrocene_subset"))]
2506nonzero_integer! {
2507 Self = NonZeroIsize,
2508 Primitive = signed isize,
2509 UnsignedPrimitive = usize,
2510 rot = 4,
2511 rot_op = "-0x5ffd",
2512 rot_result = "0x3a",
2513 swap_op = "0x1234",
2514 swapped = "0x3412",
2515 reversed = "0x2c48",
2516}
2517
2518#[cfg(target_pointer_width = "32")]
2519#[cfg(not(feature = "ferrocene_subset"))]
2520nonzero_integer! {
2521 Self = NonZeroIsize,
2522 Primitive = signed isize,
2523 UnsignedPrimitive = usize,
2524 rot = 8,
2525 rot_op = "0x10000b3",
2526 rot_result = "0xb301",
2527 swap_op = "0x12345678",
2528 swapped = "0x78563412",
2529 reversed = "0x1e6a2c48",
2530}
2531
2532#[cfg(target_pointer_width = "64")]
2533#[cfg(not(feature = "ferrocene_subset"))]
2534nonzero_integer! {
2535 Self = NonZeroIsize,
2536 Primitive = signed isize,
2537 UnsignedPrimitive = usize,
2538 rot = 12,
2539 rot_op = "0xaa00000000006e1",
2540 rot_result = "0x6e10aa",
2541 swap_op = "0x1234567890123456",
2542 swapped = "0x5634129078563412",
2543 reversed = "0x6a2c48091e6a2c48",
2544}