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 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 #[doc = sign_dependent_expr!{
1303 $signedness ?
1304 if signed {
1305 " `+` or `-` "
1306 }
1307 if unsigned {
1308 " `+` "
1309 }
1310 }]
1311 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::from_ascii(b\"+10\"), Ok(NonZero::new(10)?));")]
1325 #[doc = concat!("assert!(NonZero::<", stringify!($Int), ">::from_ascii(b\"1 \").is_err());")]
1337 #[cfg(not(feature = "ferrocene_subset"))]
1339 #[unstable(feature = "int_from_ascii", issue = "134821")]
1340 #[inline]
1341 pub const fn from_ascii(src: &[u8]) -> Result<Self, ParseIntError> {
1342 Self::from_ascii_radix(src, 10)
1343 }
1344
1345 #[doc = sign_dependent_expr!{
1349 $signedness ?
1350 if signed {
1351 " `+` or `-` "
1352 }
1353 if unsigned {
1354 " `+` "
1355 }
1356 }]
1357 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::from_ascii_radix(b\"A\", 16), Ok(NonZero::new(10)?));")]
1381 #[doc = concat!("assert!(NonZero::<", stringify!($Int), ">::from_ascii_radix(b\"1 \", 10).is_err());")]
1393 #[cfg(not(feature = "ferrocene_subset"))]
1395 #[unstable(feature = "int_from_ascii", issue = "134821")]
1396 #[inline]
1397 pub const fn from_ascii_radix(src: &[u8], radix: u32) -> Result<Self, ParseIntError> {
1398 let n = match <$Int>::from_ascii_radix(src, radix) {
1399 Ok(n) => n,
1400 Err(err) => return Err(err),
1401 };
1402 if let Some(n) = Self::new(n) {
1403 Ok(n)
1404 } else {
1405 Err(ParseIntError { kind: IntErrorKind::Zero })
1406 }
1407 }
1408
1409 #[doc = sign_dependent_expr!{
1413 $signedness ?
1414 if signed {
1415 " `+` or `-` "
1416 }
1417 if unsigned {
1418 " `+` "
1419 }
1420 }]
1421 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::from_str_radix(\"A\", 16), Ok(NonZero::new(10)?));")]
1445 #[doc = concat!("assert!(NonZero::<", stringify!($Int), ">::from_str_radix(\"1 \", 10).is_err());")]
1457 #[cfg(not(feature = "ferrocene_subset"))]
1459 #[unstable(feature = "nonzero_from_str_radix", issue = "152193")]
1460 #[inline]
1461 pub const fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError> {
1462 Self::from_ascii_radix(src.as_bytes(), radix)
1463 }
1464 }
1465
1466 #[cfg(not(feature = "ferrocene_subset"))]
1467 #[stable(feature = "nonzero_parse", since = "1.35.0")]
1468 impl FromStr for NonZero<$Int> {
1469 type Err = ParseIntError;
1470 fn from_str(src: &str) -> Result<Self, Self::Err> {
1471 Self::from_str_radix(src, 10)
1472 }
1473 }
1474
1475 nonzero_integer_signedness_dependent_impls!($signedness $Int);
1476 };
1477
1478 (
1479 Self = $Ty:ident,
1480 Primitive = unsigned $Int:ident,
1481 SignedPrimitive = $Sint:ident,
1482 rot = $rot:literal,
1483 rot_op = $rot_op:literal,
1484 rot_result = $rot_result:literal,
1485 swap_op = $swap_op:literal,
1486 swapped = $swapped:literal,
1487 reversed = $reversed:literal,
1488 $(,)?
1489 ) => {
1490 nonzero_integer! {
1491 #[stable(feature = "nonzero", since = "1.28.0")]
1492 Self = $Ty,
1493 Primitive = unsigned $Int,
1494 SignedPrimitive = $Sint,
1495 UnsignedPrimitive = $Int,
1496 rot = $rot,
1497 rot_op = $rot_op,
1498 rot_result = $rot_result,
1499 swap_op = $swap_op,
1500 swapped = $swapped,
1501 reversed = $reversed,
1502 leading_zeros_test = concat!(stringify!($Int), "::MAX"),
1503 }
1504 };
1505
1506 (
1507 Self = $Ty:ident,
1508 Primitive = signed $Int:ident,
1509 UnsignedPrimitive = $Uint:ident,
1510 rot = $rot:literal,
1511 rot_op = $rot_op:literal,
1512 rot_result = $rot_result:literal,
1513 swap_op = $swap_op:literal,
1514 swapped = $swapped:literal,
1515 reversed = $reversed:literal,
1516 ) => {
1517 nonzero_integer! {
1518 #[stable(feature = "signed_nonzero", since = "1.34.0")]
1519 Self = $Ty,
1520 Primitive = signed $Int,
1521 SignedPrimitive = $Int,
1522 UnsignedPrimitive = $Uint,
1523 rot = $rot,
1524 rot_op = $rot_op,
1525 rot_result = $rot_result,
1526 swap_op = $swap_op,
1527 swapped = $swapped,
1528 reversed = $reversed,
1529 leading_zeros_test = concat!("-1", stringify!($Int)),
1530 }
1531 };
1532}
1533
1534macro_rules! nonzero_integer_signedness_dependent_impls {
1535 (unsigned $Int:ty) => {
1537 #[stable(feature = "nonzero_div", since = "1.51.0")]
1538 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1539 impl const Div<NonZero<$Int>> for $Int {
1540 type Output = $Int;
1541
1542 #[doc(alias = "unchecked_div")]
1548 #[inline]
1549 fn div(self, other: NonZero<$Int>) -> $Int {
1550 unsafe { intrinsics::unchecked_div(self, other.get()) }
1553 }
1554 }
1555
1556 #[cfg(not(feature = "ferrocene_subset"))]
1557 #[stable(feature = "nonzero_div_assign", since = "1.79.0")]
1558 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1559 impl const DivAssign<NonZero<$Int>> for $Int {
1560 #[inline]
1566 fn div_assign(&mut self, other: NonZero<$Int>) {
1567 *self = *self / other;
1568 }
1569 }
1570
1571 #[cfg(not(feature = "ferrocene_subset"))]
1572 #[stable(feature = "nonzero_div", since = "1.51.0")]
1573 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1574 impl const Rem<NonZero<$Int>> for $Int {
1575 type Output = $Int;
1576
1577 #[inline]
1579 fn rem(self, other: NonZero<$Int>) -> $Int {
1580 unsafe { intrinsics::unchecked_rem(self, other.get()) }
1583 }
1584 }
1585
1586 #[cfg(not(feature = "ferrocene_subset"))]
1587 #[stable(feature = "nonzero_div_assign", since = "1.79.0")]
1588 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1589 impl const RemAssign<NonZero<$Int>> for $Int {
1590 #[inline]
1592 fn rem_assign(&mut self, other: NonZero<$Int>) {
1593 *self = *self % other;
1594 }
1595 }
1596
1597 #[cfg(not(feature = "ferrocene_subset"))]
1598 impl NonZero<$Int> {
1599 #[doc = concat!("let one = NonZero::new(1", stringify!($Int), ").unwrap();")]
1608 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX).unwrap();")]
1609 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ").unwrap();")]
1612 #[doc = concat!("let three = NonZero::new(3", stringify!($Int), ").unwrap();")]
1613 #[stable(feature = "unsigned_nonzero_div_ceil", since = "1.92.0")]
1616 #[rustc_const_stable(feature = "unsigned_nonzero_div_ceil", since = "1.92.0")]
1617 #[must_use = "this returns the result of the operation, \
1618 without modifying the original"]
1619 #[inline]
1620 pub const fn div_ceil(self, rhs: Self) -> Self {
1621 let v = self.get().div_ceil(rhs.get());
1622 unsafe { Self::new_unchecked(v) }
1624 }
1625 }
1626 };
1627 (signed $Int:ty) => {
1629 #[cfg(not(feature = "ferrocene_subset"))]
1630 #[stable(feature = "signed_nonzero_neg", since = "1.71.0")]
1631 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1632 impl const Neg for NonZero<$Int> {
1633 type Output = Self;
1634
1635 #[inline]
1636 fn neg(self) -> Self {
1637 unsafe { Self::new_unchecked(self.get().neg()) }
1639 }
1640 }
1641
1642 #[cfg(not(feature = "ferrocene_subset"))]
1643 forward_ref_unop! { impl Neg, neg for NonZero<$Int>,
1644 #[stable(feature = "signed_nonzero_neg", since = "1.71.0")]
1645 #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
1646 };
1647}
1648
1649#[rustfmt::skip] macro_rules! nonzero_integer_signedness_dependent_methods {
1651 (
1653 Primitive = unsigned $Int:ident,
1654 SignedPrimitive = $Sint:ty,
1655 UnsignedPrimitive = $Uint:ty,
1656 ) => {
1657 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::MIN.get(), 1", stringify!($Int), ");")]
1666 #[stable(feature = "nonzero_min_max", since = "1.70.0")]
1668 #[cfg(not(feature = "ferrocene_subset"))]
1669 pub const MIN: Self = Self::new(1).unwrap();
1670
1671 #[doc = concat!("equal to [`", stringify!($Int), "::MAX`].")]
1674 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::MAX.get(), ", stringify!($Int), "::MAX);")]
1681 #[stable(feature = "nonzero_min_max", since = "1.70.0")]
1683 #[cfg(not(feature = "ferrocene_subset"))]
1684 pub const MAX: Self = Self::new(<$Int>::MAX).unwrap();
1685
1686 #[doc = concat!("let one = NonZero::new(1", stringify!($Int), ")?;")]
1699 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1700 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
1701 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1708 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1709 #[must_use = "this returns the result of the operation, \
1710 without modifying the original"]
1711 #[inline]
1712 #[cfg(not(feature = "ferrocene_subset"))]
1713 pub const fn checked_add(self, other: $Int) -> Option<Self> {
1714 if let Some(result) = self.get().checked_add(other) {
1715 Some(unsafe { Self::new_unchecked(result) })
1723 } else {
1724 None
1725 }
1726 }
1727
1728 #[doc = concat!("Return [`NonZero::<", stringify!($Int), ">::MAX`] on overflow.")]
1730 #[doc = concat!("let one = NonZero::new(1", stringify!($Int), ")?;")]
1739 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1740 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
1741 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1748 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1749 #[must_use = "this returns the result of the operation, \
1750 without modifying the original"]
1751 #[inline]
1752 #[cfg(not(feature = "ferrocene_subset"))]
1753 pub const fn saturating_add(self, other: $Int) -> Self {
1754 unsafe { Self::new_unchecked(self.get().saturating_add(other)) }
1762 }
1763
1764 #[doc = concat!("`self + rhs > ", stringify!($Int), "::MAX`.")]
1770 #[doc = concat!("let one = NonZero::new(1", stringify!($Int), ")?;")]
1781 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1782 #[unstable(feature = "nonzero_ops", issue = "84186")]
1788 #[must_use = "this returns the result of the operation, \
1789 without modifying the original"]
1790 #[inline]
1791 #[cfg(not(feature = "ferrocene_subset"))]
1792 pub const unsafe fn unchecked_add(self, other: $Int) -> Self {
1793 unsafe { Self::new_unchecked(self.get().unchecked_add(other)) }
1795 }
1796
1797 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1810 #[doc = concat!("let three = NonZero::new(3", stringify!($Int), ")?;")]
1811 #[doc = concat!("let four = NonZero::new(4", stringify!($Int), ")?;")]
1812 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
1813 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1821 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1822 #[must_use = "this returns the result of the operation, \
1823 without modifying the original"]
1824 #[inline]
1825 #[cfg(not(feature = "ferrocene_subset"))]
1826 pub const fn checked_next_power_of_two(self) -> Option<Self> {
1827 if let Some(nz) = self.get().checked_next_power_of_two() {
1828 Some(unsafe { Self::new_unchecked(nz) })
1831 } else {
1832 None
1833 }
1834 }
1835
1836 #[doc = concat!("[`", stringify!($Int), "::ilog2`],")]
1840 #[doc = concat!("assert_eq!(NonZero::new(7", stringify!($Int), ")?.ilog2(), 2);")]
1851 #[doc = concat!("assert_eq!(NonZero::new(8", stringify!($Int), ")?.ilog2(), 3);")]
1852 #[doc = concat!("assert_eq!(NonZero::new(9", stringify!($Int), ")?.ilog2(), 3);")]
1853 #[stable(feature = "int_log", since = "1.67.0")]
1857 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
1858 #[must_use = "this returns the result of the operation, \
1859 without modifying the original"]
1860 #[inline]
1861 pub const fn ilog2(self) -> u32 {
1862 Self::BITS - 1 - self.leading_zeros()
1863 }
1864
1865 #[doc = concat!("[`", stringify!($Int), "::ilog10`],")]
1869 #[doc = concat!("assert_eq!(NonZero::new(99", stringify!($Int), ")?.ilog10(), 1);")]
1880 #[doc = concat!("assert_eq!(NonZero::new(100", stringify!($Int), ")?.ilog10(), 2);")]
1881 #[doc = concat!("assert_eq!(NonZero::new(101", stringify!($Int), ")?.ilog10(), 2);")]
1882 #[stable(feature = "int_log", since = "1.67.0")]
1886 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
1887 #[must_use = "this returns the result of the operation, \
1888 without modifying the original"]
1889 #[inline]
1890 pub const fn ilog10(self) -> u32 {
1891 super::int_log10::$Int(self)
1892 }
1893
1894 #[doc = concat!("let one = NonZero::new(1", stringify!($Int), ")?;")]
1908 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1909 #[doc = concat!("let four = NonZero::new(4", stringify!($Int), ")?;")]
1910 #[stable(feature = "num_midpoint", since = "1.85.0")]
1917 #[rustc_const_stable(feature = "num_midpoint", since = "1.85.0")]
1918 #[must_use = "this returns the result of the operation, \
1919 without modifying the original"]
1920 #[doc(alias = "average_floor")]
1921 #[doc(alias = "average")]
1922 #[inline]
1923 #[cfg(not(feature = "ferrocene_subset"))]
1924 pub const fn midpoint(self, rhs: Self) -> Self {
1925 unsafe { Self::new_unchecked(self.get().midpoint(rhs.get())) }
1930 }
1931
1932 #[doc = concat!("let eight = NonZero::new(8", stringify!($Int), ")?;")]
1945 #[doc = concat!("let ten = NonZero::new(10", stringify!($Int), ")?;")]
1947 #[must_use]
1952 #[stable(feature = "nonzero_is_power_of_two", since = "1.59.0")]
1953 #[rustc_const_stable(feature = "nonzero_is_power_of_two", since = "1.59.0")]
1954 #[inline]
1955 #[cfg(not(feature = "ferrocene_subset"))]
1956 pub const fn is_power_of_two(self) -> bool {
1957 intrinsics::ctpop(self.get()) < 2
1963 }
1964
1965 #[doc = concat!("let ten = NonZero::new(10", stringify!($Int), ")?;")]
1975 #[doc = concat!("let three = NonZero::new(3", stringify!($Int), ")?;")]
1976 #[stable(feature = "isqrt", since = "1.84.0")]
1982 #[rustc_const_stable(feature = "isqrt", since = "1.84.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 isqrt(self) -> Self {
1988 let result = self.get().isqrt();
1989
1990 unsafe { Self::new_unchecked(result) }
1996 }
1997
1998 #[doc = concat!("let n = NonZero::<", stringify!($Int), ">::MAX;")]
2006 #[doc = concat!("assert_eq!(n.cast_signed(), NonZero::new(-1", stringify!($Sint), ").unwrap());")]
2008 #[stable(feature = "integer_sign_cast", since = "1.87.0")]
2010 #[rustc_const_stable(feature = "integer_sign_cast", since = "1.87.0")]
2011 #[must_use = "this returns the result of the operation, \
2012 without modifying the original"]
2013 #[inline(always)]
2014 #[cfg(not(feature = "ferrocene_subset"))]
2015 pub const fn cast_signed(self) -> NonZero<$Sint> {
2016 unsafe { NonZero::new_unchecked(self.get().cast_signed()) }
2018 }
2019
2020 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::MIN.bit_width(), NonZero::new(1)?);")]
2032 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b111)?.bit_width(), NonZero::new(3)?);")]
2033 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1110)?.bit_width(), NonZero::new(4)?);")]
2034 #[unstable(feature = "uint_bit_width", issue = "142326")]
2038 #[must_use = "this returns the result of the operation, \
2039 without modifying the original"]
2040 #[inline(always)]
2041 #[cfg(not(feature = "ferrocene_subset"))]
2042 pub const fn bit_width(self) -> NonZero<u32> {
2043 unsafe { NonZero::new_unchecked(Self::BITS - self.leading_zeros()) }
2046 }
2047 };
2048
2049 (
2051 Primitive = signed $Int:ident,
2052 SignedPrimitive = $Sint:ty,
2053 UnsignedPrimitive = $Uint:ty,
2054 ) => {
2055 #[doc = concat!("equal to [`", stringify!($Int), "::MIN`].")]
2058 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::MIN.get(), ", stringify!($Int), "::MIN);")]
2069 #[stable(feature = "nonzero_min_max", since = "1.70.0")]
2071 #[cfg(not(feature = "ferrocene_subset"))]
2072 pub const MIN: Self = Self::new(<$Int>::MIN).unwrap();
2073
2074 #[doc = concat!("equal to [`", stringify!($Int), "::MAX`].")]
2077 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::MAX.get(), ", stringify!($Int), "::MAX);")]
2088 #[stable(feature = "nonzero_min_max", since = "1.70.0")]
2090 #[cfg(not(feature = "ferrocene_subset"))]
2091 pub const MAX: Self = Self::new(<$Int>::MAX).unwrap();
2092
2093 #[doc = concat!("See [`", stringify!($Int), "::abs`]")]
2095 #[doc = concat!("let pos = NonZero::new(1", stringify!($Int), ")?;")]
2105 #[doc = concat!("let neg = NonZero::new(-1", stringify!($Int), ")?;")]
2106 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
2113 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
2114 #[must_use = "this returns the result of the operation, \
2115 without modifying the original"]
2116 #[inline]
2117 #[cfg(not(feature = "ferrocene_subset"))]
2118 pub const fn abs(self) -> Self {
2119 unsafe { Self::new_unchecked(self.get().abs()) }
2121 }
2122
2123 #[doc = concat!("`self == NonZero::<", stringify!($Int), ">::MIN`.")]
2126 #[doc = concat!("let pos = NonZero::new(1", stringify!($Int), ")?;")]
2136 #[doc = concat!("let neg = NonZero::new(-1", stringify!($Int), ")?;")]
2137 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2138 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
2145 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
2146 #[must_use = "this returns the result of the operation, \
2147 without modifying the original"]
2148 #[inline]
2149 #[cfg(not(feature = "ferrocene_subset"))]
2150 pub const fn checked_abs(self) -> Option<Self> {
2151 if let Some(nz) = self.get().checked_abs() {
2152 Some(unsafe { Self::new_unchecked(nz) })
2154 } else {
2155 None
2156 }
2157 }
2158
2159 #[doc = concat!("[`", stringify!($Int), "::overflowing_abs`].")]
2162 #[doc = concat!("let pos = NonZero::new(1", stringify!($Int), ")?;")]
2171 #[doc = concat!("let neg = NonZero::new(-1", stringify!($Int), ")?;")]
2172 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2173 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
2181 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
2182 #[must_use = "this returns the result of the operation, \
2183 without modifying the original"]
2184 #[inline]
2185 #[cfg(not(feature = "ferrocene_subset"))]
2186 pub const fn overflowing_abs(self) -> (Self, bool) {
2187 let (nz, flag) = self.get().overflowing_abs();
2188 (
2189 unsafe { Self::new_unchecked(nz) },
2191 flag,
2192 )
2193 }
2194
2195 #[doc = concat!("[`", stringify!($Int), "::saturating_abs`].")]
2197 #[doc = concat!("let pos = NonZero::new(1", stringify!($Int), ")?;")]
2206 #[doc = concat!("let neg = NonZero::new(-1", stringify!($Int), ")?;")]
2207 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2208 #[doc = concat!("let min_plus = NonZero::new(", stringify!($Int), "::MIN + 1)?;")]
2209 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
2210 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
2219 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
2220 #[must_use = "this returns the result of the operation, \
2221 without modifying the original"]
2222 #[inline]
2223 #[cfg(not(feature = "ferrocene_subset"))]
2224 pub const fn saturating_abs(self) -> Self {
2225 unsafe { Self::new_unchecked(self.get().saturating_abs()) }
2227 }
2228
2229 #[doc = concat!("[`", stringify!($Int), "::wrapping_abs`].")]
2231 #[doc = concat!("let pos = NonZero::new(1", stringify!($Int), ")?;")]
2240 #[doc = concat!("let neg = NonZero::new(-1", stringify!($Int), ")?;")]
2241 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2242 #[doc = concat!("# let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
2243 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
2252 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
2253 #[must_use = "this returns the result of the operation, \
2254 without modifying the original"]
2255 #[inline]
2256 #[cfg(not(feature = "ferrocene_subset"))]
2257 pub const fn wrapping_abs(self) -> Self {
2258 unsafe { Self::new_unchecked(self.get().wrapping_abs()) }
2260 }
2261
2262 #[doc = concat!("let u_pos = NonZero::new(1", stringify!($Uint), ")?;")]
2273 #[doc = concat!("let i_pos = NonZero::new(1", stringify!($Int), ")?;")]
2274 #[doc = concat!("let i_neg = NonZero::new(-1", stringify!($Int), ")?;")]
2275 #[doc = concat!("let i_min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2276 #[doc = concat!("let u_max = NonZero::new(", stringify!($Uint), "::MAX / 2 + 1)?;")]
2277 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
2285 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
2286 #[must_use = "this returns the result of the operation, \
2287 without modifying the original"]
2288 #[inline]
2289 #[cfg(not(feature = "ferrocene_subset"))]
2290 pub const fn unsigned_abs(self) -> NonZero<$Uint> {
2291 unsafe { NonZero::new_unchecked(self.get().unsigned_abs()) }
2293 }
2294
2295 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2306 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2307 #[must_use]
2314 #[inline]
2315 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2316 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2317 #[cfg(not(feature = "ferrocene_subset"))]
2318 pub const fn is_positive(self) -> bool {
2319 self.get().is_positive()
2320 }
2321
2322 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2333 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2334 #[must_use]
2341 #[inline]
2342 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2343 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2344 #[cfg(not(feature = "ferrocene_subset"))]
2345 pub const fn is_negative(self) -> bool {
2346 self.get().is_negative()
2347 }
2348
2349 #[doc = concat!("returning `None` if `self == NonZero::<", stringify!($Int), ">::MIN`.")]
2351 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2360 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2361 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2362 #[inline]
2369 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2370 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2371 #[cfg(not(feature = "ferrocene_subset"))]
2372 pub const fn checked_neg(self) -> Option<Self> {
2373 if let Some(result) = self.get().checked_neg() {
2374 return Some(unsafe { Self::new_unchecked(result) });
2376 }
2377 None
2378 }
2379
2380 #[doc = concat!("See [`", stringify!($Int), "::overflowing_neg`]")]
2383 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2393 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2394 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2395 #[inline]
2402 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2403 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2404 #[cfg(not(feature = "ferrocene_subset"))]
2405 pub const fn overflowing_neg(self) -> (Self, bool) {
2406 let (result, overflow) = self.get().overflowing_neg();
2407 ((unsafe { Self::new_unchecked(result) }), overflow)
2409 }
2410
2411 #[doc = concat!("returning [`NonZero::<", stringify!($Int), ">::MAX`]")]
2413 #[doc = concat!("if `self == NonZero::<", stringify!($Int), ">::MIN`")]
2414 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2424 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2425 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2426 #[doc = concat!("let min_plus_one = NonZero::new(", stringify!($Int), "::MIN + 1)?;")]
2427 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
2428 #[inline]
2436 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2437 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2438 #[cfg(not(feature = "ferrocene_subset"))]
2439 pub const fn saturating_neg(self) -> Self {
2440 if let Some(result) = self.checked_neg() {
2441 return result;
2442 }
2443 Self::MAX
2444 }
2445
2446 #[doc = concat!("See [`", stringify!($Int), "::wrapping_neg`]")]
2450 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2460 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2461 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2462 #[inline]
2469 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2470 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2471 #[cfg(not(feature = "ferrocene_subset"))]
2472 pub const fn wrapping_neg(self) -> Self {
2473 let result = self.get().wrapping_neg();
2474 unsafe { Self::new_unchecked(result) }
2476 }
2477
2478 #[doc = concat!("let n = NonZero::new(-1", stringify!($Int), ").unwrap();")]
2486 #[doc = concat!("assert_eq!(n.cast_unsigned(), NonZero::<", stringify!($Uint), ">::MAX);")]
2488 #[stable(feature = "integer_sign_cast", since = "1.87.0")]
2490 #[rustc_const_stable(feature = "integer_sign_cast", since = "1.87.0")]
2491 #[must_use = "this returns the result of the operation, \
2492 without modifying the original"]
2493 #[inline(always)]
2494 #[cfg(not(feature = "ferrocene_subset"))]
2495 pub const fn cast_unsigned(self) -> NonZero<$Uint> {
2496 unsafe { NonZero::new_unchecked(self.get().cast_unsigned()) }
2498 }
2499
2500 };
2501}
2502
2503nonzero_integer! {
2504 Self = NonZeroU8,
2505 Primitive = unsigned u8,
2506 SignedPrimitive = i8,
2507 rot = 2,
2508 rot_op = "0x82",
2509 rot_result = "0xa",
2510 swap_op = "0x12",
2511 swapped = "0x12",
2512 reversed = "0x48",
2513}
2514
2515nonzero_integer! {
2516 Self = NonZeroU16,
2517 Primitive = unsigned u16,
2518 SignedPrimitive = i16,
2519 rot = 4,
2520 rot_op = "0xa003",
2521 rot_result = "0x3a",
2522 swap_op = "0x1234",
2523 swapped = "0x3412",
2524 reversed = "0x2c48",
2525}
2526
2527nonzero_integer! {
2528 Self = NonZeroU32,
2529 Primitive = unsigned u32,
2530 SignedPrimitive = i32,
2531 rot = 8,
2532 rot_op = "0x10000b3",
2533 rot_result = "0xb301",
2534 swap_op = "0x12345678",
2535 swapped = "0x78563412",
2536 reversed = "0x1e6a2c48",
2537}
2538
2539nonzero_integer! {
2540 Self = NonZeroU64,
2541 Primitive = unsigned u64,
2542 SignedPrimitive = i64,
2543 rot = 12,
2544 rot_op = "0xaa00000000006e1",
2545 rot_result = "0x6e10aa",
2546 swap_op = "0x1234567890123456",
2547 swapped = "0x5634129078563412",
2548 reversed = "0x6a2c48091e6a2c48",
2549}
2550
2551nonzero_integer! {
2552 Self = NonZeroU128,
2553 Primitive = unsigned u128,
2554 SignedPrimitive = i128,
2555 rot = 16,
2556 rot_op = "0x13f40000000000000000000000004f76",
2557 rot_result = "0x4f7613f4",
2558 swap_op = "0x12345678901234567890123456789012",
2559 swapped = "0x12907856341290785634129078563412",
2560 reversed = "0x48091e6a2c48091e6a2c48091e6a2c48",
2561}
2562
2563#[cfg(target_pointer_width = "16")]
2564nonzero_integer! {
2565 Self = NonZeroUsize,
2566 Primitive = unsigned usize,
2567 SignedPrimitive = isize,
2568 rot = 4,
2569 rot_op = "0xa003",
2570 rot_result = "0x3a",
2571 swap_op = "0x1234",
2572 swapped = "0x3412",
2573 reversed = "0x2c48",
2574}
2575
2576#[cfg(target_pointer_width = "32")]
2577nonzero_integer! {
2578 Self = NonZeroUsize,
2579 Primitive = unsigned usize,
2580 SignedPrimitive = isize,
2581 rot = 8,
2582 rot_op = "0x10000b3",
2583 rot_result = "0xb301",
2584 swap_op = "0x12345678",
2585 swapped = "0x78563412",
2586 reversed = "0x1e6a2c48",
2587}
2588
2589#[cfg(target_pointer_width = "64")]
2590nonzero_integer! {
2591 Self = NonZeroUsize,
2592 Primitive = unsigned usize,
2593 SignedPrimitive = isize,
2594 rot = 12,
2595 rot_op = "0xaa00000000006e1",
2596 rot_result = "0x6e10aa",
2597 swap_op = "0x1234567890123456",
2598 swapped = "0x5634129078563412",
2599 reversed = "0x6a2c48091e6a2c48",
2600}
2601
2602#[cfg(not(feature = "ferrocene_subset"))]
2603nonzero_integer! {
2604 Self = NonZeroI8,
2605 Primitive = signed i8,
2606 UnsignedPrimitive = u8,
2607 rot = 2,
2608 rot_op = "-0x7e",
2609 rot_result = "0xa",
2610 swap_op = "0x12",
2611 swapped = "0x12",
2612 reversed = "0x48",
2613}
2614
2615#[cfg(not(feature = "ferrocene_subset"))]
2616nonzero_integer! {
2617 Self = NonZeroI16,
2618 Primitive = signed i16,
2619 UnsignedPrimitive = u16,
2620 rot = 4,
2621 rot_op = "-0x5ffd",
2622 rot_result = "0x3a",
2623 swap_op = "0x1234",
2624 swapped = "0x3412",
2625 reversed = "0x2c48",
2626}
2627
2628#[cfg(not(feature = "ferrocene_subset"))]
2629nonzero_integer! {
2630 Self = NonZeroI32,
2631 Primitive = signed i32,
2632 UnsignedPrimitive = u32,
2633 rot = 8,
2634 rot_op = "0x10000b3",
2635 rot_result = "0xb301",
2636 swap_op = "0x12345678",
2637 swapped = "0x78563412",
2638 reversed = "0x1e6a2c48",
2639}
2640
2641#[cfg(not(feature = "ferrocene_subset"))]
2642nonzero_integer! {
2643 Self = NonZeroI64,
2644 Primitive = signed i64,
2645 UnsignedPrimitive = u64,
2646 rot = 12,
2647 rot_op = "0xaa00000000006e1",
2648 rot_result = "0x6e10aa",
2649 swap_op = "0x1234567890123456",
2650 swapped = "0x5634129078563412",
2651 reversed = "0x6a2c48091e6a2c48",
2652}
2653
2654#[cfg(not(feature = "ferrocene_subset"))]
2655nonzero_integer! {
2656 Self = NonZeroI128,
2657 Primitive = signed i128,
2658 UnsignedPrimitive = u128,
2659 rot = 16,
2660 rot_op = "0x13f40000000000000000000000004f76",
2661 rot_result = "0x4f7613f4",
2662 swap_op = "0x12345678901234567890123456789012",
2663 swapped = "0x12907856341290785634129078563412",
2664 reversed = "0x48091e6a2c48091e6a2c48091e6a2c48",
2665}
2666
2667#[cfg(target_pointer_width = "16")]
2668#[cfg(not(feature = "ferrocene_subset"))]
2669nonzero_integer! {
2670 Self = NonZeroIsize,
2671 Primitive = signed isize,
2672 UnsignedPrimitive = usize,
2673 rot = 4,
2674 rot_op = "-0x5ffd",
2675 rot_result = "0x3a",
2676 swap_op = "0x1234",
2677 swapped = "0x3412",
2678 reversed = "0x2c48",
2679}
2680
2681#[cfg(target_pointer_width = "32")]
2682#[cfg(not(feature = "ferrocene_subset"))]
2683nonzero_integer! {
2684 Self = NonZeroIsize,
2685 Primitive = signed isize,
2686 UnsignedPrimitive = usize,
2687 rot = 8,
2688 rot_op = "0x10000b3",
2689 rot_result = "0xb301",
2690 swap_op = "0x12345678",
2691 swapped = "0x78563412",
2692 reversed = "0x1e6a2c48",
2693}
2694
2695#[cfg(target_pointer_width = "64")]
2696#[cfg(not(feature = "ferrocene_subset"))]
2697nonzero_integer! {
2698 Self = NonZeroIsize,
2699 Primitive = signed isize,
2700 UnsignedPrimitive = usize,
2701 rot = 12,
2702 rot_op = "0xaa00000000006e1",
2703 rot_result = "0x6e10aa",
2704 swap_op = "0x1234567890123456",
2705 swapped = "0x5634129078563412",
2706 reversed = "0x6a2c48091e6a2c48",
2707}