1use super::{IntErrorKind, ParseIntError};
4use crate::clone::{TrivialClone, UseCloned};
5use crate::cmp::Ordering;
6use crate::hash::{Hash, Hasher};
7use crate::marker::{Destruct, Freeze, StructuralPartialEq};
8use crate::num::imp;
9use crate::ops::{BitOr, BitOrAssign, Div, DivAssign, Neg, Rem, RemAssign};
10use crate::panic::{RefUnwindSafe, UnwindSafe};
11use crate::str::FromStr;
12use crate::{fmt, intrinsics, ptr, ub_checks};
13
14#[unstable(
30 feature = "nonzero_internals",
31 reason = "implementation detail which may disappear or be replaced at any time",
32 issue = "none"
33)]
34pub unsafe trait ZeroablePrimitive: Sized + Copy + private::Sealed {
35 type NonZeroInner: Sized + Copy;
37}
38
39macro_rules! impl_zeroable_primitive {
40 ($($NonZeroInner:ident ( $primitive:ty )),+ $(,)?) => {
41 mod private {
42 #[unstable(
43 feature = "nonzero_internals",
44 reason = "implementation detail which may disappear or be replaced at any time",
45 issue = "none"
46 )]
47 pub trait Sealed {}
48 }
49
50 $(
51 #[unstable(
52 feature = "nonzero_internals",
53 reason = "implementation detail which may disappear or be replaced at any time",
54 issue = "none"
55 )]
56 impl private::Sealed for $primitive {}
57
58 #[unstable(
59 feature = "nonzero_internals",
60 reason = "implementation detail which may disappear or be replaced at any time",
61 issue = "none"
62 )]
63 unsafe impl ZeroablePrimitive for $primitive {
64 type NonZeroInner = super::niche_types::$NonZeroInner;
65 }
66 )+
67 };
68}
69
70mod private {
#[unstable(feature = "nonzero_internals", reason =
"implementation detail which may disappear or be replaced at any time",
issue = "none")]
pub trait Sealed { }
}
#[unstable(feature = "nonzero_internals", reason =
"implementation detail which may disappear or be replaced at any time", issue
= "none")]
impl private::Sealed for u8 { }
#[unstable(feature = "nonzero_internals", reason =
"implementation detail which may disappear or be replaced at any time", issue
= "none")]
unsafe impl ZeroablePrimitive for u8 {
type NonZeroInner = super::niche_types::NonZeroU8Inner;
}
#[unstable(feature = "nonzero_internals", reason =
"implementation detail which may disappear or be replaced at any time", issue
= "none")]
impl private::Sealed for u16 { }
#[unstable(feature = "nonzero_internals", reason =
"implementation detail which may disappear or be replaced at any time", issue
= "none")]
unsafe impl ZeroablePrimitive for u16 {
type NonZeroInner = super::niche_types::NonZeroU16Inner;
}
#[unstable(feature = "nonzero_internals", reason =
"implementation detail which may disappear or be replaced at any time", issue
= "none")]
impl private::Sealed for u32 { }
#[unstable(feature = "nonzero_internals", reason =
"implementation detail which may disappear or be replaced at any time", issue
= "none")]
unsafe impl ZeroablePrimitive for u32 {
type NonZeroInner = super::niche_types::NonZeroU32Inner;
}
#[unstable(feature = "nonzero_internals", reason =
"implementation detail which may disappear or be replaced at any time", issue
= "none")]
impl private::Sealed for u64 { }
#[unstable(feature = "nonzero_internals", reason =
"implementation detail which may disappear or be replaced at any time", issue
= "none")]
unsafe impl ZeroablePrimitive for u64 {
type NonZeroInner = super::niche_types::NonZeroU64Inner;
}
#[unstable(feature = "nonzero_internals", reason =
"implementation detail which may disappear or be replaced at any time", issue
= "none")]
impl private::Sealed for u128 { }
#[unstable(feature = "nonzero_internals", reason =
"implementation detail which may disappear or be replaced at any time", issue
= "none")]
unsafe impl ZeroablePrimitive for u128 {
type NonZeroInner = super::niche_types::NonZeroU128Inner;
}
#[unstable(feature = "nonzero_internals", reason =
"implementation detail which may disappear or be replaced at any time", issue
= "none")]
impl private::Sealed for usize { }
#[unstable(feature = "nonzero_internals", reason =
"implementation detail which may disappear or be replaced at any time", issue
= "none")]
unsafe impl ZeroablePrimitive for usize {
type NonZeroInner = super::niche_types::NonZeroUsizeInner;
}
#[unstable(feature = "nonzero_internals", reason =
"implementation detail which may disappear or be replaced at any time", issue
= "none")]
impl private::Sealed for i8 { }
#[unstable(feature = "nonzero_internals", reason =
"implementation detail which may disappear or be replaced at any time", issue
= "none")]
unsafe impl ZeroablePrimitive for i8 {
type NonZeroInner = super::niche_types::NonZeroI8Inner;
}
#[unstable(feature = "nonzero_internals", reason =
"implementation detail which may disappear or be replaced at any time", issue
= "none")]
impl private::Sealed for i16 { }
#[unstable(feature = "nonzero_internals", reason =
"implementation detail which may disappear or be replaced at any time", issue
= "none")]
unsafe impl ZeroablePrimitive for i16 {
type NonZeroInner = super::niche_types::NonZeroI16Inner;
}
#[unstable(feature = "nonzero_internals", reason =
"implementation detail which may disappear or be replaced at any time", issue
= "none")]
impl private::Sealed for i32 { }
#[unstable(feature = "nonzero_internals", reason =
"implementation detail which may disappear or be replaced at any time", issue
= "none")]
unsafe impl ZeroablePrimitive for i32 {
type NonZeroInner = super::niche_types::NonZeroI32Inner;
}
#[unstable(feature = "nonzero_internals", reason =
"implementation detail which may disappear or be replaced at any time", issue
= "none")]
impl private::Sealed for i64 { }
#[unstable(feature = "nonzero_internals", reason =
"implementation detail which may disappear or be replaced at any time", issue
= "none")]
unsafe impl ZeroablePrimitive for i64 {
type NonZeroInner = super::niche_types::NonZeroI64Inner;
}
#[unstable(feature = "nonzero_internals", reason =
"implementation detail which may disappear or be replaced at any time", issue
= "none")]
impl private::Sealed for i128 { }
#[unstable(feature = "nonzero_internals", reason =
"implementation detail which may disappear or be replaced at any time", issue
= "none")]
unsafe impl ZeroablePrimitive for i128 {
type NonZeroInner = super::niche_types::NonZeroI128Inner;
}
#[unstable(feature = "nonzero_internals", reason =
"implementation detail which may disappear or be replaced at any time", issue
= "none")]
impl private::Sealed for isize { }
#[unstable(feature = "nonzero_internals", reason =
"implementation detail which may disappear or be replaced at any time", issue
= "none")]
unsafe impl ZeroablePrimitive for isize {
type NonZeroInner = super::niche_types::NonZeroIsizeInner;
}
#[unstable(feature = "nonzero_internals", reason =
"implementation detail which may disappear or be replaced at any time", issue
= "none")]
impl private::Sealed for char { }
#[unstable(feature = "nonzero_internals", reason =
"implementation detail which may disappear or be replaced at any time", issue
= "none")]
unsafe impl ZeroablePrimitive for char {
type NonZeroInner = super::niche_types::NonZeroCharInner;
}impl_zeroable_primitive!(
71 NonZeroU8Inner(u8),
72 NonZeroU16Inner(u16),
73 NonZeroU32Inner(u32),
74 NonZeroU64Inner(u64),
75 NonZeroU128Inner(u128),
76 NonZeroUsizeInner(usize),
77 NonZeroI8Inner(i8),
78 NonZeroI16Inner(i16),
79 NonZeroI32Inner(i32),
80 NonZeroI64Inner(i64),
81 NonZeroI128Inner(i128),
82 NonZeroIsizeInner(isize),
83 NonZeroCharInner(char),
84);
85
86#[stable(feature = "generic_nonzero", since = "1.79.0")]
125#[repr(transparent)]
126#[rustc_nonnull_optimization_guaranteed]
127#[rustc_diagnostic_item = "NonZero"]
128pub struct NonZero<T: ZeroablePrimitive>(T::NonZeroInner);
129
130macro_rules! impl_nonzero_fmt {
131 ($(#[$Attribute:meta] $Trait:ident)*) => {
132 $(
133 #[$Attribute]
134 impl<T> fmt::$Trait for NonZero<T>
135 where
136 T: ZeroablePrimitive + fmt::$Trait,
137 {
138 #[inline]
139 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
140 self.get().fmt(f)
141 }
142 }
143 )*
144 };
145}
146
147#[stable(feature = "nonzero_fmt_exp", since = "1.84.0")]
impl<T> fmt::UpperExp for NonZero<T> where T: ZeroablePrimitive +
fmt::UpperExp {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.get().fmt(f)
}
}impl_nonzero_fmt! {
148 #[stable(feature = "nonzero", since = "1.28.0")]
149 Debug
150 #[stable(feature = "nonzero", since = "1.28.0")]
151 Display
152 #[stable(feature = "nonzero", since = "1.28.0")]
153 Binary
154 #[stable(feature = "nonzero", since = "1.28.0")]
155 Octal
156 #[stable(feature = "nonzero", since = "1.28.0")]
157 LowerHex
158 #[stable(feature = "nonzero", since = "1.28.0")]
159 UpperHex
160 #[stable(feature = "nonzero_fmt_exp", since = "1.84.0")]
161 LowerExp
162 #[stable(feature = "nonzero_fmt_exp", since = "1.84.0")]
163 UpperExp
164}
165
166macro_rules! impl_nonzero_auto_trait {
167 (unsafe $Trait:ident) => {
168 #[stable(feature = "nonzero", since = "1.28.0")]
169 unsafe impl<T> $Trait for NonZero<T> where T: ZeroablePrimitive + $Trait {}
170 };
171 ($Trait:ident) => {
172 #[stable(feature = "nonzero", since = "1.28.0")]
173 impl<T> $Trait for NonZero<T> where T: ZeroablePrimitive + $Trait {}
174 };
175}
176
177#[stable(feature = "nonzero", since = "1.28.0")]
unsafe impl<T> Freeze for NonZero<T> where T: ZeroablePrimitive + Freeze { }impl_nonzero_auto_trait!(unsafe Freeze);
180#[stable(feature = "nonzero", since = "1.28.0")]
impl<T> RefUnwindSafe for NonZero<T> where T: ZeroablePrimitive +
RefUnwindSafe {
}impl_nonzero_auto_trait!(RefUnwindSafe);
181#[stable(feature = "nonzero", since = "1.28.0")]
unsafe impl<T> Send for NonZero<T> where T: ZeroablePrimitive + Send { }impl_nonzero_auto_trait!(unsafe Send);
182#[stable(feature = "nonzero", since = "1.28.0")]
unsafe impl<T> Sync for NonZero<T> where T: ZeroablePrimitive + Sync { }impl_nonzero_auto_trait!(unsafe Sync);
183#[stable(feature = "nonzero", since = "1.28.0")]
impl<T> Unpin for NonZero<T> where T: ZeroablePrimitive + Unpin { }impl_nonzero_auto_trait!(Unpin);
184#[stable(feature = "nonzero", since = "1.28.0")]
impl<T> UnwindSafe for NonZero<T> where T: ZeroablePrimitive + UnwindSafe { }impl_nonzero_auto_trait!(UnwindSafe);
185
186#[stable(feature = "nonzero", since = "1.28.0")]
187#[rustc_const_unstable(feature = "const_clone", issue = "142757")]
188impl<T> const Clone for NonZero<T>
189where
190 T: ZeroablePrimitive,
191{
192 #[inline]
193 fn clone(&self) -> Self {
194 *self
195 }
196}
197
198#[unstable(feature = "ergonomic_clones", issue = "132290")]
199impl<T> UseCloned for NonZero<T> where T: ZeroablePrimitive {}
200
201#[stable(feature = "nonzero", since = "1.28.0")]
202impl<T> Copy for NonZero<T> where T: ZeroablePrimitive {}
203
204#[doc(hidden)]
205#[unstable(feature = "trivial_clone", issue = "none")]
206#[rustc_const_unstable(feature = "const_clone", issue = "142757")]
207unsafe impl<T> const TrivialClone for NonZero<T> where T: ZeroablePrimitive {}
208
209#[stable(feature = "nonzero", since = "1.28.0")]
210#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
211impl<T> const PartialEq for NonZero<T>
212where
213 T: ZeroablePrimitive + [const] PartialEq,
214{
215 #[inline]
216 fn eq(&self, other: &Self) -> bool {
217 self.get() == other.get()
218 }
219
220 #[inline]
221 fn ne(&self, other: &Self) -> bool {
222 self.get() != other.get()
223 }
224}
225
226#[unstable(feature = "structural_match", issue = "31434")]
227impl<T> StructuralPartialEq for NonZero<T> where T: ZeroablePrimitive + StructuralPartialEq {}
228
229#[stable(feature = "nonzero", since = "1.28.0")]
230#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
231impl<T> const Eq for NonZero<T> where T: ZeroablePrimitive + [const] Eq {}
232
233#[stable(feature = "nonzero", since = "1.28.0")]
234#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
235impl<T> const PartialOrd for NonZero<T>
236where
237 T: ZeroablePrimitive + [const] PartialOrd,
238{
239 #[inline]
240 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
241 self.get().partial_cmp(&other.get())
242 }
243
244 #[inline]
245 fn lt(&self, other: &Self) -> bool {
246 self.get() < other.get()
247 }
248
249 #[inline]
250 fn le(&self, other: &Self) -> bool {
251 self.get() <= other.get()
252 }
253
254 #[inline]
255 fn gt(&self, other: &Self) -> bool {
256 self.get() > other.get()
257 }
258
259 #[inline]
260 fn ge(&self, other: &Self) -> bool {
261 self.get() >= other.get()
262 }
263}
264
265#[stable(feature = "nonzero", since = "1.28.0")]
266#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
267impl<T> const Ord for NonZero<T>
268where
269 T: ZeroablePrimitive + [const] Ord + [const] Destruct,
272{
273 #[inline]
274 fn cmp(&self, other: &Self) -> Ordering {
275 self.get().cmp(&other.get())
276 }
277
278 #[inline]
279 fn max(self, other: Self) -> Self {
280 unsafe { Self::new_unchecked(self.get().max(other.get())) }
282 }
283
284 #[inline]
285 fn min(self, other: Self) -> Self {
286 unsafe { Self::new_unchecked(self.get().min(other.get())) }
288 }
289
290 #[inline]
291 fn clamp(self, min: Self, max: Self) -> Self {
292 unsafe { Self::new_unchecked(self.get().clamp(min.get(), max.get())) }
294 }
295}
296
297#[stable(feature = "nonzero", since = "1.28.0")]
298impl<T> Hash for NonZero<T>
299where
300 T: ZeroablePrimitive + Hash,
301{
302 #[inline]
303 fn hash<H>(&self, state: &mut H)
304 where
305 H: Hasher,
306 {
307 self.get().hash(state)
308 }
309}
310
311#[stable(feature = "from_nonzero", since = "1.31.0")]
312#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
313impl<T> const From<NonZero<T>> for T
314where
315 T: ZeroablePrimitive,
316{
317 #[inline]
318 fn from(nonzero: NonZero<T>) -> Self {
319 nonzero.get()
321 }
322}
323
324#[stable(feature = "nonzero_bitor", since = "1.45.0")]
325#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
326impl<T> const BitOr for NonZero<T>
327where
328 T: ZeroablePrimitive + [const] BitOr<Output = T>,
329{
330 type Output = Self;
331
332 #[inline]
333 fn bitor(self, rhs: Self) -> Self::Output {
334 unsafe { Self::new_unchecked(self.get() | rhs.get()) }
336 }
337}
338
339#[stable(feature = "nonzero_bitor", since = "1.45.0")]
340#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
341impl<T> const BitOr<T> for NonZero<T>
342where
343 T: ZeroablePrimitive + [const] BitOr<Output = T>,
344{
345 type Output = Self;
346
347 #[inline]
348 fn bitor(self, rhs: T) -> Self::Output {
349 unsafe { Self::new_unchecked(self.get() | rhs) }
351 }
352}
353
354#[stable(feature = "nonzero_bitor", since = "1.45.0")]
355#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
356impl<T> const BitOr<NonZero<T>> for T
357where
358 T: ZeroablePrimitive + [const] BitOr<Output = T>,
359{
360 type Output = NonZero<T>;
361
362 #[inline]
363 fn bitor(self, rhs: NonZero<T>) -> Self::Output {
364 unsafe { NonZero::new_unchecked(self | rhs.get()) }
366 }
367}
368
369#[stable(feature = "nonzero_bitor", since = "1.45.0")]
370#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
371impl<T> const BitOrAssign for NonZero<T>
372where
373 T: ZeroablePrimitive,
374 Self: [const] BitOr<Output = Self>,
375{
376 #[inline]
377 fn bitor_assign(&mut self, rhs: Self) {
378 *self = *self | rhs;
379 }
380}
381
382#[stable(feature = "nonzero_bitor", since = "1.45.0")]
383#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
384impl<T> const BitOrAssign<T> for NonZero<T>
385where
386 T: ZeroablePrimitive,
387 Self: [const] BitOr<T, Output = Self>,
388{
389 #[inline]
390 fn bitor_assign(&mut self, rhs: T) {
391 *self = *self | rhs;
392 }
393}
394
395impl<T> NonZero<T>
396where
397 T: ZeroablePrimitive,
398{
399 #[stable(feature = "nonzero", since = "1.28.0")]
401 #[rustc_const_stable(feature = "const_nonzero_int_methods", since = "1.47.0")]
402 #[must_use]
403 #[inline]
404 pub const fn new(n: T) -> Option<Self> {
405 unsafe { intrinsics::transmute_unchecked(n) }
408 }
409
410 #[stable(feature = "nonzero", since = "1.28.0")]
417 #[rustc_const_stable(feature = "nonzero", since = "1.28.0")]
418 #[must_use]
419 #[inline]
420 #[track_caller]
421 pub const unsafe fn new_unchecked(n: T) -> Self {
422 match Self::new(n) {
423 Some(n) => n,
424 None => {
425 unsafe {
427 {
#[rustc_no_mir_inline]
#[inline]
#[rustc_nounwind]
#[track_caller]
const fn precondition_check() {
if !false {
let msg =
"unsafe precondition(s) violated: NonZero::new_unchecked requires the argument to be non-zero\n\nThis indicates a bug in the program. This Undefined Behavior check is optional, and cannot be relied on for safety.";
::core::panicking::panic_nounwind_fmt(::core::fmt::Arguments::from_str(msg),
false);
}
}
if ::core::ub_checks::check_language_ub() { precondition_check(); }
};ub_checks::assert_unsafe_precondition!(
428 check_language_ub,
429 "NonZero::new_unchecked requires the argument to be non-zero",
430 () => false,
431 );
432 intrinsics::unreachable()
433 }
434 }
435 }
436 }
437
438 #[unstable(feature = "nonzero_from_mut", issue = "106290")]
441 #[must_use]
442 #[inline]
443 pub fn from_mut(n: &mut T) -> Option<&mut Self> {
444 let opt_n = unsafe { &mut *(ptr::from_mut(n).cast::<Option<Self>>()) };
447
448 opt_n.as_mut()
449 }
450
451 #[unstable(feature = "nonzero_from_mut", issue = "106290")]
459 #[must_use]
460 #[inline]
461 #[track_caller]
462 pub unsafe fn from_mut_unchecked(n: &mut T) -> &mut Self {
463 match Self::from_mut(n) {
464 Some(n) => n,
465 None => {
466 unsafe {
468 {
#[rustc_no_mir_inline]
#[inline]
#[rustc_nounwind]
#[track_caller]
const fn precondition_check() {
if !false {
let msg =
"unsafe precondition(s) violated: NonZero::from_mut_unchecked requires the argument to dereference as non-zero\n\nThis indicates a bug in the program. This Undefined Behavior check is optional, and cannot be relied on for safety.";
::core::panicking::panic_nounwind_fmt(::core::fmt::Arguments::from_str(msg),
false);
}
}
if ::core::ub_checks::check_library_ub() { precondition_check(); }
};ub_checks::assert_unsafe_precondition!(
469 check_library_ub,
470 "NonZero::from_mut_unchecked requires the argument to dereference as non-zero",
471 () => false,
472 );
473 intrinsics::unreachable()
474 }
475 }
476 }
477 }
478
479 #[stable(feature = "nonzero", since = "1.28.0")]
481 #[rustc_const_stable(feature = "const_nonzero_get", since = "1.34.0")]
482 #[inline]
483 pub const fn get(self) -> T {
484 unsafe { intrinsics::transmute_unchecked(self) }
503 }
504}
505
506macro_rules! nonzero_integer {
507 (
508 #[$stability:meta]
509 Self = $Ty:ident,
510 Primitive = $signedness:ident $Int:ident,
511 SignedPrimitive = $Sint:ty,
512 UnsignedPrimitive = $Uint:ty,
513
514 rot = $rot:literal,
516 rot_op = $rot_op:literal,
517 rot_result = $rot_result:literal,
518 swap_op = $swap_op:literal,
519 swapped = $swapped:literal,
520 reversed = $reversed:literal,
521 leading_zeros_test = $leading_zeros_test:expr,
522 ) => {
523 #[doc = sign_dependent_expr!{
524 $signedness ?
525 if signed {
526 concat!("An [`", stringify!($Int), "`] that is known not to equal zero.")
527 }
528 if unsigned {
529 concat!("A [`", stringify!($Int), "`] that is known not to equal zero.")
530 }
531 }]
532 #[doc = concat!("For example, `Option<", stringify!($Ty), ">` is the same size as `", stringify!($Int), "`:")]
535 #[doc = concat!("assert_eq!(size_of::<Option<core::num::", stringify!($Ty), ">>(), size_of::<", stringify!($Int), ">());")]
538 #[doc = concat!("`", stringify!($Ty), "` is guaranteed to have the same layout and bit validity as `", stringify!($Int), "`")]
543 #[doc = concat!("`Option<", stringify!($Ty), ">` is guaranteed to be compatible with `", stringify!($Int), "`,")]
545 #[doc = concat!("`", stringify!($Ty), "` and `Option<", stringify!($Ty), ">`")]
549 #[doc = concat!("use std::num::", stringify!($Ty), ";")]
553 #[doc = concat!("assert_eq!(size_of::<", stringify!($Ty), ">(), size_of::<Option<", stringify!($Ty), ">>());")]
555 #[doc = concat!("assert_eq!(align_of::<", stringify!($Ty), ">(), align_of::<Option<", stringify!($Ty), ">>());")]
556 #[doc = concat!("`", stringify!($Ty), "`")]
563 #[doc = concat!("use std::num::", stringify!($Ty), ";")]
566 #[doc = concat!("const TEN: ", stringify!($Ty), " = ", stringify!($Ty) , r#"::new(10).expect("ten is non-zero");"#)]
568 #[$stability]
572 pub type $Ty = NonZero<$Int>;
573
574 impl NonZero<$Int> {
575 #[doc = concat!("This value is equal to [`", stringify!($Int), "::BITS`].")]
578 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::BITS, ", stringify!($Int), "::BITS);")]
585 #[stable(feature = "nonzero_bits", since = "1.67.0")]
587 pub const BITS: u32 = <$Int>::BITS;
588
589 #[doc = concat!("let n = NonZero::<", stringify!($Int), ">::new(", $leading_zeros_test, ")?;")]
601 #[stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
607 #[rustc_const_stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
608 #[must_use = "this returns the result of the operation, \
609 without modifying the original"]
610 #[inline]
611 pub const fn leading_zeros(self) -> u32 {
612 unsafe {
614 intrinsics::ctlz_nonzero(self.get() as $Uint)
615 }
616 }
617
618 #[doc = concat!("let n = NonZero::<", stringify!($Int), ">::new(0b0101000)?;")]
631 #[stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
637 #[rustc_const_stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
638 #[must_use = "this returns the result of the operation, \
639 without modifying the original"]
640 #[inline]
641 pub const fn trailing_zeros(self) -> u32 {
642 unsafe {
644 intrinsics::cttz_nonzero(self.get() as $Uint)
645 }
646 }
647
648 #[doc = concat!("let a = NonZero::<", stringify!($Int), ">::new(0b_01100100)?;")]
657 #[doc = concat!("let b = NonZero::<", stringify!($Int), ">::new(0b_01000000)?;")]
658 #[stable(feature = "isolate_most_least_significant_one", since = "CURRENT_RUSTC_VERSION")]
664 #[rustc_const_stable(feature = "isolate_most_least_significant_one", since = "CURRENT_RUSTC_VERSION")]
665 #[must_use = "this returns the result of the operation, \
666 without modifying the original"]
667 #[inline(always)]
668 pub const fn isolate_highest_one(self) -> Self {
669 unsafe {
675 let bit = (((1 as $Uint) << (<$Uint>::BITS - 1)).unchecked_shr(self.leading_zeros()));
676 NonZero::new_unchecked(bit as $Int)
677 }
678 }
679
680 #[doc = concat!("let a = NonZero::<", stringify!($Int), ">::new(0b_01100100)?;")]
689 #[doc = concat!("let b = NonZero::<", stringify!($Int), ">::new(0b_00000100)?;")]
690 #[stable(feature = "isolate_most_least_significant_one", since = "CURRENT_RUSTC_VERSION")]
696 #[rustc_const_stable(feature = "isolate_most_least_significant_one", since = "CURRENT_RUSTC_VERSION")]
697 #[must_use = "this returns the result of the operation, \
698 without modifying the original"]
699 #[inline(always)]
700 pub const fn isolate_lowest_one(self) -> Self {
701 let n = self.get();
702 let n = n & n.wrapping_neg();
703
704 unsafe { NonZero::new_unchecked(n) }
707 }
708
709 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1)?.highest_one(), 0);")]
718 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1_0000)?.highest_one(), 4);")]
719 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1_1111)?.highest_one(), 4);")]
720 #[stable(feature = "int_lowest_highest_one", since = "CURRENT_RUSTC_VERSION")]
724 #[rustc_const_stable(feature = "int_lowest_highest_one", since = "CURRENT_RUSTC_VERSION")]
725 #[must_use = "this returns the result of the operation, \
726 without modifying the original"]
727 #[inline(always)]
728 pub const fn highest_one(self) -> u32 {
729 Self::BITS - 1 - self.leading_zeros()
730 }
731
732 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1)?.lowest_one(), 0);")]
741 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1_0000)?.lowest_one(), 4);")]
742 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1_1111)?.lowest_one(), 0);")]
743 #[stable(feature = "int_lowest_highest_one", since = "CURRENT_RUSTC_VERSION")]
747 #[rustc_const_stable(feature = "int_lowest_highest_one", since = "CURRENT_RUSTC_VERSION")]
748 #[must_use = "this returns the result of the operation, \
749 without modifying the original"]
750 #[inline(always)]
751 pub const fn lowest_one(self) -> u32 {
752 self.trailing_zeros()
753 }
754
755 #[doc = concat!("let a = NonZero::<", stringify!($Int), ">::new(0b100_0000)?;")]
765 #[doc = concat!("let b = NonZero::<", stringify!($Int), ">::new(0b100_0011)?;")]
766 #[stable(feature = "non_zero_count_ones", since = "1.86.0")]
774 #[rustc_const_stable(feature = "non_zero_count_ones", since = "1.86.0")]
775 #[doc(alias = "popcount")]
776 #[doc(alias = "popcnt")]
777 #[must_use = "this returns the result of the operation, \
778 without modifying the original"]
779 #[inline(always)]
780 pub const fn count_ones(self) -> NonZero<u32> {
781 unsafe { NonZero::new_unchecked(self.get().count_ones()) }
785 }
786
787 #[doc = concat!("let n = NonZero::new(", $rot_op, stringify!($Int), ")?;")]
801 #[doc = concat!("let m = NonZero::new(", $rot_result, ")?;")]
802 #[doc = concat!("assert_eq!(n.rotate_left(", $rot, "), m);")]
804 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
808 #[must_use = "this returns the result of the operation, \
809 without modifying the original"]
810 #[inline(always)]
811 pub const fn rotate_left(self, n: u32) -> Self {
812 let result = self.get().rotate_left(n);
813 unsafe { Self::new_unchecked(result) }
815 }
816
817 #[doc = concat!("let n = NonZero::new(", $rot_result, stringify!($Int), ")?;")]
832 #[doc = concat!("let m = NonZero::new(", $rot_op, ")?;")]
833 #[doc = concat!("assert_eq!(n.rotate_right(", $rot, "), m);")]
835 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
839 #[must_use = "this returns the result of the operation, \
840 without modifying the original"]
841 #[inline(always)]
842 pub const fn rotate_right(self, n: u32) -> Self {
843 let result = self.get().rotate_right(n);
844 unsafe { Self::new_unchecked(result) }
846 }
847
848 #[doc = concat!("let n = NonZero::new(", $swap_op, stringify!($Int), ")?;")]
859 #[doc = concat!("assert_eq!(m, NonZero::new(", $swapped, ")?);")]
862 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
866 #[must_use = "this returns the result of the operation, \
867 without modifying the original"]
868 #[inline(always)]
869 pub const fn swap_bytes(self) -> Self {
870 let result = self.get().swap_bytes();
871 unsafe { Self::new_unchecked(result) }
873 }
874
875 #[doc = concat!("let n = NonZero::new(", $swap_op, stringify!($Int), ")?;")]
887 #[doc = concat!("assert_eq!(m, NonZero::new(", $reversed, ")?);")]
890 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
894 #[must_use = "this returns the result of the operation, \
895 without modifying the original"]
896 #[inline(always)]
897 pub const fn reverse_bits(self) -> Self {
898 let result = self.get().reverse_bits();
899 unsafe { Self::new_unchecked(result) }
901 }
902
903 #[doc = concat!("use std::num::", stringify!($Ty), ";")]
914 #[doc = concat!("let n = NonZero::new(0x1A", stringify!($Int), ")?;")]
918 #[doc = concat!(" assert_eq!(", stringify!($Ty), "::from_be(n), n)")]
921 #[doc = concat!(" assert_eq!(", stringify!($Ty), "::from_be(n), n.swap_bytes())")]
923 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
928 #[must_use]
929 #[inline(always)]
930 pub const fn from_be(x: Self) -> Self {
931 let result = $Int::from_be(x.get());
932 unsafe { Self::new_unchecked(result) }
934 }
935
936 #[doc = concat!("use std::num::", stringify!($Ty), ";")]
947 #[doc = concat!("let n = NonZero::new(0x1A", stringify!($Int), ")?;")]
951 #[doc = concat!(" assert_eq!(", stringify!($Ty), "::from_le(n), n)")]
954 #[doc = concat!(" assert_eq!(", stringify!($Ty), "::from_le(n), n.swap_bytes())")]
956 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
961 #[must_use]
962 #[inline(always)]
963 pub const fn from_le(x: Self) -> Self {
964 let result = $Int::from_le(x.get());
965 unsafe { Self::new_unchecked(result) }
967 }
968
969 #[doc = concat!("let n = NonZero::new(0x1A", stringify!($Int), ")?;")]
983 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
993 #[must_use = "this returns the result of the operation, \
994 without modifying the original"]
995 #[inline(always)]
996 pub const fn to_be(self) -> Self {
997 let result = self.get().to_be();
998 unsafe { Self::new_unchecked(result) }
1000 }
1001
1002 #[doc = concat!("let n = NonZero::new(0x1A", stringify!($Int), ")?;")]
1016 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
1026 #[must_use = "this returns the result of the operation, \
1027 without modifying the original"]
1028 #[inline(always)]
1029 pub const fn to_le(self) -> Self {
1030 let result = self.get().to_le();
1031 unsafe { Self::new_unchecked(result) }
1033 }
1034
1035 nonzero_integer_signedness_dependent_methods! {
1036 Primitive = $signedness $Int,
1037 SignedPrimitive = $Sint,
1038 UnsignedPrimitive = $Uint,
1039 }
1040
1041 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1053 #[doc = concat!("let four = NonZero::new(4", stringify!($Int), ")?;")]
1054 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
1055 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1062 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1063 #[must_use = "this returns the result of the operation, \
1064 without modifying the original"]
1065 #[inline]
1066 pub const fn checked_mul(self, other: Self) -> Option<Self> {
1067 if let Some(result) = self.get().checked_mul(other.get()) {
1068 Some(unsafe { Self::new_unchecked(result) })
1076 } else {
1077 None
1078 }
1079 }
1080
1081 #[doc = concat!("Return [`NonZero::<", stringify!($Int), ">::MAX`] on overflow.")]
1083 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1092 #[doc = concat!("let four = NonZero::new(4", stringify!($Int), ")?;")]
1093 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
1094 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1101 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1102 #[must_use = "this returns the result of the operation, \
1103 without modifying the original"]
1104 #[inline]
1105 pub const fn saturating_mul(self, other: Self) -> Self {
1106 unsafe { Self::new_unchecked(self.get().saturating_mul(other.get())) }
1115 }
1116
1117 #[doc = sign_dependent_expr!{
1126 $signedness ?
1127 if signed {
1128 concat!("`self * rhs > ", stringify!($Int), "::MAX`, ",
1129 "or `self * rhs < ", stringify!($Int), "::MIN`.")
1130 }
1131 if unsigned {
1132 concat!("`self * rhs > ", stringify!($Int), "::MAX`.")
1133 }
1134 }]
1135 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1146 #[doc = concat!("let four = NonZero::new(4", stringify!($Int), ")?;")]
1147 #[unstable(feature = "nonzero_ops", issue = "84186")]
1153 #[must_use = "this returns the result of the operation, \
1154 without modifying the original"]
1155 #[inline]
1156 pub const unsafe fn unchecked_mul(self, other: Self) -> Self {
1157 unsafe { Self::new_unchecked(self.get().unchecked_mul(other.get())) }
1159 }
1160
1161 #[doc = concat!("let three = NonZero::new(3", stringify!($Int), ")?;")]
1173 #[doc = concat!("let twenty_seven = NonZero::new(27", stringify!($Int), ")?;")]
1174 #[doc = concat!("let half_max = NonZero::new(", stringify!($Int), "::MAX / 2)?;")]
1175 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1182 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1183 #[must_use = "this returns the result of the operation, \
1184 without modifying the original"]
1185 #[inline]
1186 pub const fn checked_pow(self, other: u32) -> Option<Self> {
1187 if let Some(result) = self.get().checked_pow(other) {
1188 Some(unsafe { Self::new_unchecked(result) })
1196 } else {
1197 None
1198 }
1199 }
1200
1201 #[doc = sign_dependent_expr!{
1203 $signedness ?
1204 if signed {
1205 concat!("Return [`NonZero::<", stringify!($Int), ">::MIN`] ",
1206 "or [`NonZero::<", stringify!($Int), ">::MAX`] on overflow.")
1207 }
1208 if unsigned {
1209 concat!("Return [`NonZero::<", stringify!($Int), ">::MAX`] on overflow.")
1210 }
1211 }]
1212 #[doc = concat!("let three = NonZero::new(3", stringify!($Int), ")?;")]
1221 #[doc = concat!("let twenty_seven = NonZero::new(27", stringify!($Int), ")?;")]
1222 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
1223 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1230 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1231 #[must_use = "this returns the result of the operation, \
1232 without modifying the original"]
1233 #[inline]
1234 pub const fn saturating_pow(self, other: u32) -> Self {
1235 unsafe { Self::new_unchecked(self.get().saturating_pow(other)) }
1244 }
1245
1246 #[doc = sign_dependent_expr!{
1250 $signedness ?
1251 if signed {
1252 " `+` or `-` "
1253 }
1254 if unsigned {
1255 " `+` "
1256 }
1257 }]
1258 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::from_ascii(b\"+10\"), Ok(NonZero::new(10)?));")]
1272 #[doc = concat!("assert!(NonZero::<", stringify!($Int), ">::from_ascii(b\"1 \").is_err());")]
1284 #[unstable(feature = "int_from_ascii", issue = "134821")]
1286 #[inline]
1287 pub const fn from_ascii(src: &[u8]) -> Result<Self, ParseIntError> {
1288 Self::from_ascii_radix(src, 10)
1289 }
1290
1291 #[doc = sign_dependent_expr!{
1295 $signedness ?
1296 if signed {
1297 " `+` or `-` "
1298 }
1299 if unsigned {
1300 " `+` "
1301 }
1302 }]
1303 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::from_ascii_radix(b\"A\", 16), Ok(NonZero::new(10)?));")]
1327 #[doc = concat!("assert!(NonZero::<", stringify!($Int), ">::from_ascii_radix(b\"1 \", 10).is_err());")]
1339 #[unstable(feature = "int_from_ascii", issue = "134821")]
1341 #[inline]
1342 pub const fn from_ascii_radix(src: &[u8], radix: u32) -> Result<Self, ParseIntError> {
1343 let n = match <$Int>::from_ascii_radix(src, radix) {
1344 Ok(n) => n,
1345 Err(err) => return Err(err),
1346 };
1347 if let Some(n) = Self::new(n) {
1348 Ok(n)
1349 } else {
1350 Err(ParseIntError { kind: IntErrorKind::Zero })
1351 }
1352 }
1353
1354 #[doc = sign_dependent_expr!{
1358 $signedness ?
1359 if signed {
1360 " `+` or `-` "
1361 }
1362 if unsigned {
1363 " `+` "
1364 }
1365 }]
1366 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::from_str_radix(\"A\", 16), Ok(NonZero::new(10)?));")]
1390 #[doc = concat!("assert!(NonZero::<", stringify!($Int), ">::from_str_radix(\"1 \", 10).is_err());")]
1402 #[unstable(feature = "nonzero_from_str_radix", issue = "152193")]
1404 #[inline]
1405 pub const fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError> {
1406 Self::from_ascii_radix(src.as_bytes(), radix)
1407 }
1408 }
1409
1410 #[stable(feature = "nonzero_parse", since = "1.35.0")]
1411 impl FromStr for NonZero<$Int> {
1412 type Err = ParseIntError;
1413 fn from_str(src: &str) -> Result<Self, Self::Err> {
1414 Self::from_str_radix(src, 10)
1415 }
1416 }
1417
1418 nonzero_integer_signedness_dependent_impls!($signedness $Int);
1419 };
1420
1421 (
1422 Self = $Ty:ident,
1423 Primitive = unsigned $Int:ident,
1424 SignedPrimitive = $Sint:ident,
1425 rot = $rot:literal,
1426 rot_op = $rot_op:literal,
1427 rot_result = $rot_result:literal,
1428 swap_op = $swap_op:literal,
1429 swapped = $swapped:literal,
1430 reversed = $reversed:literal,
1431 $(,)?
1432 ) => {
1433 nonzero_integer! {
1434 #[stable(feature = "nonzero", since = "1.28.0")]
1435 Self = $Ty,
1436 Primitive = unsigned $Int,
1437 SignedPrimitive = $Sint,
1438 UnsignedPrimitive = $Int,
1439 rot = $rot,
1440 rot_op = $rot_op,
1441 rot_result = $rot_result,
1442 swap_op = $swap_op,
1443 swapped = $swapped,
1444 reversed = $reversed,
1445 leading_zeros_test = concat!(stringify!($Int), "::MAX"),
1446 }
1447 };
1448
1449 (
1450 Self = $Ty:ident,
1451 Primitive = signed $Int:ident,
1452 UnsignedPrimitive = $Uint:ident,
1453 rot = $rot:literal,
1454 rot_op = $rot_op:literal,
1455 rot_result = $rot_result:literal,
1456 swap_op = $swap_op:literal,
1457 swapped = $swapped:literal,
1458 reversed = $reversed:literal,
1459 ) => {
1460 nonzero_integer! {
1461 #[stable(feature = "signed_nonzero", since = "1.34.0")]
1462 Self = $Ty,
1463 Primitive = signed $Int,
1464 SignedPrimitive = $Int,
1465 UnsignedPrimitive = $Uint,
1466 rot = $rot,
1467 rot_op = $rot_op,
1468 rot_result = $rot_result,
1469 swap_op = $swap_op,
1470 swapped = $swapped,
1471 reversed = $reversed,
1472 leading_zeros_test = concat!("-1", stringify!($Int)),
1473 }
1474 };
1475}
1476
1477macro_rules! nonzero_integer_signedness_dependent_impls {
1478 (unsigned $Int:ty) => {
1480 #[stable(feature = "nonzero_div", since = "1.51.0")]
1481 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1482 impl const Div<NonZero<$Int>> for $Int {
1483 type Output = $Int;
1484
1485 #[doc(alias = "unchecked_div")]
1491 #[inline]
1492 fn div(self, other: NonZero<$Int>) -> $Int {
1493 unsafe { intrinsics::unchecked_div(self, other.get()) }
1496 }
1497 }
1498
1499 #[stable(feature = "nonzero_div_assign", since = "1.79.0")]
1500 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1501 impl const DivAssign<NonZero<$Int>> for $Int {
1502 #[inline]
1508 fn div_assign(&mut self, other: NonZero<$Int>) {
1509 *self = *self / other;
1510 }
1511 }
1512
1513 #[stable(feature = "nonzero_div", since = "1.51.0")]
1514 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1515 impl const Rem<NonZero<$Int>> for $Int {
1516 type Output = $Int;
1517
1518 #[inline]
1520 fn rem(self, other: NonZero<$Int>) -> $Int {
1521 unsafe { intrinsics::unchecked_rem(self, other.get()) }
1524 }
1525 }
1526
1527 #[stable(feature = "nonzero_div_assign", since = "1.79.0")]
1528 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1529 impl const RemAssign<NonZero<$Int>> for $Int {
1530 #[inline]
1532 fn rem_assign(&mut self, other: NonZero<$Int>) {
1533 *self = *self % other;
1534 }
1535 }
1536
1537 impl NonZero<$Int> {
1538 #[doc = concat!("let one = NonZero::new(1", stringify!($Int), ").unwrap();")]
1547 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX).unwrap();")]
1548 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ").unwrap();")]
1551 #[doc = concat!("let three = NonZero::new(3", stringify!($Int), ").unwrap();")]
1552 #[stable(feature = "unsigned_nonzero_div_ceil", since = "1.92.0")]
1555 #[rustc_const_stable(feature = "unsigned_nonzero_div_ceil", since = "1.92.0")]
1556 #[must_use = "this returns the result of the operation, \
1557 without modifying the original"]
1558 #[inline]
1559 pub const fn div_ceil(self, rhs: Self) -> Self {
1560 let v = self.get().div_ceil(rhs.get());
1561 unsafe { Self::new_unchecked(v) }
1563 }
1564 }
1565 };
1566 (signed $Int:ty) => {
1568 #[stable(feature = "signed_nonzero_neg", since = "1.71.0")]
1569 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1570 impl const Neg for NonZero<$Int> {
1571 type Output = Self;
1572
1573 #[inline]
1574 fn neg(self) -> Self {
1575 unsafe { Self::new_unchecked(self.get().neg()) }
1577 }
1578 }
1579
1580 forward_ref_unop! { impl Neg, neg for NonZero<$Int>,
1581 #[stable(feature = "signed_nonzero_neg", since = "1.71.0")]
1582 #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
1583 };
1584}
1585
1586#[rustfmt::skip] macro_rules! nonzero_integer_signedness_dependent_methods {
1588 (
1590 Primitive = unsigned $Int:ident,
1591 SignedPrimitive = $Sint:ty,
1592 UnsignedPrimitive = $Uint:ty,
1593 ) => {
1594 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::MIN.get(), 1", stringify!($Int), ");")]
1603 #[stable(feature = "nonzero_min_max", since = "1.70.0")]
1605 pub const MIN: Self = Self::new(1).unwrap();
1606
1607 #[doc = concat!("equal to [`", stringify!($Int), "::MAX`].")]
1610 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::MAX.get(), ", stringify!($Int), "::MAX);")]
1617 #[stable(feature = "nonzero_min_max", since = "1.70.0")]
1619 pub const MAX: Self = Self::new(<$Int>::MAX).unwrap();
1620
1621 #[doc = concat!("let one = NonZero::new(1", stringify!($Int), ")?;")]
1634 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1635 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
1636 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1643 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1644 #[must_use = "this returns the result of the operation, \
1645 without modifying the original"]
1646 #[inline]
1647 pub const fn checked_add(self, other: $Int) -> Option<Self> {
1648 if let Some(result) = self.get().checked_add(other) {
1649 Some(unsafe { Self::new_unchecked(result) })
1657 } else {
1658 None
1659 }
1660 }
1661
1662 #[doc = concat!("Return [`NonZero::<", stringify!($Int), ">::MAX`] on overflow.")]
1664 #[doc = concat!("let one = NonZero::new(1", stringify!($Int), ")?;")]
1673 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1674 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
1675 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1682 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1683 #[must_use = "this returns the result of the operation, \
1684 without modifying the original"]
1685 #[inline]
1686 pub const fn saturating_add(self, other: $Int) -> Self {
1687 unsafe { Self::new_unchecked(self.get().saturating_add(other)) }
1695 }
1696
1697 #[doc = concat!("`self + rhs > ", stringify!($Int), "::MAX`.")]
1706 #[doc = concat!("let one = NonZero::new(1", stringify!($Int), ")?;")]
1717 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1718 #[unstable(feature = "nonzero_ops", issue = "84186")]
1724 #[must_use = "this returns the result of the operation, \
1725 without modifying the original"]
1726 #[inline]
1727 pub const unsafe fn unchecked_add(self, other: $Int) -> Self {
1728 unsafe { Self::new_unchecked(self.get().unchecked_add(other)) }
1730 }
1731
1732 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1745 #[doc = concat!("let three = NonZero::new(3", stringify!($Int), ")?;")]
1746 #[doc = concat!("let four = NonZero::new(4", stringify!($Int), ")?;")]
1747 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
1748 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1756 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1757 #[must_use = "this returns the result of the operation, \
1758 without modifying the original"]
1759 #[inline]
1760 pub const fn checked_next_power_of_two(self) -> Option<Self> {
1761 if let Some(nz) = self.get().checked_next_power_of_two() {
1762 Some(unsafe { Self::new_unchecked(nz) })
1765 } else {
1766 None
1767 }
1768 }
1769
1770 #[doc = concat!("[`", stringify!($Int), "::ilog2`],")]
1774 #[doc = concat!("assert_eq!(NonZero::new(7", stringify!($Int), ")?.ilog2(), 2);")]
1785 #[doc = concat!("assert_eq!(NonZero::new(8", stringify!($Int), ")?.ilog2(), 3);")]
1786 #[doc = concat!("assert_eq!(NonZero::new(9", stringify!($Int), ")?.ilog2(), 3);")]
1787 #[stable(feature = "int_log", since = "1.67.0")]
1791 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
1792 #[must_use = "this returns the result of the operation, \
1793 without modifying the original"]
1794 #[inline]
1795 pub const fn ilog2(self) -> u32 {
1796 Self::BITS - 1 - self.leading_zeros()
1797 }
1798
1799 #[doc = concat!("[`", stringify!($Int), "::ilog10`],")]
1803 #[doc = concat!("assert_eq!(NonZero::new(99", stringify!($Int), ")?.ilog10(), 1);")]
1814 #[doc = concat!("assert_eq!(NonZero::new(100", stringify!($Int), ")?.ilog10(), 2);")]
1815 #[doc = concat!("assert_eq!(NonZero::new(101", stringify!($Int), ")?.ilog10(), 2);")]
1816 #[stable(feature = "int_log", since = "1.67.0")]
1820 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
1821 #[must_use = "this returns the result of the operation, \
1822 without modifying the original"]
1823 #[inline]
1824 pub const fn ilog10(self) -> u32 {
1825 imp::int_log10::$Int(self)
1826 }
1827
1828 #[doc = concat!("let one = NonZero::new(1", stringify!($Int), ")?;")]
1842 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1843 #[doc = concat!("let four = NonZero::new(4", stringify!($Int), ")?;")]
1844 #[stable(feature = "num_midpoint", since = "1.85.0")]
1851 #[rustc_const_stable(feature = "num_midpoint", since = "1.85.0")]
1852 #[must_use = "this returns the result of the operation, \
1853 without modifying the original"]
1854 #[doc(alias = "average_floor")]
1855 #[doc(alias = "average")]
1856 #[inline]
1857 pub const fn midpoint(self, rhs: Self) -> Self {
1858 unsafe { Self::new_unchecked(self.get().midpoint(rhs.get())) }
1863 }
1864
1865 #[doc = concat!("let eight = NonZero::new(8", stringify!($Int), ")?;")]
1878 #[doc = concat!("let ten = NonZero::new(10", stringify!($Int), ")?;")]
1880 #[must_use]
1885 #[stable(feature = "nonzero_is_power_of_two", since = "1.59.0")]
1886 #[rustc_const_stable(feature = "nonzero_is_power_of_two", since = "1.59.0")]
1887 #[inline]
1888 pub const fn is_power_of_two(self) -> bool {
1889 intrinsics::ctpop(self.get()) < 2
1895 }
1896
1897 #[doc = concat!("let ten = NonZero::new(10", stringify!($Int), ")?;")]
1907 #[doc = concat!("let three = NonZero::new(3", stringify!($Int), ")?;")]
1908 #[stable(feature = "isqrt", since = "1.84.0")]
1914 #[rustc_const_stable(feature = "isqrt", since = "1.84.0")]
1915 #[must_use = "this returns the result of the operation, \
1916 without modifying the original"]
1917 #[inline]
1918 pub const fn isqrt(self) -> Self {
1919 let result = self.get().isqrt();
1920
1921 unsafe { Self::new_unchecked(result) }
1927 }
1928
1929 #[doc = concat!("let n = NonZero::<", stringify!($Int), ">::MAX;")]
1937 #[doc = concat!("assert_eq!(n.cast_signed(), NonZero::new(-1", stringify!($Sint), ").unwrap());")]
1939 #[stable(feature = "integer_sign_cast", since = "1.87.0")]
1941 #[rustc_const_stable(feature = "integer_sign_cast", since = "1.87.0")]
1942 #[must_use = "this returns the result of the operation, \
1943 without modifying the original"]
1944 #[inline(always)]
1945 pub const fn cast_signed(self) -> NonZero<$Sint> {
1946 unsafe { NonZero::new_unchecked(self.get().cast_signed()) }
1948 }
1949
1950 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1)?.bit_width(), NonZero::new(1)?);")]
1960 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b111)?.bit_width(), NonZero::new(3)?);")]
1961 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1110)?.bit_width(), NonZero::new(4)?);")]
1962 #[stable(feature = "uint_bit_width", since = "CURRENT_RUSTC_VERSION")]
1966 #[rustc_const_stable(feature = "uint_bit_width", since = "CURRENT_RUSTC_VERSION")]
1967 #[must_use = "this returns the result of the operation, \
1968 without modifying the original"]
1969 #[inline(always)]
1970 pub const fn bit_width(self) -> NonZero<u32> {
1971 unsafe { NonZero::new_unchecked(Self::BITS - self.leading_zeros()) }
1974 }
1975 };
1976
1977 (
1979 Primitive = signed $Int:ident,
1980 SignedPrimitive = $Sint:ty,
1981 UnsignedPrimitive = $Uint:ty,
1982 ) => {
1983 #[doc = concat!("equal to [`", stringify!($Int), "::MIN`].")]
1986 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::MIN.get(), ", stringify!($Int), "::MIN);")]
1997 #[stable(feature = "nonzero_min_max", since = "1.70.0")]
1999 pub const MIN: Self = Self::new(<$Int>::MIN).unwrap();
2000
2001 #[doc = concat!("equal to [`", stringify!($Int), "::MAX`].")]
2004 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::MAX.get(), ", stringify!($Int), "::MAX);")]
2015 #[stable(feature = "nonzero_min_max", since = "1.70.0")]
2017 pub const MAX: Self = Self::new(<$Int>::MAX).unwrap();
2018
2019 #[doc = concat!("See [`", stringify!($Int), "::abs`]")]
2021 #[doc = concat!("let pos = NonZero::new(1", stringify!($Int), ")?;")]
2031 #[doc = concat!("let neg = NonZero::new(-1", stringify!($Int), ")?;")]
2032 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
2039 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
2040 #[must_use = "this returns the result of the operation, \
2041 without modifying the original"]
2042 #[inline]
2043 pub const fn abs(self) -> Self {
2044 unsafe { Self::new_unchecked(self.get().abs()) }
2046 }
2047
2048 #[doc = concat!("`self == NonZero::<", stringify!($Int), ">::MIN`.")]
2051 #[doc = concat!("let pos = NonZero::new(1", stringify!($Int), ")?;")]
2061 #[doc = concat!("let neg = NonZero::new(-1", stringify!($Int), ")?;")]
2062 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2063 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
2070 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
2071 #[must_use = "this returns the result of the operation, \
2072 without modifying the original"]
2073 #[inline]
2074 pub const fn checked_abs(self) -> Option<Self> {
2075 if let Some(nz) = self.get().checked_abs() {
2076 Some(unsafe { Self::new_unchecked(nz) })
2078 } else {
2079 None
2080 }
2081 }
2082
2083 #[doc = concat!("[`", stringify!($Int), "::overflowing_abs`].")]
2086 #[doc = concat!("let pos = NonZero::new(1", stringify!($Int), ")?;")]
2095 #[doc = concat!("let neg = NonZero::new(-1", stringify!($Int), ")?;")]
2096 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2097 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
2105 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
2106 #[must_use = "this returns the result of the operation, \
2107 without modifying the original"]
2108 #[inline]
2109 pub const fn overflowing_abs(self) -> (Self, bool) {
2110 let (nz, flag) = self.get().overflowing_abs();
2111 (
2112 unsafe { Self::new_unchecked(nz) },
2114 flag,
2115 )
2116 }
2117
2118 #[doc = concat!("[`", stringify!($Int), "::saturating_abs`].")]
2120 #[doc = concat!("let pos = NonZero::new(1", stringify!($Int), ")?;")]
2129 #[doc = concat!("let neg = NonZero::new(-1", stringify!($Int), ")?;")]
2130 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2131 #[doc = concat!("let min_plus = NonZero::new(", stringify!($Int), "::MIN + 1)?;")]
2132 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
2133 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
2142 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
2143 #[must_use = "this returns the result of the operation, \
2144 without modifying the original"]
2145 #[inline]
2146 pub const fn saturating_abs(self) -> Self {
2147 unsafe { Self::new_unchecked(self.get().saturating_abs()) }
2149 }
2150
2151 #[doc = concat!("[`", stringify!($Int), "::wrapping_abs`].")]
2153 #[doc = concat!("let pos = NonZero::new(1", stringify!($Int), ")?;")]
2162 #[doc = concat!("let neg = NonZero::new(-1", stringify!($Int), ")?;")]
2163 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2164 #[doc = concat!("# let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
2165 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
2174 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
2175 #[must_use = "this returns the result of the operation, \
2176 without modifying the original"]
2177 #[inline]
2178 pub const fn wrapping_abs(self) -> Self {
2179 unsafe { Self::new_unchecked(self.get().wrapping_abs()) }
2181 }
2182
2183 #[doc = concat!("let u_pos = NonZero::new(1", stringify!($Uint), ")?;")]
2194 #[doc = concat!("let i_pos = NonZero::new(1", stringify!($Int), ")?;")]
2195 #[doc = concat!("let i_neg = NonZero::new(-1", stringify!($Int), ")?;")]
2196 #[doc = concat!("let i_min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2197 #[doc = concat!("let u_max = NonZero::new(", stringify!($Uint), "::MAX / 2 + 1)?;")]
2198 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
2206 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
2207 #[must_use = "this returns the result of the operation, \
2208 without modifying the original"]
2209 #[inline]
2210 pub const fn unsigned_abs(self) -> NonZero<$Uint> {
2211 unsafe { NonZero::new_unchecked(self.get().unsigned_abs()) }
2213 }
2214
2215 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2226 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2227 #[must_use]
2234 #[inline]
2235 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2236 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2237 pub const fn is_positive(self) -> bool {
2238 self.get().is_positive()
2239 }
2240
2241 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2252 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2253 #[must_use]
2260 #[inline]
2261 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2262 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2263 pub const fn is_negative(self) -> bool {
2264 self.get().is_negative()
2265 }
2266
2267 #[doc = concat!("returning `None` if `self == NonZero::<", stringify!($Int), ">::MIN`.")]
2269 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2278 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2279 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2280 #[inline]
2287 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2288 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2289 pub const fn checked_neg(self) -> Option<Self> {
2290 if let Some(result) = self.get().checked_neg() {
2291 return Some(unsafe { Self::new_unchecked(result) });
2293 }
2294 None
2295 }
2296
2297 #[doc = concat!("See [`", stringify!($Int), "::overflowing_neg`]")]
2300 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2310 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2311 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2312 #[inline]
2319 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2320 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2321 pub const fn overflowing_neg(self) -> (Self, bool) {
2322 let (result, overflow) = self.get().overflowing_neg();
2323 ((unsafe { Self::new_unchecked(result) }), overflow)
2325 }
2326
2327 #[doc = concat!("returning [`NonZero::<", stringify!($Int), ">::MAX`]")]
2329 #[doc = concat!("if `self == NonZero::<", stringify!($Int), ">::MIN`")]
2330 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2340 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2341 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2342 #[doc = concat!("let min_plus_one = NonZero::new(", stringify!($Int), "::MIN + 1)?;")]
2343 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
2344 #[inline]
2352 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2353 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2354 pub const fn saturating_neg(self) -> Self {
2355 if let Some(result) = self.checked_neg() {
2356 return result;
2357 }
2358 Self::MAX
2359 }
2360
2361 #[doc = concat!("See [`", stringify!($Int), "::wrapping_neg`]")]
2365 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2375 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2376 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2377 #[inline]
2384 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2385 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2386 pub const fn wrapping_neg(self) -> Self {
2387 let result = self.get().wrapping_neg();
2388 unsafe { Self::new_unchecked(result) }
2390 }
2391
2392 #[doc = concat!("let n = NonZero::new(-1", stringify!($Int), ").unwrap();")]
2400 #[doc = concat!("assert_eq!(n.cast_unsigned(), NonZero::<", stringify!($Uint), ">::MAX);")]
2402 #[stable(feature = "integer_sign_cast", since = "1.87.0")]
2404 #[rustc_const_stable(feature = "integer_sign_cast", since = "1.87.0")]
2405 #[must_use = "this returns the result of the operation, \
2406 without modifying the original"]
2407 #[inline(always)]
2408 pub const fn cast_unsigned(self) -> NonZero<$Uint> {
2409 unsafe { NonZero::new_unchecked(self.get().cast_unsigned()) }
2411 }
2412
2413 };
2414}
2415
2416#[doc = "A [`u8`] that is known not to equal zero."]
#[doc = "For example, `Option<NonZeroU8>` is the same size as `u8`:"]
#[doc =
"assert_eq!(size_of::<Option<core::num::NonZeroU8>>(), size_of::<u8>());"]
#[doc =
"`NonZeroU8` is guaranteed to have the same layout and bit validity as `u8`"]
#[doc = "`Option<NonZeroU8>` is guaranteed to be compatible with `u8`,"]
#[doc = "`NonZeroU8` and `Option<NonZeroU8>`"]
#[doc = "use std::num::NonZeroU8;"]
#[doc = "assert_eq!(size_of::<NonZeroU8>(), size_of::<Option<NonZeroU8>>());"]
#[doc =
"assert_eq!(align_of::<NonZeroU8>(), align_of::<Option<NonZeroU8>>());"]
#[doc = "`NonZeroU8`"]
#[doc = "use std::num::NonZeroU8;"]
#[doc =
"const TEN: NonZeroU8 = NonZeroU8::new(10).expect(\"ten is non-zero\");"]
#[stable(feature = "nonzero", since = "1.28.0")]
pub type NonZeroU8 = NonZero<u8>;
impl NonZero<u8> {
#[doc = "This value is equal to [`u8::BITS`]."]
#[doc = "assert_eq!(NonZero::<u8>::BITS, u8::BITS);"]
#[stable(feature = "nonzero_bits", since = "1.67.0")]
pub const BITS: u32 = <u8>::BITS;
#[doc = "let n = NonZero::<u8>::new(u8::MAX)?;"]
#[stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
#[rustc_const_stable(feature = "nonzero_leading_trailing_zeros", since =
"1.53.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn leading_zeros(self) -> u32 {
unsafe { intrinsics::ctlz_nonzero(self.get() as u8) }
}
#[doc = "let n = NonZero::<u8>::new(0b0101000)?;"]
#[stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
#[rustc_const_stable(feature = "nonzero_leading_trailing_zeros", since =
"1.53.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn trailing_zeros(self) -> u32 {
unsafe { intrinsics::cttz_nonzero(self.get() as u8) }
}
#[doc = "let a = NonZero::<u8>::new(0b_01100100)?;"]
#[doc = "let b = NonZero::<u8>::new(0b_01000000)?;"]
#[stable(feature = "isolate_most_least_significant_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn isolate_highest_one(self) -> Self {
unsafe {
let bit =
(((1 as u8) <<
(<u8>::BITS - 1)).unchecked_shr(self.leading_zeros()));
NonZero::new_unchecked(bit as u8)
}
}
#[doc = "let a = NonZero::<u8>::new(0b_01100100)?;"]
#[doc = "let b = NonZero::<u8>::new(0b_00000100)?;"]
#[stable(feature = "isolate_most_least_significant_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn isolate_lowest_one(self) -> Self {
let n = self.get();
let n = n & n.wrapping_neg();
unsafe { NonZero::new_unchecked(n) }
}
#[doc = "assert_eq!(NonZero::<u8>::new(0b1)?.highest_one(), 0);"]
#[doc = "assert_eq!(NonZero::<u8>::new(0b1_0000)?.highest_one(), 4);"]
#[doc = "assert_eq!(NonZero::<u8>::new(0b1_1111)?.highest_one(), 4);"]
#[stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn highest_one(self) -> u32 {
Self::BITS - 1 - self.leading_zeros()
}
#[doc = "assert_eq!(NonZero::<u8>::new(0b1)?.lowest_one(), 0);"]
#[doc = "assert_eq!(NonZero::<u8>::new(0b1_0000)?.lowest_one(), 4);"]
#[doc = "assert_eq!(NonZero::<u8>::new(0b1_1111)?.lowest_one(), 0);"]
#[stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn lowest_one(self) -> u32 { self.trailing_zeros() }
#[doc = "let a = NonZero::<u8>::new(0b100_0000)?;"]
#[doc = "let b = NonZero::<u8>::new(0b100_0011)?;"]
#[stable(feature = "non_zero_count_ones", since = "1.86.0")]
#[rustc_const_stable(feature = "non_zero_count_ones", since = "1.86.0")]
#[doc(alias = "popcount")]
#[doc(alias = "popcnt")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn count_ones(self) -> NonZero<u32> {
unsafe { NonZero::new_unchecked(self.get().count_ones()) }
}
#[doc = "let n = NonZero::new(0x82u8)?;"]
#[doc = "let m = NonZero::new(0xa)?;"]
#[doc = "assert_eq!(n.rotate_left(2), m);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn rotate_left(self, n: u32) -> Self {
let result = self.get().rotate_left(n);
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0xau8)?;"]
#[doc = "let m = NonZero::new(0x82)?;"]
#[doc = "assert_eq!(n.rotate_right(2), m);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn rotate_right(self, n: u32) -> Self {
let result = self.get().rotate_right(n);
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x12u8)?;"]
#[doc = "assert_eq!(m, NonZero::new(0x12)?);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn swap_bytes(self) -> Self {
let result = self.get().swap_bytes();
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x12u8)?;"]
#[doc = "assert_eq!(m, NonZero::new(0x48)?);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn reverse_bits(self) -> Self {
let result = self.get().reverse_bits();
unsafe { Self::new_unchecked(result) }
}
#[doc = "use std::num::NonZeroU8;"]
#[doc = "let n = NonZero::new(0x1Au8)?;"]
#[doc = " assert_eq!(NonZeroU8::from_be(n), n)"]
#[doc = " assert_eq!(NonZeroU8::from_be(n), n.swap_bytes())"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use]
#[inline(always)]
pub const fn from_be(x: Self) -> Self {
let result = u8::from_be(x.get());
unsafe { Self::new_unchecked(result) }
}
#[doc = "use std::num::NonZeroU8;"]
#[doc = "let n = NonZero::new(0x1Au8)?;"]
#[doc = " assert_eq!(NonZeroU8::from_le(n), n)"]
#[doc = " assert_eq!(NonZeroU8::from_le(n), n.swap_bytes())"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use]
#[inline(always)]
pub const fn from_le(x: Self) -> Self {
let result = u8::from_le(x.get());
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x1Au8)?;"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn to_be(self) -> Self {
let result = self.get().to_be();
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x1Au8)?;"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn to_le(self) -> Self {
let result = self.get().to_le();
unsafe { Self::new_unchecked(result) }
}
#[doc = "assert_eq!(NonZero::<u8>::MIN.get(), 1u8);"]
#[stable(feature = "nonzero_min_max", since = "1.70.0")]
pub const MIN: Self = Self::new(1).unwrap();
#[doc = "equal to [`u8::MAX`]."]
#[doc = "assert_eq!(NonZero::<u8>::MAX.get(), u8::MAX);"]
#[stable(feature = "nonzero_min_max", since = "1.70.0")]
pub const MAX: Self = Self::new(<u8>::MAX).unwrap();
#[doc = "let one = NonZero::new(1u8)?;"]
#[doc = "let two = NonZero::new(2u8)?;"]
#[doc = "let max = NonZero::new(u8::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_add(self, other: u8) -> Option<Self> {
if let Some(result) = self.get().checked_add(other) {
Some(unsafe { Self::new_unchecked(result) })
} else { None }
}
#[doc = "Return [`NonZero::<u8>::MAX`] on overflow."]
#[doc = "let one = NonZero::new(1u8)?;"]
#[doc = "let two = NonZero::new(2u8)?;"]
#[doc = "let max = NonZero::new(u8::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_add(self, other: u8) -> Self {
unsafe { Self::new_unchecked(self.get().saturating_add(other)) }
}
#[doc = "`self + rhs > u8::MAX`."]
#[doc = "let one = NonZero::new(1u8)?;"]
#[doc = "let two = NonZero::new(2u8)?;"]
#[unstable(feature = "nonzero_ops", issue = "84186")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const unsafe fn unchecked_add(self, other: u8) -> Self {
unsafe { Self::new_unchecked(self.get().unchecked_add(other)) }
}
#[doc = "let two = NonZero::new(2u8)?;"]
#[doc = "let three = NonZero::new(3u8)?;"]
#[doc = "let four = NonZero::new(4u8)?;"]
#[doc = "let max = NonZero::new(u8::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_next_power_of_two(self) -> Option<Self> {
if let Some(nz) = self.get().checked_next_power_of_two() {
Some(unsafe { Self::new_unchecked(nz) })
} else { None }
}
#[doc = "[`u8::ilog2`],"]
#[doc = "assert_eq!(NonZero::new(7u8)?.ilog2(), 2);"]
#[doc = "assert_eq!(NonZero::new(8u8)?.ilog2(), 3);"]
#[doc = "assert_eq!(NonZero::new(9u8)?.ilog2(), 3);"]
#[stable(feature = "int_log", since = "1.67.0")]
#[rustc_const_stable(feature = "int_log", since = "1.67.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn ilog2(self) -> u32 { Self::BITS - 1 - self.leading_zeros() }
#[doc = "[`u8::ilog10`],"]
#[doc = "assert_eq!(NonZero::new(99u8)?.ilog10(), 1);"]
#[doc = "assert_eq!(NonZero::new(100u8)?.ilog10(), 2);"]
#[doc = "assert_eq!(NonZero::new(101u8)?.ilog10(), 2);"]
#[stable(feature = "int_log", since = "1.67.0")]
#[rustc_const_stable(feature = "int_log", since = "1.67.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn ilog10(self) -> u32 { imp::int_log10::u8(self) }
#[doc = "let one = NonZero::new(1u8)?;"]
#[doc = "let two = NonZero::new(2u8)?;"]
#[doc = "let four = NonZero::new(4u8)?;"]
#[stable(feature = "num_midpoint", since = "1.85.0")]
#[rustc_const_stable(feature = "num_midpoint", since = "1.85.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[doc(alias = "average_floor")]
#[doc(alias = "average")]
#[inline]
pub const fn midpoint(self, rhs: Self) -> Self {
unsafe { Self::new_unchecked(self.get().midpoint(rhs.get())) }
}
#[doc = "let eight = NonZero::new(8u8)?;"]
#[doc = "let ten = NonZero::new(10u8)?;"]
#[must_use]
#[stable(feature = "nonzero_is_power_of_two", since = "1.59.0")]
#[rustc_const_stable(feature = "nonzero_is_power_of_two", since =
"1.59.0")]
#[inline]
pub const fn is_power_of_two(self) -> bool {
intrinsics::ctpop(self.get()) < 2
}
#[doc = "let ten = NonZero::new(10u8)?;"]
#[doc = "let three = NonZero::new(3u8)?;"]
#[stable(feature = "isqrt", since = "1.84.0")]
#[rustc_const_stable(feature = "isqrt", since = "1.84.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn isqrt(self) -> Self {
let result = self.get().isqrt();
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::<u8>::MAX;"]
#[doc = "assert_eq!(n.cast_signed(), NonZero::new(-1i8).unwrap());"]
#[stable(feature = "integer_sign_cast", since = "1.87.0")]
#[rustc_const_stable(feature = "integer_sign_cast", since = "1.87.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn cast_signed(self) -> NonZero<i8> {
unsafe { NonZero::new_unchecked(self.get().cast_signed()) }
}
#[doc =
"assert_eq!(NonZero::<u8>::new(0b1)?.bit_width(), NonZero::new(1)?);"]
#[doc =
"assert_eq!(NonZero::<u8>::new(0b111)?.bit_width(), NonZero::new(3)?);"]
#[doc =
"assert_eq!(NonZero::<u8>::new(0b1110)?.bit_width(), NonZero::new(4)?);"]
#[stable(feature = "uint_bit_width", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "uint_bit_width", since =
"CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn bit_width(self) -> NonZero<u32> {
unsafe { NonZero::new_unchecked(Self::BITS - self.leading_zeros()) }
}
#[doc = "let two = NonZero::new(2u8)?;"]
#[doc = "let four = NonZero::new(4u8)?;"]
#[doc = "let max = NonZero::new(u8::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_mul(self, other: Self) -> Option<Self> {
if let Some(result) = self.get().checked_mul(other.get()) {
Some(unsafe { Self::new_unchecked(result) })
} else { None }
}
#[doc = "Return [`NonZero::<u8>::MAX`] on overflow."]
#[doc = "let two = NonZero::new(2u8)?;"]
#[doc = "let four = NonZero::new(4u8)?;"]
#[doc = "let max = NonZero::new(u8::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_mul(self, other: Self) -> Self {
unsafe { Self::new_unchecked(self.get().saturating_mul(other.get())) }
}
#[doc = "`self * rhs > u8::MAX`."]
#[doc = "let two = NonZero::new(2u8)?;"]
#[doc = "let four = NonZero::new(4u8)?;"]
#[unstable(feature = "nonzero_ops", issue = "84186")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const unsafe fn unchecked_mul(self, other: Self) -> Self {
unsafe { Self::new_unchecked(self.get().unchecked_mul(other.get())) }
}
#[doc = "let three = NonZero::new(3u8)?;"]
#[doc = "let twenty_seven = NonZero::new(27u8)?;"]
#[doc = "let half_max = NonZero::new(u8::MAX / 2)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_pow(self, other: u32) -> Option<Self> {
if let Some(result) = self.get().checked_pow(other) {
Some(unsafe { Self::new_unchecked(result) })
} else { None }
}
#[doc = "Return [`NonZero::<u8>::MAX`] on overflow."]
#[doc = "let three = NonZero::new(3u8)?;"]
#[doc = "let twenty_seven = NonZero::new(27u8)?;"]
#[doc = "let max = NonZero::new(u8::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_pow(self, other: u32) -> Self {
unsafe { Self::new_unchecked(self.get().saturating_pow(other)) }
}
#[doc = " `+` "]
#[doc =
"assert_eq!(NonZero::<u8>::from_ascii(b\"+10\"), Ok(NonZero::new(10)?));"]
#[doc = "assert!(NonZero::<u8>::from_ascii(b\"1 \").is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[inline]
pub const fn from_ascii(src: &[u8]) -> Result<Self, ParseIntError> {
Self::from_ascii_radix(src, 10)
}
#[doc = " `+` "]
#[doc =
"assert_eq!(NonZero::<u8>::from_ascii_radix(b\"A\", 16), Ok(NonZero::new(10)?));"]
#[doc = "assert!(NonZero::<u8>::from_ascii_radix(b\"1 \", 10).is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[inline]
pub const fn from_ascii_radix(src: &[u8], radix: u32)
-> Result<Self, ParseIntError> {
let n =
match <u8>::from_ascii_radix(src, radix) {
Ok(n) => n,
Err(err) => return Err(err),
};
if let Some(n) = Self::new(n) {
Ok(n)
} else { Err(ParseIntError { kind: IntErrorKind::Zero }) }
}
#[doc = " `+` "]
#[doc =
"assert_eq!(NonZero::<u8>::from_str_radix(\"A\", 16), Ok(NonZero::new(10)?));"]
#[doc = "assert!(NonZero::<u8>::from_str_radix(\"1 \", 10).is_err());"]
#[unstable(feature = "nonzero_from_str_radix", issue = "152193")]
#[inline]
pub const fn from_str_radix(src: &str, radix: u32)
-> Result<Self, ParseIntError> {
Self::from_ascii_radix(src.as_bytes(), radix)
}
}
#[stable(feature = "nonzero_parse", since = "1.35.0")]
impl FromStr for NonZero<u8> {
type Err = ParseIntError;
fn from_str(src: &str) -> Result<Self, Self::Err> {
Self::from_str_radix(src, 10)
}
}
#[stable(feature = "nonzero_div", since = "1.51.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const Div<NonZero<u8>> for u8 {
type Output = u8;
#[doc(alias = "unchecked_div")]
#[inline]
fn div(self, other: NonZero<u8>) -> u8 {
unsafe { intrinsics::unchecked_div(self, other.get()) }
}
}
#[stable(feature = "nonzero_div_assign", since = "1.79.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const DivAssign<NonZero<u8>> for u8 {
#[inline]
fn div_assign(&mut self, other: NonZero<u8>) { *self = *self / other; }
}
#[stable(feature = "nonzero_div", since = "1.51.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const Rem<NonZero<u8>> for u8 {
type Output = u8;
#[inline]
fn rem(self, other: NonZero<u8>) -> u8 {
unsafe { intrinsics::unchecked_rem(self, other.get()) }
}
}
#[stable(feature = "nonzero_div_assign", since = "1.79.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const RemAssign<NonZero<u8>> for u8 {
#[inline]
fn rem_assign(&mut self, other: NonZero<u8>) { *self = *self % other; }
}
impl NonZero<u8> {
#[doc = "let one = NonZero::new(1u8).unwrap();"]
#[doc = "let max = NonZero::new(u8::MAX).unwrap();"]
#[doc = "let two = NonZero::new(2u8).unwrap();"]
#[doc = "let three = NonZero::new(3u8).unwrap();"]
#[stable(feature = "unsigned_nonzero_div_ceil", since = "1.92.0")]
#[rustc_const_stable(feature = "unsigned_nonzero_div_ceil", since =
"1.92.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn div_ceil(self, rhs: Self) -> Self {
let v = self.get().div_ceil(rhs.get());
unsafe { Self::new_unchecked(v) }
}
}nonzero_integer! {
2417 Self = NonZeroU8,
2418 Primitive = unsigned u8,
2419 SignedPrimitive = i8,
2420 rot = 2,
2421 rot_op = "0x82",
2422 rot_result = "0xa",
2423 swap_op = "0x12",
2424 swapped = "0x12",
2425 reversed = "0x48",
2426}
2427
2428#[doc = "A [`u16`] that is known not to equal zero."]
#[doc = "For example, `Option<NonZeroU16>` is the same size as `u16`:"]
#[doc =
"assert_eq!(size_of::<Option<core::num::NonZeroU16>>(), size_of::<u16>());"]
#[doc =
"`NonZeroU16` is guaranteed to have the same layout and bit validity as `u16`"]
#[doc = "`Option<NonZeroU16>` is guaranteed to be compatible with `u16`,"]
#[doc = "`NonZeroU16` and `Option<NonZeroU16>`"]
#[doc = "use std::num::NonZeroU16;"]
#[doc =
"assert_eq!(size_of::<NonZeroU16>(), size_of::<Option<NonZeroU16>>());"]
#[doc =
"assert_eq!(align_of::<NonZeroU16>(), align_of::<Option<NonZeroU16>>());"]
#[doc = "`NonZeroU16`"]
#[doc = "use std::num::NonZeroU16;"]
#[doc =
"const TEN: NonZeroU16 = NonZeroU16::new(10).expect(\"ten is non-zero\");"]
#[stable(feature = "nonzero", since = "1.28.0")]
pub type NonZeroU16 = NonZero<u16>;
impl NonZero<u16> {
#[doc = "This value is equal to [`u16::BITS`]."]
#[doc = "assert_eq!(NonZero::<u16>::BITS, u16::BITS);"]
#[stable(feature = "nonzero_bits", since = "1.67.0")]
pub const BITS: u32 = <u16>::BITS;
#[doc = "let n = NonZero::<u16>::new(u16::MAX)?;"]
#[stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
#[rustc_const_stable(feature = "nonzero_leading_trailing_zeros", since =
"1.53.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn leading_zeros(self) -> u32 {
unsafe { intrinsics::ctlz_nonzero(self.get() as u16) }
}
#[doc = "let n = NonZero::<u16>::new(0b0101000)?;"]
#[stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
#[rustc_const_stable(feature = "nonzero_leading_trailing_zeros", since =
"1.53.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn trailing_zeros(self) -> u32 {
unsafe { intrinsics::cttz_nonzero(self.get() as u16) }
}
#[doc = "let a = NonZero::<u16>::new(0b_01100100)?;"]
#[doc = "let b = NonZero::<u16>::new(0b_01000000)?;"]
#[stable(feature = "isolate_most_least_significant_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn isolate_highest_one(self) -> Self {
unsafe {
let bit =
(((1 as u16) <<
(<u16>::BITS - 1)).unchecked_shr(self.leading_zeros()));
NonZero::new_unchecked(bit as u16)
}
}
#[doc = "let a = NonZero::<u16>::new(0b_01100100)?;"]
#[doc = "let b = NonZero::<u16>::new(0b_00000100)?;"]
#[stable(feature = "isolate_most_least_significant_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn isolate_lowest_one(self) -> Self {
let n = self.get();
let n = n & n.wrapping_neg();
unsafe { NonZero::new_unchecked(n) }
}
#[doc = "assert_eq!(NonZero::<u16>::new(0b1)?.highest_one(), 0);"]
#[doc = "assert_eq!(NonZero::<u16>::new(0b1_0000)?.highest_one(), 4);"]
#[doc = "assert_eq!(NonZero::<u16>::new(0b1_1111)?.highest_one(), 4);"]
#[stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn highest_one(self) -> u32 {
Self::BITS - 1 - self.leading_zeros()
}
#[doc = "assert_eq!(NonZero::<u16>::new(0b1)?.lowest_one(), 0);"]
#[doc = "assert_eq!(NonZero::<u16>::new(0b1_0000)?.lowest_one(), 4);"]
#[doc = "assert_eq!(NonZero::<u16>::new(0b1_1111)?.lowest_one(), 0);"]
#[stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn lowest_one(self) -> u32 { self.trailing_zeros() }
#[doc = "let a = NonZero::<u16>::new(0b100_0000)?;"]
#[doc = "let b = NonZero::<u16>::new(0b100_0011)?;"]
#[stable(feature = "non_zero_count_ones", since = "1.86.0")]
#[rustc_const_stable(feature = "non_zero_count_ones", since = "1.86.0")]
#[doc(alias = "popcount")]
#[doc(alias = "popcnt")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn count_ones(self) -> NonZero<u32> {
unsafe { NonZero::new_unchecked(self.get().count_ones()) }
}
#[doc = "let n = NonZero::new(0xa003u16)?;"]
#[doc = "let m = NonZero::new(0x3a)?;"]
#[doc = "assert_eq!(n.rotate_left(4), m);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn rotate_left(self, n: u32) -> Self {
let result = self.get().rotate_left(n);
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x3au16)?;"]
#[doc = "let m = NonZero::new(0xa003)?;"]
#[doc = "assert_eq!(n.rotate_right(4), m);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn rotate_right(self, n: u32) -> Self {
let result = self.get().rotate_right(n);
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x1234u16)?;"]
#[doc = "assert_eq!(m, NonZero::new(0x3412)?);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn swap_bytes(self) -> Self {
let result = self.get().swap_bytes();
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x1234u16)?;"]
#[doc = "assert_eq!(m, NonZero::new(0x2c48)?);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn reverse_bits(self) -> Self {
let result = self.get().reverse_bits();
unsafe { Self::new_unchecked(result) }
}
#[doc = "use std::num::NonZeroU16;"]
#[doc = "let n = NonZero::new(0x1Au16)?;"]
#[doc = " assert_eq!(NonZeroU16::from_be(n), n)"]
#[doc = " assert_eq!(NonZeroU16::from_be(n), n.swap_bytes())"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use]
#[inline(always)]
pub const fn from_be(x: Self) -> Self {
let result = u16::from_be(x.get());
unsafe { Self::new_unchecked(result) }
}
#[doc = "use std::num::NonZeroU16;"]
#[doc = "let n = NonZero::new(0x1Au16)?;"]
#[doc = " assert_eq!(NonZeroU16::from_le(n), n)"]
#[doc = " assert_eq!(NonZeroU16::from_le(n), n.swap_bytes())"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use]
#[inline(always)]
pub const fn from_le(x: Self) -> Self {
let result = u16::from_le(x.get());
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x1Au16)?;"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn to_be(self) -> Self {
let result = self.get().to_be();
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x1Au16)?;"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn to_le(self) -> Self {
let result = self.get().to_le();
unsafe { Self::new_unchecked(result) }
}
#[doc = "assert_eq!(NonZero::<u16>::MIN.get(), 1u16);"]
#[stable(feature = "nonzero_min_max", since = "1.70.0")]
pub const MIN: Self = Self::new(1).unwrap();
#[doc = "equal to [`u16::MAX`]."]
#[doc = "assert_eq!(NonZero::<u16>::MAX.get(), u16::MAX);"]
#[stable(feature = "nonzero_min_max", since = "1.70.0")]
pub const MAX: Self = Self::new(<u16>::MAX).unwrap();
#[doc = "let one = NonZero::new(1u16)?;"]
#[doc = "let two = NonZero::new(2u16)?;"]
#[doc = "let max = NonZero::new(u16::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_add(self, other: u16) -> Option<Self> {
if let Some(result) = self.get().checked_add(other) {
Some(unsafe { Self::new_unchecked(result) })
} else { None }
}
#[doc = "Return [`NonZero::<u16>::MAX`] on overflow."]
#[doc = "let one = NonZero::new(1u16)?;"]
#[doc = "let two = NonZero::new(2u16)?;"]
#[doc = "let max = NonZero::new(u16::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_add(self, other: u16) -> Self {
unsafe { Self::new_unchecked(self.get().saturating_add(other)) }
}
#[doc = "`self + rhs > u16::MAX`."]
#[doc = "let one = NonZero::new(1u16)?;"]
#[doc = "let two = NonZero::new(2u16)?;"]
#[unstable(feature = "nonzero_ops", issue = "84186")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const unsafe fn unchecked_add(self, other: u16) -> Self {
unsafe { Self::new_unchecked(self.get().unchecked_add(other)) }
}
#[doc = "let two = NonZero::new(2u16)?;"]
#[doc = "let three = NonZero::new(3u16)?;"]
#[doc = "let four = NonZero::new(4u16)?;"]
#[doc = "let max = NonZero::new(u16::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_next_power_of_two(self) -> Option<Self> {
if let Some(nz) = self.get().checked_next_power_of_two() {
Some(unsafe { Self::new_unchecked(nz) })
} else { None }
}
#[doc = "[`u16::ilog2`],"]
#[doc = "assert_eq!(NonZero::new(7u16)?.ilog2(), 2);"]
#[doc = "assert_eq!(NonZero::new(8u16)?.ilog2(), 3);"]
#[doc = "assert_eq!(NonZero::new(9u16)?.ilog2(), 3);"]
#[stable(feature = "int_log", since = "1.67.0")]
#[rustc_const_stable(feature = "int_log", since = "1.67.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn ilog2(self) -> u32 { Self::BITS - 1 - self.leading_zeros() }
#[doc = "[`u16::ilog10`],"]
#[doc = "assert_eq!(NonZero::new(99u16)?.ilog10(), 1);"]
#[doc = "assert_eq!(NonZero::new(100u16)?.ilog10(), 2);"]
#[doc = "assert_eq!(NonZero::new(101u16)?.ilog10(), 2);"]
#[stable(feature = "int_log", since = "1.67.0")]
#[rustc_const_stable(feature = "int_log", since = "1.67.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn ilog10(self) -> u32 { imp::int_log10::u16(self) }
#[doc = "let one = NonZero::new(1u16)?;"]
#[doc = "let two = NonZero::new(2u16)?;"]
#[doc = "let four = NonZero::new(4u16)?;"]
#[stable(feature = "num_midpoint", since = "1.85.0")]
#[rustc_const_stable(feature = "num_midpoint", since = "1.85.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[doc(alias = "average_floor")]
#[doc(alias = "average")]
#[inline]
pub const fn midpoint(self, rhs: Self) -> Self {
unsafe { Self::new_unchecked(self.get().midpoint(rhs.get())) }
}
#[doc = "let eight = NonZero::new(8u16)?;"]
#[doc = "let ten = NonZero::new(10u16)?;"]
#[must_use]
#[stable(feature = "nonzero_is_power_of_two", since = "1.59.0")]
#[rustc_const_stable(feature = "nonzero_is_power_of_two", since =
"1.59.0")]
#[inline]
pub const fn is_power_of_two(self) -> bool {
intrinsics::ctpop(self.get()) < 2
}
#[doc = "let ten = NonZero::new(10u16)?;"]
#[doc = "let three = NonZero::new(3u16)?;"]
#[stable(feature = "isqrt", since = "1.84.0")]
#[rustc_const_stable(feature = "isqrt", since = "1.84.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn isqrt(self) -> Self {
let result = self.get().isqrt();
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::<u16>::MAX;"]
#[doc = "assert_eq!(n.cast_signed(), NonZero::new(-1i16).unwrap());"]
#[stable(feature = "integer_sign_cast", since = "1.87.0")]
#[rustc_const_stable(feature = "integer_sign_cast", since = "1.87.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn cast_signed(self) -> NonZero<i16> {
unsafe { NonZero::new_unchecked(self.get().cast_signed()) }
}
#[doc =
"assert_eq!(NonZero::<u16>::new(0b1)?.bit_width(), NonZero::new(1)?);"]
#[doc =
"assert_eq!(NonZero::<u16>::new(0b111)?.bit_width(), NonZero::new(3)?);"]
#[doc =
"assert_eq!(NonZero::<u16>::new(0b1110)?.bit_width(), NonZero::new(4)?);"]
#[stable(feature = "uint_bit_width", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "uint_bit_width", since =
"CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn bit_width(self) -> NonZero<u32> {
unsafe { NonZero::new_unchecked(Self::BITS - self.leading_zeros()) }
}
#[doc = "let two = NonZero::new(2u16)?;"]
#[doc = "let four = NonZero::new(4u16)?;"]
#[doc = "let max = NonZero::new(u16::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_mul(self, other: Self) -> Option<Self> {
if let Some(result) = self.get().checked_mul(other.get()) {
Some(unsafe { Self::new_unchecked(result) })
} else { None }
}
#[doc = "Return [`NonZero::<u16>::MAX`] on overflow."]
#[doc = "let two = NonZero::new(2u16)?;"]
#[doc = "let four = NonZero::new(4u16)?;"]
#[doc = "let max = NonZero::new(u16::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_mul(self, other: Self) -> Self {
unsafe { Self::new_unchecked(self.get().saturating_mul(other.get())) }
}
#[doc = "`self * rhs > u16::MAX`."]
#[doc = "let two = NonZero::new(2u16)?;"]
#[doc = "let four = NonZero::new(4u16)?;"]
#[unstable(feature = "nonzero_ops", issue = "84186")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const unsafe fn unchecked_mul(self, other: Self) -> Self {
unsafe { Self::new_unchecked(self.get().unchecked_mul(other.get())) }
}
#[doc = "let three = NonZero::new(3u16)?;"]
#[doc = "let twenty_seven = NonZero::new(27u16)?;"]
#[doc = "let half_max = NonZero::new(u16::MAX / 2)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_pow(self, other: u32) -> Option<Self> {
if let Some(result) = self.get().checked_pow(other) {
Some(unsafe { Self::new_unchecked(result) })
} else { None }
}
#[doc = "Return [`NonZero::<u16>::MAX`] on overflow."]
#[doc = "let three = NonZero::new(3u16)?;"]
#[doc = "let twenty_seven = NonZero::new(27u16)?;"]
#[doc = "let max = NonZero::new(u16::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_pow(self, other: u32) -> Self {
unsafe { Self::new_unchecked(self.get().saturating_pow(other)) }
}
#[doc = " `+` "]
#[doc =
"assert_eq!(NonZero::<u16>::from_ascii(b\"+10\"), Ok(NonZero::new(10)?));"]
#[doc = "assert!(NonZero::<u16>::from_ascii(b\"1 \").is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[inline]
pub const fn from_ascii(src: &[u8]) -> Result<Self, ParseIntError> {
Self::from_ascii_radix(src, 10)
}
#[doc = " `+` "]
#[doc =
"assert_eq!(NonZero::<u16>::from_ascii_radix(b\"A\", 16), Ok(NonZero::new(10)?));"]
#[doc =
"assert!(NonZero::<u16>::from_ascii_radix(b\"1 \", 10).is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[inline]
pub const fn from_ascii_radix(src: &[u8], radix: u32)
-> Result<Self, ParseIntError> {
let n =
match <u16>::from_ascii_radix(src, radix) {
Ok(n) => n,
Err(err) => return Err(err),
};
if let Some(n) = Self::new(n) {
Ok(n)
} else { Err(ParseIntError { kind: IntErrorKind::Zero }) }
}
#[doc = " `+` "]
#[doc =
"assert_eq!(NonZero::<u16>::from_str_radix(\"A\", 16), Ok(NonZero::new(10)?));"]
#[doc = "assert!(NonZero::<u16>::from_str_radix(\"1 \", 10).is_err());"]
#[unstable(feature = "nonzero_from_str_radix", issue = "152193")]
#[inline]
pub const fn from_str_radix(src: &str, radix: u32)
-> Result<Self, ParseIntError> {
Self::from_ascii_radix(src.as_bytes(), radix)
}
}
#[stable(feature = "nonzero_parse", since = "1.35.0")]
impl FromStr for NonZero<u16> {
type Err = ParseIntError;
fn from_str(src: &str) -> Result<Self, Self::Err> {
Self::from_str_radix(src, 10)
}
}
#[stable(feature = "nonzero_div", since = "1.51.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const Div<NonZero<u16>> for u16 {
type Output = u16;
#[doc(alias = "unchecked_div")]
#[inline]
fn div(self, other: NonZero<u16>) -> u16 {
unsafe { intrinsics::unchecked_div(self, other.get()) }
}
}
#[stable(feature = "nonzero_div_assign", since = "1.79.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const DivAssign<NonZero<u16>> for u16 {
#[inline]
fn div_assign(&mut self, other: NonZero<u16>) { *self = *self / other; }
}
#[stable(feature = "nonzero_div", since = "1.51.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const Rem<NonZero<u16>> for u16 {
type Output = u16;
#[inline]
fn rem(self, other: NonZero<u16>) -> u16 {
unsafe { intrinsics::unchecked_rem(self, other.get()) }
}
}
#[stable(feature = "nonzero_div_assign", since = "1.79.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const RemAssign<NonZero<u16>> for u16 {
#[inline]
fn rem_assign(&mut self, other: NonZero<u16>) { *self = *self % other; }
}
impl NonZero<u16> {
#[doc = "let one = NonZero::new(1u16).unwrap();"]
#[doc = "let max = NonZero::new(u16::MAX).unwrap();"]
#[doc = "let two = NonZero::new(2u16).unwrap();"]
#[doc = "let three = NonZero::new(3u16).unwrap();"]
#[stable(feature = "unsigned_nonzero_div_ceil", since = "1.92.0")]
#[rustc_const_stable(feature = "unsigned_nonzero_div_ceil", since =
"1.92.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn div_ceil(self, rhs: Self) -> Self {
let v = self.get().div_ceil(rhs.get());
unsafe { Self::new_unchecked(v) }
}
}nonzero_integer! {
2429 Self = NonZeroU16,
2430 Primitive = unsigned u16,
2431 SignedPrimitive = i16,
2432 rot = 4,
2433 rot_op = "0xa003",
2434 rot_result = "0x3a",
2435 swap_op = "0x1234",
2436 swapped = "0x3412",
2437 reversed = "0x2c48",
2438}
2439
2440#[doc = "A [`u32`] that is known not to equal zero."]
#[doc = "For example, `Option<NonZeroU32>` is the same size as `u32`:"]
#[doc =
"assert_eq!(size_of::<Option<core::num::NonZeroU32>>(), size_of::<u32>());"]
#[doc =
"`NonZeroU32` is guaranteed to have the same layout and bit validity as `u32`"]
#[doc = "`Option<NonZeroU32>` is guaranteed to be compatible with `u32`,"]
#[doc = "`NonZeroU32` and `Option<NonZeroU32>`"]
#[doc = "use std::num::NonZeroU32;"]
#[doc =
"assert_eq!(size_of::<NonZeroU32>(), size_of::<Option<NonZeroU32>>());"]
#[doc =
"assert_eq!(align_of::<NonZeroU32>(), align_of::<Option<NonZeroU32>>());"]
#[doc = "`NonZeroU32`"]
#[doc = "use std::num::NonZeroU32;"]
#[doc =
"const TEN: NonZeroU32 = NonZeroU32::new(10).expect(\"ten is non-zero\");"]
#[stable(feature = "nonzero", since = "1.28.0")]
pub type NonZeroU32 = NonZero<u32>;
impl NonZero<u32> {
#[doc = "This value is equal to [`u32::BITS`]."]
#[doc = "assert_eq!(NonZero::<u32>::BITS, u32::BITS);"]
#[stable(feature = "nonzero_bits", since = "1.67.0")]
pub const BITS: u32 = <u32>::BITS;
#[doc = "let n = NonZero::<u32>::new(u32::MAX)?;"]
#[stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
#[rustc_const_stable(feature = "nonzero_leading_trailing_zeros", since =
"1.53.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn leading_zeros(self) -> u32 {
unsafe { intrinsics::ctlz_nonzero(self.get() as u32) }
}
#[doc = "let n = NonZero::<u32>::new(0b0101000)?;"]
#[stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
#[rustc_const_stable(feature = "nonzero_leading_trailing_zeros", since =
"1.53.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn trailing_zeros(self) -> u32 {
unsafe { intrinsics::cttz_nonzero(self.get() as u32) }
}
#[doc = "let a = NonZero::<u32>::new(0b_01100100)?;"]
#[doc = "let b = NonZero::<u32>::new(0b_01000000)?;"]
#[stable(feature = "isolate_most_least_significant_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn isolate_highest_one(self) -> Self {
unsafe {
let bit =
(((1 as u32) <<
(<u32>::BITS - 1)).unchecked_shr(self.leading_zeros()));
NonZero::new_unchecked(bit as u32)
}
}
#[doc = "let a = NonZero::<u32>::new(0b_01100100)?;"]
#[doc = "let b = NonZero::<u32>::new(0b_00000100)?;"]
#[stable(feature = "isolate_most_least_significant_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn isolate_lowest_one(self) -> Self {
let n = self.get();
let n = n & n.wrapping_neg();
unsafe { NonZero::new_unchecked(n) }
}
#[doc = "assert_eq!(NonZero::<u32>::new(0b1)?.highest_one(), 0);"]
#[doc = "assert_eq!(NonZero::<u32>::new(0b1_0000)?.highest_one(), 4);"]
#[doc = "assert_eq!(NonZero::<u32>::new(0b1_1111)?.highest_one(), 4);"]
#[stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn highest_one(self) -> u32 {
Self::BITS - 1 - self.leading_zeros()
}
#[doc = "assert_eq!(NonZero::<u32>::new(0b1)?.lowest_one(), 0);"]
#[doc = "assert_eq!(NonZero::<u32>::new(0b1_0000)?.lowest_one(), 4);"]
#[doc = "assert_eq!(NonZero::<u32>::new(0b1_1111)?.lowest_one(), 0);"]
#[stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn lowest_one(self) -> u32 { self.trailing_zeros() }
#[doc = "let a = NonZero::<u32>::new(0b100_0000)?;"]
#[doc = "let b = NonZero::<u32>::new(0b100_0011)?;"]
#[stable(feature = "non_zero_count_ones", since = "1.86.0")]
#[rustc_const_stable(feature = "non_zero_count_ones", since = "1.86.0")]
#[doc(alias = "popcount")]
#[doc(alias = "popcnt")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn count_ones(self) -> NonZero<u32> {
unsafe { NonZero::new_unchecked(self.get().count_ones()) }
}
#[doc = "let n = NonZero::new(0x10000b3u32)?;"]
#[doc = "let m = NonZero::new(0xb301)?;"]
#[doc = "assert_eq!(n.rotate_left(8), m);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn rotate_left(self, n: u32) -> Self {
let result = self.get().rotate_left(n);
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0xb301u32)?;"]
#[doc = "let m = NonZero::new(0x10000b3)?;"]
#[doc = "assert_eq!(n.rotate_right(8), m);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn rotate_right(self, n: u32) -> Self {
let result = self.get().rotate_right(n);
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x12345678u32)?;"]
#[doc = "assert_eq!(m, NonZero::new(0x78563412)?);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn swap_bytes(self) -> Self {
let result = self.get().swap_bytes();
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x12345678u32)?;"]
#[doc = "assert_eq!(m, NonZero::new(0x1e6a2c48)?);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn reverse_bits(self) -> Self {
let result = self.get().reverse_bits();
unsafe { Self::new_unchecked(result) }
}
#[doc = "use std::num::NonZeroU32;"]
#[doc = "let n = NonZero::new(0x1Au32)?;"]
#[doc = " assert_eq!(NonZeroU32::from_be(n), n)"]
#[doc = " assert_eq!(NonZeroU32::from_be(n), n.swap_bytes())"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use]
#[inline(always)]
pub const fn from_be(x: Self) -> Self {
let result = u32::from_be(x.get());
unsafe { Self::new_unchecked(result) }
}
#[doc = "use std::num::NonZeroU32;"]
#[doc = "let n = NonZero::new(0x1Au32)?;"]
#[doc = " assert_eq!(NonZeroU32::from_le(n), n)"]
#[doc = " assert_eq!(NonZeroU32::from_le(n), n.swap_bytes())"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use]
#[inline(always)]
pub const fn from_le(x: Self) -> Self {
let result = u32::from_le(x.get());
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x1Au32)?;"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn to_be(self) -> Self {
let result = self.get().to_be();
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x1Au32)?;"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn to_le(self) -> Self {
let result = self.get().to_le();
unsafe { Self::new_unchecked(result) }
}
#[doc = "assert_eq!(NonZero::<u32>::MIN.get(), 1u32);"]
#[stable(feature = "nonzero_min_max", since = "1.70.0")]
pub const MIN: Self = Self::new(1).unwrap();
#[doc = "equal to [`u32::MAX`]."]
#[doc = "assert_eq!(NonZero::<u32>::MAX.get(), u32::MAX);"]
#[stable(feature = "nonzero_min_max", since = "1.70.0")]
pub const MAX: Self = Self::new(<u32>::MAX).unwrap();
#[doc = "let one = NonZero::new(1u32)?;"]
#[doc = "let two = NonZero::new(2u32)?;"]
#[doc = "let max = NonZero::new(u32::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_add(self, other: u32) -> Option<Self> {
if let Some(result) = self.get().checked_add(other) {
Some(unsafe { Self::new_unchecked(result) })
} else { None }
}
#[doc = "Return [`NonZero::<u32>::MAX`] on overflow."]
#[doc = "let one = NonZero::new(1u32)?;"]
#[doc = "let two = NonZero::new(2u32)?;"]
#[doc = "let max = NonZero::new(u32::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_add(self, other: u32) -> Self {
unsafe { Self::new_unchecked(self.get().saturating_add(other)) }
}
#[doc = "`self + rhs > u32::MAX`."]
#[doc = "let one = NonZero::new(1u32)?;"]
#[doc = "let two = NonZero::new(2u32)?;"]
#[unstable(feature = "nonzero_ops", issue = "84186")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const unsafe fn unchecked_add(self, other: u32) -> Self {
unsafe { Self::new_unchecked(self.get().unchecked_add(other)) }
}
#[doc = "let two = NonZero::new(2u32)?;"]
#[doc = "let three = NonZero::new(3u32)?;"]
#[doc = "let four = NonZero::new(4u32)?;"]
#[doc = "let max = NonZero::new(u32::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_next_power_of_two(self) -> Option<Self> {
if let Some(nz) = self.get().checked_next_power_of_two() {
Some(unsafe { Self::new_unchecked(nz) })
} else { None }
}
#[doc = "[`u32::ilog2`],"]
#[doc = "assert_eq!(NonZero::new(7u32)?.ilog2(), 2);"]
#[doc = "assert_eq!(NonZero::new(8u32)?.ilog2(), 3);"]
#[doc = "assert_eq!(NonZero::new(9u32)?.ilog2(), 3);"]
#[stable(feature = "int_log", since = "1.67.0")]
#[rustc_const_stable(feature = "int_log", since = "1.67.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn ilog2(self) -> u32 { Self::BITS - 1 - self.leading_zeros() }
#[doc = "[`u32::ilog10`],"]
#[doc = "assert_eq!(NonZero::new(99u32)?.ilog10(), 1);"]
#[doc = "assert_eq!(NonZero::new(100u32)?.ilog10(), 2);"]
#[doc = "assert_eq!(NonZero::new(101u32)?.ilog10(), 2);"]
#[stable(feature = "int_log", since = "1.67.0")]
#[rustc_const_stable(feature = "int_log", since = "1.67.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn ilog10(self) -> u32 { imp::int_log10::u32(self) }
#[doc = "let one = NonZero::new(1u32)?;"]
#[doc = "let two = NonZero::new(2u32)?;"]
#[doc = "let four = NonZero::new(4u32)?;"]
#[stable(feature = "num_midpoint", since = "1.85.0")]
#[rustc_const_stable(feature = "num_midpoint", since = "1.85.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[doc(alias = "average_floor")]
#[doc(alias = "average")]
#[inline]
pub const fn midpoint(self, rhs: Self) -> Self {
unsafe { Self::new_unchecked(self.get().midpoint(rhs.get())) }
}
#[doc = "let eight = NonZero::new(8u32)?;"]
#[doc = "let ten = NonZero::new(10u32)?;"]
#[must_use]
#[stable(feature = "nonzero_is_power_of_two", since = "1.59.0")]
#[rustc_const_stable(feature = "nonzero_is_power_of_two", since =
"1.59.0")]
#[inline]
pub const fn is_power_of_two(self) -> bool {
intrinsics::ctpop(self.get()) < 2
}
#[doc = "let ten = NonZero::new(10u32)?;"]
#[doc = "let three = NonZero::new(3u32)?;"]
#[stable(feature = "isqrt", since = "1.84.0")]
#[rustc_const_stable(feature = "isqrt", since = "1.84.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn isqrt(self) -> Self {
let result = self.get().isqrt();
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::<u32>::MAX;"]
#[doc = "assert_eq!(n.cast_signed(), NonZero::new(-1i32).unwrap());"]
#[stable(feature = "integer_sign_cast", since = "1.87.0")]
#[rustc_const_stable(feature = "integer_sign_cast", since = "1.87.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn cast_signed(self) -> NonZero<i32> {
unsafe { NonZero::new_unchecked(self.get().cast_signed()) }
}
#[doc =
"assert_eq!(NonZero::<u32>::new(0b1)?.bit_width(), NonZero::new(1)?);"]
#[doc =
"assert_eq!(NonZero::<u32>::new(0b111)?.bit_width(), NonZero::new(3)?);"]
#[doc =
"assert_eq!(NonZero::<u32>::new(0b1110)?.bit_width(), NonZero::new(4)?);"]
#[stable(feature = "uint_bit_width", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "uint_bit_width", since =
"CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn bit_width(self) -> NonZero<u32> {
unsafe { NonZero::new_unchecked(Self::BITS - self.leading_zeros()) }
}
#[doc = "let two = NonZero::new(2u32)?;"]
#[doc = "let four = NonZero::new(4u32)?;"]
#[doc = "let max = NonZero::new(u32::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_mul(self, other: Self) -> Option<Self> {
if let Some(result) = self.get().checked_mul(other.get()) {
Some(unsafe { Self::new_unchecked(result) })
} else { None }
}
#[doc = "Return [`NonZero::<u32>::MAX`] on overflow."]
#[doc = "let two = NonZero::new(2u32)?;"]
#[doc = "let four = NonZero::new(4u32)?;"]
#[doc = "let max = NonZero::new(u32::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_mul(self, other: Self) -> Self {
unsafe { Self::new_unchecked(self.get().saturating_mul(other.get())) }
}
#[doc = "`self * rhs > u32::MAX`."]
#[doc = "let two = NonZero::new(2u32)?;"]
#[doc = "let four = NonZero::new(4u32)?;"]
#[unstable(feature = "nonzero_ops", issue = "84186")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const unsafe fn unchecked_mul(self, other: Self) -> Self {
unsafe { Self::new_unchecked(self.get().unchecked_mul(other.get())) }
}
#[doc = "let three = NonZero::new(3u32)?;"]
#[doc = "let twenty_seven = NonZero::new(27u32)?;"]
#[doc = "let half_max = NonZero::new(u32::MAX / 2)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_pow(self, other: u32) -> Option<Self> {
if let Some(result) = self.get().checked_pow(other) {
Some(unsafe { Self::new_unchecked(result) })
} else { None }
}
#[doc = "Return [`NonZero::<u32>::MAX`] on overflow."]
#[doc = "let three = NonZero::new(3u32)?;"]
#[doc = "let twenty_seven = NonZero::new(27u32)?;"]
#[doc = "let max = NonZero::new(u32::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_pow(self, other: u32) -> Self {
unsafe { Self::new_unchecked(self.get().saturating_pow(other)) }
}
#[doc = " `+` "]
#[doc =
"assert_eq!(NonZero::<u32>::from_ascii(b\"+10\"), Ok(NonZero::new(10)?));"]
#[doc = "assert!(NonZero::<u32>::from_ascii(b\"1 \").is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[inline]
pub const fn from_ascii(src: &[u8]) -> Result<Self, ParseIntError> {
Self::from_ascii_radix(src, 10)
}
#[doc = " `+` "]
#[doc =
"assert_eq!(NonZero::<u32>::from_ascii_radix(b\"A\", 16), Ok(NonZero::new(10)?));"]
#[doc =
"assert!(NonZero::<u32>::from_ascii_radix(b\"1 \", 10).is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[inline]
pub const fn from_ascii_radix(src: &[u8], radix: u32)
-> Result<Self, ParseIntError> {
let n =
match <u32>::from_ascii_radix(src, radix) {
Ok(n) => n,
Err(err) => return Err(err),
};
if let Some(n) = Self::new(n) {
Ok(n)
} else { Err(ParseIntError { kind: IntErrorKind::Zero }) }
}
#[doc = " `+` "]
#[doc =
"assert_eq!(NonZero::<u32>::from_str_radix(\"A\", 16), Ok(NonZero::new(10)?));"]
#[doc = "assert!(NonZero::<u32>::from_str_radix(\"1 \", 10).is_err());"]
#[unstable(feature = "nonzero_from_str_radix", issue = "152193")]
#[inline]
pub const fn from_str_radix(src: &str, radix: u32)
-> Result<Self, ParseIntError> {
Self::from_ascii_radix(src.as_bytes(), radix)
}
}
#[stable(feature = "nonzero_parse", since = "1.35.0")]
impl FromStr for NonZero<u32> {
type Err = ParseIntError;
fn from_str(src: &str) -> Result<Self, Self::Err> {
Self::from_str_radix(src, 10)
}
}
#[stable(feature = "nonzero_div", since = "1.51.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const Div<NonZero<u32>> for u32 {
type Output = u32;
#[doc(alias = "unchecked_div")]
#[inline]
fn div(self, other: NonZero<u32>) -> u32 {
unsafe { intrinsics::unchecked_div(self, other.get()) }
}
}
#[stable(feature = "nonzero_div_assign", since = "1.79.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const DivAssign<NonZero<u32>> for u32 {
#[inline]
fn div_assign(&mut self, other: NonZero<u32>) { *self = *self / other; }
}
#[stable(feature = "nonzero_div", since = "1.51.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const Rem<NonZero<u32>> for u32 {
type Output = u32;
#[inline]
fn rem(self, other: NonZero<u32>) -> u32 {
unsafe { intrinsics::unchecked_rem(self, other.get()) }
}
}
#[stable(feature = "nonzero_div_assign", since = "1.79.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const RemAssign<NonZero<u32>> for u32 {
#[inline]
fn rem_assign(&mut self, other: NonZero<u32>) { *self = *self % other; }
}
impl NonZero<u32> {
#[doc = "let one = NonZero::new(1u32).unwrap();"]
#[doc = "let max = NonZero::new(u32::MAX).unwrap();"]
#[doc = "let two = NonZero::new(2u32).unwrap();"]
#[doc = "let three = NonZero::new(3u32).unwrap();"]
#[stable(feature = "unsigned_nonzero_div_ceil", since = "1.92.0")]
#[rustc_const_stable(feature = "unsigned_nonzero_div_ceil", since =
"1.92.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn div_ceil(self, rhs: Self) -> Self {
let v = self.get().div_ceil(rhs.get());
unsafe { Self::new_unchecked(v) }
}
}nonzero_integer! {
2441 Self = NonZeroU32,
2442 Primitive = unsigned u32,
2443 SignedPrimitive = i32,
2444 rot = 8,
2445 rot_op = "0x10000b3",
2446 rot_result = "0xb301",
2447 swap_op = "0x12345678",
2448 swapped = "0x78563412",
2449 reversed = "0x1e6a2c48",
2450}
2451
2452#[doc = "A [`u64`] that is known not to equal zero."]
#[doc = "For example, `Option<NonZeroU64>` is the same size as `u64`:"]
#[doc =
"assert_eq!(size_of::<Option<core::num::NonZeroU64>>(), size_of::<u64>());"]
#[doc =
"`NonZeroU64` is guaranteed to have the same layout and bit validity as `u64`"]
#[doc = "`Option<NonZeroU64>` is guaranteed to be compatible with `u64`,"]
#[doc = "`NonZeroU64` and `Option<NonZeroU64>`"]
#[doc = "use std::num::NonZeroU64;"]
#[doc =
"assert_eq!(size_of::<NonZeroU64>(), size_of::<Option<NonZeroU64>>());"]
#[doc =
"assert_eq!(align_of::<NonZeroU64>(), align_of::<Option<NonZeroU64>>());"]
#[doc = "`NonZeroU64`"]
#[doc = "use std::num::NonZeroU64;"]
#[doc =
"const TEN: NonZeroU64 = NonZeroU64::new(10).expect(\"ten is non-zero\");"]
#[stable(feature = "nonzero", since = "1.28.0")]
pub type NonZeroU64 = NonZero<u64>;
impl NonZero<u64> {
#[doc = "This value is equal to [`u64::BITS`]."]
#[doc = "assert_eq!(NonZero::<u64>::BITS, u64::BITS);"]
#[stable(feature = "nonzero_bits", since = "1.67.0")]
pub const BITS: u32 = <u64>::BITS;
#[doc = "let n = NonZero::<u64>::new(u64::MAX)?;"]
#[stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
#[rustc_const_stable(feature = "nonzero_leading_trailing_zeros", since =
"1.53.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn leading_zeros(self) -> u32 {
unsafe { intrinsics::ctlz_nonzero(self.get() as u64) }
}
#[doc = "let n = NonZero::<u64>::new(0b0101000)?;"]
#[stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
#[rustc_const_stable(feature = "nonzero_leading_trailing_zeros", since =
"1.53.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn trailing_zeros(self) -> u32 {
unsafe { intrinsics::cttz_nonzero(self.get() as u64) }
}
#[doc = "let a = NonZero::<u64>::new(0b_01100100)?;"]
#[doc = "let b = NonZero::<u64>::new(0b_01000000)?;"]
#[stable(feature = "isolate_most_least_significant_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn isolate_highest_one(self) -> Self {
unsafe {
let bit =
(((1 as u64) <<
(<u64>::BITS - 1)).unchecked_shr(self.leading_zeros()));
NonZero::new_unchecked(bit as u64)
}
}
#[doc = "let a = NonZero::<u64>::new(0b_01100100)?;"]
#[doc = "let b = NonZero::<u64>::new(0b_00000100)?;"]
#[stable(feature = "isolate_most_least_significant_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn isolate_lowest_one(self) -> Self {
let n = self.get();
let n = n & n.wrapping_neg();
unsafe { NonZero::new_unchecked(n) }
}
#[doc = "assert_eq!(NonZero::<u64>::new(0b1)?.highest_one(), 0);"]
#[doc = "assert_eq!(NonZero::<u64>::new(0b1_0000)?.highest_one(), 4);"]
#[doc = "assert_eq!(NonZero::<u64>::new(0b1_1111)?.highest_one(), 4);"]
#[stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn highest_one(self) -> u32 {
Self::BITS - 1 - self.leading_zeros()
}
#[doc = "assert_eq!(NonZero::<u64>::new(0b1)?.lowest_one(), 0);"]
#[doc = "assert_eq!(NonZero::<u64>::new(0b1_0000)?.lowest_one(), 4);"]
#[doc = "assert_eq!(NonZero::<u64>::new(0b1_1111)?.lowest_one(), 0);"]
#[stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn lowest_one(self) -> u32 { self.trailing_zeros() }
#[doc = "let a = NonZero::<u64>::new(0b100_0000)?;"]
#[doc = "let b = NonZero::<u64>::new(0b100_0011)?;"]
#[stable(feature = "non_zero_count_ones", since = "1.86.0")]
#[rustc_const_stable(feature = "non_zero_count_ones", since = "1.86.0")]
#[doc(alias = "popcount")]
#[doc(alias = "popcnt")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn count_ones(self) -> NonZero<u32> {
unsafe { NonZero::new_unchecked(self.get().count_ones()) }
}
#[doc = "let n = NonZero::new(0xaa00000000006e1u64)?;"]
#[doc = "let m = NonZero::new(0x6e10aa)?;"]
#[doc = "assert_eq!(n.rotate_left(12), m);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn rotate_left(self, n: u32) -> Self {
let result = self.get().rotate_left(n);
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x6e10aau64)?;"]
#[doc = "let m = NonZero::new(0xaa00000000006e1)?;"]
#[doc = "assert_eq!(n.rotate_right(12), m);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn rotate_right(self, n: u32) -> Self {
let result = self.get().rotate_right(n);
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x1234567890123456u64)?;"]
#[doc = "assert_eq!(m, NonZero::new(0x5634129078563412)?);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn swap_bytes(self) -> Self {
let result = self.get().swap_bytes();
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x1234567890123456u64)?;"]
#[doc = "assert_eq!(m, NonZero::new(0x6a2c48091e6a2c48)?);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn reverse_bits(self) -> Self {
let result = self.get().reverse_bits();
unsafe { Self::new_unchecked(result) }
}
#[doc = "use std::num::NonZeroU64;"]
#[doc = "let n = NonZero::new(0x1Au64)?;"]
#[doc = " assert_eq!(NonZeroU64::from_be(n), n)"]
#[doc = " assert_eq!(NonZeroU64::from_be(n), n.swap_bytes())"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use]
#[inline(always)]
pub const fn from_be(x: Self) -> Self {
let result = u64::from_be(x.get());
unsafe { Self::new_unchecked(result) }
}
#[doc = "use std::num::NonZeroU64;"]
#[doc = "let n = NonZero::new(0x1Au64)?;"]
#[doc = " assert_eq!(NonZeroU64::from_le(n), n)"]
#[doc = " assert_eq!(NonZeroU64::from_le(n), n.swap_bytes())"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use]
#[inline(always)]
pub const fn from_le(x: Self) -> Self {
let result = u64::from_le(x.get());
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x1Au64)?;"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn to_be(self) -> Self {
let result = self.get().to_be();
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x1Au64)?;"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn to_le(self) -> Self {
let result = self.get().to_le();
unsafe { Self::new_unchecked(result) }
}
#[doc = "assert_eq!(NonZero::<u64>::MIN.get(), 1u64);"]
#[stable(feature = "nonzero_min_max", since = "1.70.0")]
pub const MIN: Self = Self::new(1).unwrap();
#[doc = "equal to [`u64::MAX`]."]
#[doc = "assert_eq!(NonZero::<u64>::MAX.get(), u64::MAX);"]
#[stable(feature = "nonzero_min_max", since = "1.70.0")]
pub const MAX: Self = Self::new(<u64>::MAX).unwrap();
#[doc = "let one = NonZero::new(1u64)?;"]
#[doc = "let two = NonZero::new(2u64)?;"]
#[doc = "let max = NonZero::new(u64::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_add(self, other: u64) -> Option<Self> {
if let Some(result) = self.get().checked_add(other) {
Some(unsafe { Self::new_unchecked(result) })
} else { None }
}
#[doc = "Return [`NonZero::<u64>::MAX`] on overflow."]
#[doc = "let one = NonZero::new(1u64)?;"]
#[doc = "let two = NonZero::new(2u64)?;"]
#[doc = "let max = NonZero::new(u64::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_add(self, other: u64) -> Self {
unsafe { Self::new_unchecked(self.get().saturating_add(other)) }
}
#[doc = "`self + rhs > u64::MAX`."]
#[doc = "let one = NonZero::new(1u64)?;"]
#[doc = "let two = NonZero::new(2u64)?;"]
#[unstable(feature = "nonzero_ops", issue = "84186")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const unsafe fn unchecked_add(self, other: u64) -> Self {
unsafe { Self::new_unchecked(self.get().unchecked_add(other)) }
}
#[doc = "let two = NonZero::new(2u64)?;"]
#[doc = "let three = NonZero::new(3u64)?;"]
#[doc = "let four = NonZero::new(4u64)?;"]
#[doc = "let max = NonZero::new(u64::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_next_power_of_two(self) -> Option<Self> {
if let Some(nz) = self.get().checked_next_power_of_two() {
Some(unsafe { Self::new_unchecked(nz) })
} else { None }
}
#[doc = "[`u64::ilog2`],"]
#[doc = "assert_eq!(NonZero::new(7u64)?.ilog2(), 2);"]
#[doc = "assert_eq!(NonZero::new(8u64)?.ilog2(), 3);"]
#[doc = "assert_eq!(NonZero::new(9u64)?.ilog2(), 3);"]
#[stable(feature = "int_log", since = "1.67.0")]
#[rustc_const_stable(feature = "int_log", since = "1.67.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn ilog2(self) -> u32 { Self::BITS - 1 - self.leading_zeros() }
#[doc = "[`u64::ilog10`],"]
#[doc = "assert_eq!(NonZero::new(99u64)?.ilog10(), 1);"]
#[doc = "assert_eq!(NonZero::new(100u64)?.ilog10(), 2);"]
#[doc = "assert_eq!(NonZero::new(101u64)?.ilog10(), 2);"]
#[stable(feature = "int_log", since = "1.67.0")]
#[rustc_const_stable(feature = "int_log", since = "1.67.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn ilog10(self) -> u32 { imp::int_log10::u64(self) }
#[doc = "let one = NonZero::new(1u64)?;"]
#[doc = "let two = NonZero::new(2u64)?;"]
#[doc = "let four = NonZero::new(4u64)?;"]
#[stable(feature = "num_midpoint", since = "1.85.0")]
#[rustc_const_stable(feature = "num_midpoint", since = "1.85.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[doc(alias = "average_floor")]
#[doc(alias = "average")]
#[inline]
pub const fn midpoint(self, rhs: Self) -> Self {
unsafe { Self::new_unchecked(self.get().midpoint(rhs.get())) }
}
#[doc = "let eight = NonZero::new(8u64)?;"]
#[doc = "let ten = NonZero::new(10u64)?;"]
#[must_use]
#[stable(feature = "nonzero_is_power_of_two", since = "1.59.0")]
#[rustc_const_stable(feature = "nonzero_is_power_of_two", since =
"1.59.0")]
#[inline]
pub const fn is_power_of_two(self) -> bool {
intrinsics::ctpop(self.get()) < 2
}
#[doc = "let ten = NonZero::new(10u64)?;"]
#[doc = "let three = NonZero::new(3u64)?;"]
#[stable(feature = "isqrt", since = "1.84.0")]
#[rustc_const_stable(feature = "isqrt", since = "1.84.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn isqrt(self) -> Self {
let result = self.get().isqrt();
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::<u64>::MAX;"]
#[doc = "assert_eq!(n.cast_signed(), NonZero::new(-1i64).unwrap());"]
#[stable(feature = "integer_sign_cast", since = "1.87.0")]
#[rustc_const_stable(feature = "integer_sign_cast", since = "1.87.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn cast_signed(self) -> NonZero<i64> {
unsafe { NonZero::new_unchecked(self.get().cast_signed()) }
}
#[doc =
"assert_eq!(NonZero::<u64>::new(0b1)?.bit_width(), NonZero::new(1)?);"]
#[doc =
"assert_eq!(NonZero::<u64>::new(0b111)?.bit_width(), NonZero::new(3)?);"]
#[doc =
"assert_eq!(NonZero::<u64>::new(0b1110)?.bit_width(), NonZero::new(4)?);"]
#[stable(feature = "uint_bit_width", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "uint_bit_width", since =
"CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn bit_width(self) -> NonZero<u32> {
unsafe { NonZero::new_unchecked(Self::BITS - self.leading_zeros()) }
}
#[doc = "let two = NonZero::new(2u64)?;"]
#[doc = "let four = NonZero::new(4u64)?;"]
#[doc = "let max = NonZero::new(u64::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_mul(self, other: Self) -> Option<Self> {
if let Some(result) = self.get().checked_mul(other.get()) {
Some(unsafe { Self::new_unchecked(result) })
} else { None }
}
#[doc = "Return [`NonZero::<u64>::MAX`] on overflow."]
#[doc = "let two = NonZero::new(2u64)?;"]
#[doc = "let four = NonZero::new(4u64)?;"]
#[doc = "let max = NonZero::new(u64::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_mul(self, other: Self) -> Self {
unsafe { Self::new_unchecked(self.get().saturating_mul(other.get())) }
}
#[doc = "`self * rhs > u64::MAX`."]
#[doc = "let two = NonZero::new(2u64)?;"]
#[doc = "let four = NonZero::new(4u64)?;"]
#[unstable(feature = "nonzero_ops", issue = "84186")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const unsafe fn unchecked_mul(self, other: Self) -> Self {
unsafe { Self::new_unchecked(self.get().unchecked_mul(other.get())) }
}
#[doc = "let three = NonZero::new(3u64)?;"]
#[doc = "let twenty_seven = NonZero::new(27u64)?;"]
#[doc = "let half_max = NonZero::new(u64::MAX / 2)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_pow(self, other: u32) -> Option<Self> {
if let Some(result) = self.get().checked_pow(other) {
Some(unsafe { Self::new_unchecked(result) })
} else { None }
}
#[doc = "Return [`NonZero::<u64>::MAX`] on overflow."]
#[doc = "let three = NonZero::new(3u64)?;"]
#[doc = "let twenty_seven = NonZero::new(27u64)?;"]
#[doc = "let max = NonZero::new(u64::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_pow(self, other: u32) -> Self {
unsafe { Self::new_unchecked(self.get().saturating_pow(other)) }
}
#[doc = " `+` "]
#[doc =
"assert_eq!(NonZero::<u64>::from_ascii(b\"+10\"), Ok(NonZero::new(10)?));"]
#[doc = "assert!(NonZero::<u64>::from_ascii(b\"1 \").is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[inline]
pub const fn from_ascii(src: &[u8]) -> Result<Self, ParseIntError> {
Self::from_ascii_radix(src, 10)
}
#[doc = " `+` "]
#[doc =
"assert_eq!(NonZero::<u64>::from_ascii_radix(b\"A\", 16), Ok(NonZero::new(10)?));"]
#[doc =
"assert!(NonZero::<u64>::from_ascii_radix(b\"1 \", 10).is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[inline]
pub const fn from_ascii_radix(src: &[u8], radix: u32)
-> Result<Self, ParseIntError> {
let n =
match <u64>::from_ascii_radix(src, radix) {
Ok(n) => n,
Err(err) => return Err(err),
};
if let Some(n) = Self::new(n) {
Ok(n)
} else { Err(ParseIntError { kind: IntErrorKind::Zero }) }
}
#[doc = " `+` "]
#[doc =
"assert_eq!(NonZero::<u64>::from_str_radix(\"A\", 16), Ok(NonZero::new(10)?));"]
#[doc = "assert!(NonZero::<u64>::from_str_radix(\"1 \", 10).is_err());"]
#[unstable(feature = "nonzero_from_str_radix", issue = "152193")]
#[inline]
pub const fn from_str_radix(src: &str, radix: u32)
-> Result<Self, ParseIntError> {
Self::from_ascii_radix(src.as_bytes(), radix)
}
}
#[stable(feature = "nonzero_parse", since = "1.35.0")]
impl FromStr for NonZero<u64> {
type Err = ParseIntError;
fn from_str(src: &str) -> Result<Self, Self::Err> {
Self::from_str_radix(src, 10)
}
}
#[stable(feature = "nonzero_div", since = "1.51.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const Div<NonZero<u64>> for u64 {
type Output = u64;
#[doc(alias = "unchecked_div")]
#[inline]
fn div(self, other: NonZero<u64>) -> u64 {
unsafe { intrinsics::unchecked_div(self, other.get()) }
}
}
#[stable(feature = "nonzero_div_assign", since = "1.79.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const DivAssign<NonZero<u64>> for u64 {
#[inline]
fn div_assign(&mut self, other: NonZero<u64>) { *self = *self / other; }
}
#[stable(feature = "nonzero_div", since = "1.51.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const Rem<NonZero<u64>> for u64 {
type Output = u64;
#[inline]
fn rem(self, other: NonZero<u64>) -> u64 {
unsafe { intrinsics::unchecked_rem(self, other.get()) }
}
}
#[stable(feature = "nonzero_div_assign", since = "1.79.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const RemAssign<NonZero<u64>> for u64 {
#[inline]
fn rem_assign(&mut self, other: NonZero<u64>) { *self = *self % other; }
}
impl NonZero<u64> {
#[doc = "let one = NonZero::new(1u64).unwrap();"]
#[doc = "let max = NonZero::new(u64::MAX).unwrap();"]
#[doc = "let two = NonZero::new(2u64).unwrap();"]
#[doc = "let three = NonZero::new(3u64).unwrap();"]
#[stable(feature = "unsigned_nonzero_div_ceil", since = "1.92.0")]
#[rustc_const_stable(feature = "unsigned_nonzero_div_ceil", since =
"1.92.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn div_ceil(self, rhs: Self) -> Self {
let v = self.get().div_ceil(rhs.get());
unsafe { Self::new_unchecked(v) }
}
}nonzero_integer! {
2453 Self = NonZeroU64,
2454 Primitive = unsigned u64,
2455 SignedPrimitive = i64,
2456 rot = 12,
2457 rot_op = "0xaa00000000006e1",
2458 rot_result = "0x6e10aa",
2459 swap_op = "0x1234567890123456",
2460 swapped = "0x5634129078563412",
2461 reversed = "0x6a2c48091e6a2c48",
2462}
2463
2464#[doc = "A [`u128`] that is known not to equal zero."]
#[doc = "For example, `Option<NonZeroU128>` is the same size as `u128`:"]
#[doc =
"assert_eq!(size_of::<Option<core::num::NonZeroU128>>(), size_of::<u128>());"]
#[doc =
"`NonZeroU128` is guaranteed to have the same layout and bit validity as `u128`"]
#[doc = "`Option<NonZeroU128>` is guaranteed to be compatible with `u128`,"]
#[doc = "`NonZeroU128` and `Option<NonZeroU128>`"]
#[doc = "use std::num::NonZeroU128;"]
#[doc =
"assert_eq!(size_of::<NonZeroU128>(), size_of::<Option<NonZeroU128>>());"]
#[doc =
"assert_eq!(align_of::<NonZeroU128>(), align_of::<Option<NonZeroU128>>());"]
#[doc = "`NonZeroU128`"]
#[doc = "use std::num::NonZeroU128;"]
#[doc =
"const TEN: NonZeroU128 = NonZeroU128::new(10).expect(\"ten is non-zero\");"]
#[stable(feature = "nonzero", since = "1.28.0")]
pub type NonZeroU128 = NonZero<u128>;
impl NonZero<u128> {
#[doc = "This value is equal to [`u128::BITS`]."]
#[doc = "assert_eq!(NonZero::<u128>::BITS, u128::BITS);"]
#[stable(feature = "nonzero_bits", since = "1.67.0")]
pub const BITS: u32 = <u128>::BITS;
#[doc = "let n = NonZero::<u128>::new(u128::MAX)?;"]
#[stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
#[rustc_const_stable(feature = "nonzero_leading_trailing_zeros", since =
"1.53.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn leading_zeros(self) -> u32 {
unsafe { intrinsics::ctlz_nonzero(self.get() as u128) }
}
#[doc = "let n = NonZero::<u128>::new(0b0101000)?;"]
#[stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
#[rustc_const_stable(feature = "nonzero_leading_trailing_zeros", since =
"1.53.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn trailing_zeros(self) -> u32 {
unsafe { intrinsics::cttz_nonzero(self.get() as u128) }
}
#[doc = "let a = NonZero::<u128>::new(0b_01100100)?;"]
#[doc = "let b = NonZero::<u128>::new(0b_01000000)?;"]
#[stable(feature = "isolate_most_least_significant_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn isolate_highest_one(self) -> Self {
unsafe {
let bit =
(((1 as u128) <<
(<u128>::BITS - 1)).unchecked_shr(self.leading_zeros()));
NonZero::new_unchecked(bit as u128)
}
}
#[doc = "let a = NonZero::<u128>::new(0b_01100100)?;"]
#[doc = "let b = NonZero::<u128>::new(0b_00000100)?;"]
#[stable(feature = "isolate_most_least_significant_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn isolate_lowest_one(self) -> Self {
let n = self.get();
let n = n & n.wrapping_neg();
unsafe { NonZero::new_unchecked(n) }
}
#[doc = "assert_eq!(NonZero::<u128>::new(0b1)?.highest_one(), 0);"]
#[doc = "assert_eq!(NonZero::<u128>::new(0b1_0000)?.highest_one(), 4);"]
#[doc = "assert_eq!(NonZero::<u128>::new(0b1_1111)?.highest_one(), 4);"]
#[stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn highest_one(self) -> u32 {
Self::BITS - 1 - self.leading_zeros()
}
#[doc = "assert_eq!(NonZero::<u128>::new(0b1)?.lowest_one(), 0);"]
#[doc = "assert_eq!(NonZero::<u128>::new(0b1_0000)?.lowest_one(), 4);"]
#[doc = "assert_eq!(NonZero::<u128>::new(0b1_1111)?.lowest_one(), 0);"]
#[stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn lowest_one(self) -> u32 { self.trailing_zeros() }
#[doc = "let a = NonZero::<u128>::new(0b100_0000)?;"]
#[doc = "let b = NonZero::<u128>::new(0b100_0011)?;"]
#[stable(feature = "non_zero_count_ones", since = "1.86.0")]
#[rustc_const_stable(feature = "non_zero_count_ones", since = "1.86.0")]
#[doc(alias = "popcount")]
#[doc(alias = "popcnt")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn count_ones(self) -> NonZero<u32> {
unsafe { NonZero::new_unchecked(self.get().count_ones()) }
}
#[doc = "let n = NonZero::new(0x13f40000000000000000000000004f76u128)?;"]
#[doc = "let m = NonZero::new(0x4f7613f4)?;"]
#[doc = "assert_eq!(n.rotate_left(16), m);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn rotate_left(self, n: u32) -> Self {
let result = self.get().rotate_left(n);
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x4f7613f4u128)?;"]
#[doc = "let m = NonZero::new(0x13f40000000000000000000000004f76)?;"]
#[doc = "assert_eq!(n.rotate_right(16), m);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn rotate_right(self, n: u32) -> Self {
let result = self.get().rotate_right(n);
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x12345678901234567890123456789012u128)?;"]
#[doc =
"assert_eq!(m, NonZero::new(0x12907856341290785634129078563412)?);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn swap_bytes(self) -> Self {
let result = self.get().swap_bytes();
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x12345678901234567890123456789012u128)?;"]
#[doc =
"assert_eq!(m, NonZero::new(0x48091e6a2c48091e6a2c48091e6a2c48)?);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn reverse_bits(self) -> Self {
let result = self.get().reverse_bits();
unsafe { Self::new_unchecked(result) }
}
#[doc = "use std::num::NonZeroU128;"]
#[doc = "let n = NonZero::new(0x1Au128)?;"]
#[doc = " assert_eq!(NonZeroU128::from_be(n), n)"]
#[doc = " assert_eq!(NonZeroU128::from_be(n), n.swap_bytes())"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use]
#[inline(always)]
pub const fn from_be(x: Self) -> Self {
let result = u128::from_be(x.get());
unsafe { Self::new_unchecked(result) }
}
#[doc = "use std::num::NonZeroU128;"]
#[doc = "let n = NonZero::new(0x1Au128)?;"]
#[doc = " assert_eq!(NonZeroU128::from_le(n), n)"]
#[doc = " assert_eq!(NonZeroU128::from_le(n), n.swap_bytes())"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use]
#[inline(always)]
pub const fn from_le(x: Self) -> Self {
let result = u128::from_le(x.get());
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x1Au128)?;"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn to_be(self) -> Self {
let result = self.get().to_be();
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x1Au128)?;"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn to_le(self) -> Self {
let result = self.get().to_le();
unsafe { Self::new_unchecked(result) }
}
#[doc = "assert_eq!(NonZero::<u128>::MIN.get(), 1u128);"]
#[stable(feature = "nonzero_min_max", since = "1.70.0")]
pub const MIN: Self = Self::new(1).unwrap();
#[doc = "equal to [`u128::MAX`]."]
#[doc = "assert_eq!(NonZero::<u128>::MAX.get(), u128::MAX);"]
#[stable(feature = "nonzero_min_max", since = "1.70.0")]
pub const MAX: Self = Self::new(<u128>::MAX).unwrap();
#[doc = "let one = NonZero::new(1u128)?;"]
#[doc = "let two = NonZero::new(2u128)?;"]
#[doc = "let max = NonZero::new(u128::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_add(self, other: u128) -> Option<Self> {
if let Some(result) = self.get().checked_add(other) {
Some(unsafe { Self::new_unchecked(result) })
} else { None }
}
#[doc = "Return [`NonZero::<u128>::MAX`] on overflow."]
#[doc = "let one = NonZero::new(1u128)?;"]
#[doc = "let two = NonZero::new(2u128)?;"]
#[doc = "let max = NonZero::new(u128::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_add(self, other: u128) -> Self {
unsafe { Self::new_unchecked(self.get().saturating_add(other)) }
}
#[doc = "`self + rhs > u128::MAX`."]
#[doc = "let one = NonZero::new(1u128)?;"]
#[doc = "let two = NonZero::new(2u128)?;"]
#[unstable(feature = "nonzero_ops", issue = "84186")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const unsafe fn unchecked_add(self, other: u128) -> Self {
unsafe { Self::new_unchecked(self.get().unchecked_add(other)) }
}
#[doc = "let two = NonZero::new(2u128)?;"]
#[doc = "let three = NonZero::new(3u128)?;"]
#[doc = "let four = NonZero::new(4u128)?;"]
#[doc = "let max = NonZero::new(u128::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_next_power_of_two(self) -> Option<Self> {
if let Some(nz) = self.get().checked_next_power_of_two() {
Some(unsafe { Self::new_unchecked(nz) })
} else { None }
}
#[doc = "[`u128::ilog2`],"]
#[doc = "assert_eq!(NonZero::new(7u128)?.ilog2(), 2);"]
#[doc = "assert_eq!(NonZero::new(8u128)?.ilog2(), 3);"]
#[doc = "assert_eq!(NonZero::new(9u128)?.ilog2(), 3);"]
#[stable(feature = "int_log", since = "1.67.0")]
#[rustc_const_stable(feature = "int_log", since = "1.67.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn ilog2(self) -> u32 { Self::BITS - 1 - self.leading_zeros() }
#[doc = "[`u128::ilog10`],"]
#[doc = "assert_eq!(NonZero::new(99u128)?.ilog10(), 1);"]
#[doc = "assert_eq!(NonZero::new(100u128)?.ilog10(), 2);"]
#[doc = "assert_eq!(NonZero::new(101u128)?.ilog10(), 2);"]
#[stable(feature = "int_log", since = "1.67.0")]
#[rustc_const_stable(feature = "int_log", since = "1.67.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn ilog10(self) -> u32 { imp::int_log10::u128(self) }
#[doc = "let one = NonZero::new(1u128)?;"]
#[doc = "let two = NonZero::new(2u128)?;"]
#[doc = "let four = NonZero::new(4u128)?;"]
#[stable(feature = "num_midpoint", since = "1.85.0")]
#[rustc_const_stable(feature = "num_midpoint", since = "1.85.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[doc(alias = "average_floor")]
#[doc(alias = "average")]
#[inline]
pub const fn midpoint(self, rhs: Self) -> Self {
unsafe { Self::new_unchecked(self.get().midpoint(rhs.get())) }
}
#[doc = "let eight = NonZero::new(8u128)?;"]
#[doc = "let ten = NonZero::new(10u128)?;"]
#[must_use]
#[stable(feature = "nonzero_is_power_of_two", since = "1.59.0")]
#[rustc_const_stable(feature = "nonzero_is_power_of_two", since =
"1.59.0")]
#[inline]
pub const fn is_power_of_two(self) -> bool {
intrinsics::ctpop(self.get()) < 2
}
#[doc = "let ten = NonZero::new(10u128)?;"]
#[doc = "let three = NonZero::new(3u128)?;"]
#[stable(feature = "isqrt", since = "1.84.0")]
#[rustc_const_stable(feature = "isqrt", since = "1.84.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn isqrt(self) -> Self {
let result = self.get().isqrt();
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::<u128>::MAX;"]
#[doc = "assert_eq!(n.cast_signed(), NonZero::new(-1i128).unwrap());"]
#[stable(feature = "integer_sign_cast", since = "1.87.0")]
#[rustc_const_stable(feature = "integer_sign_cast", since = "1.87.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn cast_signed(self) -> NonZero<i128> {
unsafe { NonZero::new_unchecked(self.get().cast_signed()) }
}
#[doc =
"assert_eq!(NonZero::<u128>::new(0b1)?.bit_width(), NonZero::new(1)?);"]
#[doc =
"assert_eq!(NonZero::<u128>::new(0b111)?.bit_width(), NonZero::new(3)?);"]
#[doc =
"assert_eq!(NonZero::<u128>::new(0b1110)?.bit_width(), NonZero::new(4)?);"]
#[stable(feature = "uint_bit_width", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "uint_bit_width", since =
"CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn bit_width(self) -> NonZero<u32> {
unsafe { NonZero::new_unchecked(Self::BITS - self.leading_zeros()) }
}
#[doc = "let two = NonZero::new(2u128)?;"]
#[doc = "let four = NonZero::new(4u128)?;"]
#[doc = "let max = NonZero::new(u128::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_mul(self, other: Self) -> Option<Self> {
if let Some(result) = self.get().checked_mul(other.get()) {
Some(unsafe { Self::new_unchecked(result) })
} else { None }
}
#[doc = "Return [`NonZero::<u128>::MAX`] on overflow."]
#[doc = "let two = NonZero::new(2u128)?;"]
#[doc = "let four = NonZero::new(4u128)?;"]
#[doc = "let max = NonZero::new(u128::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_mul(self, other: Self) -> Self {
unsafe { Self::new_unchecked(self.get().saturating_mul(other.get())) }
}
#[doc = "`self * rhs > u128::MAX`."]
#[doc = "let two = NonZero::new(2u128)?;"]
#[doc = "let four = NonZero::new(4u128)?;"]
#[unstable(feature = "nonzero_ops", issue = "84186")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const unsafe fn unchecked_mul(self, other: Self) -> Self {
unsafe { Self::new_unchecked(self.get().unchecked_mul(other.get())) }
}
#[doc = "let three = NonZero::new(3u128)?;"]
#[doc = "let twenty_seven = NonZero::new(27u128)?;"]
#[doc = "let half_max = NonZero::new(u128::MAX / 2)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_pow(self, other: u32) -> Option<Self> {
if let Some(result) = self.get().checked_pow(other) {
Some(unsafe { Self::new_unchecked(result) })
} else { None }
}
#[doc = "Return [`NonZero::<u128>::MAX`] on overflow."]
#[doc = "let three = NonZero::new(3u128)?;"]
#[doc = "let twenty_seven = NonZero::new(27u128)?;"]
#[doc = "let max = NonZero::new(u128::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_pow(self, other: u32) -> Self {
unsafe { Self::new_unchecked(self.get().saturating_pow(other)) }
}
#[doc = " `+` "]
#[doc =
"assert_eq!(NonZero::<u128>::from_ascii(b\"+10\"), Ok(NonZero::new(10)?));"]
#[doc = "assert!(NonZero::<u128>::from_ascii(b\"1 \").is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[inline]
pub const fn from_ascii(src: &[u8]) -> Result<Self, ParseIntError> {
Self::from_ascii_radix(src, 10)
}
#[doc = " `+` "]
#[doc =
"assert_eq!(NonZero::<u128>::from_ascii_radix(b\"A\", 16), Ok(NonZero::new(10)?));"]
#[doc =
"assert!(NonZero::<u128>::from_ascii_radix(b\"1 \", 10).is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[inline]
pub const fn from_ascii_radix(src: &[u8], radix: u32)
-> Result<Self, ParseIntError> {
let n =
match <u128>::from_ascii_radix(src, radix) {
Ok(n) => n,
Err(err) => return Err(err),
};
if let Some(n) = Self::new(n) {
Ok(n)
} else { Err(ParseIntError { kind: IntErrorKind::Zero }) }
}
#[doc = " `+` "]
#[doc =
"assert_eq!(NonZero::<u128>::from_str_radix(\"A\", 16), Ok(NonZero::new(10)?));"]
#[doc = "assert!(NonZero::<u128>::from_str_radix(\"1 \", 10).is_err());"]
#[unstable(feature = "nonzero_from_str_radix", issue = "152193")]
#[inline]
pub const fn from_str_radix(src: &str, radix: u32)
-> Result<Self, ParseIntError> {
Self::from_ascii_radix(src.as_bytes(), radix)
}
}
#[stable(feature = "nonzero_parse", since = "1.35.0")]
impl FromStr for NonZero<u128> {
type Err = ParseIntError;
fn from_str(src: &str) -> Result<Self, Self::Err> {
Self::from_str_radix(src, 10)
}
}
#[stable(feature = "nonzero_div", since = "1.51.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const Div<NonZero<u128>> for u128 {
type Output = u128;
#[doc(alias = "unchecked_div")]
#[inline]
fn div(self, other: NonZero<u128>) -> u128 {
unsafe { intrinsics::unchecked_div(self, other.get()) }
}
}
#[stable(feature = "nonzero_div_assign", since = "1.79.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const DivAssign<NonZero<u128>> for u128 {
#[inline]
fn div_assign(&mut self, other: NonZero<u128>) { *self = *self / other; }
}
#[stable(feature = "nonzero_div", since = "1.51.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const Rem<NonZero<u128>> for u128 {
type Output = u128;
#[inline]
fn rem(self, other: NonZero<u128>) -> u128 {
unsafe { intrinsics::unchecked_rem(self, other.get()) }
}
}
#[stable(feature = "nonzero_div_assign", since = "1.79.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const RemAssign<NonZero<u128>> for u128 {
#[inline]
fn rem_assign(&mut self, other: NonZero<u128>) { *self = *self % other; }
}
impl NonZero<u128> {
#[doc = "let one = NonZero::new(1u128).unwrap();"]
#[doc = "let max = NonZero::new(u128::MAX).unwrap();"]
#[doc = "let two = NonZero::new(2u128).unwrap();"]
#[doc = "let three = NonZero::new(3u128).unwrap();"]
#[stable(feature = "unsigned_nonzero_div_ceil", since = "1.92.0")]
#[rustc_const_stable(feature = "unsigned_nonzero_div_ceil", since =
"1.92.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn div_ceil(self, rhs: Self) -> Self {
let v = self.get().div_ceil(rhs.get());
unsafe { Self::new_unchecked(v) }
}
}nonzero_integer! {
2465 Self = NonZeroU128,
2466 Primitive = unsigned u128,
2467 SignedPrimitive = i128,
2468 rot = 16,
2469 rot_op = "0x13f40000000000000000000000004f76",
2470 rot_result = "0x4f7613f4",
2471 swap_op = "0x12345678901234567890123456789012",
2472 swapped = "0x12907856341290785634129078563412",
2473 reversed = "0x48091e6a2c48091e6a2c48091e6a2c48",
2474}
2475
2476#[cfg(target_pointer_width = "16")]
2477nonzero_integer! {
2478 Self = NonZeroUsize,
2479 Primitive = unsigned usize,
2480 SignedPrimitive = isize,
2481 rot = 4,
2482 rot_op = "0xa003",
2483 rot_result = "0x3a",
2484 swap_op = "0x1234",
2485 swapped = "0x3412",
2486 reversed = "0x2c48",
2487}
2488
2489#[cfg(target_pointer_width = "32")]
2490nonzero_integer! {
2491 Self = NonZeroUsize,
2492 Primitive = unsigned usize,
2493 SignedPrimitive = isize,
2494 rot = 8,
2495 rot_op = "0x10000b3",
2496 rot_result = "0xb301",
2497 swap_op = "0x12345678",
2498 swapped = "0x78563412",
2499 reversed = "0x1e6a2c48",
2500}
2501
2502#[cfg(target_pointer_width = "64")]
2503#[doc = "A [`usize`] that is known not to equal zero."]
#[doc = "For example, `Option<NonZeroUsize>` is the same size as `usize`:"]
#[doc =
"assert_eq!(size_of::<Option<core::num::NonZeroUsize>>(), size_of::<usize>());"]
#[doc =
"`NonZeroUsize` is guaranteed to have the same layout and bit validity as `usize`"]
#[doc = "`Option<NonZeroUsize>` is guaranteed to be compatible with `usize`,"]
#[doc = "`NonZeroUsize` and `Option<NonZeroUsize>`"]
#[doc = "use std::num::NonZeroUsize;"]
#[doc =
"assert_eq!(size_of::<NonZeroUsize>(), size_of::<Option<NonZeroUsize>>());"]
#[doc =
"assert_eq!(align_of::<NonZeroUsize>(), align_of::<Option<NonZeroUsize>>());"]
#[doc = "`NonZeroUsize`"]
#[doc = "use std::num::NonZeroUsize;"]
#[doc =
"const TEN: NonZeroUsize = NonZeroUsize::new(10).expect(\"ten is non-zero\");"]
#[stable(feature = "nonzero", since = "1.28.0")]
pub type NonZeroUsize = NonZero<usize>;
impl NonZero<usize> {
#[doc = "This value is equal to [`usize::BITS`]."]
#[doc = "assert_eq!(NonZero::<usize>::BITS, usize::BITS);"]
#[stable(feature = "nonzero_bits", since = "1.67.0")]
pub const BITS: u32 = <usize>::BITS;
#[doc = "let n = NonZero::<usize>::new(usize::MAX)?;"]
#[stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
#[rustc_const_stable(feature = "nonzero_leading_trailing_zeros", since =
"1.53.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn leading_zeros(self) -> u32 {
unsafe { intrinsics::ctlz_nonzero(self.get() as usize) }
}
#[doc = "let n = NonZero::<usize>::new(0b0101000)?;"]
#[stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
#[rustc_const_stable(feature = "nonzero_leading_trailing_zeros", since =
"1.53.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn trailing_zeros(self) -> u32 {
unsafe { intrinsics::cttz_nonzero(self.get() as usize) }
}
#[doc = "let a = NonZero::<usize>::new(0b_01100100)?;"]
#[doc = "let b = NonZero::<usize>::new(0b_01000000)?;"]
#[stable(feature = "isolate_most_least_significant_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn isolate_highest_one(self) -> Self {
unsafe {
let bit =
(((1 as usize) <<
(<usize>::BITS - 1)).unchecked_shr(self.leading_zeros()));
NonZero::new_unchecked(bit as usize)
}
}
#[doc = "let a = NonZero::<usize>::new(0b_01100100)?;"]
#[doc = "let b = NonZero::<usize>::new(0b_00000100)?;"]
#[stable(feature = "isolate_most_least_significant_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn isolate_lowest_one(self) -> Self {
let n = self.get();
let n = n & n.wrapping_neg();
unsafe { NonZero::new_unchecked(n) }
}
#[doc = "assert_eq!(NonZero::<usize>::new(0b1)?.highest_one(), 0);"]
#[doc = "assert_eq!(NonZero::<usize>::new(0b1_0000)?.highest_one(), 4);"]
#[doc = "assert_eq!(NonZero::<usize>::new(0b1_1111)?.highest_one(), 4);"]
#[stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn highest_one(self) -> u32 {
Self::BITS - 1 - self.leading_zeros()
}
#[doc = "assert_eq!(NonZero::<usize>::new(0b1)?.lowest_one(), 0);"]
#[doc = "assert_eq!(NonZero::<usize>::new(0b1_0000)?.lowest_one(), 4);"]
#[doc = "assert_eq!(NonZero::<usize>::new(0b1_1111)?.lowest_one(), 0);"]
#[stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn lowest_one(self) -> u32 { self.trailing_zeros() }
#[doc = "let a = NonZero::<usize>::new(0b100_0000)?;"]
#[doc = "let b = NonZero::<usize>::new(0b100_0011)?;"]
#[stable(feature = "non_zero_count_ones", since = "1.86.0")]
#[rustc_const_stable(feature = "non_zero_count_ones", since = "1.86.0")]
#[doc(alias = "popcount")]
#[doc(alias = "popcnt")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn count_ones(self) -> NonZero<u32> {
unsafe { NonZero::new_unchecked(self.get().count_ones()) }
}
#[doc = "let n = NonZero::new(0xaa00000000006e1usize)?;"]
#[doc = "let m = NonZero::new(0x6e10aa)?;"]
#[doc = "assert_eq!(n.rotate_left(12), m);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn rotate_left(self, n: u32) -> Self {
let result = self.get().rotate_left(n);
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x6e10aausize)?;"]
#[doc = "let m = NonZero::new(0xaa00000000006e1)?;"]
#[doc = "assert_eq!(n.rotate_right(12), m);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn rotate_right(self, n: u32) -> Self {
let result = self.get().rotate_right(n);
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x1234567890123456usize)?;"]
#[doc = "assert_eq!(m, NonZero::new(0x5634129078563412)?);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn swap_bytes(self) -> Self {
let result = self.get().swap_bytes();
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x1234567890123456usize)?;"]
#[doc = "assert_eq!(m, NonZero::new(0x6a2c48091e6a2c48)?);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn reverse_bits(self) -> Self {
let result = self.get().reverse_bits();
unsafe { Self::new_unchecked(result) }
}
#[doc = "use std::num::NonZeroUsize;"]
#[doc = "let n = NonZero::new(0x1Ausize)?;"]
#[doc = " assert_eq!(NonZeroUsize::from_be(n), n)"]
#[doc = " assert_eq!(NonZeroUsize::from_be(n), n.swap_bytes())"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use]
#[inline(always)]
pub const fn from_be(x: Self) -> Self {
let result = usize::from_be(x.get());
unsafe { Self::new_unchecked(result) }
}
#[doc = "use std::num::NonZeroUsize;"]
#[doc = "let n = NonZero::new(0x1Ausize)?;"]
#[doc = " assert_eq!(NonZeroUsize::from_le(n), n)"]
#[doc = " assert_eq!(NonZeroUsize::from_le(n), n.swap_bytes())"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use]
#[inline(always)]
pub const fn from_le(x: Self) -> Self {
let result = usize::from_le(x.get());
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x1Ausize)?;"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn to_be(self) -> Self {
let result = self.get().to_be();
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x1Ausize)?;"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn to_le(self) -> Self {
let result = self.get().to_le();
unsafe { Self::new_unchecked(result) }
}
#[doc = "assert_eq!(NonZero::<usize>::MIN.get(), 1usize);"]
#[stable(feature = "nonzero_min_max", since = "1.70.0")]
pub const MIN: Self = Self::new(1).unwrap();
#[doc = "equal to [`usize::MAX`]."]
#[doc = "assert_eq!(NonZero::<usize>::MAX.get(), usize::MAX);"]
#[stable(feature = "nonzero_min_max", since = "1.70.0")]
pub const MAX: Self = Self::new(<usize>::MAX).unwrap();
#[doc = "let one = NonZero::new(1usize)?;"]
#[doc = "let two = NonZero::new(2usize)?;"]
#[doc = "let max = NonZero::new(usize::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_add(self, other: usize) -> Option<Self> {
if let Some(result) = self.get().checked_add(other) {
Some(unsafe { Self::new_unchecked(result) })
} else { None }
}
#[doc = "Return [`NonZero::<usize>::MAX`] on overflow."]
#[doc = "let one = NonZero::new(1usize)?;"]
#[doc = "let two = NonZero::new(2usize)?;"]
#[doc = "let max = NonZero::new(usize::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_add(self, other: usize) -> Self {
unsafe { Self::new_unchecked(self.get().saturating_add(other)) }
}
#[doc = "`self + rhs > usize::MAX`."]
#[doc = "let one = NonZero::new(1usize)?;"]
#[doc = "let two = NonZero::new(2usize)?;"]
#[unstable(feature = "nonzero_ops", issue = "84186")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const unsafe fn unchecked_add(self, other: usize) -> Self {
unsafe { Self::new_unchecked(self.get().unchecked_add(other)) }
}
#[doc = "let two = NonZero::new(2usize)?;"]
#[doc = "let three = NonZero::new(3usize)?;"]
#[doc = "let four = NonZero::new(4usize)?;"]
#[doc = "let max = NonZero::new(usize::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_next_power_of_two(self) -> Option<Self> {
if let Some(nz) = self.get().checked_next_power_of_two() {
Some(unsafe { Self::new_unchecked(nz) })
} else { None }
}
#[doc = "[`usize::ilog2`],"]
#[doc = "assert_eq!(NonZero::new(7usize)?.ilog2(), 2);"]
#[doc = "assert_eq!(NonZero::new(8usize)?.ilog2(), 3);"]
#[doc = "assert_eq!(NonZero::new(9usize)?.ilog2(), 3);"]
#[stable(feature = "int_log", since = "1.67.0")]
#[rustc_const_stable(feature = "int_log", since = "1.67.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn ilog2(self) -> u32 { Self::BITS - 1 - self.leading_zeros() }
#[doc = "[`usize::ilog10`],"]
#[doc = "assert_eq!(NonZero::new(99usize)?.ilog10(), 1);"]
#[doc = "assert_eq!(NonZero::new(100usize)?.ilog10(), 2);"]
#[doc = "assert_eq!(NonZero::new(101usize)?.ilog10(), 2);"]
#[stable(feature = "int_log", since = "1.67.0")]
#[rustc_const_stable(feature = "int_log", since = "1.67.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn ilog10(self) -> u32 { imp::int_log10::usize(self) }
#[doc = "let one = NonZero::new(1usize)?;"]
#[doc = "let two = NonZero::new(2usize)?;"]
#[doc = "let four = NonZero::new(4usize)?;"]
#[stable(feature = "num_midpoint", since = "1.85.0")]
#[rustc_const_stable(feature = "num_midpoint", since = "1.85.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[doc(alias = "average_floor")]
#[doc(alias = "average")]
#[inline]
pub const fn midpoint(self, rhs: Self) -> Self {
unsafe { Self::new_unchecked(self.get().midpoint(rhs.get())) }
}
#[doc = "let eight = NonZero::new(8usize)?;"]
#[doc = "let ten = NonZero::new(10usize)?;"]
#[must_use]
#[stable(feature = "nonzero_is_power_of_two", since = "1.59.0")]
#[rustc_const_stable(feature = "nonzero_is_power_of_two", since =
"1.59.0")]
#[inline]
pub const fn is_power_of_two(self) -> bool {
intrinsics::ctpop(self.get()) < 2
}
#[doc = "let ten = NonZero::new(10usize)?;"]
#[doc = "let three = NonZero::new(3usize)?;"]
#[stable(feature = "isqrt", since = "1.84.0")]
#[rustc_const_stable(feature = "isqrt", since = "1.84.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn isqrt(self) -> Self {
let result = self.get().isqrt();
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::<usize>::MAX;"]
#[doc = "assert_eq!(n.cast_signed(), NonZero::new(-1isize).unwrap());"]
#[stable(feature = "integer_sign_cast", since = "1.87.0")]
#[rustc_const_stable(feature = "integer_sign_cast", since = "1.87.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn cast_signed(self) -> NonZero<isize> {
unsafe { NonZero::new_unchecked(self.get().cast_signed()) }
}
#[doc =
"assert_eq!(NonZero::<usize>::new(0b1)?.bit_width(), NonZero::new(1)?);"]
#[doc =
"assert_eq!(NonZero::<usize>::new(0b111)?.bit_width(), NonZero::new(3)?);"]
#[doc =
"assert_eq!(NonZero::<usize>::new(0b1110)?.bit_width(), NonZero::new(4)?);"]
#[stable(feature = "uint_bit_width", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "uint_bit_width", since =
"CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn bit_width(self) -> NonZero<u32> {
unsafe { NonZero::new_unchecked(Self::BITS - self.leading_zeros()) }
}
#[doc = "let two = NonZero::new(2usize)?;"]
#[doc = "let four = NonZero::new(4usize)?;"]
#[doc = "let max = NonZero::new(usize::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_mul(self, other: Self) -> Option<Self> {
if let Some(result) = self.get().checked_mul(other.get()) {
Some(unsafe { Self::new_unchecked(result) })
} else { None }
}
#[doc = "Return [`NonZero::<usize>::MAX`] on overflow."]
#[doc = "let two = NonZero::new(2usize)?;"]
#[doc = "let four = NonZero::new(4usize)?;"]
#[doc = "let max = NonZero::new(usize::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_mul(self, other: Self) -> Self {
unsafe { Self::new_unchecked(self.get().saturating_mul(other.get())) }
}
#[doc = "`self * rhs > usize::MAX`."]
#[doc = "let two = NonZero::new(2usize)?;"]
#[doc = "let four = NonZero::new(4usize)?;"]
#[unstable(feature = "nonzero_ops", issue = "84186")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const unsafe fn unchecked_mul(self, other: Self) -> Self {
unsafe { Self::new_unchecked(self.get().unchecked_mul(other.get())) }
}
#[doc = "let three = NonZero::new(3usize)?;"]
#[doc = "let twenty_seven = NonZero::new(27usize)?;"]
#[doc = "let half_max = NonZero::new(usize::MAX / 2)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_pow(self, other: u32) -> Option<Self> {
if let Some(result) = self.get().checked_pow(other) {
Some(unsafe { Self::new_unchecked(result) })
} else { None }
}
#[doc = "Return [`NonZero::<usize>::MAX`] on overflow."]
#[doc = "let three = NonZero::new(3usize)?;"]
#[doc = "let twenty_seven = NonZero::new(27usize)?;"]
#[doc = "let max = NonZero::new(usize::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_pow(self, other: u32) -> Self {
unsafe { Self::new_unchecked(self.get().saturating_pow(other)) }
}
#[doc = " `+` "]
#[doc =
"assert_eq!(NonZero::<usize>::from_ascii(b\"+10\"), Ok(NonZero::new(10)?));"]
#[doc = "assert!(NonZero::<usize>::from_ascii(b\"1 \").is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[inline]
pub const fn from_ascii(src: &[u8]) -> Result<Self, ParseIntError> {
Self::from_ascii_radix(src, 10)
}
#[doc = " `+` "]
#[doc =
"assert_eq!(NonZero::<usize>::from_ascii_radix(b\"A\", 16), Ok(NonZero::new(10)?));"]
#[doc =
"assert!(NonZero::<usize>::from_ascii_radix(b\"1 \", 10).is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[inline]
pub const fn from_ascii_radix(src: &[u8], radix: u32)
-> Result<Self, ParseIntError> {
let n =
match <usize>::from_ascii_radix(src, radix) {
Ok(n) => n,
Err(err) => return Err(err),
};
if let Some(n) = Self::new(n) {
Ok(n)
} else { Err(ParseIntError { kind: IntErrorKind::Zero }) }
}
#[doc = " `+` "]
#[doc =
"assert_eq!(NonZero::<usize>::from_str_radix(\"A\", 16), Ok(NonZero::new(10)?));"]
#[doc = "assert!(NonZero::<usize>::from_str_radix(\"1 \", 10).is_err());"]
#[unstable(feature = "nonzero_from_str_radix", issue = "152193")]
#[inline]
pub const fn from_str_radix(src: &str, radix: u32)
-> Result<Self, ParseIntError> {
Self::from_ascii_radix(src.as_bytes(), radix)
}
}
#[stable(feature = "nonzero_parse", since = "1.35.0")]
impl FromStr for NonZero<usize> {
type Err = ParseIntError;
fn from_str(src: &str) -> Result<Self, Self::Err> {
Self::from_str_radix(src, 10)
}
}
#[stable(feature = "nonzero_div", since = "1.51.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const Div<NonZero<usize>> for usize {
type Output = usize;
#[doc(alias = "unchecked_div")]
#[inline]
fn div(self, other: NonZero<usize>) -> usize {
unsafe { intrinsics::unchecked_div(self, other.get()) }
}
}
#[stable(feature = "nonzero_div_assign", since = "1.79.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const DivAssign<NonZero<usize>> for usize {
#[inline]
fn div_assign(&mut self, other: NonZero<usize>) { *self = *self / other; }
}
#[stable(feature = "nonzero_div", since = "1.51.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const Rem<NonZero<usize>> for usize {
type Output = usize;
#[inline]
fn rem(self, other: NonZero<usize>) -> usize {
unsafe { intrinsics::unchecked_rem(self, other.get()) }
}
}
#[stable(feature = "nonzero_div_assign", since = "1.79.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const RemAssign<NonZero<usize>> for usize {
#[inline]
fn rem_assign(&mut self, other: NonZero<usize>) { *self = *self % other; }
}
impl NonZero<usize> {
#[doc = "let one = NonZero::new(1usize).unwrap();"]
#[doc = "let max = NonZero::new(usize::MAX).unwrap();"]
#[doc = "let two = NonZero::new(2usize).unwrap();"]
#[doc = "let three = NonZero::new(3usize).unwrap();"]
#[stable(feature = "unsigned_nonzero_div_ceil", since = "1.92.0")]
#[rustc_const_stable(feature = "unsigned_nonzero_div_ceil", since =
"1.92.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn div_ceil(self, rhs: Self) -> Self {
let v = self.get().div_ceil(rhs.get());
unsafe { Self::new_unchecked(v) }
}
}nonzero_integer! {
2504 Self = NonZeroUsize,
2505 Primitive = unsigned usize,
2506 SignedPrimitive = isize,
2507 rot = 12,
2508 rot_op = "0xaa00000000006e1",
2509 rot_result = "0x6e10aa",
2510 swap_op = "0x1234567890123456",
2511 swapped = "0x5634129078563412",
2512 reversed = "0x6a2c48091e6a2c48",
2513}
2514
2515#[doc = "An [`i8`] that is known not to equal zero."]
#[doc = "For example, `Option<NonZeroI8>` is the same size as `i8`:"]
#[doc =
"assert_eq!(size_of::<Option<core::num::NonZeroI8>>(), size_of::<i8>());"]
#[doc =
"`NonZeroI8` is guaranteed to have the same layout and bit validity as `i8`"]
#[doc = "`Option<NonZeroI8>` is guaranteed to be compatible with `i8`,"]
#[doc = "`NonZeroI8` and `Option<NonZeroI8>`"]
#[doc = "use std::num::NonZeroI8;"]
#[doc = "assert_eq!(size_of::<NonZeroI8>(), size_of::<Option<NonZeroI8>>());"]
#[doc =
"assert_eq!(align_of::<NonZeroI8>(), align_of::<Option<NonZeroI8>>());"]
#[doc = "`NonZeroI8`"]
#[doc = "use std::num::NonZeroI8;"]
#[doc =
"const TEN: NonZeroI8 = NonZeroI8::new(10).expect(\"ten is non-zero\");"]
#[stable(feature = "signed_nonzero", since = "1.34.0")]
pub type NonZeroI8 = NonZero<i8>;
impl NonZero<i8> {
#[doc = "This value is equal to [`i8::BITS`]."]
#[doc = "assert_eq!(NonZero::<i8>::BITS, i8::BITS);"]
#[stable(feature = "nonzero_bits", since = "1.67.0")]
pub const BITS: u32 = <i8>::BITS;
#[doc = "let n = NonZero::<i8>::new(-1i8)?;"]
#[stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
#[rustc_const_stable(feature = "nonzero_leading_trailing_zeros", since =
"1.53.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn leading_zeros(self) -> u32 {
unsafe { intrinsics::ctlz_nonzero(self.get() as u8) }
}
#[doc = "let n = NonZero::<i8>::new(0b0101000)?;"]
#[stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
#[rustc_const_stable(feature = "nonzero_leading_trailing_zeros", since =
"1.53.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn trailing_zeros(self) -> u32 {
unsafe { intrinsics::cttz_nonzero(self.get() as u8) }
}
#[doc = "let a = NonZero::<i8>::new(0b_01100100)?;"]
#[doc = "let b = NonZero::<i8>::new(0b_01000000)?;"]
#[stable(feature = "isolate_most_least_significant_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn isolate_highest_one(self) -> Self {
unsafe {
let bit =
(((1 as u8) <<
(<u8>::BITS - 1)).unchecked_shr(self.leading_zeros()));
NonZero::new_unchecked(bit as i8)
}
}
#[doc = "let a = NonZero::<i8>::new(0b_01100100)?;"]
#[doc = "let b = NonZero::<i8>::new(0b_00000100)?;"]
#[stable(feature = "isolate_most_least_significant_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn isolate_lowest_one(self) -> Self {
let n = self.get();
let n = n & n.wrapping_neg();
unsafe { NonZero::new_unchecked(n) }
}
#[doc = "assert_eq!(NonZero::<i8>::new(0b1)?.highest_one(), 0);"]
#[doc = "assert_eq!(NonZero::<i8>::new(0b1_0000)?.highest_one(), 4);"]
#[doc = "assert_eq!(NonZero::<i8>::new(0b1_1111)?.highest_one(), 4);"]
#[stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn highest_one(self) -> u32 {
Self::BITS - 1 - self.leading_zeros()
}
#[doc = "assert_eq!(NonZero::<i8>::new(0b1)?.lowest_one(), 0);"]
#[doc = "assert_eq!(NonZero::<i8>::new(0b1_0000)?.lowest_one(), 4);"]
#[doc = "assert_eq!(NonZero::<i8>::new(0b1_1111)?.lowest_one(), 0);"]
#[stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn lowest_one(self) -> u32 { self.trailing_zeros() }
#[doc = "let a = NonZero::<i8>::new(0b100_0000)?;"]
#[doc = "let b = NonZero::<i8>::new(0b100_0011)?;"]
#[stable(feature = "non_zero_count_ones", since = "1.86.0")]
#[rustc_const_stable(feature = "non_zero_count_ones", since = "1.86.0")]
#[doc(alias = "popcount")]
#[doc(alias = "popcnt")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn count_ones(self) -> NonZero<u32> {
unsafe { NonZero::new_unchecked(self.get().count_ones()) }
}
#[doc = "let n = NonZero::new(-0x7ei8)?;"]
#[doc = "let m = NonZero::new(0xa)?;"]
#[doc = "assert_eq!(n.rotate_left(2), m);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn rotate_left(self, n: u32) -> Self {
let result = self.get().rotate_left(n);
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0xai8)?;"]
#[doc = "let m = NonZero::new(-0x7e)?;"]
#[doc = "assert_eq!(n.rotate_right(2), m);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn rotate_right(self, n: u32) -> Self {
let result = self.get().rotate_right(n);
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x12i8)?;"]
#[doc = "assert_eq!(m, NonZero::new(0x12)?);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn swap_bytes(self) -> Self {
let result = self.get().swap_bytes();
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x12i8)?;"]
#[doc = "assert_eq!(m, NonZero::new(0x48)?);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn reverse_bits(self) -> Self {
let result = self.get().reverse_bits();
unsafe { Self::new_unchecked(result) }
}
#[doc = "use std::num::NonZeroI8;"]
#[doc = "let n = NonZero::new(0x1Ai8)?;"]
#[doc = " assert_eq!(NonZeroI8::from_be(n), n)"]
#[doc = " assert_eq!(NonZeroI8::from_be(n), n.swap_bytes())"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use]
#[inline(always)]
pub const fn from_be(x: Self) -> Self {
let result = i8::from_be(x.get());
unsafe { Self::new_unchecked(result) }
}
#[doc = "use std::num::NonZeroI8;"]
#[doc = "let n = NonZero::new(0x1Ai8)?;"]
#[doc = " assert_eq!(NonZeroI8::from_le(n), n)"]
#[doc = " assert_eq!(NonZeroI8::from_le(n), n.swap_bytes())"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use]
#[inline(always)]
pub const fn from_le(x: Self) -> Self {
let result = i8::from_le(x.get());
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x1Ai8)?;"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn to_be(self) -> Self {
let result = self.get().to_be();
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x1Ai8)?;"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn to_le(self) -> Self {
let result = self.get().to_le();
unsafe { Self::new_unchecked(result) }
}
#[doc = "equal to [`i8::MIN`]."]
#[doc = "assert_eq!(NonZero::<i8>::MIN.get(), i8::MIN);"]
#[stable(feature = "nonzero_min_max", since = "1.70.0")]
pub const MIN: Self = Self::new(<i8>::MIN).unwrap();
#[doc = "equal to [`i8::MAX`]."]
#[doc = "assert_eq!(NonZero::<i8>::MAX.get(), i8::MAX);"]
#[stable(feature = "nonzero_min_max", since = "1.70.0")]
pub const MAX: Self = Self::new(<i8>::MAX).unwrap();
#[doc = "See [`i8::abs`]"]
#[doc = "let pos = NonZero::new(1i8)?;"]
#[doc = "let neg = NonZero::new(-1i8)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn abs(self) -> Self {
unsafe { Self::new_unchecked(self.get().abs()) }
}
#[doc = "`self == NonZero::<i8>::MIN`."]
#[doc = "let pos = NonZero::new(1i8)?;"]
#[doc = "let neg = NonZero::new(-1i8)?;"]
#[doc = "let min = NonZero::new(i8::MIN)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_abs(self) -> Option<Self> {
if let Some(nz) = self.get().checked_abs() {
Some(unsafe { Self::new_unchecked(nz) })
} else { None }
}
#[doc = "[`i8::overflowing_abs`]."]
#[doc = "let pos = NonZero::new(1i8)?;"]
#[doc = "let neg = NonZero::new(-1i8)?;"]
#[doc = "let min = NonZero::new(i8::MIN)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn overflowing_abs(self) -> (Self, bool) {
let (nz, flag) = self.get().overflowing_abs();
(unsafe { Self::new_unchecked(nz) }, flag)
}
#[doc = "[`i8::saturating_abs`]."]
#[doc = "let pos = NonZero::new(1i8)?;"]
#[doc = "let neg = NonZero::new(-1i8)?;"]
#[doc = "let min = NonZero::new(i8::MIN)?;"]
#[doc = "let min_plus = NonZero::new(i8::MIN + 1)?;"]
#[doc = "let max = NonZero::new(i8::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_abs(self) -> Self {
unsafe { Self::new_unchecked(self.get().saturating_abs()) }
}
#[doc = "[`i8::wrapping_abs`]."]
#[doc = "let pos = NonZero::new(1i8)?;"]
#[doc = "let neg = NonZero::new(-1i8)?;"]
#[doc = "let min = NonZero::new(i8::MIN)?;"]
#[doc = "# let max = NonZero::new(i8::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn wrapping_abs(self) -> Self {
unsafe { Self::new_unchecked(self.get().wrapping_abs()) }
}
#[doc = "let u_pos = NonZero::new(1u8)?;"]
#[doc = "let i_pos = NonZero::new(1i8)?;"]
#[doc = "let i_neg = NonZero::new(-1i8)?;"]
#[doc = "let i_min = NonZero::new(i8::MIN)?;"]
#[doc = "let u_max = NonZero::new(u8::MAX / 2 + 1)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn unsigned_abs(self) -> NonZero<u8> {
unsafe { NonZero::new_unchecked(self.get().unsigned_abs()) }
}
#[doc = "let pos_five = NonZero::new(5i8)?;"]
#[doc = "let neg_five = NonZero::new(-5i8)?;"]
#[must_use]
#[inline]
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
pub const fn is_positive(self) -> bool { self.get().is_positive() }
#[doc = "let pos_five = NonZero::new(5i8)?;"]
#[doc = "let neg_five = NonZero::new(-5i8)?;"]
#[must_use]
#[inline]
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
pub const fn is_negative(self) -> bool { self.get().is_negative() }
#[doc = "returning `None` if `self == NonZero::<i8>::MIN`."]
#[doc = "let pos_five = NonZero::new(5i8)?;"]
#[doc = "let neg_five = NonZero::new(-5i8)?;"]
#[doc = "let min = NonZero::new(i8::MIN)?;"]
#[inline]
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
pub const fn checked_neg(self) -> Option<Self> {
if let Some(result) = self.get().checked_neg() {
return Some(unsafe { Self::new_unchecked(result) });
}
None
}
#[doc = "See [`i8::overflowing_neg`]"]
#[doc = "let pos_five = NonZero::new(5i8)?;"]
#[doc = "let neg_five = NonZero::new(-5i8)?;"]
#[doc = "let min = NonZero::new(i8::MIN)?;"]
#[inline]
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
pub const fn overflowing_neg(self) -> (Self, bool) {
let (result, overflow) = self.get().overflowing_neg();
((unsafe { Self::new_unchecked(result) }), overflow)
}
#[doc = "returning [`NonZero::<i8>::MAX`]"]
#[doc = "if `self == NonZero::<i8>::MIN`"]
#[doc = "let pos_five = NonZero::new(5i8)?;"]
#[doc = "let neg_five = NonZero::new(-5i8)?;"]
#[doc = "let min = NonZero::new(i8::MIN)?;"]
#[doc = "let min_plus_one = NonZero::new(i8::MIN + 1)?;"]
#[doc = "let max = NonZero::new(i8::MAX)?;"]
#[inline]
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
pub const fn saturating_neg(self) -> Self {
if let Some(result) = self.checked_neg() { return result; }
Self::MAX
}
#[doc = "See [`i8::wrapping_neg`]"]
#[doc = "let pos_five = NonZero::new(5i8)?;"]
#[doc = "let neg_five = NonZero::new(-5i8)?;"]
#[doc = "let min = NonZero::new(i8::MIN)?;"]
#[inline]
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
pub const fn wrapping_neg(self) -> Self {
let result = self.get().wrapping_neg();
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(-1i8).unwrap();"]
#[doc = "assert_eq!(n.cast_unsigned(), NonZero::<u8>::MAX);"]
#[stable(feature = "integer_sign_cast", since = "1.87.0")]
#[rustc_const_stable(feature = "integer_sign_cast", since = "1.87.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn cast_unsigned(self) -> NonZero<u8> {
unsafe { NonZero::new_unchecked(self.get().cast_unsigned()) }
}
#[doc = "let two = NonZero::new(2i8)?;"]
#[doc = "let four = NonZero::new(4i8)?;"]
#[doc = "let max = NonZero::new(i8::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_mul(self, other: Self) -> Option<Self> {
if let Some(result) = self.get().checked_mul(other.get()) {
Some(unsafe { Self::new_unchecked(result) })
} else { None }
}
#[doc = "Return [`NonZero::<i8>::MAX`] on overflow."]
#[doc = "let two = NonZero::new(2i8)?;"]
#[doc = "let four = NonZero::new(4i8)?;"]
#[doc = "let max = NonZero::new(i8::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_mul(self, other: Self) -> Self {
unsafe { Self::new_unchecked(self.get().saturating_mul(other.get())) }
}
#[doc = "`self * rhs > i8::MAX`, or `self * rhs < i8::MIN`."]
#[doc = "let two = NonZero::new(2i8)?;"]
#[doc = "let four = NonZero::new(4i8)?;"]
#[unstable(feature = "nonzero_ops", issue = "84186")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const unsafe fn unchecked_mul(self, other: Self) -> Self {
unsafe { Self::new_unchecked(self.get().unchecked_mul(other.get())) }
}
#[doc = "let three = NonZero::new(3i8)?;"]
#[doc = "let twenty_seven = NonZero::new(27i8)?;"]
#[doc = "let half_max = NonZero::new(i8::MAX / 2)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_pow(self, other: u32) -> Option<Self> {
if let Some(result) = self.get().checked_pow(other) {
Some(unsafe { Self::new_unchecked(result) })
} else { None }
}
#[doc =
"Return [`NonZero::<i8>::MIN`] or [`NonZero::<i8>::MAX`] on overflow."]
#[doc = "let three = NonZero::new(3i8)?;"]
#[doc = "let twenty_seven = NonZero::new(27i8)?;"]
#[doc = "let max = NonZero::new(i8::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_pow(self, other: u32) -> Self {
unsafe { Self::new_unchecked(self.get().saturating_pow(other)) }
}
#[doc = " `+` or `-` "]
#[doc =
"assert_eq!(NonZero::<i8>::from_ascii(b\"+10\"), Ok(NonZero::new(10)?));"]
#[doc = "assert!(NonZero::<i8>::from_ascii(b\"1 \").is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[inline]
pub const fn from_ascii(src: &[u8]) -> Result<Self, ParseIntError> {
Self::from_ascii_radix(src, 10)
}
#[doc = " `+` or `-` "]
#[doc =
"assert_eq!(NonZero::<i8>::from_ascii_radix(b\"A\", 16), Ok(NonZero::new(10)?));"]
#[doc = "assert!(NonZero::<i8>::from_ascii_radix(b\"1 \", 10).is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[inline]
pub const fn from_ascii_radix(src: &[u8], radix: u32)
-> Result<Self, ParseIntError> {
let n =
match <i8>::from_ascii_radix(src, radix) {
Ok(n) => n,
Err(err) => return Err(err),
};
if let Some(n) = Self::new(n) {
Ok(n)
} else { Err(ParseIntError { kind: IntErrorKind::Zero }) }
}
#[doc = " `+` or `-` "]
#[doc =
"assert_eq!(NonZero::<i8>::from_str_radix(\"A\", 16), Ok(NonZero::new(10)?));"]
#[doc = "assert!(NonZero::<i8>::from_str_radix(\"1 \", 10).is_err());"]
#[unstable(feature = "nonzero_from_str_radix", issue = "152193")]
#[inline]
pub const fn from_str_radix(src: &str, radix: u32)
-> Result<Self, ParseIntError> {
Self::from_ascii_radix(src.as_bytes(), radix)
}
}
#[stable(feature = "nonzero_parse", since = "1.35.0")]
impl FromStr for NonZero<i8> {
type Err = ParseIntError;
fn from_str(src: &str) -> Result<Self, Self::Err> {
Self::from_str_radix(src, 10)
}
}
#[stable(feature = "signed_nonzero_neg", since = "1.71.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const Neg for NonZero<i8> {
type Output = Self;
#[inline]
fn neg(self) -> Self { unsafe { Self::new_unchecked(self.get().neg()) } }
}
#[stable(feature = "signed_nonzero_neg", since = "1.71.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const Neg for &NonZero<i8> {
type Output = <NonZero<i8> as Neg>::Output;
#[inline]
fn neg(self) -> <NonZero<i8> as Neg>::Output { Neg::neg(*self) }
}nonzero_integer! {
2516 Self = NonZeroI8,
2517 Primitive = signed i8,
2518 UnsignedPrimitive = u8,
2519 rot = 2,
2520 rot_op = "-0x7e",
2521 rot_result = "0xa",
2522 swap_op = "0x12",
2523 swapped = "0x12",
2524 reversed = "0x48",
2525}
2526
2527#[doc = "An [`i16`] that is known not to equal zero."]
#[doc = "For example, `Option<NonZeroI16>` is the same size as `i16`:"]
#[doc =
"assert_eq!(size_of::<Option<core::num::NonZeroI16>>(), size_of::<i16>());"]
#[doc =
"`NonZeroI16` is guaranteed to have the same layout and bit validity as `i16`"]
#[doc = "`Option<NonZeroI16>` is guaranteed to be compatible with `i16`,"]
#[doc = "`NonZeroI16` and `Option<NonZeroI16>`"]
#[doc = "use std::num::NonZeroI16;"]
#[doc =
"assert_eq!(size_of::<NonZeroI16>(), size_of::<Option<NonZeroI16>>());"]
#[doc =
"assert_eq!(align_of::<NonZeroI16>(), align_of::<Option<NonZeroI16>>());"]
#[doc = "`NonZeroI16`"]
#[doc = "use std::num::NonZeroI16;"]
#[doc =
"const TEN: NonZeroI16 = NonZeroI16::new(10).expect(\"ten is non-zero\");"]
#[stable(feature = "signed_nonzero", since = "1.34.0")]
pub type NonZeroI16 = NonZero<i16>;
impl NonZero<i16> {
#[doc = "This value is equal to [`i16::BITS`]."]
#[doc = "assert_eq!(NonZero::<i16>::BITS, i16::BITS);"]
#[stable(feature = "nonzero_bits", since = "1.67.0")]
pub const BITS: u32 = <i16>::BITS;
#[doc = "let n = NonZero::<i16>::new(-1i16)?;"]
#[stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
#[rustc_const_stable(feature = "nonzero_leading_trailing_zeros", since =
"1.53.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn leading_zeros(self) -> u32 {
unsafe { intrinsics::ctlz_nonzero(self.get() as u16) }
}
#[doc = "let n = NonZero::<i16>::new(0b0101000)?;"]
#[stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
#[rustc_const_stable(feature = "nonzero_leading_trailing_zeros", since =
"1.53.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn trailing_zeros(self) -> u32 {
unsafe { intrinsics::cttz_nonzero(self.get() as u16) }
}
#[doc = "let a = NonZero::<i16>::new(0b_01100100)?;"]
#[doc = "let b = NonZero::<i16>::new(0b_01000000)?;"]
#[stable(feature = "isolate_most_least_significant_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn isolate_highest_one(self) -> Self {
unsafe {
let bit =
(((1 as u16) <<
(<u16>::BITS - 1)).unchecked_shr(self.leading_zeros()));
NonZero::new_unchecked(bit as i16)
}
}
#[doc = "let a = NonZero::<i16>::new(0b_01100100)?;"]
#[doc = "let b = NonZero::<i16>::new(0b_00000100)?;"]
#[stable(feature = "isolate_most_least_significant_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn isolate_lowest_one(self) -> Self {
let n = self.get();
let n = n & n.wrapping_neg();
unsafe { NonZero::new_unchecked(n) }
}
#[doc = "assert_eq!(NonZero::<i16>::new(0b1)?.highest_one(), 0);"]
#[doc = "assert_eq!(NonZero::<i16>::new(0b1_0000)?.highest_one(), 4);"]
#[doc = "assert_eq!(NonZero::<i16>::new(0b1_1111)?.highest_one(), 4);"]
#[stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn highest_one(self) -> u32 {
Self::BITS - 1 - self.leading_zeros()
}
#[doc = "assert_eq!(NonZero::<i16>::new(0b1)?.lowest_one(), 0);"]
#[doc = "assert_eq!(NonZero::<i16>::new(0b1_0000)?.lowest_one(), 4);"]
#[doc = "assert_eq!(NonZero::<i16>::new(0b1_1111)?.lowest_one(), 0);"]
#[stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn lowest_one(self) -> u32 { self.trailing_zeros() }
#[doc = "let a = NonZero::<i16>::new(0b100_0000)?;"]
#[doc = "let b = NonZero::<i16>::new(0b100_0011)?;"]
#[stable(feature = "non_zero_count_ones", since = "1.86.0")]
#[rustc_const_stable(feature = "non_zero_count_ones", since = "1.86.0")]
#[doc(alias = "popcount")]
#[doc(alias = "popcnt")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn count_ones(self) -> NonZero<u32> {
unsafe { NonZero::new_unchecked(self.get().count_ones()) }
}
#[doc = "let n = NonZero::new(-0x5ffdi16)?;"]
#[doc = "let m = NonZero::new(0x3a)?;"]
#[doc = "assert_eq!(n.rotate_left(4), m);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn rotate_left(self, n: u32) -> Self {
let result = self.get().rotate_left(n);
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x3ai16)?;"]
#[doc = "let m = NonZero::new(-0x5ffd)?;"]
#[doc = "assert_eq!(n.rotate_right(4), m);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn rotate_right(self, n: u32) -> Self {
let result = self.get().rotate_right(n);
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x1234i16)?;"]
#[doc = "assert_eq!(m, NonZero::new(0x3412)?);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn swap_bytes(self) -> Self {
let result = self.get().swap_bytes();
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x1234i16)?;"]
#[doc = "assert_eq!(m, NonZero::new(0x2c48)?);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn reverse_bits(self) -> Self {
let result = self.get().reverse_bits();
unsafe { Self::new_unchecked(result) }
}
#[doc = "use std::num::NonZeroI16;"]
#[doc = "let n = NonZero::new(0x1Ai16)?;"]
#[doc = " assert_eq!(NonZeroI16::from_be(n), n)"]
#[doc = " assert_eq!(NonZeroI16::from_be(n), n.swap_bytes())"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use]
#[inline(always)]
pub const fn from_be(x: Self) -> Self {
let result = i16::from_be(x.get());
unsafe { Self::new_unchecked(result) }
}
#[doc = "use std::num::NonZeroI16;"]
#[doc = "let n = NonZero::new(0x1Ai16)?;"]
#[doc = " assert_eq!(NonZeroI16::from_le(n), n)"]
#[doc = " assert_eq!(NonZeroI16::from_le(n), n.swap_bytes())"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use]
#[inline(always)]
pub const fn from_le(x: Self) -> Self {
let result = i16::from_le(x.get());
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x1Ai16)?;"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn to_be(self) -> Self {
let result = self.get().to_be();
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x1Ai16)?;"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn to_le(self) -> Self {
let result = self.get().to_le();
unsafe { Self::new_unchecked(result) }
}
#[doc = "equal to [`i16::MIN`]."]
#[doc = "assert_eq!(NonZero::<i16>::MIN.get(), i16::MIN);"]
#[stable(feature = "nonzero_min_max", since = "1.70.0")]
pub const MIN: Self = Self::new(<i16>::MIN).unwrap();
#[doc = "equal to [`i16::MAX`]."]
#[doc = "assert_eq!(NonZero::<i16>::MAX.get(), i16::MAX);"]
#[stable(feature = "nonzero_min_max", since = "1.70.0")]
pub const MAX: Self = Self::new(<i16>::MAX).unwrap();
#[doc = "See [`i16::abs`]"]
#[doc = "let pos = NonZero::new(1i16)?;"]
#[doc = "let neg = NonZero::new(-1i16)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn abs(self) -> Self {
unsafe { Self::new_unchecked(self.get().abs()) }
}
#[doc = "`self == NonZero::<i16>::MIN`."]
#[doc = "let pos = NonZero::new(1i16)?;"]
#[doc = "let neg = NonZero::new(-1i16)?;"]
#[doc = "let min = NonZero::new(i16::MIN)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_abs(self) -> Option<Self> {
if let Some(nz) = self.get().checked_abs() {
Some(unsafe { Self::new_unchecked(nz) })
} else { None }
}
#[doc = "[`i16::overflowing_abs`]."]
#[doc = "let pos = NonZero::new(1i16)?;"]
#[doc = "let neg = NonZero::new(-1i16)?;"]
#[doc = "let min = NonZero::new(i16::MIN)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn overflowing_abs(self) -> (Self, bool) {
let (nz, flag) = self.get().overflowing_abs();
(unsafe { Self::new_unchecked(nz) }, flag)
}
#[doc = "[`i16::saturating_abs`]."]
#[doc = "let pos = NonZero::new(1i16)?;"]
#[doc = "let neg = NonZero::new(-1i16)?;"]
#[doc = "let min = NonZero::new(i16::MIN)?;"]
#[doc = "let min_plus = NonZero::new(i16::MIN + 1)?;"]
#[doc = "let max = NonZero::new(i16::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_abs(self) -> Self {
unsafe { Self::new_unchecked(self.get().saturating_abs()) }
}
#[doc = "[`i16::wrapping_abs`]."]
#[doc = "let pos = NonZero::new(1i16)?;"]
#[doc = "let neg = NonZero::new(-1i16)?;"]
#[doc = "let min = NonZero::new(i16::MIN)?;"]
#[doc = "# let max = NonZero::new(i16::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn wrapping_abs(self) -> Self {
unsafe { Self::new_unchecked(self.get().wrapping_abs()) }
}
#[doc = "let u_pos = NonZero::new(1u16)?;"]
#[doc = "let i_pos = NonZero::new(1i16)?;"]
#[doc = "let i_neg = NonZero::new(-1i16)?;"]
#[doc = "let i_min = NonZero::new(i16::MIN)?;"]
#[doc = "let u_max = NonZero::new(u16::MAX / 2 + 1)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn unsigned_abs(self) -> NonZero<u16> {
unsafe { NonZero::new_unchecked(self.get().unsigned_abs()) }
}
#[doc = "let pos_five = NonZero::new(5i16)?;"]
#[doc = "let neg_five = NonZero::new(-5i16)?;"]
#[must_use]
#[inline]
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
pub const fn is_positive(self) -> bool { self.get().is_positive() }
#[doc = "let pos_five = NonZero::new(5i16)?;"]
#[doc = "let neg_five = NonZero::new(-5i16)?;"]
#[must_use]
#[inline]
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
pub const fn is_negative(self) -> bool { self.get().is_negative() }
#[doc = "returning `None` if `self == NonZero::<i16>::MIN`."]
#[doc = "let pos_five = NonZero::new(5i16)?;"]
#[doc = "let neg_five = NonZero::new(-5i16)?;"]
#[doc = "let min = NonZero::new(i16::MIN)?;"]
#[inline]
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
pub const fn checked_neg(self) -> Option<Self> {
if let Some(result) = self.get().checked_neg() {
return Some(unsafe { Self::new_unchecked(result) });
}
None
}
#[doc = "See [`i16::overflowing_neg`]"]
#[doc = "let pos_five = NonZero::new(5i16)?;"]
#[doc = "let neg_five = NonZero::new(-5i16)?;"]
#[doc = "let min = NonZero::new(i16::MIN)?;"]
#[inline]
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
pub const fn overflowing_neg(self) -> (Self, bool) {
let (result, overflow) = self.get().overflowing_neg();
((unsafe { Self::new_unchecked(result) }), overflow)
}
#[doc = "returning [`NonZero::<i16>::MAX`]"]
#[doc = "if `self == NonZero::<i16>::MIN`"]
#[doc = "let pos_five = NonZero::new(5i16)?;"]
#[doc = "let neg_five = NonZero::new(-5i16)?;"]
#[doc = "let min = NonZero::new(i16::MIN)?;"]
#[doc = "let min_plus_one = NonZero::new(i16::MIN + 1)?;"]
#[doc = "let max = NonZero::new(i16::MAX)?;"]
#[inline]
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
pub const fn saturating_neg(self) -> Self {
if let Some(result) = self.checked_neg() { return result; }
Self::MAX
}
#[doc = "See [`i16::wrapping_neg`]"]
#[doc = "let pos_five = NonZero::new(5i16)?;"]
#[doc = "let neg_five = NonZero::new(-5i16)?;"]
#[doc = "let min = NonZero::new(i16::MIN)?;"]
#[inline]
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
pub const fn wrapping_neg(self) -> Self {
let result = self.get().wrapping_neg();
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(-1i16).unwrap();"]
#[doc = "assert_eq!(n.cast_unsigned(), NonZero::<u16>::MAX);"]
#[stable(feature = "integer_sign_cast", since = "1.87.0")]
#[rustc_const_stable(feature = "integer_sign_cast", since = "1.87.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn cast_unsigned(self) -> NonZero<u16> {
unsafe { NonZero::new_unchecked(self.get().cast_unsigned()) }
}
#[doc = "let two = NonZero::new(2i16)?;"]
#[doc = "let four = NonZero::new(4i16)?;"]
#[doc = "let max = NonZero::new(i16::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_mul(self, other: Self) -> Option<Self> {
if let Some(result) = self.get().checked_mul(other.get()) {
Some(unsafe { Self::new_unchecked(result) })
} else { None }
}
#[doc = "Return [`NonZero::<i16>::MAX`] on overflow."]
#[doc = "let two = NonZero::new(2i16)?;"]
#[doc = "let four = NonZero::new(4i16)?;"]
#[doc = "let max = NonZero::new(i16::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_mul(self, other: Self) -> Self {
unsafe { Self::new_unchecked(self.get().saturating_mul(other.get())) }
}
#[doc = "`self * rhs > i16::MAX`, or `self * rhs < i16::MIN`."]
#[doc = "let two = NonZero::new(2i16)?;"]
#[doc = "let four = NonZero::new(4i16)?;"]
#[unstable(feature = "nonzero_ops", issue = "84186")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const unsafe fn unchecked_mul(self, other: Self) -> Self {
unsafe { Self::new_unchecked(self.get().unchecked_mul(other.get())) }
}
#[doc = "let three = NonZero::new(3i16)?;"]
#[doc = "let twenty_seven = NonZero::new(27i16)?;"]
#[doc = "let half_max = NonZero::new(i16::MAX / 2)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_pow(self, other: u32) -> Option<Self> {
if let Some(result) = self.get().checked_pow(other) {
Some(unsafe { Self::new_unchecked(result) })
} else { None }
}
#[doc =
"Return [`NonZero::<i16>::MIN`] or [`NonZero::<i16>::MAX`] on overflow."]
#[doc = "let three = NonZero::new(3i16)?;"]
#[doc = "let twenty_seven = NonZero::new(27i16)?;"]
#[doc = "let max = NonZero::new(i16::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_pow(self, other: u32) -> Self {
unsafe { Self::new_unchecked(self.get().saturating_pow(other)) }
}
#[doc = " `+` or `-` "]
#[doc =
"assert_eq!(NonZero::<i16>::from_ascii(b\"+10\"), Ok(NonZero::new(10)?));"]
#[doc = "assert!(NonZero::<i16>::from_ascii(b\"1 \").is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[inline]
pub const fn from_ascii(src: &[u8]) -> Result<Self, ParseIntError> {
Self::from_ascii_radix(src, 10)
}
#[doc = " `+` or `-` "]
#[doc =
"assert_eq!(NonZero::<i16>::from_ascii_radix(b\"A\", 16), Ok(NonZero::new(10)?));"]
#[doc =
"assert!(NonZero::<i16>::from_ascii_radix(b\"1 \", 10).is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[inline]
pub const fn from_ascii_radix(src: &[u8], radix: u32)
-> Result<Self, ParseIntError> {
let n =
match <i16>::from_ascii_radix(src, radix) {
Ok(n) => n,
Err(err) => return Err(err),
};
if let Some(n) = Self::new(n) {
Ok(n)
} else { Err(ParseIntError { kind: IntErrorKind::Zero }) }
}
#[doc = " `+` or `-` "]
#[doc =
"assert_eq!(NonZero::<i16>::from_str_radix(\"A\", 16), Ok(NonZero::new(10)?));"]
#[doc = "assert!(NonZero::<i16>::from_str_radix(\"1 \", 10).is_err());"]
#[unstable(feature = "nonzero_from_str_radix", issue = "152193")]
#[inline]
pub const fn from_str_radix(src: &str, radix: u32)
-> Result<Self, ParseIntError> {
Self::from_ascii_radix(src.as_bytes(), radix)
}
}
#[stable(feature = "nonzero_parse", since = "1.35.0")]
impl FromStr for NonZero<i16> {
type Err = ParseIntError;
fn from_str(src: &str) -> Result<Self, Self::Err> {
Self::from_str_radix(src, 10)
}
}
#[stable(feature = "signed_nonzero_neg", since = "1.71.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const Neg for NonZero<i16> {
type Output = Self;
#[inline]
fn neg(self) -> Self { unsafe { Self::new_unchecked(self.get().neg()) } }
}
#[stable(feature = "signed_nonzero_neg", since = "1.71.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const Neg for &NonZero<i16> {
type Output = <NonZero<i16> as Neg>::Output;
#[inline]
fn neg(self) -> <NonZero<i16> as Neg>::Output { Neg::neg(*self) }
}nonzero_integer! {
2528 Self = NonZeroI16,
2529 Primitive = signed i16,
2530 UnsignedPrimitive = u16,
2531 rot = 4,
2532 rot_op = "-0x5ffd",
2533 rot_result = "0x3a",
2534 swap_op = "0x1234",
2535 swapped = "0x3412",
2536 reversed = "0x2c48",
2537}
2538
2539#[doc = "An [`i32`] that is known not to equal zero."]
#[doc = "For example, `Option<NonZeroI32>` is the same size as `i32`:"]
#[doc =
"assert_eq!(size_of::<Option<core::num::NonZeroI32>>(), size_of::<i32>());"]
#[doc =
"`NonZeroI32` is guaranteed to have the same layout and bit validity as `i32`"]
#[doc = "`Option<NonZeroI32>` is guaranteed to be compatible with `i32`,"]
#[doc = "`NonZeroI32` and `Option<NonZeroI32>`"]
#[doc = "use std::num::NonZeroI32;"]
#[doc =
"assert_eq!(size_of::<NonZeroI32>(), size_of::<Option<NonZeroI32>>());"]
#[doc =
"assert_eq!(align_of::<NonZeroI32>(), align_of::<Option<NonZeroI32>>());"]
#[doc = "`NonZeroI32`"]
#[doc = "use std::num::NonZeroI32;"]
#[doc =
"const TEN: NonZeroI32 = NonZeroI32::new(10).expect(\"ten is non-zero\");"]
#[stable(feature = "signed_nonzero", since = "1.34.0")]
pub type NonZeroI32 = NonZero<i32>;
impl NonZero<i32> {
#[doc = "This value is equal to [`i32::BITS`]."]
#[doc = "assert_eq!(NonZero::<i32>::BITS, i32::BITS);"]
#[stable(feature = "nonzero_bits", since = "1.67.0")]
pub const BITS: u32 = <i32>::BITS;
#[doc = "let n = NonZero::<i32>::new(-1i32)?;"]
#[stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
#[rustc_const_stable(feature = "nonzero_leading_trailing_zeros", since =
"1.53.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn leading_zeros(self) -> u32 {
unsafe { intrinsics::ctlz_nonzero(self.get() as u32) }
}
#[doc = "let n = NonZero::<i32>::new(0b0101000)?;"]
#[stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
#[rustc_const_stable(feature = "nonzero_leading_trailing_zeros", since =
"1.53.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn trailing_zeros(self) -> u32 {
unsafe { intrinsics::cttz_nonzero(self.get() as u32) }
}
#[doc = "let a = NonZero::<i32>::new(0b_01100100)?;"]
#[doc = "let b = NonZero::<i32>::new(0b_01000000)?;"]
#[stable(feature = "isolate_most_least_significant_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn isolate_highest_one(self) -> Self {
unsafe {
let bit =
(((1 as u32) <<
(<u32>::BITS - 1)).unchecked_shr(self.leading_zeros()));
NonZero::new_unchecked(bit as i32)
}
}
#[doc = "let a = NonZero::<i32>::new(0b_01100100)?;"]
#[doc = "let b = NonZero::<i32>::new(0b_00000100)?;"]
#[stable(feature = "isolate_most_least_significant_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn isolate_lowest_one(self) -> Self {
let n = self.get();
let n = n & n.wrapping_neg();
unsafe { NonZero::new_unchecked(n) }
}
#[doc = "assert_eq!(NonZero::<i32>::new(0b1)?.highest_one(), 0);"]
#[doc = "assert_eq!(NonZero::<i32>::new(0b1_0000)?.highest_one(), 4);"]
#[doc = "assert_eq!(NonZero::<i32>::new(0b1_1111)?.highest_one(), 4);"]
#[stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn highest_one(self) -> u32 {
Self::BITS - 1 - self.leading_zeros()
}
#[doc = "assert_eq!(NonZero::<i32>::new(0b1)?.lowest_one(), 0);"]
#[doc = "assert_eq!(NonZero::<i32>::new(0b1_0000)?.lowest_one(), 4);"]
#[doc = "assert_eq!(NonZero::<i32>::new(0b1_1111)?.lowest_one(), 0);"]
#[stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn lowest_one(self) -> u32 { self.trailing_zeros() }
#[doc = "let a = NonZero::<i32>::new(0b100_0000)?;"]
#[doc = "let b = NonZero::<i32>::new(0b100_0011)?;"]
#[stable(feature = "non_zero_count_ones", since = "1.86.0")]
#[rustc_const_stable(feature = "non_zero_count_ones", since = "1.86.0")]
#[doc(alias = "popcount")]
#[doc(alias = "popcnt")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn count_ones(self) -> NonZero<u32> {
unsafe { NonZero::new_unchecked(self.get().count_ones()) }
}
#[doc = "let n = NonZero::new(0x10000b3i32)?;"]
#[doc = "let m = NonZero::new(0xb301)?;"]
#[doc = "assert_eq!(n.rotate_left(8), m);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn rotate_left(self, n: u32) -> Self {
let result = self.get().rotate_left(n);
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0xb301i32)?;"]
#[doc = "let m = NonZero::new(0x10000b3)?;"]
#[doc = "assert_eq!(n.rotate_right(8), m);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn rotate_right(self, n: u32) -> Self {
let result = self.get().rotate_right(n);
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x12345678i32)?;"]
#[doc = "assert_eq!(m, NonZero::new(0x78563412)?);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn swap_bytes(self) -> Self {
let result = self.get().swap_bytes();
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x12345678i32)?;"]
#[doc = "assert_eq!(m, NonZero::new(0x1e6a2c48)?);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn reverse_bits(self) -> Self {
let result = self.get().reverse_bits();
unsafe { Self::new_unchecked(result) }
}
#[doc = "use std::num::NonZeroI32;"]
#[doc = "let n = NonZero::new(0x1Ai32)?;"]
#[doc = " assert_eq!(NonZeroI32::from_be(n), n)"]
#[doc = " assert_eq!(NonZeroI32::from_be(n), n.swap_bytes())"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use]
#[inline(always)]
pub const fn from_be(x: Self) -> Self {
let result = i32::from_be(x.get());
unsafe { Self::new_unchecked(result) }
}
#[doc = "use std::num::NonZeroI32;"]
#[doc = "let n = NonZero::new(0x1Ai32)?;"]
#[doc = " assert_eq!(NonZeroI32::from_le(n), n)"]
#[doc = " assert_eq!(NonZeroI32::from_le(n), n.swap_bytes())"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use]
#[inline(always)]
pub const fn from_le(x: Self) -> Self {
let result = i32::from_le(x.get());
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x1Ai32)?;"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn to_be(self) -> Self {
let result = self.get().to_be();
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x1Ai32)?;"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn to_le(self) -> Self {
let result = self.get().to_le();
unsafe { Self::new_unchecked(result) }
}
#[doc = "equal to [`i32::MIN`]."]
#[doc = "assert_eq!(NonZero::<i32>::MIN.get(), i32::MIN);"]
#[stable(feature = "nonzero_min_max", since = "1.70.0")]
pub const MIN: Self = Self::new(<i32>::MIN).unwrap();
#[doc = "equal to [`i32::MAX`]."]
#[doc = "assert_eq!(NonZero::<i32>::MAX.get(), i32::MAX);"]
#[stable(feature = "nonzero_min_max", since = "1.70.0")]
pub const MAX: Self = Self::new(<i32>::MAX).unwrap();
#[doc = "See [`i32::abs`]"]
#[doc = "let pos = NonZero::new(1i32)?;"]
#[doc = "let neg = NonZero::new(-1i32)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn abs(self) -> Self {
unsafe { Self::new_unchecked(self.get().abs()) }
}
#[doc = "`self == NonZero::<i32>::MIN`."]
#[doc = "let pos = NonZero::new(1i32)?;"]
#[doc = "let neg = NonZero::new(-1i32)?;"]
#[doc = "let min = NonZero::new(i32::MIN)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_abs(self) -> Option<Self> {
if let Some(nz) = self.get().checked_abs() {
Some(unsafe { Self::new_unchecked(nz) })
} else { None }
}
#[doc = "[`i32::overflowing_abs`]."]
#[doc = "let pos = NonZero::new(1i32)?;"]
#[doc = "let neg = NonZero::new(-1i32)?;"]
#[doc = "let min = NonZero::new(i32::MIN)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn overflowing_abs(self) -> (Self, bool) {
let (nz, flag) = self.get().overflowing_abs();
(unsafe { Self::new_unchecked(nz) }, flag)
}
#[doc = "[`i32::saturating_abs`]."]
#[doc = "let pos = NonZero::new(1i32)?;"]
#[doc = "let neg = NonZero::new(-1i32)?;"]
#[doc = "let min = NonZero::new(i32::MIN)?;"]
#[doc = "let min_plus = NonZero::new(i32::MIN + 1)?;"]
#[doc = "let max = NonZero::new(i32::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_abs(self) -> Self {
unsafe { Self::new_unchecked(self.get().saturating_abs()) }
}
#[doc = "[`i32::wrapping_abs`]."]
#[doc = "let pos = NonZero::new(1i32)?;"]
#[doc = "let neg = NonZero::new(-1i32)?;"]
#[doc = "let min = NonZero::new(i32::MIN)?;"]
#[doc = "# let max = NonZero::new(i32::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn wrapping_abs(self) -> Self {
unsafe { Self::new_unchecked(self.get().wrapping_abs()) }
}
#[doc = "let u_pos = NonZero::new(1u32)?;"]
#[doc = "let i_pos = NonZero::new(1i32)?;"]
#[doc = "let i_neg = NonZero::new(-1i32)?;"]
#[doc = "let i_min = NonZero::new(i32::MIN)?;"]
#[doc = "let u_max = NonZero::new(u32::MAX / 2 + 1)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn unsigned_abs(self) -> NonZero<u32> {
unsafe { NonZero::new_unchecked(self.get().unsigned_abs()) }
}
#[doc = "let pos_five = NonZero::new(5i32)?;"]
#[doc = "let neg_five = NonZero::new(-5i32)?;"]
#[must_use]
#[inline]
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
pub const fn is_positive(self) -> bool { self.get().is_positive() }
#[doc = "let pos_five = NonZero::new(5i32)?;"]
#[doc = "let neg_five = NonZero::new(-5i32)?;"]
#[must_use]
#[inline]
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
pub const fn is_negative(self) -> bool { self.get().is_negative() }
#[doc = "returning `None` if `self == NonZero::<i32>::MIN`."]
#[doc = "let pos_five = NonZero::new(5i32)?;"]
#[doc = "let neg_five = NonZero::new(-5i32)?;"]
#[doc = "let min = NonZero::new(i32::MIN)?;"]
#[inline]
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
pub const fn checked_neg(self) -> Option<Self> {
if let Some(result) = self.get().checked_neg() {
return Some(unsafe { Self::new_unchecked(result) });
}
None
}
#[doc = "See [`i32::overflowing_neg`]"]
#[doc = "let pos_five = NonZero::new(5i32)?;"]
#[doc = "let neg_five = NonZero::new(-5i32)?;"]
#[doc = "let min = NonZero::new(i32::MIN)?;"]
#[inline]
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
pub const fn overflowing_neg(self) -> (Self, bool) {
let (result, overflow) = self.get().overflowing_neg();
((unsafe { Self::new_unchecked(result) }), overflow)
}
#[doc = "returning [`NonZero::<i32>::MAX`]"]
#[doc = "if `self == NonZero::<i32>::MIN`"]
#[doc = "let pos_five = NonZero::new(5i32)?;"]
#[doc = "let neg_five = NonZero::new(-5i32)?;"]
#[doc = "let min = NonZero::new(i32::MIN)?;"]
#[doc = "let min_plus_one = NonZero::new(i32::MIN + 1)?;"]
#[doc = "let max = NonZero::new(i32::MAX)?;"]
#[inline]
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
pub const fn saturating_neg(self) -> Self {
if let Some(result) = self.checked_neg() { return result; }
Self::MAX
}
#[doc = "See [`i32::wrapping_neg`]"]
#[doc = "let pos_five = NonZero::new(5i32)?;"]
#[doc = "let neg_five = NonZero::new(-5i32)?;"]
#[doc = "let min = NonZero::new(i32::MIN)?;"]
#[inline]
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
pub const fn wrapping_neg(self) -> Self {
let result = self.get().wrapping_neg();
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(-1i32).unwrap();"]
#[doc = "assert_eq!(n.cast_unsigned(), NonZero::<u32>::MAX);"]
#[stable(feature = "integer_sign_cast", since = "1.87.0")]
#[rustc_const_stable(feature = "integer_sign_cast", since = "1.87.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn cast_unsigned(self) -> NonZero<u32> {
unsafe { NonZero::new_unchecked(self.get().cast_unsigned()) }
}
#[doc = "let two = NonZero::new(2i32)?;"]
#[doc = "let four = NonZero::new(4i32)?;"]
#[doc = "let max = NonZero::new(i32::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_mul(self, other: Self) -> Option<Self> {
if let Some(result) = self.get().checked_mul(other.get()) {
Some(unsafe { Self::new_unchecked(result) })
} else { None }
}
#[doc = "Return [`NonZero::<i32>::MAX`] on overflow."]
#[doc = "let two = NonZero::new(2i32)?;"]
#[doc = "let four = NonZero::new(4i32)?;"]
#[doc = "let max = NonZero::new(i32::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_mul(self, other: Self) -> Self {
unsafe { Self::new_unchecked(self.get().saturating_mul(other.get())) }
}
#[doc = "`self * rhs > i32::MAX`, or `self * rhs < i32::MIN`."]
#[doc = "let two = NonZero::new(2i32)?;"]
#[doc = "let four = NonZero::new(4i32)?;"]
#[unstable(feature = "nonzero_ops", issue = "84186")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const unsafe fn unchecked_mul(self, other: Self) -> Self {
unsafe { Self::new_unchecked(self.get().unchecked_mul(other.get())) }
}
#[doc = "let three = NonZero::new(3i32)?;"]
#[doc = "let twenty_seven = NonZero::new(27i32)?;"]
#[doc = "let half_max = NonZero::new(i32::MAX / 2)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_pow(self, other: u32) -> Option<Self> {
if let Some(result) = self.get().checked_pow(other) {
Some(unsafe { Self::new_unchecked(result) })
} else { None }
}
#[doc =
"Return [`NonZero::<i32>::MIN`] or [`NonZero::<i32>::MAX`] on overflow."]
#[doc = "let three = NonZero::new(3i32)?;"]
#[doc = "let twenty_seven = NonZero::new(27i32)?;"]
#[doc = "let max = NonZero::new(i32::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_pow(self, other: u32) -> Self {
unsafe { Self::new_unchecked(self.get().saturating_pow(other)) }
}
#[doc = " `+` or `-` "]
#[doc =
"assert_eq!(NonZero::<i32>::from_ascii(b\"+10\"), Ok(NonZero::new(10)?));"]
#[doc = "assert!(NonZero::<i32>::from_ascii(b\"1 \").is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[inline]
pub const fn from_ascii(src: &[u8]) -> Result<Self, ParseIntError> {
Self::from_ascii_radix(src, 10)
}
#[doc = " `+` or `-` "]
#[doc =
"assert_eq!(NonZero::<i32>::from_ascii_radix(b\"A\", 16), Ok(NonZero::new(10)?));"]
#[doc =
"assert!(NonZero::<i32>::from_ascii_radix(b\"1 \", 10).is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[inline]
pub const fn from_ascii_radix(src: &[u8], radix: u32)
-> Result<Self, ParseIntError> {
let n =
match <i32>::from_ascii_radix(src, radix) {
Ok(n) => n,
Err(err) => return Err(err),
};
if let Some(n) = Self::new(n) {
Ok(n)
} else { Err(ParseIntError { kind: IntErrorKind::Zero }) }
}
#[doc = " `+` or `-` "]
#[doc =
"assert_eq!(NonZero::<i32>::from_str_radix(\"A\", 16), Ok(NonZero::new(10)?));"]
#[doc = "assert!(NonZero::<i32>::from_str_radix(\"1 \", 10).is_err());"]
#[unstable(feature = "nonzero_from_str_radix", issue = "152193")]
#[inline]
pub const fn from_str_radix(src: &str, radix: u32)
-> Result<Self, ParseIntError> {
Self::from_ascii_radix(src.as_bytes(), radix)
}
}
#[stable(feature = "nonzero_parse", since = "1.35.0")]
impl FromStr for NonZero<i32> {
type Err = ParseIntError;
fn from_str(src: &str) -> Result<Self, Self::Err> {
Self::from_str_radix(src, 10)
}
}
#[stable(feature = "signed_nonzero_neg", since = "1.71.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const Neg for NonZero<i32> {
type Output = Self;
#[inline]
fn neg(self) -> Self { unsafe { Self::new_unchecked(self.get().neg()) } }
}
#[stable(feature = "signed_nonzero_neg", since = "1.71.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const Neg for &NonZero<i32> {
type Output = <NonZero<i32> as Neg>::Output;
#[inline]
fn neg(self) -> <NonZero<i32> as Neg>::Output { Neg::neg(*self) }
}nonzero_integer! {
2540 Self = NonZeroI32,
2541 Primitive = signed i32,
2542 UnsignedPrimitive = u32,
2543 rot = 8,
2544 rot_op = "0x10000b3",
2545 rot_result = "0xb301",
2546 swap_op = "0x12345678",
2547 swapped = "0x78563412",
2548 reversed = "0x1e6a2c48",
2549}
2550
2551#[doc = "An [`i64`] that is known not to equal zero."]
#[doc = "For example, `Option<NonZeroI64>` is the same size as `i64`:"]
#[doc =
"assert_eq!(size_of::<Option<core::num::NonZeroI64>>(), size_of::<i64>());"]
#[doc =
"`NonZeroI64` is guaranteed to have the same layout and bit validity as `i64`"]
#[doc = "`Option<NonZeroI64>` is guaranteed to be compatible with `i64`,"]
#[doc = "`NonZeroI64` and `Option<NonZeroI64>`"]
#[doc = "use std::num::NonZeroI64;"]
#[doc =
"assert_eq!(size_of::<NonZeroI64>(), size_of::<Option<NonZeroI64>>());"]
#[doc =
"assert_eq!(align_of::<NonZeroI64>(), align_of::<Option<NonZeroI64>>());"]
#[doc = "`NonZeroI64`"]
#[doc = "use std::num::NonZeroI64;"]
#[doc =
"const TEN: NonZeroI64 = NonZeroI64::new(10).expect(\"ten is non-zero\");"]
#[stable(feature = "signed_nonzero", since = "1.34.0")]
pub type NonZeroI64 = NonZero<i64>;
impl NonZero<i64> {
#[doc = "This value is equal to [`i64::BITS`]."]
#[doc = "assert_eq!(NonZero::<i64>::BITS, i64::BITS);"]
#[stable(feature = "nonzero_bits", since = "1.67.0")]
pub const BITS: u32 = <i64>::BITS;
#[doc = "let n = NonZero::<i64>::new(-1i64)?;"]
#[stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
#[rustc_const_stable(feature = "nonzero_leading_trailing_zeros", since =
"1.53.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn leading_zeros(self) -> u32 {
unsafe { intrinsics::ctlz_nonzero(self.get() as u64) }
}
#[doc = "let n = NonZero::<i64>::new(0b0101000)?;"]
#[stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
#[rustc_const_stable(feature = "nonzero_leading_trailing_zeros", since =
"1.53.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn trailing_zeros(self) -> u32 {
unsafe { intrinsics::cttz_nonzero(self.get() as u64) }
}
#[doc = "let a = NonZero::<i64>::new(0b_01100100)?;"]
#[doc = "let b = NonZero::<i64>::new(0b_01000000)?;"]
#[stable(feature = "isolate_most_least_significant_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn isolate_highest_one(self) -> Self {
unsafe {
let bit =
(((1 as u64) <<
(<u64>::BITS - 1)).unchecked_shr(self.leading_zeros()));
NonZero::new_unchecked(bit as i64)
}
}
#[doc = "let a = NonZero::<i64>::new(0b_01100100)?;"]
#[doc = "let b = NonZero::<i64>::new(0b_00000100)?;"]
#[stable(feature = "isolate_most_least_significant_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn isolate_lowest_one(self) -> Self {
let n = self.get();
let n = n & n.wrapping_neg();
unsafe { NonZero::new_unchecked(n) }
}
#[doc = "assert_eq!(NonZero::<i64>::new(0b1)?.highest_one(), 0);"]
#[doc = "assert_eq!(NonZero::<i64>::new(0b1_0000)?.highest_one(), 4);"]
#[doc = "assert_eq!(NonZero::<i64>::new(0b1_1111)?.highest_one(), 4);"]
#[stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn highest_one(self) -> u32 {
Self::BITS - 1 - self.leading_zeros()
}
#[doc = "assert_eq!(NonZero::<i64>::new(0b1)?.lowest_one(), 0);"]
#[doc = "assert_eq!(NonZero::<i64>::new(0b1_0000)?.lowest_one(), 4);"]
#[doc = "assert_eq!(NonZero::<i64>::new(0b1_1111)?.lowest_one(), 0);"]
#[stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn lowest_one(self) -> u32 { self.trailing_zeros() }
#[doc = "let a = NonZero::<i64>::new(0b100_0000)?;"]
#[doc = "let b = NonZero::<i64>::new(0b100_0011)?;"]
#[stable(feature = "non_zero_count_ones", since = "1.86.0")]
#[rustc_const_stable(feature = "non_zero_count_ones", since = "1.86.0")]
#[doc(alias = "popcount")]
#[doc(alias = "popcnt")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn count_ones(self) -> NonZero<u32> {
unsafe { NonZero::new_unchecked(self.get().count_ones()) }
}
#[doc = "let n = NonZero::new(0xaa00000000006e1i64)?;"]
#[doc = "let m = NonZero::new(0x6e10aa)?;"]
#[doc = "assert_eq!(n.rotate_left(12), m);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn rotate_left(self, n: u32) -> Self {
let result = self.get().rotate_left(n);
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x6e10aai64)?;"]
#[doc = "let m = NonZero::new(0xaa00000000006e1)?;"]
#[doc = "assert_eq!(n.rotate_right(12), m);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn rotate_right(self, n: u32) -> Self {
let result = self.get().rotate_right(n);
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x1234567890123456i64)?;"]
#[doc = "assert_eq!(m, NonZero::new(0x5634129078563412)?);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn swap_bytes(self) -> Self {
let result = self.get().swap_bytes();
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x1234567890123456i64)?;"]
#[doc = "assert_eq!(m, NonZero::new(0x6a2c48091e6a2c48)?);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn reverse_bits(self) -> Self {
let result = self.get().reverse_bits();
unsafe { Self::new_unchecked(result) }
}
#[doc = "use std::num::NonZeroI64;"]
#[doc = "let n = NonZero::new(0x1Ai64)?;"]
#[doc = " assert_eq!(NonZeroI64::from_be(n), n)"]
#[doc = " assert_eq!(NonZeroI64::from_be(n), n.swap_bytes())"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use]
#[inline(always)]
pub const fn from_be(x: Self) -> Self {
let result = i64::from_be(x.get());
unsafe { Self::new_unchecked(result) }
}
#[doc = "use std::num::NonZeroI64;"]
#[doc = "let n = NonZero::new(0x1Ai64)?;"]
#[doc = " assert_eq!(NonZeroI64::from_le(n), n)"]
#[doc = " assert_eq!(NonZeroI64::from_le(n), n.swap_bytes())"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use]
#[inline(always)]
pub const fn from_le(x: Self) -> Self {
let result = i64::from_le(x.get());
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x1Ai64)?;"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn to_be(self) -> Self {
let result = self.get().to_be();
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x1Ai64)?;"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn to_le(self) -> Self {
let result = self.get().to_le();
unsafe { Self::new_unchecked(result) }
}
#[doc = "equal to [`i64::MIN`]."]
#[doc = "assert_eq!(NonZero::<i64>::MIN.get(), i64::MIN);"]
#[stable(feature = "nonzero_min_max", since = "1.70.0")]
pub const MIN: Self = Self::new(<i64>::MIN).unwrap();
#[doc = "equal to [`i64::MAX`]."]
#[doc = "assert_eq!(NonZero::<i64>::MAX.get(), i64::MAX);"]
#[stable(feature = "nonzero_min_max", since = "1.70.0")]
pub const MAX: Self = Self::new(<i64>::MAX).unwrap();
#[doc = "See [`i64::abs`]"]
#[doc = "let pos = NonZero::new(1i64)?;"]
#[doc = "let neg = NonZero::new(-1i64)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn abs(self) -> Self {
unsafe { Self::new_unchecked(self.get().abs()) }
}
#[doc = "`self == NonZero::<i64>::MIN`."]
#[doc = "let pos = NonZero::new(1i64)?;"]
#[doc = "let neg = NonZero::new(-1i64)?;"]
#[doc = "let min = NonZero::new(i64::MIN)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_abs(self) -> Option<Self> {
if let Some(nz) = self.get().checked_abs() {
Some(unsafe { Self::new_unchecked(nz) })
} else { None }
}
#[doc = "[`i64::overflowing_abs`]."]
#[doc = "let pos = NonZero::new(1i64)?;"]
#[doc = "let neg = NonZero::new(-1i64)?;"]
#[doc = "let min = NonZero::new(i64::MIN)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn overflowing_abs(self) -> (Self, bool) {
let (nz, flag) = self.get().overflowing_abs();
(unsafe { Self::new_unchecked(nz) }, flag)
}
#[doc = "[`i64::saturating_abs`]."]
#[doc = "let pos = NonZero::new(1i64)?;"]
#[doc = "let neg = NonZero::new(-1i64)?;"]
#[doc = "let min = NonZero::new(i64::MIN)?;"]
#[doc = "let min_plus = NonZero::new(i64::MIN + 1)?;"]
#[doc = "let max = NonZero::new(i64::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_abs(self) -> Self {
unsafe { Self::new_unchecked(self.get().saturating_abs()) }
}
#[doc = "[`i64::wrapping_abs`]."]
#[doc = "let pos = NonZero::new(1i64)?;"]
#[doc = "let neg = NonZero::new(-1i64)?;"]
#[doc = "let min = NonZero::new(i64::MIN)?;"]
#[doc = "# let max = NonZero::new(i64::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn wrapping_abs(self) -> Self {
unsafe { Self::new_unchecked(self.get().wrapping_abs()) }
}
#[doc = "let u_pos = NonZero::new(1u64)?;"]
#[doc = "let i_pos = NonZero::new(1i64)?;"]
#[doc = "let i_neg = NonZero::new(-1i64)?;"]
#[doc = "let i_min = NonZero::new(i64::MIN)?;"]
#[doc = "let u_max = NonZero::new(u64::MAX / 2 + 1)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn unsigned_abs(self) -> NonZero<u64> {
unsafe { NonZero::new_unchecked(self.get().unsigned_abs()) }
}
#[doc = "let pos_five = NonZero::new(5i64)?;"]
#[doc = "let neg_five = NonZero::new(-5i64)?;"]
#[must_use]
#[inline]
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
pub const fn is_positive(self) -> bool { self.get().is_positive() }
#[doc = "let pos_five = NonZero::new(5i64)?;"]
#[doc = "let neg_five = NonZero::new(-5i64)?;"]
#[must_use]
#[inline]
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
pub const fn is_negative(self) -> bool { self.get().is_negative() }
#[doc = "returning `None` if `self == NonZero::<i64>::MIN`."]
#[doc = "let pos_five = NonZero::new(5i64)?;"]
#[doc = "let neg_five = NonZero::new(-5i64)?;"]
#[doc = "let min = NonZero::new(i64::MIN)?;"]
#[inline]
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
pub const fn checked_neg(self) -> Option<Self> {
if let Some(result) = self.get().checked_neg() {
return Some(unsafe { Self::new_unchecked(result) });
}
None
}
#[doc = "See [`i64::overflowing_neg`]"]
#[doc = "let pos_five = NonZero::new(5i64)?;"]
#[doc = "let neg_five = NonZero::new(-5i64)?;"]
#[doc = "let min = NonZero::new(i64::MIN)?;"]
#[inline]
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
pub const fn overflowing_neg(self) -> (Self, bool) {
let (result, overflow) = self.get().overflowing_neg();
((unsafe { Self::new_unchecked(result) }), overflow)
}
#[doc = "returning [`NonZero::<i64>::MAX`]"]
#[doc = "if `self == NonZero::<i64>::MIN`"]
#[doc = "let pos_five = NonZero::new(5i64)?;"]
#[doc = "let neg_five = NonZero::new(-5i64)?;"]
#[doc = "let min = NonZero::new(i64::MIN)?;"]
#[doc = "let min_plus_one = NonZero::new(i64::MIN + 1)?;"]
#[doc = "let max = NonZero::new(i64::MAX)?;"]
#[inline]
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
pub const fn saturating_neg(self) -> Self {
if let Some(result) = self.checked_neg() { return result; }
Self::MAX
}
#[doc = "See [`i64::wrapping_neg`]"]
#[doc = "let pos_five = NonZero::new(5i64)?;"]
#[doc = "let neg_five = NonZero::new(-5i64)?;"]
#[doc = "let min = NonZero::new(i64::MIN)?;"]
#[inline]
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
pub const fn wrapping_neg(self) -> Self {
let result = self.get().wrapping_neg();
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(-1i64).unwrap();"]
#[doc = "assert_eq!(n.cast_unsigned(), NonZero::<u64>::MAX);"]
#[stable(feature = "integer_sign_cast", since = "1.87.0")]
#[rustc_const_stable(feature = "integer_sign_cast", since = "1.87.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn cast_unsigned(self) -> NonZero<u64> {
unsafe { NonZero::new_unchecked(self.get().cast_unsigned()) }
}
#[doc = "let two = NonZero::new(2i64)?;"]
#[doc = "let four = NonZero::new(4i64)?;"]
#[doc = "let max = NonZero::new(i64::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_mul(self, other: Self) -> Option<Self> {
if let Some(result) = self.get().checked_mul(other.get()) {
Some(unsafe { Self::new_unchecked(result) })
} else { None }
}
#[doc = "Return [`NonZero::<i64>::MAX`] on overflow."]
#[doc = "let two = NonZero::new(2i64)?;"]
#[doc = "let four = NonZero::new(4i64)?;"]
#[doc = "let max = NonZero::new(i64::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_mul(self, other: Self) -> Self {
unsafe { Self::new_unchecked(self.get().saturating_mul(other.get())) }
}
#[doc = "`self * rhs > i64::MAX`, or `self * rhs < i64::MIN`."]
#[doc = "let two = NonZero::new(2i64)?;"]
#[doc = "let four = NonZero::new(4i64)?;"]
#[unstable(feature = "nonzero_ops", issue = "84186")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const unsafe fn unchecked_mul(self, other: Self) -> Self {
unsafe { Self::new_unchecked(self.get().unchecked_mul(other.get())) }
}
#[doc = "let three = NonZero::new(3i64)?;"]
#[doc = "let twenty_seven = NonZero::new(27i64)?;"]
#[doc = "let half_max = NonZero::new(i64::MAX / 2)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_pow(self, other: u32) -> Option<Self> {
if let Some(result) = self.get().checked_pow(other) {
Some(unsafe { Self::new_unchecked(result) })
} else { None }
}
#[doc =
"Return [`NonZero::<i64>::MIN`] or [`NonZero::<i64>::MAX`] on overflow."]
#[doc = "let three = NonZero::new(3i64)?;"]
#[doc = "let twenty_seven = NonZero::new(27i64)?;"]
#[doc = "let max = NonZero::new(i64::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_pow(self, other: u32) -> Self {
unsafe { Self::new_unchecked(self.get().saturating_pow(other)) }
}
#[doc = " `+` or `-` "]
#[doc =
"assert_eq!(NonZero::<i64>::from_ascii(b\"+10\"), Ok(NonZero::new(10)?));"]
#[doc = "assert!(NonZero::<i64>::from_ascii(b\"1 \").is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[inline]
pub const fn from_ascii(src: &[u8]) -> Result<Self, ParseIntError> {
Self::from_ascii_radix(src, 10)
}
#[doc = " `+` or `-` "]
#[doc =
"assert_eq!(NonZero::<i64>::from_ascii_radix(b\"A\", 16), Ok(NonZero::new(10)?));"]
#[doc =
"assert!(NonZero::<i64>::from_ascii_radix(b\"1 \", 10).is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[inline]
pub const fn from_ascii_radix(src: &[u8], radix: u32)
-> Result<Self, ParseIntError> {
let n =
match <i64>::from_ascii_radix(src, radix) {
Ok(n) => n,
Err(err) => return Err(err),
};
if let Some(n) = Self::new(n) {
Ok(n)
} else { Err(ParseIntError { kind: IntErrorKind::Zero }) }
}
#[doc = " `+` or `-` "]
#[doc =
"assert_eq!(NonZero::<i64>::from_str_radix(\"A\", 16), Ok(NonZero::new(10)?));"]
#[doc = "assert!(NonZero::<i64>::from_str_radix(\"1 \", 10).is_err());"]
#[unstable(feature = "nonzero_from_str_radix", issue = "152193")]
#[inline]
pub const fn from_str_radix(src: &str, radix: u32)
-> Result<Self, ParseIntError> {
Self::from_ascii_radix(src.as_bytes(), radix)
}
}
#[stable(feature = "nonzero_parse", since = "1.35.0")]
impl FromStr for NonZero<i64> {
type Err = ParseIntError;
fn from_str(src: &str) -> Result<Self, Self::Err> {
Self::from_str_radix(src, 10)
}
}
#[stable(feature = "signed_nonzero_neg", since = "1.71.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const Neg for NonZero<i64> {
type Output = Self;
#[inline]
fn neg(self) -> Self { unsafe { Self::new_unchecked(self.get().neg()) } }
}
#[stable(feature = "signed_nonzero_neg", since = "1.71.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const Neg for &NonZero<i64> {
type Output = <NonZero<i64> as Neg>::Output;
#[inline]
fn neg(self) -> <NonZero<i64> as Neg>::Output { Neg::neg(*self) }
}nonzero_integer! {
2552 Self = NonZeroI64,
2553 Primitive = signed i64,
2554 UnsignedPrimitive = u64,
2555 rot = 12,
2556 rot_op = "0xaa00000000006e1",
2557 rot_result = "0x6e10aa",
2558 swap_op = "0x1234567890123456",
2559 swapped = "0x5634129078563412",
2560 reversed = "0x6a2c48091e6a2c48",
2561}
2562
2563#[doc = "An [`i128`] that is known not to equal zero."]
#[doc = "For example, `Option<NonZeroI128>` is the same size as `i128`:"]
#[doc =
"assert_eq!(size_of::<Option<core::num::NonZeroI128>>(), size_of::<i128>());"]
#[doc =
"`NonZeroI128` is guaranteed to have the same layout and bit validity as `i128`"]
#[doc = "`Option<NonZeroI128>` is guaranteed to be compatible with `i128`,"]
#[doc = "`NonZeroI128` and `Option<NonZeroI128>`"]
#[doc = "use std::num::NonZeroI128;"]
#[doc =
"assert_eq!(size_of::<NonZeroI128>(), size_of::<Option<NonZeroI128>>());"]
#[doc =
"assert_eq!(align_of::<NonZeroI128>(), align_of::<Option<NonZeroI128>>());"]
#[doc = "`NonZeroI128`"]
#[doc = "use std::num::NonZeroI128;"]
#[doc =
"const TEN: NonZeroI128 = NonZeroI128::new(10).expect(\"ten is non-zero\");"]
#[stable(feature = "signed_nonzero", since = "1.34.0")]
pub type NonZeroI128 = NonZero<i128>;
impl NonZero<i128> {
#[doc = "This value is equal to [`i128::BITS`]."]
#[doc = "assert_eq!(NonZero::<i128>::BITS, i128::BITS);"]
#[stable(feature = "nonzero_bits", since = "1.67.0")]
pub const BITS: u32 = <i128>::BITS;
#[doc = "let n = NonZero::<i128>::new(-1i128)?;"]
#[stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
#[rustc_const_stable(feature = "nonzero_leading_trailing_zeros", since =
"1.53.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn leading_zeros(self) -> u32 {
unsafe { intrinsics::ctlz_nonzero(self.get() as u128) }
}
#[doc = "let n = NonZero::<i128>::new(0b0101000)?;"]
#[stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
#[rustc_const_stable(feature = "nonzero_leading_trailing_zeros", since =
"1.53.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn trailing_zeros(self) -> u32 {
unsafe { intrinsics::cttz_nonzero(self.get() as u128) }
}
#[doc = "let a = NonZero::<i128>::new(0b_01100100)?;"]
#[doc = "let b = NonZero::<i128>::new(0b_01000000)?;"]
#[stable(feature = "isolate_most_least_significant_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn isolate_highest_one(self) -> Self {
unsafe {
let bit =
(((1 as u128) <<
(<u128>::BITS - 1)).unchecked_shr(self.leading_zeros()));
NonZero::new_unchecked(bit as i128)
}
}
#[doc = "let a = NonZero::<i128>::new(0b_01100100)?;"]
#[doc = "let b = NonZero::<i128>::new(0b_00000100)?;"]
#[stable(feature = "isolate_most_least_significant_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn isolate_lowest_one(self) -> Self {
let n = self.get();
let n = n & n.wrapping_neg();
unsafe { NonZero::new_unchecked(n) }
}
#[doc = "assert_eq!(NonZero::<i128>::new(0b1)?.highest_one(), 0);"]
#[doc = "assert_eq!(NonZero::<i128>::new(0b1_0000)?.highest_one(), 4);"]
#[doc = "assert_eq!(NonZero::<i128>::new(0b1_1111)?.highest_one(), 4);"]
#[stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn highest_one(self) -> u32 {
Self::BITS - 1 - self.leading_zeros()
}
#[doc = "assert_eq!(NonZero::<i128>::new(0b1)?.lowest_one(), 0);"]
#[doc = "assert_eq!(NonZero::<i128>::new(0b1_0000)?.lowest_one(), 4);"]
#[doc = "assert_eq!(NonZero::<i128>::new(0b1_1111)?.lowest_one(), 0);"]
#[stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn lowest_one(self) -> u32 { self.trailing_zeros() }
#[doc = "let a = NonZero::<i128>::new(0b100_0000)?;"]
#[doc = "let b = NonZero::<i128>::new(0b100_0011)?;"]
#[stable(feature = "non_zero_count_ones", since = "1.86.0")]
#[rustc_const_stable(feature = "non_zero_count_ones", since = "1.86.0")]
#[doc(alias = "popcount")]
#[doc(alias = "popcnt")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn count_ones(self) -> NonZero<u32> {
unsafe { NonZero::new_unchecked(self.get().count_ones()) }
}
#[doc = "let n = NonZero::new(0x13f40000000000000000000000004f76i128)?;"]
#[doc = "let m = NonZero::new(0x4f7613f4)?;"]
#[doc = "assert_eq!(n.rotate_left(16), m);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn rotate_left(self, n: u32) -> Self {
let result = self.get().rotate_left(n);
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x4f7613f4i128)?;"]
#[doc = "let m = NonZero::new(0x13f40000000000000000000000004f76)?;"]
#[doc = "assert_eq!(n.rotate_right(16), m);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn rotate_right(self, n: u32) -> Self {
let result = self.get().rotate_right(n);
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x12345678901234567890123456789012i128)?;"]
#[doc =
"assert_eq!(m, NonZero::new(0x12907856341290785634129078563412)?);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn swap_bytes(self) -> Self {
let result = self.get().swap_bytes();
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x12345678901234567890123456789012i128)?;"]
#[doc =
"assert_eq!(m, NonZero::new(0x48091e6a2c48091e6a2c48091e6a2c48)?);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn reverse_bits(self) -> Self {
let result = self.get().reverse_bits();
unsafe { Self::new_unchecked(result) }
}
#[doc = "use std::num::NonZeroI128;"]
#[doc = "let n = NonZero::new(0x1Ai128)?;"]
#[doc = " assert_eq!(NonZeroI128::from_be(n), n)"]
#[doc = " assert_eq!(NonZeroI128::from_be(n), n.swap_bytes())"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use]
#[inline(always)]
pub const fn from_be(x: Self) -> Self {
let result = i128::from_be(x.get());
unsafe { Self::new_unchecked(result) }
}
#[doc = "use std::num::NonZeroI128;"]
#[doc = "let n = NonZero::new(0x1Ai128)?;"]
#[doc = " assert_eq!(NonZeroI128::from_le(n), n)"]
#[doc = " assert_eq!(NonZeroI128::from_le(n), n.swap_bytes())"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use]
#[inline(always)]
pub const fn from_le(x: Self) -> Self {
let result = i128::from_le(x.get());
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x1Ai128)?;"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn to_be(self) -> Self {
let result = self.get().to_be();
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x1Ai128)?;"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn to_le(self) -> Self {
let result = self.get().to_le();
unsafe { Self::new_unchecked(result) }
}
#[doc = "equal to [`i128::MIN`]."]
#[doc = "assert_eq!(NonZero::<i128>::MIN.get(), i128::MIN);"]
#[stable(feature = "nonzero_min_max", since = "1.70.0")]
pub const MIN: Self = Self::new(<i128>::MIN).unwrap();
#[doc = "equal to [`i128::MAX`]."]
#[doc = "assert_eq!(NonZero::<i128>::MAX.get(), i128::MAX);"]
#[stable(feature = "nonzero_min_max", since = "1.70.0")]
pub const MAX: Self = Self::new(<i128>::MAX).unwrap();
#[doc = "See [`i128::abs`]"]
#[doc = "let pos = NonZero::new(1i128)?;"]
#[doc = "let neg = NonZero::new(-1i128)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn abs(self) -> Self {
unsafe { Self::new_unchecked(self.get().abs()) }
}
#[doc = "`self == NonZero::<i128>::MIN`."]
#[doc = "let pos = NonZero::new(1i128)?;"]
#[doc = "let neg = NonZero::new(-1i128)?;"]
#[doc = "let min = NonZero::new(i128::MIN)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_abs(self) -> Option<Self> {
if let Some(nz) = self.get().checked_abs() {
Some(unsafe { Self::new_unchecked(nz) })
} else { None }
}
#[doc = "[`i128::overflowing_abs`]."]
#[doc = "let pos = NonZero::new(1i128)?;"]
#[doc = "let neg = NonZero::new(-1i128)?;"]
#[doc = "let min = NonZero::new(i128::MIN)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn overflowing_abs(self) -> (Self, bool) {
let (nz, flag) = self.get().overflowing_abs();
(unsafe { Self::new_unchecked(nz) }, flag)
}
#[doc = "[`i128::saturating_abs`]."]
#[doc = "let pos = NonZero::new(1i128)?;"]
#[doc = "let neg = NonZero::new(-1i128)?;"]
#[doc = "let min = NonZero::new(i128::MIN)?;"]
#[doc = "let min_plus = NonZero::new(i128::MIN + 1)?;"]
#[doc = "let max = NonZero::new(i128::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_abs(self) -> Self {
unsafe { Self::new_unchecked(self.get().saturating_abs()) }
}
#[doc = "[`i128::wrapping_abs`]."]
#[doc = "let pos = NonZero::new(1i128)?;"]
#[doc = "let neg = NonZero::new(-1i128)?;"]
#[doc = "let min = NonZero::new(i128::MIN)?;"]
#[doc = "# let max = NonZero::new(i128::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn wrapping_abs(self) -> Self {
unsafe { Self::new_unchecked(self.get().wrapping_abs()) }
}
#[doc = "let u_pos = NonZero::new(1u128)?;"]
#[doc = "let i_pos = NonZero::new(1i128)?;"]
#[doc = "let i_neg = NonZero::new(-1i128)?;"]
#[doc = "let i_min = NonZero::new(i128::MIN)?;"]
#[doc = "let u_max = NonZero::new(u128::MAX / 2 + 1)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn unsigned_abs(self) -> NonZero<u128> {
unsafe { NonZero::new_unchecked(self.get().unsigned_abs()) }
}
#[doc = "let pos_five = NonZero::new(5i128)?;"]
#[doc = "let neg_five = NonZero::new(-5i128)?;"]
#[must_use]
#[inline]
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
pub const fn is_positive(self) -> bool { self.get().is_positive() }
#[doc = "let pos_five = NonZero::new(5i128)?;"]
#[doc = "let neg_five = NonZero::new(-5i128)?;"]
#[must_use]
#[inline]
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
pub const fn is_negative(self) -> bool { self.get().is_negative() }
#[doc = "returning `None` if `self == NonZero::<i128>::MIN`."]
#[doc = "let pos_five = NonZero::new(5i128)?;"]
#[doc = "let neg_five = NonZero::new(-5i128)?;"]
#[doc = "let min = NonZero::new(i128::MIN)?;"]
#[inline]
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
pub const fn checked_neg(self) -> Option<Self> {
if let Some(result) = self.get().checked_neg() {
return Some(unsafe { Self::new_unchecked(result) });
}
None
}
#[doc = "See [`i128::overflowing_neg`]"]
#[doc = "let pos_five = NonZero::new(5i128)?;"]
#[doc = "let neg_five = NonZero::new(-5i128)?;"]
#[doc = "let min = NonZero::new(i128::MIN)?;"]
#[inline]
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
pub const fn overflowing_neg(self) -> (Self, bool) {
let (result, overflow) = self.get().overflowing_neg();
((unsafe { Self::new_unchecked(result) }), overflow)
}
#[doc = "returning [`NonZero::<i128>::MAX`]"]
#[doc = "if `self == NonZero::<i128>::MIN`"]
#[doc = "let pos_five = NonZero::new(5i128)?;"]
#[doc = "let neg_five = NonZero::new(-5i128)?;"]
#[doc = "let min = NonZero::new(i128::MIN)?;"]
#[doc = "let min_plus_one = NonZero::new(i128::MIN + 1)?;"]
#[doc = "let max = NonZero::new(i128::MAX)?;"]
#[inline]
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
pub const fn saturating_neg(self) -> Self {
if let Some(result) = self.checked_neg() { return result; }
Self::MAX
}
#[doc = "See [`i128::wrapping_neg`]"]
#[doc = "let pos_five = NonZero::new(5i128)?;"]
#[doc = "let neg_five = NonZero::new(-5i128)?;"]
#[doc = "let min = NonZero::new(i128::MIN)?;"]
#[inline]
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
pub const fn wrapping_neg(self) -> Self {
let result = self.get().wrapping_neg();
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(-1i128).unwrap();"]
#[doc = "assert_eq!(n.cast_unsigned(), NonZero::<u128>::MAX);"]
#[stable(feature = "integer_sign_cast", since = "1.87.0")]
#[rustc_const_stable(feature = "integer_sign_cast", since = "1.87.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn cast_unsigned(self) -> NonZero<u128> {
unsafe { NonZero::new_unchecked(self.get().cast_unsigned()) }
}
#[doc = "let two = NonZero::new(2i128)?;"]
#[doc = "let four = NonZero::new(4i128)?;"]
#[doc = "let max = NonZero::new(i128::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_mul(self, other: Self) -> Option<Self> {
if let Some(result) = self.get().checked_mul(other.get()) {
Some(unsafe { Self::new_unchecked(result) })
} else { None }
}
#[doc = "Return [`NonZero::<i128>::MAX`] on overflow."]
#[doc = "let two = NonZero::new(2i128)?;"]
#[doc = "let four = NonZero::new(4i128)?;"]
#[doc = "let max = NonZero::new(i128::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_mul(self, other: Self) -> Self {
unsafe { Self::new_unchecked(self.get().saturating_mul(other.get())) }
}
#[doc = "`self * rhs > i128::MAX`, or `self * rhs < i128::MIN`."]
#[doc = "let two = NonZero::new(2i128)?;"]
#[doc = "let four = NonZero::new(4i128)?;"]
#[unstable(feature = "nonzero_ops", issue = "84186")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const unsafe fn unchecked_mul(self, other: Self) -> Self {
unsafe { Self::new_unchecked(self.get().unchecked_mul(other.get())) }
}
#[doc = "let three = NonZero::new(3i128)?;"]
#[doc = "let twenty_seven = NonZero::new(27i128)?;"]
#[doc = "let half_max = NonZero::new(i128::MAX / 2)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_pow(self, other: u32) -> Option<Self> {
if let Some(result) = self.get().checked_pow(other) {
Some(unsafe { Self::new_unchecked(result) })
} else { None }
}
#[doc =
"Return [`NonZero::<i128>::MIN`] or [`NonZero::<i128>::MAX`] on overflow."]
#[doc = "let three = NonZero::new(3i128)?;"]
#[doc = "let twenty_seven = NonZero::new(27i128)?;"]
#[doc = "let max = NonZero::new(i128::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_pow(self, other: u32) -> Self {
unsafe { Self::new_unchecked(self.get().saturating_pow(other)) }
}
#[doc = " `+` or `-` "]
#[doc =
"assert_eq!(NonZero::<i128>::from_ascii(b\"+10\"), Ok(NonZero::new(10)?));"]
#[doc = "assert!(NonZero::<i128>::from_ascii(b\"1 \").is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[inline]
pub const fn from_ascii(src: &[u8]) -> Result<Self, ParseIntError> {
Self::from_ascii_radix(src, 10)
}
#[doc = " `+` or `-` "]
#[doc =
"assert_eq!(NonZero::<i128>::from_ascii_radix(b\"A\", 16), Ok(NonZero::new(10)?));"]
#[doc =
"assert!(NonZero::<i128>::from_ascii_radix(b\"1 \", 10).is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[inline]
pub const fn from_ascii_radix(src: &[u8], radix: u32)
-> Result<Self, ParseIntError> {
let n =
match <i128>::from_ascii_radix(src, radix) {
Ok(n) => n,
Err(err) => return Err(err),
};
if let Some(n) = Self::new(n) {
Ok(n)
} else { Err(ParseIntError { kind: IntErrorKind::Zero }) }
}
#[doc = " `+` or `-` "]
#[doc =
"assert_eq!(NonZero::<i128>::from_str_radix(\"A\", 16), Ok(NonZero::new(10)?));"]
#[doc = "assert!(NonZero::<i128>::from_str_radix(\"1 \", 10).is_err());"]
#[unstable(feature = "nonzero_from_str_radix", issue = "152193")]
#[inline]
pub const fn from_str_radix(src: &str, radix: u32)
-> Result<Self, ParseIntError> {
Self::from_ascii_radix(src.as_bytes(), radix)
}
}
#[stable(feature = "nonzero_parse", since = "1.35.0")]
impl FromStr for NonZero<i128> {
type Err = ParseIntError;
fn from_str(src: &str) -> Result<Self, Self::Err> {
Self::from_str_radix(src, 10)
}
}
#[stable(feature = "signed_nonzero_neg", since = "1.71.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const Neg for NonZero<i128> {
type Output = Self;
#[inline]
fn neg(self) -> Self { unsafe { Self::new_unchecked(self.get().neg()) } }
}
#[stable(feature = "signed_nonzero_neg", since = "1.71.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const Neg for &NonZero<i128> {
type Output = <NonZero<i128> as Neg>::Output;
#[inline]
fn neg(self) -> <NonZero<i128> as Neg>::Output { Neg::neg(*self) }
}nonzero_integer! {
2564 Self = NonZeroI128,
2565 Primitive = signed i128,
2566 UnsignedPrimitive = u128,
2567 rot = 16,
2568 rot_op = "0x13f40000000000000000000000004f76",
2569 rot_result = "0x4f7613f4",
2570 swap_op = "0x12345678901234567890123456789012",
2571 swapped = "0x12907856341290785634129078563412",
2572 reversed = "0x48091e6a2c48091e6a2c48091e6a2c48",
2573}
2574
2575#[cfg(target_pointer_width = "16")]
2576nonzero_integer! {
2577 Self = NonZeroIsize,
2578 Primitive = signed isize,
2579 UnsignedPrimitive = usize,
2580 rot = 4,
2581 rot_op = "-0x5ffd",
2582 rot_result = "0x3a",
2583 swap_op = "0x1234",
2584 swapped = "0x3412",
2585 reversed = "0x2c48",
2586}
2587
2588#[cfg(target_pointer_width = "32")]
2589nonzero_integer! {
2590 Self = NonZeroIsize,
2591 Primitive = signed isize,
2592 UnsignedPrimitive = usize,
2593 rot = 8,
2594 rot_op = "0x10000b3",
2595 rot_result = "0xb301",
2596 swap_op = "0x12345678",
2597 swapped = "0x78563412",
2598 reversed = "0x1e6a2c48",
2599}
2600
2601#[cfg(target_pointer_width = "64")]
2602#[doc = "An [`isize`] that is known not to equal zero."]
#[doc = "For example, `Option<NonZeroIsize>` is the same size as `isize`:"]
#[doc =
"assert_eq!(size_of::<Option<core::num::NonZeroIsize>>(), size_of::<isize>());"]
#[doc =
"`NonZeroIsize` is guaranteed to have the same layout and bit validity as `isize`"]
#[doc = "`Option<NonZeroIsize>` is guaranteed to be compatible with `isize`,"]
#[doc = "`NonZeroIsize` and `Option<NonZeroIsize>`"]
#[doc = "use std::num::NonZeroIsize;"]
#[doc =
"assert_eq!(size_of::<NonZeroIsize>(), size_of::<Option<NonZeroIsize>>());"]
#[doc =
"assert_eq!(align_of::<NonZeroIsize>(), align_of::<Option<NonZeroIsize>>());"]
#[doc = "`NonZeroIsize`"]
#[doc = "use std::num::NonZeroIsize;"]
#[doc =
"const TEN: NonZeroIsize = NonZeroIsize::new(10).expect(\"ten is non-zero\");"]
#[stable(feature = "signed_nonzero", since = "1.34.0")]
pub type NonZeroIsize = NonZero<isize>;
impl NonZero<isize> {
#[doc = "This value is equal to [`isize::BITS`]."]
#[doc = "assert_eq!(NonZero::<isize>::BITS, isize::BITS);"]
#[stable(feature = "nonzero_bits", since = "1.67.0")]
pub const BITS: u32 = <isize>::BITS;
#[doc = "let n = NonZero::<isize>::new(-1isize)?;"]
#[stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
#[rustc_const_stable(feature = "nonzero_leading_trailing_zeros", since =
"1.53.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn leading_zeros(self) -> u32 {
unsafe { intrinsics::ctlz_nonzero(self.get() as usize) }
}
#[doc = "let n = NonZero::<isize>::new(0b0101000)?;"]
#[stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
#[rustc_const_stable(feature = "nonzero_leading_trailing_zeros", since =
"1.53.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn trailing_zeros(self) -> u32 {
unsafe { intrinsics::cttz_nonzero(self.get() as usize) }
}
#[doc = "let a = NonZero::<isize>::new(0b_01100100)?;"]
#[doc = "let b = NonZero::<isize>::new(0b_01000000)?;"]
#[stable(feature = "isolate_most_least_significant_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn isolate_highest_one(self) -> Self {
unsafe {
let bit =
(((1 as usize) <<
(<usize>::BITS - 1)).unchecked_shr(self.leading_zeros()));
NonZero::new_unchecked(bit as isize)
}
}
#[doc = "let a = NonZero::<isize>::new(0b_01100100)?;"]
#[doc = "let b = NonZero::<isize>::new(0b_00000100)?;"]
#[stable(feature = "isolate_most_least_significant_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn isolate_lowest_one(self) -> Self {
let n = self.get();
let n = n & n.wrapping_neg();
unsafe { NonZero::new_unchecked(n) }
}
#[doc = "assert_eq!(NonZero::<isize>::new(0b1)?.highest_one(), 0);"]
#[doc = "assert_eq!(NonZero::<isize>::new(0b1_0000)?.highest_one(), 4);"]
#[doc = "assert_eq!(NonZero::<isize>::new(0b1_1111)?.highest_one(), 4);"]
#[stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn highest_one(self) -> u32 {
Self::BITS - 1 - self.leading_zeros()
}
#[doc = "assert_eq!(NonZero::<isize>::new(0b1)?.lowest_one(), 0);"]
#[doc = "assert_eq!(NonZero::<isize>::new(0b1_0000)?.lowest_one(), 4);"]
#[doc = "assert_eq!(NonZero::<isize>::new(0b1_1111)?.lowest_one(), 0);"]
#[stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"CURRENT_RUSTC_VERSION")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn lowest_one(self) -> u32 { self.trailing_zeros() }
#[doc = "let a = NonZero::<isize>::new(0b100_0000)?;"]
#[doc = "let b = NonZero::<isize>::new(0b100_0011)?;"]
#[stable(feature = "non_zero_count_ones", since = "1.86.0")]
#[rustc_const_stable(feature = "non_zero_count_ones", since = "1.86.0")]
#[doc(alias = "popcount")]
#[doc(alias = "popcnt")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn count_ones(self) -> NonZero<u32> {
unsafe { NonZero::new_unchecked(self.get().count_ones()) }
}
#[doc = "let n = NonZero::new(0xaa00000000006e1isize)?;"]
#[doc = "let m = NonZero::new(0x6e10aa)?;"]
#[doc = "assert_eq!(n.rotate_left(12), m);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn rotate_left(self, n: u32) -> Self {
let result = self.get().rotate_left(n);
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x6e10aaisize)?;"]
#[doc = "let m = NonZero::new(0xaa00000000006e1)?;"]
#[doc = "assert_eq!(n.rotate_right(12), m);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn rotate_right(self, n: u32) -> Self {
let result = self.get().rotate_right(n);
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x1234567890123456isize)?;"]
#[doc = "assert_eq!(m, NonZero::new(0x5634129078563412)?);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn swap_bytes(self) -> Self {
let result = self.get().swap_bytes();
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x1234567890123456isize)?;"]
#[doc = "assert_eq!(m, NonZero::new(0x6a2c48091e6a2c48)?);"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn reverse_bits(self) -> Self {
let result = self.get().reverse_bits();
unsafe { Self::new_unchecked(result) }
}
#[doc = "use std::num::NonZeroIsize;"]
#[doc = "let n = NonZero::new(0x1Aisize)?;"]
#[doc = " assert_eq!(NonZeroIsize::from_be(n), n)"]
#[doc = " assert_eq!(NonZeroIsize::from_be(n), n.swap_bytes())"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use]
#[inline(always)]
pub const fn from_be(x: Self) -> Self {
let result = isize::from_be(x.get());
unsafe { Self::new_unchecked(result) }
}
#[doc = "use std::num::NonZeroIsize;"]
#[doc = "let n = NonZero::new(0x1Aisize)?;"]
#[doc = " assert_eq!(NonZeroIsize::from_le(n), n)"]
#[doc = " assert_eq!(NonZeroIsize::from_le(n), n.swap_bytes())"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use]
#[inline(always)]
pub const fn from_le(x: Self) -> Self {
let result = isize::from_le(x.get());
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x1Aisize)?;"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn to_be(self) -> Self {
let result = self.get().to_be();
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(0x1Aisize)?;"]
#[unstable(feature = "nonzero_bitwise", issue = "128281")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn to_le(self) -> Self {
let result = self.get().to_le();
unsafe { Self::new_unchecked(result) }
}
#[doc = "equal to [`isize::MIN`]."]
#[doc = "assert_eq!(NonZero::<isize>::MIN.get(), isize::MIN);"]
#[stable(feature = "nonzero_min_max", since = "1.70.0")]
pub const MIN: Self = Self::new(<isize>::MIN).unwrap();
#[doc = "equal to [`isize::MAX`]."]
#[doc = "assert_eq!(NonZero::<isize>::MAX.get(), isize::MAX);"]
#[stable(feature = "nonzero_min_max", since = "1.70.0")]
pub const MAX: Self = Self::new(<isize>::MAX).unwrap();
#[doc = "See [`isize::abs`]"]
#[doc = "let pos = NonZero::new(1isize)?;"]
#[doc = "let neg = NonZero::new(-1isize)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn abs(self) -> Self {
unsafe { Self::new_unchecked(self.get().abs()) }
}
#[doc = "`self == NonZero::<isize>::MIN`."]
#[doc = "let pos = NonZero::new(1isize)?;"]
#[doc = "let neg = NonZero::new(-1isize)?;"]
#[doc = "let min = NonZero::new(isize::MIN)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_abs(self) -> Option<Self> {
if let Some(nz) = self.get().checked_abs() {
Some(unsafe { Self::new_unchecked(nz) })
} else { None }
}
#[doc = "[`isize::overflowing_abs`]."]
#[doc = "let pos = NonZero::new(1isize)?;"]
#[doc = "let neg = NonZero::new(-1isize)?;"]
#[doc = "let min = NonZero::new(isize::MIN)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn overflowing_abs(self) -> (Self, bool) {
let (nz, flag) = self.get().overflowing_abs();
(unsafe { Self::new_unchecked(nz) }, flag)
}
#[doc = "[`isize::saturating_abs`]."]
#[doc = "let pos = NonZero::new(1isize)?;"]
#[doc = "let neg = NonZero::new(-1isize)?;"]
#[doc = "let min = NonZero::new(isize::MIN)?;"]
#[doc = "let min_plus = NonZero::new(isize::MIN + 1)?;"]
#[doc = "let max = NonZero::new(isize::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_abs(self) -> Self {
unsafe { Self::new_unchecked(self.get().saturating_abs()) }
}
#[doc = "[`isize::wrapping_abs`]."]
#[doc = "let pos = NonZero::new(1isize)?;"]
#[doc = "let neg = NonZero::new(-1isize)?;"]
#[doc = "let min = NonZero::new(isize::MIN)?;"]
#[doc = "# let max = NonZero::new(isize::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn wrapping_abs(self) -> Self {
unsafe { Self::new_unchecked(self.get().wrapping_abs()) }
}
#[doc = "let u_pos = NonZero::new(1usize)?;"]
#[doc = "let i_pos = NonZero::new(1isize)?;"]
#[doc = "let i_neg = NonZero::new(-1isize)?;"]
#[doc = "let i_min = NonZero::new(isize::MIN)?;"]
#[doc = "let u_max = NonZero::new(usize::MAX / 2 + 1)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn unsigned_abs(self) -> NonZero<usize> {
unsafe { NonZero::new_unchecked(self.get().unsigned_abs()) }
}
#[doc = "let pos_five = NonZero::new(5isize)?;"]
#[doc = "let neg_five = NonZero::new(-5isize)?;"]
#[must_use]
#[inline]
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
pub const fn is_positive(self) -> bool { self.get().is_positive() }
#[doc = "let pos_five = NonZero::new(5isize)?;"]
#[doc = "let neg_five = NonZero::new(-5isize)?;"]
#[must_use]
#[inline]
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
pub const fn is_negative(self) -> bool { self.get().is_negative() }
#[doc = "returning `None` if `self == NonZero::<isize>::MIN`."]
#[doc = "let pos_five = NonZero::new(5isize)?;"]
#[doc = "let neg_five = NonZero::new(-5isize)?;"]
#[doc = "let min = NonZero::new(isize::MIN)?;"]
#[inline]
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
pub const fn checked_neg(self) -> Option<Self> {
if let Some(result) = self.get().checked_neg() {
return Some(unsafe { Self::new_unchecked(result) });
}
None
}
#[doc = "See [`isize::overflowing_neg`]"]
#[doc = "let pos_five = NonZero::new(5isize)?;"]
#[doc = "let neg_five = NonZero::new(-5isize)?;"]
#[doc = "let min = NonZero::new(isize::MIN)?;"]
#[inline]
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
pub const fn overflowing_neg(self) -> (Self, bool) {
let (result, overflow) = self.get().overflowing_neg();
((unsafe { Self::new_unchecked(result) }), overflow)
}
#[doc = "returning [`NonZero::<isize>::MAX`]"]
#[doc = "if `self == NonZero::<isize>::MIN`"]
#[doc = "let pos_five = NonZero::new(5isize)?;"]
#[doc = "let neg_five = NonZero::new(-5isize)?;"]
#[doc = "let min = NonZero::new(isize::MIN)?;"]
#[doc = "let min_plus_one = NonZero::new(isize::MIN + 1)?;"]
#[doc = "let max = NonZero::new(isize::MAX)?;"]
#[inline]
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
pub const fn saturating_neg(self) -> Self {
if let Some(result) = self.checked_neg() { return result; }
Self::MAX
}
#[doc = "See [`isize::wrapping_neg`]"]
#[doc = "let pos_five = NonZero::new(5isize)?;"]
#[doc = "let neg_five = NonZero::new(-5isize)?;"]
#[doc = "let min = NonZero::new(isize::MIN)?;"]
#[inline]
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
pub const fn wrapping_neg(self) -> Self {
let result = self.get().wrapping_neg();
unsafe { Self::new_unchecked(result) }
}
#[doc = "let n = NonZero::new(-1isize).unwrap();"]
#[doc = "assert_eq!(n.cast_unsigned(), NonZero::<usize>::MAX);"]
#[stable(feature = "integer_sign_cast", since = "1.87.0")]
#[rustc_const_stable(feature = "integer_sign_cast", since = "1.87.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn cast_unsigned(self) -> NonZero<usize> {
unsafe { NonZero::new_unchecked(self.get().cast_unsigned()) }
}
#[doc = "let two = NonZero::new(2isize)?;"]
#[doc = "let four = NonZero::new(4isize)?;"]
#[doc = "let max = NonZero::new(isize::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_mul(self, other: Self) -> Option<Self> {
if let Some(result) = self.get().checked_mul(other.get()) {
Some(unsafe { Self::new_unchecked(result) })
} else { None }
}
#[doc = "Return [`NonZero::<isize>::MAX`] on overflow."]
#[doc = "let two = NonZero::new(2isize)?;"]
#[doc = "let four = NonZero::new(4isize)?;"]
#[doc = "let max = NonZero::new(isize::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_mul(self, other: Self) -> Self {
unsafe { Self::new_unchecked(self.get().saturating_mul(other.get())) }
}
#[doc = "`self * rhs > isize::MAX`, or `self * rhs < isize::MIN`."]
#[doc = "let two = NonZero::new(2isize)?;"]
#[doc = "let four = NonZero::new(4isize)?;"]
#[unstable(feature = "nonzero_ops", issue = "84186")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const unsafe fn unchecked_mul(self, other: Self) -> Self {
unsafe { Self::new_unchecked(self.get().unchecked_mul(other.get())) }
}
#[doc = "let three = NonZero::new(3isize)?;"]
#[doc = "let twenty_seven = NonZero::new(27isize)?;"]
#[doc = "let half_max = NonZero::new(isize::MAX / 2)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_pow(self, other: u32) -> Option<Self> {
if let Some(result) = self.get().checked_pow(other) {
Some(unsafe { Self::new_unchecked(result) })
} else { None }
}
#[doc =
"Return [`NonZero::<isize>::MIN`] or [`NonZero::<isize>::MAX`] on overflow."]
#[doc = "let three = NonZero::new(3isize)?;"]
#[doc = "let twenty_seven = NonZero::new(27isize)?;"]
#[doc = "let max = NonZero::new(isize::MAX)?;"]
#[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
#[rustc_const_stable(feature = "const_nonzero_checked_ops", since =
"1.64.0")]
#[must_use =
"this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_pow(self, other: u32) -> Self {
unsafe { Self::new_unchecked(self.get().saturating_pow(other)) }
}
#[doc = " `+` or `-` "]
#[doc =
"assert_eq!(NonZero::<isize>::from_ascii(b\"+10\"), Ok(NonZero::new(10)?));"]
#[doc = "assert!(NonZero::<isize>::from_ascii(b\"1 \").is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[inline]
pub const fn from_ascii(src: &[u8]) -> Result<Self, ParseIntError> {
Self::from_ascii_radix(src, 10)
}
#[doc = " `+` or `-` "]
#[doc =
"assert_eq!(NonZero::<isize>::from_ascii_radix(b\"A\", 16), Ok(NonZero::new(10)?));"]
#[doc =
"assert!(NonZero::<isize>::from_ascii_radix(b\"1 \", 10).is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[inline]
pub const fn from_ascii_radix(src: &[u8], radix: u32)
-> Result<Self, ParseIntError> {
let n =
match <isize>::from_ascii_radix(src, radix) {
Ok(n) => n,
Err(err) => return Err(err),
};
if let Some(n) = Self::new(n) {
Ok(n)
} else { Err(ParseIntError { kind: IntErrorKind::Zero }) }
}
#[doc = " `+` or `-` "]
#[doc =
"assert_eq!(NonZero::<isize>::from_str_radix(\"A\", 16), Ok(NonZero::new(10)?));"]
#[doc = "assert!(NonZero::<isize>::from_str_radix(\"1 \", 10).is_err());"]
#[unstable(feature = "nonzero_from_str_radix", issue = "152193")]
#[inline]
pub const fn from_str_radix(src: &str, radix: u32)
-> Result<Self, ParseIntError> {
Self::from_ascii_radix(src.as_bytes(), radix)
}
}
#[stable(feature = "nonzero_parse", since = "1.35.0")]
impl FromStr for NonZero<isize> {
type Err = ParseIntError;
fn from_str(src: &str) -> Result<Self, Self::Err> {
Self::from_str_radix(src, 10)
}
}
#[stable(feature = "signed_nonzero_neg", since = "1.71.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const Neg for NonZero<isize> {
type Output = Self;
#[inline]
fn neg(self) -> Self { unsafe { Self::new_unchecked(self.get().neg()) } }
}
#[stable(feature = "signed_nonzero_neg", since = "1.71.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const Neg for &NonZero<isize> {
type Output = <NonZero<isize> as Neg>::Output;
#[inline]
fn neg(self) -> <NonZero<isize> as Neg>::Output { Neg::neg(*self) }
}nonzero_integer! {
2603 Self = NonZeroIsize,
2604 Primitive = signed isize,
2605 UnsignedPrimitive = usize,
2606 rot = 12,
2607 rot_op = "0xaa00000000006e1",
2608 rot_result = "0x6e10aa",
2609 swap_op = "0x1234567890123456",
2610 swapped = "0x5634129078563412",
2611 reversed = "0x6a2c48091e6a2c48",
2612}