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 impl(self) unsafe trait ZeroablePrimitive: Sized + Copy {
35 type NonZeroInner: Sized + Copy;
37}
38
39macro_rules! impl_zeroable_primitive {
40 ($($NonZeroInner:ident ( $primitive:ty )),+ $(,)?) => {
41 $(
42 #[unstable(
43 feature = "nonzero_internals",
44 reason = "implementation detail which may disappear or be replaced at any time",
45 issue = "none"
46 )]
47 unsafe impl ZeroablePrimitive for $primitive {
48 type NonZeroInner = super::niche_types::$NonZeroInner;
49 }
50 )+
51 };
52}
53
54#[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")]
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")]
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")]
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")]
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")]
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")]
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")]
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")]
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")]
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")]
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")]
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")]
unsafe impl ZeroablePrimitive for char {
type NonZeroInner = super::niche_types::NonZeroCharInner;
}impl_zeroable_primitive!(
55 NonZeroU8Inner(u8),
56 NonZeroU16Inner(u16),
57 NonZeroU32Inner(u32),
58 NonZeroU64Inner(u64),
59 NonZeroU128Inner(u128),
60 NonZeroUsizeInner(usize),
61 NonZeroI8Inner(i8),
62 NonZeroI16Inner(i16),
63 NonZeroI32Inner(i32),
64 NonZeroI64Inner(i64),
65 NonZeroI128Inner(i128),
66 NonZeroIsizeInner(isize),
67 NonZeroCharInner(char),
68);
69
70#[stable(feature = "generic_nonzero", since = "1.79.0")]
109#[repr(transparent)]
110#[rustc_nonnull_optimization_guaranteed]
111#[rustc_diagnostic_item = "NonZero"]
112pub struct NonZero<T: ZeroablePrimitive>(T::NonZeroInner);
113
114macro_rules! impl_nonzero_fmt {
115 ($(#[$Attribute:meta] $Trait:ident)*) => {
116 $(
117 #[$Attribute]
118 impl<T> fmt::$Trait for NonZero<T>
119 where
120 T: ZeroablePrimitive + fmt::$Trait,
121 {
122 #[inline]
123 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
124 self.get().fmt(f)
125 }
126 }
127 )*
128 };
129}
130
131#[stable(feature = "nonzero", since = "1.28.0")]
impl<T> fmt::Debug for NonZero<T> where T: ZeroablePrimitive + fmt::Debug {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.get().fmt(f)
}
}
#[stable(feature = "nonzero", since = "1.28.0")]
impl<T> fmt::Display for NonZero<T> where T: ZeroablePrimitive + fmt::Display
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.get().fmt(f)
}
}
#[stable(feature = "nonzero", since = "1.28.0")]
impl<T> fmt::Binary for NonZero<T> where T: ZeroablePrimitive + fmt::Binary {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.get().fmt(f)
}
}
#[stable(feature = "nonzero", since = "1.28.0")]
impl<T> fmt::Octal for NonZero<T> where T: ZeroablePrimitive + fmt::Octal {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.get().fmt(f)
}
}
#[stable(feature = "nonzero", since = "1.28.0")]
impl<T> fmt::LowerHex for NonZero<T> where T: ZeroablePrimitive +
fmt::LowerHex {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.get().fmt(f)
}
}
#[stable(feature = "nonzero", since = "1.28.0")]
impl<T> fmt::UpperHex for NonZero<T> where T: ZeroablePrimitive +
fmt::UpperHex {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.get().fmt(f)
}
}
#[stable(feature = "nonzero_fmt_exp", since = "1.84.0")]
impl<T> fmt::LowerExp for NonZero<T> where T: ZeroablePrimitive +
fmt::LowerExp {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.get().fmt(f)
}
}
#[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! {
132 #[stable(feature = "nonzero", since = "1.28.0")]
133 Debug
134 #[stable(feature = "nonzero", since = "1.28.0")]
135 Display
136 #[stable(feature = "nonzero", since = "1.28.0")]
137 Binary
138 #[stable(feature = "nonzero", since = "1.28.0")]
139 Octal
140 #[stable(feature = "nonzero", since = "1.28.0")]
141 LowerHex
142 #[stable(feature = "nonzero", since = "1.28.0")]
143 UpperHex
144 #[stable(feature = "nonzero_fmt_exp", since = "1.84.0")]
145 LowerExp
146 #[stable(feature = "nonzero_fmt_exp", since = "1.84.0")]
147 UpperExp
148}
149
150macro_rules! impl_nonzero_auto_trait {
151 (unsafe $Trait:ident) => {
152 #[stable(feature = "nonzero", since = "1.28.0")]
153 unsafe impl<T> $Trait for NonZero<T> where T: ZeroablePrimitive + $Trait {}
154 };
155 ($Trait:ident) => {
156 #[stable(feature = "nonzero", since = "1.28.0")]
157 impl<T> $Trait for NonZero<T> where T: ZeroablePrimitive + $Trait {}
158 };
159}
160
161#[stable(feature = "nonzero", since = "1.28.0")]
unsafe impl<T> Freeze for NonZero<T> where T: ZeroablePrimitive + Freeze { }impl_nonzero_auto_trait!(unsafe Freeze);
164#[stable(feature = "nonzero", since = "1.28.0")]
impl<T> RefUnwindSafe for NonZero<T> where T: ZeroablePrimitive +
RefUnwindSafe {
}impl_nonzero_auto_trait!(RefUnwindSafe);
165#[stable(feature = "nonzero", since = "1.28.0")]
unsafe impl<T> Send for NonZero<T> where T: ZeroablePrimitive + Send { }impl_nonzero_auto_trait!(unsafe Send);
166#[stable(feature = "nonzero", since = "1.28.0")]
unsafe impl<T> Sync for NonZero<T> where T: ZeroablePrimitive + Sync { }impl_nonzero_auto_trait!(unsafe Sync);
167#[stable(feature = "nonzero", since = "1.28.0")]
impl<T> Unpin for NonZero<T> where T: ZeroablePrimitive + Unpin { }impl_nonzero_auto_trait!(Unpin);
168#[stable(feature = "nonzero", since = "1.28.0")]
impl<T> UnwindSafe for NonZero<T> where T: ZeroablePrimitive + UnwindSafe { }impl_nonzero_auto_trait!(UnwindSafe);
169
170#[stable(feature = "nonzero", since = "1.28.0")]
171#[rustc_const_unstable(feature = "const_clone", issue = "142757")]
172const impl<T> Clone for NonZero<T>
173where
174 T: ZeroablePrimitive,
175{
176 #[inline]
177 fn clone(&self) -> Self {
178 *self
179 }
180}
181
182#[unstable(feature = "ergonomic_clones", issue = "132290")]
183impl<T> UseCloned for NonZero<T> where T: ZeroablePrimitive {}
184
185#[stable(feature = "nonzero", since = "1.28.0")]
186impl<T> Copy for NonZero<T> where T: ZeroablePrimitive {}
187
188#[doc(hidden)]
189#[unstable(feature = "trivial_clone", issue = "none")]
190#[rustc_const_unstable(feature = "const_clone", issue = "142757")]
191const unsafe impl<T> TrivialClone for NonZero<T> where T: ZeroablePrimitive {}
192
193#[stable(feature = "nonzero", since = "1.28.0")]
194#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
195const impl<T> PartialEq for NonZero<T>
196where
197 T: ZeroablePrimitive + [const] PartialEq,
198{
199 #[inline]
200 fn eq(&self, other: &Self) -> bool {
201 self.get() == other.get()
202 }
203
204 #[inline]
205 fn ne(&self, other: &Self) -> bool {
206 self.get() != other.get()
207 }
208}
209
210#[unstable(feature = "structural_match", issue = "31434")]
211impl<T> StructuralPartialEq for NonZero<T> where T: ZeroablePrimitive + StructuralPartialEq {}
212
213#[stable(feature = "nonzero", since = "1.28.0")]
214#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
215const impl<T> Eq for NonZero<T> where T: ZeroablePrimitive + [const] Eq {}
216
217#[stable(feature = "nonzero", since = "1.28.0")]
218#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
219const impl<T> PartialOrd for NonZero<T>
220where
221 T: ZeroablePrimitive + [const] PartialOrd,
222{
223 #[inline]
224 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
225 self.get().partial_cmp(&other.get())
226 }
227
228 #[inline]
229 fn lt(&self, other: &Self) -> bool {
230 self.get() < other.get()
231 }
232
233 #[inline]
234 fn le(&self, other: &Self) -> bool {
235 self.get() <= other.get()
236 }
237
238 #[inline]
239 fn gt(&self, other: &Self) -> bool {
240 self.get() > other.get()
241 }
242
243 #[inline]
244 fn ge(&self, other: &Self) -> bool {
245 self.get() >= other.get()
246 }
247}
248
249#[stable(feature = "nonzero", since = "1.28.0")]
250#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
251const impl<T> Ord for NonZero<T>
252where
253 T: ZeroablePrimitive + [const] Ord + [const] Destruct,
256{
257 #[inline]
258 fn cmp(&self, other: &Self) -> Ordering {
259 self.get().cmp(&other.get())
260 }
261
262 #[inline]
263 fn max(self, other: Self) -> Self {
264 unsafe { Self::new_unchecked(self.get().max(other.get())) }
266 }
267
268 #[inline]
269 fn min(self, other: Self) -> Self {
270 unsafe { Self::new_unchecked(self.get().min(other.get())) }
272 }
273
274 #[inline]
275 fn clamp(self, min: Self, max: Self) -> Self {
276 unsafe { Self::new_unchecked(self.get().clamp(min.get(), max.get())) }
278 }
279}
280
281#[stable(feature = "nonzero", since = "1.28.0")]
282impl<T> Hash for NonZero<T>
283where
284 T: ZeroablePrimitive + Hash,
285{
286 #[inline]
287 fn hash<H>(&self, state: &mut H)
288 where
289 H: Hasher,
290 {
291 self.get().hash(state)
292 }
293}
294
295#[stable(feature = "from_nonzero", since = "1.31.0")]
296#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
297const impl<T> From<NonZero<T>> for T
298where
299 T: ZeroablePrimitive,
300{
301 #[inline]
302 fn from(nonzero: NonZero<T>) -> Self {
303 nonzero.get()
305 }
306}
307
308#[stable(feature = "nonzero_bitor", since = "1.45.0")]
309#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
310const impl<T> BitOr for NonZero<T>
311where
312 T: ZeroablePrimitive + [const] BitOr<Output = T>,
313{
314 type Output = Self;
315
316 #[inline]
317 fn bitor(self, rhs: Self) -> Self::Output {
318 unsafe { Self::new_unchecked(self.get() | rhs.get()) }
320 }
321}
322
323#[stable(feature = "nonzero_bitor", since = "1.45.0")]
324#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
325const impl<T> BitOr<T> for NonZero<T>
326where
327 T: ZeroablePrimitive + [const] BitOr<Output = T>,
328{
329 type Output = Self;
330
331 #[inline]
332 fn bitor(self, rhs: T) -> Self::Output {
333 unsafe { Self::new_unchecked(self.get() | rhs) }
335 }
336}
337
338#[stable(feature = "nonzero_bitor", since = "1.45.0")]
339#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
340const impl<T> BitOr<NonZero<T>> for T
341where
342 T: ZeroablePrimitive + [const] BitOr<Output = T>,
343{
344 type Output = NonZero<T>;
345
346 #[inline]
347 fn bitor(self, rhs: NonZero<T>) -> Self::Output {
348 unsafe { NonZero::new_unchecked(self | rhs.get()) }
350 }
351}
352
353#[stable(feature = "nonzero_bitor", since = "1.45.0")]
354#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
355const impl<T> BitOrAssign for NonZero<T>
356where
357 T: ZeroablePrimitive,
358 Self: [const] BitOr<Output = Self>,
359{
360 #[inline]
361 fn bitor_assign(&mut self, rhs: Self) {
362 *self = *self | rhs;
363 }
364}
365
366#[stable(feature = "nonzero_bitor", since = "1.45.0")]
367#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
368const impl<T> BitOrAssign<T> for NonZero<T>
369where
370 T: ZeroablePrimitive,
371 Self: [const] BitOr<T, Output = Self>,
372{
373 #[inline]
374 fn bitor_assign(&mut self, rhs: T) {
375 *self = *self | rhs;
376 }
377}
378
379impl<T> NonZero<T>
380where
381 T: ZeroablePrimitive,
382{
383 #[stable(feature = "nonzero", since = "1.28.0")]
385 #[rustc_const_stable(feature = "const_nonzero_int_methods", since = "1.47.0")]
386 #[must_use]
387 #[inline]
388 pub const fn new(n: T) -> Option<Self> {
389 unsafe { intrinsics::transmute_unchecked(n) }
392 }
393
394 #[stable(feature = "nonzero", since = "1.28.0")]
401 #[rustc_const_stable(feature = "nonzero", since = "1.28.0")]
402 #[must_use]
403 #[inline]
404 #[track_caller]
405 pub const unsafe fn new_unchecked(n: T) -> Self {
406 match Self::new(n) {
407 Some(n) => n,
408 None => {
409 unsafe {
411 {
#[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!(
412 check_language_ub,
413 "NonZero::new_unchecked requires the argument to be non-zero",
414 () => false,
415 );
416 intrinsics::unreachable()
417 }
418 }
419 }
420 }
421
422 #[unstable(feature = "nonzero_from_mut", issue = "106290")]
425 #[must_use]
426 #[inline]
427 pub fn from_mut(n: &mut T) -> Option<&mut Self> {
428 let opt_n = unsafe { &mut *(ptr::from_mut(n).cast::<Option<Self>>()) };
431
432 opt_n.as_mut()
433 }
434
435 #[unstable(feature = "nonzero_from_mut", issue = "106290")]
443 #[must_use]
444 #[inline]
445 #[track_caller]
446 pub unsafe fn from_mut_unchecked(n: &mut T) -> &mut Self {
447 match Self::from_mut(n) {
448 Some(n) => n,
449 None => {
450 unsafe {
452 {
#[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!(
453 check_library_ub,
454 "NonZero::from_mut_unchecked requires the argument to dereference as non-zero",
455 () => false,
456 );
457 intrinsics::unreachable()
458 }
459 }
460 }
461 }
462
463 #[stable(feature = "nonzero", since = "1.28.0")]
465 #[rustc_const_stable(feature = "const_nonzero_get", since = "1.34.0")]
466 #[inline]
467 pub const fn get(self) -> T {
468 unsafe { intrinsics::transmute_unchecked(self) }
487 }
488}
489
490macro_rules! nonzero_integer {
491 (
492 #[$stability:meta]
493 Self = $Ty:ident,
494 Primitive = $signedness:ident $Int:ident,
495 SignedPrimitive = $Sint:ty,
496 UnsignedPrimitive = $Uint:ty,
497
498 rot = $rot:literal,
500 rot_op = $rot_op:literal,
501 rot_result = $rot_result:literal,
502 swap_op = $swap_op:literal,
503 swapped = $swapped:literal,
504 reversed = $reversed:literal,
505 leading_zeros_test = $leading_zeros_test:expr,
506 ) => {
507 #[doc = sign_dependent_expr!{
508 $signedness ?
509 if signed {
510 concat!("An [`", stringify!($Int), "`] that is known not to equal zero.")
511 }
512 if unsigned {
513 concat!("A [`", stringify!($Int), "`] that is known not to equal zero.")
514 }
515 }]
516 #[doc = concat!("For example, `Option<", stringify!($Ty), ">` is the same size as `", stringify!($Int), "`:")]
519 #[doc = concat!("assert_eq!(size_of::<Option<core::num::", stringify!($Ty), ">>(), size_of::<", stringify!($Int), ">());")]
522 #[doc = concat!("`", stringify!($Ty), "` is guaranteed to have the same layout and bit validity as `", stringify!($Int), "`")]
527 #[doc = concat!("`Option<", stringify!($Ty), ">` is guaranteed to be ABI-compatible with `", stringify!($Int), "`,")]
529 #[doc = concat!("`", stringify!($Ty), "` and `Option<", stringify!($Ty), ">`")]
533 #[doc = concat!("use std::num::", stringify!($Ty), ";")]
537 #[doc = concat!("assert_eq!(size_of::<", stringify!($Ty), ">(), size_of::<Option<", stringify!($Ty), ">>());")]
539 #[doc = concat!("assert_eq!(align_of::<", stringify!($Ty), ">(), align_of::<Option<", stringify!($Ty), ">>());")]
540 #[doc = concat!("`", stringify!($Ty), "`")]
547 #[doc = concat!("use std::num::", stringify!($Ty), ";")]
550 #[doc = concat!("const TEN: ", stringify!($Ty), " = ", stringify!($Ty) , r#"::new(10).expect("ten is non-zero");"#)]
552 #[$stability]
556 pub type $Ty = NonZero<$Int>;
557
558 impl NonZero<$Int> {
559 #[doc = concat!("This value is equal to [`", stringify!($Int), "::BITS`].")]
562 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::BITS, ", stringify!($Int), "::BITS);")]
569 #[stable(feature = "nonzero_bits", since = "1.67.0")]
571 pub const BITS: u32 = <$Int>::BITS;
572
573 #[doc = concat!("let n = NonZero::<", stringify!($Int), ">::new(", $leading_zeros_test, ")?;")]
585 #[stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
591 #[rustc_const_stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
592 #[must_use = "this returns the result of the operation, \
593 without modifying the original"]
594 #[inline]
595 pub const fn leading_zeros(self) -> u32 {
596 unsafe {
598 intrinsics::ctlz_nonzero(self.get() as $Uint)
599 }
600 }
601
602 #[doc = concat!("let n = NonZero::<", stringify!($Int), ">::new(0b0101000)?;")]
615 #[stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
621 #[rustc_const_stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
622 #[must_use = "this returns the result of the operation, \
623 without modifying the original"]
624 #[inline]
625 pub const fn trailing_zeros(self) -> u32 {
626 unsafe {
628 intrinsics::cttz_nonzero(self.get() as $Uint)
629 }
630 }
631
632 #[doc = concat!("let a = NonZero::<", stringify!($Int), ">::new(0b_01100100)?;")]
641 #[doc = concat!("let b = NonZero::<", stringify!($Int), ">::new(0b_01000000)?;")]
642 #[stable(feature = "isolate_most_least_significant_one", since = "1.97.0")]
648 #[rustc_const_stable(feature = "isolate_most_least_significant_one", since = "1.97.0")]
649 #[must_use = "this returns the result of the operation, \
650 without modifying the original"]
651 #[inline(always)]
652 pub const fn isolate_highest_one(self) -> Self {
653 unsafe {
659 let bit = (((1 as $Uint) << (<$Uint>::BITS - 1)).unchecked_shr(self.leading_zeros()));
660 NonZero::new_unchecked(bit as $Int)
661 }
662 }
663
664 #[doc = concat!("let a = NonZero::<", stringify!($Int), ">::new(0b_01100100)?;")]
673 #[doc = concat!("let b = NonZero::<", stringify!($Int), ">::new(0b_00000100)?;")]
674 #[stable(feature = "isolate_most_least_significant_one", since = "1.97.0")]
680 #[rustc_const_stable(feature = "isolate_most_least_significant_one", since = "1.97.0")]
681 #[must_use = "this returns the result of the operation, \
682 without modifying the original"]
683 #[inline(always)]
684 pub const fn isolate_lowest_one(self) -> Self {
685 let n = self.get();
686 let n = n & n.wrapping_neg();
687
688 unsafe { NonZero::new_unchecked(n) }
691 }
692
693 #[doc = sign_dependent_expr!{
696 $signedness ?
697 if signed {
698 ""
699 }
700 if unsigned {
701 "Note that this is equivalent to [`ilog2`](Self::ilog2)."
702 }
703 }]
704 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1)?.highest_one(), 0);")]
712 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1_0000)?.highest_one(), 4);")]
713 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1_1111)?.highest_one(), 4);")]
714 #[stable(feature = "int_lowest_highest_one", since = "1.97.0")]
718 #[rustc_const_stable(feature = "int_lowest_highest_one", since = "1.97.0")]
719 #[must_use = "this returns the result of the operation, \
720 without modifying the original"]
721 #[inline(always)]
722 pub const fn highest_one(self) -> u32 {
723 Self::BITS - 1 - self.leading_zeros()
724 }
725
726 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1)?.lowest_one(), 0);")]
735 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1_0000)?.lowest_one(), 4);")]
736 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1_1111)?.lowest_one(), 0);")]
737 #[stable(feature = "int_lowest_highest_one", since = "1.97.0")]
741 #[rustc_const_stable(feature = "int_lowest_highest_one", since = "1.97.0")]
742 #[must_use = "this returns the result of the operation, \
743 without modifying the original"]
744 #[inline(always)]
745 pub const fn lowest_one(self) -> u32 {
746 self.trailing_zeros()
747 }
748
749 #[doc = concat!("let a = NonZero::<", stringify!($Int), ">::new(0b100_0000)?;")]
759 #[doc = concat!("let b = NonZero::<", stringify!($Int), ">::new(0b100_0011)?;")]
760 #[stable(feature = "non_zero_count_ones", since = "1.86.0")]
768 #[rustc_const_stable(feature = "non_zero_count_ones", since = "1.86.0")]
769 #[doc(alias = "popcount")]
770 #[doc(alias = "popcnt")]
771 #[must_use = "this returns the result of the operation, \
772 without modifying the original"]
773 #[inline(always)]
774 pub const fn count_ones(self) -> NonZero<u32> {
775 unsafe { NonZero::new_unchecked(self.get().count_ones()) }
779 }
780
781 #[doc = concat!("let n = NonZero::new(", $rot_op, stringify!($Int), ")?;")]
795 #[doc = concat!("let m = NonZero::new(", $rot_result, ")?;")]
796 #[doc = concat!("assert_eq!(n.rotate_left(", $rot, "), m);")]
798 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
802 #[must_use = "this returns the result of the operation, \
803 without modifying the original"]
804 #[inline(always)]
805 pub const fn rotate_left(self, n: u32) -> Self {
806 let result = self.get().rotate_left(n);
807 unsafe { Self::new_unchecked(result) }
809 }
810
811 #[doc = concat!("let n = NonZero::new(", $rot_result, stringify!($Int), ")?;")]
826 #[doc = concat!("let m = NonZero::new(", $rot_op, ")?;")]
827 #[doc = concat!("assert_eq!(n.rotate_right(", $rot, "), m);")]
829 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
833 #[must_use = "this returns the result of the operation, \
834 without modifying the original"]
835 #[inline(always)]
836 pub const fn rotate_right(self, n: u32) -> Self {
837 let result = self.get().rotate_right(n);
838 unsafe { Self::new_unchecked(result) }
840 }
841
842 #[doc = concat!("let n = NonZero::new(", $swap_op, stringify!($Int), ")?;")]
853 #[doc = concat!("assert_eq!(m, NonZero::new(", $swapped, ")?);")]
856 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
860 #[must_use = "this returns the result of the operation, \
861 without modifying the original"]
862 #[inline(always)]
863 pub const fn swap_bytes(self) -> Self {
864 let result = self.get().swap_bytes();
865 unsafe { Self::new_unchecked(result) }
867 }
868
869 #[doc = concat!("let n = NonZero::new(", $swap_op, stringify!($Int), ")?;")]
881 #[doc = concat!("assert_eq!(m, NonZero::new(", $reversed, ")?);")]
884 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
888 #[must_use = "this returns the result of the operation, \
889 without modifying the original"]
890 #[inline(always)]
891 pub const fn reverse_bits(self) -> Self {
892 let result = self.get().reverse_bits();
893 unsafe { Self::new_unchecked(result) }
895 }
896
897 #[doc = concat!("use std::num::", stringify!($Ty), ";")]
908 #[doc = concat!("let n = NonZero::new(0x1A", stringify!($Int), ")?;")]
912 #[doc = concat!(" assert_eq!(", stringify!($Ty), "::from_be(n), n)")]
915 #[doc = concat!(" assert_eq!(", stringify!($Ty), "::from_be(n), n.swap_bytes())")]
917 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
922 #[must_use]
923 #[inline(always)]
924 pub const fn from_be(x: Self) -> Self {
925 let result = $Int::from_be(x.get());
926 unsafe { Self::new_unchecked(result) }
928 }
929
930 #[doc = concat!("use std::num::", stringify!($Ty), ";")]
941 #[doc = concat!("let n = NonZero::new(0x1A", stringify!($Int), ")?;")]
945 #[doc = concat!(" assert_eq!(", stringify!($Ty), "::from_le(n), n)")]
948 #[doc = concat!(" assert_eq!(", stringify!($Ty), "::from_le(n), n.swap_bytes())")]
950 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
955 #[must_use]
956 #[inline(always)]
957 pub const fn from_le(x: Self) -> Self {
958 let result = $Int::from_le(x.get());
959 unsafe { Self::new_unchecked(result) }
961 }
962
963 #[doc = concat!("let n = NonZero::new(0x1A", stringify!($Int), ")?;")]
977 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
987 #[must_use = "this returns the result of the operation, \
988 without modifying the original"]
989 #[inline(always)]
990 pub const fn to_be(self) -> Self {
991 let result = self.get().to_be();
992 unsafe { Self::new_unchecked(result) }
994 }
995
996 #[doc = concat!("let n = NonZero::new(0x1A", stringify!($Int), ")?;")]
1010 #[unstable(feature = "nonzero_bitwise", issue = "128281")]
1020 #[must_use = "this returns the result of the operation, \
1021 without modifying the original"]
1022 #[inline(always)]
1023 pub const fn to_le(self) -> Self {
1024 let result = self.get().to_le();
1025 unsafe { Self::new_unchecked(result) }
1027 }
1028
1029 nonzero_integer_signedness_dependent_methods! {
1030 Primitive = $signedness $Int,
1031 SignedPrimitive = $Sint,
1032 UnsignedPrimitive = $Uint,
1033 }
1034
1035 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1047 #[doc = concat!("let four = NonZero::new(4", stringify!($Int), ")?;")]
1048 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
1049 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1056 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1057 #[must_use = "this returns the result of the operation, \
1058 without modifying the original"]
1059 #[inline]
1060 pub const fn checked_mul(self, other: Self) -> Option<Self> {
1061 if let Some(result) = self.get().checked_mul(other.get()) {
1062 Some(unsafe { Self::new_unchecked(result) })
1070 } else {
1071 None
1072 }
1073 }
1074
1075 #[doc = concat!("Return [`NonZero::<", stringify!($Int), ">::MAX`] on overflow.")]
1077 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1086 #[doc = concat!("let four = NonZero::new(4", stringify!($Int), ")?;")]
1087 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
1088 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1095 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1096 #[must_use = "this returns the result of the operation, \
1097 without modifying the original"]
1098 #[inline]
1099 pub const fn saturating_mul(self, other: Self) -> Self {
1100 unsafe { Self::new_unchecked(self.get().saturating_mul(other.get())) }
1109 }
1110
1111 #[doc = sign_dependent_expr!{
1120 $signedness ?
1121 if signed {
1122 concat!("`self * rhs > ", stringify!($Int), "::MAX`, ",
1123 "or `self * rhs < ", stringify!($Int), "::MIN`.")
1124 }
1125 if unsigned {
1126 concat!("`self * rhs > ", stringify!($Int), "::MAX`.")
1127 }
1128 }]
1129 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1140 #[doc = concat!("let four = NonZero::new(4", stringify!($Int), ")?;")]
1141 #[unstable(feature = "nonzero_ops", issue = "84186")]
1147 #[must_use = "this returns the result of the operation, \
1148 without modifying the original"]
1149 #[inline]
1150 pub const unsafe fn unchecked_mul(self, other: Self) -> Self {
1151 unsafe { Self::new_unchecked(self.get().unchecked_mul(other.get())) }
1153 }
1154
1155 #[doc = concat!("let three = NonZero::new(3", stringify!($Int), ")?;")]
1167 #[doc = concat!("let twenty_seven = NonZero::new(27", stringify!($Int), ")?;")]
1168 #[doc = concat!("let half_max = NonZero::new(", stringify!($Int), "::MAX / 2)?;")]
1169 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1176 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1177 #[must_use = "this returns the result of the operation, \
1178 without modifying the original"]
1179 #[inline]
1180 pub const fn checked_pow(self, other: u32) -> Option<Self> {
1181 if let Some(result) = self.get().checked_pow(other) {
1182 Some(unsafe { Self::new_unchecked(result) })
1190 } else {
1191 None
1192 }
1193 }
1194
1195 #[doc = sign_dependent_expr!{
1197 $signedness ?
1198 if signed {
1199 concat!("Return [`NonZero::<", stringify!($Int), ">::MIN`] ",
1200 "or [`NonZero::<", stringify!($Int), ">::MAX`] on overflow.")
1201 }
1202 if unsigned {
1203 concat!("Return [`NonZero::<", stringify!($Int), ">::MAX`] on overflow.")
1204 }
1205 }]
1206 #[doc = concat!("let three = NonZero::new(3", stringify!($Int), ")?;")]
1215 #[doc = concat!("let twenty_seven = NonZero::new(27", stringify!($Int), ")?;")]
1216 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
1217 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1224 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1225 #[must_use = "this returns the result of the operation, \
1226 without modifying the original"]
1227 #[inline]
1228 pub const fn saturating_pow(self, other: u32) -> Self {
1229 unsafe { Self::new_unchecked(self.get().saturating_pow(other)) }
1238 }
1239
1240 #[doc = sign_dependent_expr!{
1244 $signedness ?
1245 if signed {
1246 " `+` or `-` "
1247 }
1248 if unsigned {
1249 " `+` "
1250 }
1251 }]
1252 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::from_ascii_bytes(b\"+10\"), Ok(NonZero::new(10)?));")]
1266 #[doc = concat!("assert!(NonZero::<", stringify!($Int), ">::from_ascii_bytes(b\"1 \").is_err());")]
1278 #[unstable(feature = "int_from_ascii", issue = "134821")]
1280 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
1281 #[inline]
1282 pub const fn from_ascii_bytes<T>(src: T) -> Result<Self, ParseIntError>
1283 where
1284 T: [const] AsRef<[u8]> + [const] crate::marker::Destruct
1285 {
1286 Self::from_ascii_bytes_radix_impl(src.as_ref(), 10)
1287 }
1288
1289 #[doc = sign_dependent_expr!{
1293 $signedness ?
1294 if signed {
1295 " `+` or `-` "
1296 }
1297 if unsigned {
1298 " `+` "
1299 }
1300 }]
1301 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::from_ascii_bytes_radix(b\"A\", 16), Ok(NonZero::new(10)?));")]
1325 #[doc = concat!("assert!(NonZero::<", stringify!($Int), ">::from_ascii_bytes_radix(b\"1 \", 10).is_err());")]
1337 #[unstable(feature = "int_from_ascii", issue = "134821")]
1339 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
1340 #[inline]
1341 pub const fn from_ascii_bytes_radix<T>(src: T, radix: u32) -> Result<Self, ParseIntError>
1342 where
1343 T: [const] AsRef<[u8]> + [const] crate::marker::Destruct
1344 {
1345 Self::from_ascii_bytes_radix_impl(src.as_ref(), radix)
1346 }
1347
1348 #[inline]
1349 const fn from_ascii_bytes_radix_impl(src: &[u8], radix: u32) -> Result<Self, ParseIntError> {
1350 let n = match <$Int>::from_ascii_bytes_radix_impl(src, radix) {
1351 Ok(n) => n,
1352 Err(err) => return Err(err),
1353 };
1354 if let Some(n) = Self::new(n) {
1355 Ok(n)
1356 } else {
1357 Err(ParseIntError { kind: IntErrorKind::Zero })
1358 }
1359 }
1360
1361 #[doc = sign_dependent_expr!{
1365 $signedness ?
1366 if signed {
1367 " `+` or `-` "
1368 }
1369 if unsigned {
1370 " `+` "
1371 }
1372 }]
1373 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::from_str_radix(\"A\", 16), Ok(NonZero::new(10)?));")]
1395 #[doc = concat!("assert!(NonZero::<", stringify!($Int), ">::from_str_radix(\"1 \", 10).is_err());")]
1405 #[stable(feature = "nonzero_from_str_radix", since = "1.98.0")]
1407 #[rustc_const_stable(feature = "nonzero_from_str_radix", since = "1.98.0")]
1408 #[inline]
1409 pub const fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError> {
1410 Self::from_ascii_bytes_radix_impl(src.as_bytes(), radix)
1411 }
1412 }
1413
1414 #[stable(feature = "nonzero_parse", since = "1.35.0")]
1415 impl FromStr for NonZero<$Int> {
1416 type Err = ParseIntError;
1417 fn from_str(src: &str) -> Result<Self, Self::Err> {
1418 Self::from_str_radix(src, 10)
1419 }
1420 }
1421
1422 nonzero_integer_signedness_dependent_impls!($signedness $Int);
1423 };
1424
1425 (
1426 Self = $Ty:ident,
1427 Primitive = unsigned $Int:ident,
1428 SignedPrimitive = $Sint:ident,
1429 rot = $rot:literal,
1430 rot_op = $rot_op:literal,
1431 rot_result = $rot_result:literal,
1432 swap_op = $swap_op:literal,
1433 swapped = $swapped:literal,
1434 reversed = $reversed:literal,
1435 $(,)?
1436 ) => {
1437 nonzero_integer! {
1438 #[stable(feature = "nonzero", since = "1.28.0")]
1439 Self = $Ty,
1440 Primitive = unsigned $Int,
1441 SignedPrimitive = $Sint,
1442 UnsignedPrimitive = $Int,
1443 rot = $rot,
1444 rot_op = $rot_op,
1445 rot_result = $rot_result,
1446 swap_op = $swap_op,
1447 swapped = $swapped,
1448 reversed = $reversed,
1449 leading_zeros_test = concat!(stringify!($Int), "::MAX"),
1450 }
1451 };
1452
1453 (
1454 Self = $Ty:ident,
1455 Primitive = signed $Int:ident,
1456 UnsignedPrimitive = $Uint:ident,
1457 rot = $rot:literal,
1458 rot_op = $rot_op:literal,
1459 rot_result = $rot_result:literal,
1460 swap_op = $swap_op:literal,
1461 swapped = $swapped:literal,
1462 reversed = $reversed:literal,
1463 ) => {
1464 nonzero_integer! {
1465 #[stable(feature = "signed_nonzero", since = "1.34.0")]
1466 Self = $Ty,
1467 Primitive = signed $Int,
1468 SignedPrimitive = $Int,
1469 UnsignedPrimitive = $Uint,
1470 rot = $rot,
1471 rot_op = $rot_op,
1472 rot_result = $rot_result,
1473 swap_op = $swap_op,
1474 swapped = $swapped,
1475 reversed = $reversed,
1476 leading_zeros_test = concat!("-1", stringify!($Int)),
1477 }
1478 };
1479}
1480
1481macro_rules! nonzero_integer_signedness_dependent_impls {
1482 (unsigned $Int:ty) => {
1484 #[stable(feature = "nonzero_div", since = "1.51.0")]
1485 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1486 const impl Div<NonZero<$Int>> for $Int {
1487 type Output = $Int;
1488
1489 #[doc(alias = "unchecked_div")]
1495 #[inline]
1496 fn div(self, other: NonZero<$Int>) -> $Int {
1497 unsafe { intrinsics::unchecked_div(self, other.get()) }
1500 }
1501 }
1502
1503 #[stable(feature = "nonzero_div_assign", since = "1.79.0")]
1504 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1505 const impl DivAssign<NonZero<$Int>> for $Int {
1506 #[inline]
1512 fn div_assign(&mut self, other: NonZero<$Int>) {
1513 *self = *self / other;
1514 }
1515 }
1516
1517 #[stable(feature = "nonzero_div", since = "1.51.0")]
1518 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1519 const impl Rem<NonZero<$Int>> for $Int {
1520 type Output = $Int;
1521
1522 #[inline]
1524 fn rem(self, other: NonZero<$Int>) -> $Int {
1525 unsafe { intrinsics::unchecked_rem(self, other.get()) }
1528 }
1529 }
1530
1531 #[stable(feature = "nonzero_div_assign", since = "1.79.0")]
1532 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1533 const impl RemAssign<NonZero<$Int>> for $Int {
1534 #[inline]
1536 fn rem_assign(&mut self, other: NonZero<$Int>) {
1537 *self = *self % other;
1538 }
1539 }
1540
1541 impl NonZero<$Int> {
1542 #[doc = concat!("let one = NonZero::new(1", stringify!($Int), ").unwrap();")]
1551 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX).unwrap();")]
1552 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ").unwrap();")]
1555 #[doc = concat!("let three = NonZero::new(3", stringify!($Int), ").unwrap();")]
1556 #[stable(feature = "unsigned_nonzero_div_ceil", since = "1.92.0")]
1559 #[rustc_const_stable(feature = "unsigned_nonzero_div_ceil", since = "1.92.0")]
1560 #[must_use = "this returns the result of the operation, \
1561 without modifying the original"]
1562 #[inline]
1563 pub const fn div_ceil(self, rhs: Self) -> Self {
1564 let v = (self.get() - 1) / rhs.get() + 1;
1568 unsafe { Self::new_unchecked(v) }
1570 }
1571 }
1572 };
1573 (signed $Int:ty) => {
1575 #[stable(feature = "signed_nonzero_neg", since = "1.71.0")]
1576 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1577 const impl Neg for NonZero<$Int> {
1578 type Output = Self;
1579
1580 #[inline]
1581 fn neg(self) -> Self {
1582 unsafe { Self::new_unchecked(self.get().neg()) }
1584 }
1585 }
1586
1587 forward_ref_unop! { impl Neg, neg for NonZero<$Int>,
1588 #[stable(feature = "signed_nonzero_neg", since = "1.71.0")]
1589 #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
1590 };
1591}
1592
1593#[rustfmt::skip] macro_rules! nonzero_integer_signedness_dependent_methods {
1595 (
1597 Primitive = unsigned $Int:ident,
1598 SignedPrimitive = $Sint:ty,
1599 UnsignedPrimitive = $Uint:ty,
1600 ) => {
1601 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::MIN.get(), 1", stringify!($Int), ");")]
1610 #[stable(feature = "nonzero_min_max", since = "1.70.0")]
1612 pub const MIN: Self = Self::new(1).unwrap();
1613
1614 #[doc = concat!("equal to [`", stringify!($Int), "::MAX`].")]
1617 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::MAX.get(), ", stringify!($Int), "::MAX);")]
1624 #[stable(feature = "nonzero_min_max", since = "1.70.0")]
1626 pub const MAX: Self = Self::new(<$Int>::MAX).unwrap();
1627
1628 #[doc = concat!("let one = NonZero::new(1", stringify!($Int), ")?;")]
1641 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1642 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
1643 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1650 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1651 #[must_use = "this returns the result of the operation, \
1652 without modifying the original"]
1653 #[inline]
1654 pub const fn checked_add(self, other: $Int) -> Option<Self> {
1655 if let Some(result) = self.get().checked_add(other) {
1656 Some(unsafe { Self::new_unchecked(result) })
1664 } else {
1665 None
1666 }
1667 }
1668
1669 #[doc = concat!("Return [`NonZero::<", stringify!($Int), ">::MAX`] on overflow.")]
1671 #[doc = concat!("let one = NonZero::new(1", stringify!($Int), ")?;")]
1680 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1681 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
1682 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1689 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1690 #[must_use = "this returns the result of the operation, \
1691 without modifying the original"]
1692 #[inline]
1693 pub const fn saturating_add(self, other: $Int) -> Self {
1694 unsafe { Self::new_unchecked(self.get().saturating_add(other)) }
1702 }
1703
1704 #[doc = concat!("`self + rhs > ", stringify!($Int), "::MAX`.")]
1713 #[doc = concat!("let one = NonZero::new(1", stringify!($Int), ")?;")]
1724 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1725 #[unstable(feature = "nonzero_ops", issue = "84186")]
1731 #[must_use = "this returns the result of the operation, \
1732 without modifying the original"]
1733 #[inline]
1734 pub const unsafe fn unchecked_add(self, other: $Int) -> Self {
1735 unsafe { Self::new_unchecked(self.get().unchecked_add(other)) }
1737 }
1738
1739 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1752 #[doc = concat!("let three = NonZero::new(3", stringify!($Int), ")?;")]
1753 #[doc = concat!("let four = NonZero::new(4", stringify!($Int), ")?;")]
1754 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
1755 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
1763 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
1764 #[must_use = "this returns the result of the operation, \
1765 without modifying the original"]
1766 #[inline]
1767 pub const fn checked_next_power_of_two(self) -> Option<Self> {
1768 if let Some(nz) = self.get().checked_next_power_of_two() {
1769 Some(unsafe { Self::new_unchecked(nz) })
1772 } else {
1773 None
1774 }
1775 }
1776
1777 #[doc = concat!("[`", stringify!($Int), "::ilog2`],")]
1781 #[doc = concat!("assert_eq!(NonZero::new(7", stringify!($Int), ")?.ilog2(), 2);")]
1794 #[doc = concat!("assert_eq!(NonZero::new(8", stringify!($Int), ")?.ilog2(), 3);")]
1795 #[doc = concat!("assert_eq!(NonZero::new(9", stringify!($Int), ")?.ilog2(), 3);")]
1796 #[stable(feature = "int_log", since = "1.67.0")]
1800 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
1801 #[must_use = "this returns the result of the operation, \
1802 without modifying the original"]
1803 #[inline]
1804 pub const fn ilog2(self) -> u32 {
1805 Self::BITS - 1 - self.leading_zeros()
1806 }
1807
1808 #[doc = concat!("[`", stringify!($Int), "::ilog10`],")]
1812 #[doc = concat!("assert_eq!(NonZero::new(99", stringify!($Int), ")?.ilog10(), 1);")]
1823 #[doc = concat!("assert_eq!(NonZero::new(100", stringify!($Int), ")?.ilog10(), 2);")]
1824 #[doc = concat!("assert_eq!(NonZero::new(101", stringify!($Int), ")?.ilog10(), 2);")]
1825 #[stable(feature = "int_log", since = "1.67.0")]
1829 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
1830 #[must_use = "this returns the result of the operation, \
1831 without modifying the original"]
1832 #[inline]
1833 pub const fn ilog10(self) -> u32 {
1834 imp::int_log10::$Int(self)
1835 }
1836
1837 #[doc = concat!("let one = NonZero::new(1", stringify!($Int), ")?;")]
1851 #[doc = concat!("let two = NonZero::new(2", stringify!($Int), ")?;")]
1852 #[doc = concat!("let four = NonZero::new(4", stringify!($Int), ")?;")]
1853 #[stable(feature = "num_midpoint", since = "1.85.0")]
1860 #[rustc_const_stable(feature = "num_midpoint", since = "1.85.0")]
1861 #[must_use = "this returns the result of the operation, \
1862 without modifying the original"]
1863 #[doc(alias = "average_floor")]
1864 #[doc(alias = "average")]
1865 #[inline]
1866 pub const fn midpoint(self, rhs: Self) -> Self {
1867 unsafe { Self::new_unchecked(self.get().midpoint(rhs.get())) }
1872 }
1873
1874 #[doc = concat!("let eight = NonZero::new(8", stringify!($Int), ")?;")]
1887 #[doc = concat!("let ten = NonZero::new(10", stringify!($Int), ")?;")]
1889 #[must_use]
1894 #[stable(feature = "nonzero_is_power_of_two", since = "1.59.0")]
1895 #[rustc_const_stable(feature = "nonzero_is_power_of_two", since = "1.59.0")]
1896 #[inline]
1897 pub const fn is_power_of_two(self) -> bool {
1898 intrinsics::ctpop(self.get()) < 2
1904 }
1905
1906 #[doc = concat!("let ten = NonZero::new(10", stringify!($Int), ")?;")]
1916 #[doc = concat!("let three = NonZero::new(3", stringify!($Int), ")?;")]
1917 #[stable(feature = "isqrt", since = "1.84.0")]
1923 #[rustc_const_stable(feature = "isqrt", since = "1.84.0")]
1924 #[must_use = "this returns the result of the operation, \
1925 without modifying the original"]
1926 #[inline]
1927 pub const fn isqrt(self) -> Self {
1928 let result = self.get().isqrt();
1929
1930 unsafe { Self::new_unchecked(result) }
1936 }
1937
1938 #[doc = concat!("let n = NonZero::<", stringify!($Int), ">::MAX;")]
1946 #[doc = concat!("assert_eq!(n.cast_signed(), NonZero::new(-1", stringify!($Sint), ").unwrap());")]
1948 #[stable(feature = "integer_sign_cast", since = "1.87.0")]
1950 #[rustc_const_stable(feature = "integer_sign_cast", since = "1.87.0")]
1951 #[must_use = "this returns the result of the operation, \
1952 without modifying the original"]
1953 #[inline(always)]
1954 pub const fn cast_signed(self) -> NonZero<$Sint> {
1955 unsafe { NonZero::new_unchecked(self.get().cast_signed()) }
1957 }
1958
1959 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1)?.bit_width(), NonZero::new(1)?);")]
1969 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b111)?.bit_width(), NonZero::new(3)?);")]
1970 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0b1110)?.bit_width(), NonZero::new(4)?);")]
1971 #[stable(feature = "uint_bit_width", since = "1.97.0")]
1975 #[rustc_const_stable(feature = "uint_bit_width", since = "1.97.0")]
1976 #[must_use = "this returns the result of the operation, \
1977 without modifying the original"]
1978 #[inline(always)]
1979 pub const fn bit_width(self) -> NonZero<u32> {
1980 unsafe { NonZero::new_unchecked(Self::BITS - self.leading_zeros()) }
1983 }
1984 };
1985
1986 (
1988 Primitive = signed $Int:ident,
1989 SignedPrimitive = $Sint:ty,
1990 UnsignedPrimitive = $Uint:ty,
1991 ) => {
1992 #[doc = concat!("equal to [`", stringify!($Int), "::MIN`].")]
1995 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::MIN.get(), ", stringify!($Int), "::MIN);")]
2006 #[stable(feature = "nonzero_min_max", since = "1.70.0")]
2008 pub const MIN: Self = Self::new(<$Int>::MIN).unwrap();
2009
2010 #[doc = concat!("equal to [`", stringify!($Int), "::MAX`].")]
2013 #[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::MAX.get(), ", stringify!($Int), "::MAX);")]
2024 #[stable(feature = "nonzero_min_max", since = "1.70.0")]
2026 pub const MAX: Self = Self::new(<$Int>::MAX).unwrap();
2027
2028 #[doc = concat!("See [`", stringify!($Int), "::abs`]")]
2030 #[doc = concat!("let pos = NonZero::new(1", stringify!($Int), ")?;")]
2040 #[doc = concat!("let neg = NonZero::new(-1", stringify!($Int), ")?;")]
2041 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
2048 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
2049 #[must_use = "this returns the result of the operation, \
2050 without modifying the original"]
2051 #[inline]
2052 pub const fn abs(self) -> Self {
2053 unsafe { Self::new_unchecked(self.get().abs()) }
2055 }
2056
2057 #[doc = concat!("`self == NonZero::<", stringify!($Int), ">::MIN`.")]
2060 #[doc = concat!("let pos = NonZero::new(1", stringify!($Int), ")?;")]
2070 #[doc = concat!("let neg = NonZero::new(-1", stringify!($Int), ")?;")]
2071 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2072 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
2079 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
2080 #[must_use = "this returns the result of the operation, \
2081 without modifying the original"]
2082 #[inline]
2083 pub const fn checked_abs(self) -> Option<Self> {
2084 if let Some(nz) = self.get().checked_abs() {
2085 Some(unsafe { Self::new_unchecked(nz) })
2087 } else {
2088 None
2089 }
2090 }
2091
2092 #[doc = concat!("[`", stringify!($Int), "::overflowing_abs`].")]
2095 #[doc = concat!("let pos = NonZero::new(1", stringify!($Int), ")?;")]
2104 #[doc = concat!("let neg = NonZero::new(-1", stringify!($Int), ")?;")]
2105 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2106 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
2114 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
2115 #[must_use = "this returns the result of the operation, \
2116 without modifying the original"]
2117 #[inline]
2118 pub const fn overflowing_abs(self) -> (Self, bool) {
2119 let (nz, flag) = self.get().overflowing_abs();
2120 (
2121 unsafe { Self::new_unchecked(nz) },
2123 flag,
2124 )
2125 }
2126
2127 #[doc = concat!("[`", stringify!($Int), "::saturating_abs`].")]
2129 #[doc = concat!("let pos = NonZero::new(1", stringify!($Int), ")?;")]
2138 #[doc = concat!("let neg = NonZero::new(-1", stringify!($Int), ")?;")]
2139 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2140 #[doc = concat!("let min_plus = NonZero::new(", stringify!($Int), "::MIN + 1)?;")]
2141 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
2142 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
2151 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
2152 #[must_use = "this returns the result of the operation, \
2153 without modifying the original"]
2154 #[inline]
2155 pub const fn saturating_abs(self) -> Self {
2156 unsafe { Self::new_unchecked(self.get().saturating_abs()) }
2158 }
2159
2160 #[doc = concat!("[`", stringify!($Int), "::wrapping_abs`].")]
2162 #[doc = concat!("let pos = NonZero::new(1", stringify!($Int), ")?;")]
2171 #[doc = concat!("let neg = NonZero::new(-1", stringify!($Int), ")?;")]
2172 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2173 #[doc = concat!("# let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
2174 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
2183 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
2184 #[must_use = "this returns the result of the operation, \
2185 without modifying the original"]
2186 #[inline]
2187 pub const fn wrapping_abs(self) -> Self {
2188 unsafe { Self::new_unchecked(self.get().wrapping_abs()) }
2190 }
2191
2192 #[doc = concat!("let u_pos = NonZero::new(1", stringify!($Uint), ")?;")]
2203 #[doc = concat!("let i_pos = NonZero::new(1", stringify!($Int), ")?;")]
2204 #[doc = concat!("let i_neg = NonZero::new(-1", stringify!($Int), ")?;")]
2205 #[doc = concat!("let i_min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2206 #[doc = concat!("let u_max = NonZero::new(", stringify!($Uint), "::MAX / 2 + 1)?;")]
2207 #[stable(feature = "nonzero_checked_ops", since = "1.64.0")]
2215 #[rustc_const_stable(feature = "const_nonzero_checked_ops", since = "1.64.0")]
2216 #[must_use = "this returns the result of the operation, \
2217 without modifying the original"]
2218 #[inline]
2219 pub const fn unsigned_abs(self) -> NonZero<$Uint> {
2220 unsafe { NonZero::new_unchecked(self.get().unsigned_abs()) }
2222 }
2223
2224 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2235 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2236 #[must_use]
2243 #[inline]
2244 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2245 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2246 pub const fn is_positive(self) -> bool {
2247 self.get().is_positive()
2248 }
2249
2250 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2261 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2262 #[must_use]
2269 #[inline]
2270 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2271 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2272 pub const fn is_negative(self) -> bool {
2273 self.get().is_negative()
2274 }
2275
2276 #[doc = concat!("returning `None` if `self == NonZero::<", stringify!($Int), ">::MIN`.")]
2278 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2287 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2288 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2289 #[inline]
2296 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2297 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2298 pub const fn checked_neg(self) -> Option<Self> {
2299 if let Some(result) = self.get().checked_neg() {
2300 return Some(unsafe { Self::new_unchecked(result) });
2302 }
2303 None
2304 }
2305
2306 #[doc = concat!("See [`", stringify!($Int), "::overflowing_neg`]")]
2309 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2319 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2320 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2321 #[inline]
2328 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2329 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2330 pub const fn overflowing_neg(self) -> (Self, bool) {
2331 let (result, overflow) = self.get().overflowing_neg();
2332 ((unsafe { Self::new_unchecked(result) }), overflow)
2334 }
2335
2336 #[doc = concat!("returning [`NonZero::<", stringify!($Int), ">::MAX`]")]
2338 #[doc = concat!("if `self == NonZero::<", stringify!($Int), ">::MIN`")]
2339 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2349 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2350 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2351 #[doc = concat!("let min_plus_one = NonZero::new(", stringify!($Int), "::MIN + 1)?;")]
2352 #[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX)?;")]
2353 #[inline]
2361 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2362 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2363 pub const fn saturating_neg(self) -> Self {
2364 if let Some(result) = self.checked_neg() {
2365 return result;
2366 }
2367 Self::MAX
2368 }
2369
2370 #[doc = concat!("See [`", stringify!($Int), "::wrapping_neg`]")]
2374 #[doc = concat!("let pos_five = NonZero::new(5", stringify!($Int), ")?;")]
2384 #[doc = concat!("let neg_five = NonZero::new(-5", stringify!($Int), ")?;")]
2385 #[doc = concat!("let min = NonZero::new(", stringify!($Int), "::MIN)?;")]
2386 #[inline]
2393 #[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2394 #[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
2395 pub const fn wrapping_neg(self) -> Self {
2396 let result = self.get().wrapping_neg();
2397 unsafe { Self::new_unchecked(result) }
2399 }
2400
2401 #[doc = concat!("let n = NonZero::new(-1", stringify!($Int), ").unwrap();")]
2409 #[doc = concat!("assert_eq!(n.cast_unsigned(), NonZero::<", stringify!($Uint), ">::MAX);")]
2411 #[stable(feature = "integer_sign_cast", since = "1.87.0")]
2413 #[rustc_const_stable(feature = "integer_sign_cast", since = "1.87.0")]
2414 #[must_use = "this returns the result of the operation, \
2415 without modifying the original"]
2416 #[inline(always)]
2417 pub const fn cast_unsigned(self) -> NonZero<$Uint> {
2418 unsafe { NonZero::new_unchecked(self.get().cast_unsigned()) }
2420 }
2421
2422 };
2423}
2424
2425#[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 ABI-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 =
"1.97.0")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "1.97.0")]
#[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 =
"1.97.0")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "1.97.0")]
#[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 = "Note that this is equivalent to [`ilog2`](Self::ilog2)."]
#[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 = "1.97.0")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"1.97.0")]
#[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 = "1.97.0")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"1.97.0")]
#[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 = "1.97.0")]
#[rustc_const_stable(feature = "uint_bit_width", since = "1.97.0")]
#[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_bytes(b\"+10\"), Ok(NonZero::new(10)?));"]
#[doc = "assert!(NonZero::<u8>::from_ascii_bytes(b\"1 \").is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
#[inline]
pub const fn from_ascii_bytes<T>(src: T) -> Result<Self, ParseIntError>
where T: [const] AsRef<[u8]> + [const] crate::marker::Destruct {
Self::from_ascii_bytes_radix_impl(src.as_ref(), 10)
}
#[doc = " `+` "]
#[doc =
"assert_eq!(NonZero::<u8>::from_ascii_bytes_radix(b\"A\", 16), Ok(NonZero::new(10)?));"]
#[doc =
"assert!(NonZero::<u8>::from_ascii_bytes_radix(b\"1 \", 10).is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
#[inline]
pub const fn from_ascii_bytes_radix<T>(src: T, radix: u32)
-> Result<Self, ParseIntError> where T: [const] AsRef<[u8]> + [const]
crate::marker::Destruct {
Self::from_ascii_bytes_radix_impl(src.as_ref(), radix)
}
#[inline]
const fn from_ascii_bytes_radix_impl(src: &[u8], radix: u32)
-> Result<Self, ParseIntError> {
let n =
match <u8>::from_ascii_bytes_radix_impl(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());"]
#[stable(feature = "nonzero_from_str_radix", since = "1.98.0")]
#[rustc_const_stable(feature = "nonzero_from_str_radix", since =
"1.98.0")]
#[inline]
pub const fn from_str_radix(src: &str, radix: u32)
-> Result<Self, ParseIntError> {
Self::from_ascii_bytes_radix_impl(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")]
const impl 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")]
const impl 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")]
const impl 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")]
const impl 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() - 1) / rhs.get() + 1;
unsafe { Self::new_unchecked(v) }
}
}nonzero_integer! {
2426 Self = NonZeroU8,
2427 Primitive = unsigned u8,
2428 SignedPrimitive = i8,
2429 rot = 2,
2430 rot_op = "0x82",
2431 rot_result = "0xa",
2432 swap_op = "0x12",
2433 swapped = "0x12",
2434 reversed = "0x48",
2435}
2436
2437#[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 ABI-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 =
"1.97.0")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "1.97.0")]
#[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 =
"1.97.0")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "1.97.0")]
#[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 = "Note that this is equivalent to [`ilog2`](Self::ilog2)."]
#[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 = "1.97.0")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"1.97.0")]
#[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 = "1.97.0")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"1.97.0")]
#[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 = "1.97.0")]
#[rustc_const_stable(feature = "uint_bit_width", since = "1.97.0")]
#[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_bytes(b\"+10\"), Ok(NonZero::new(10)?));"]
#[doc = "assert!(NonZero::<u16>::from_ascii_bytes(b\"1 \").is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
#[inline]
pub const fn from_ascii_bytes<T>(src: T) -> Result<Self, ParseIntError>
where T: [const] AsRef<[u8]> + [const] crate::marker::Destruct {
Self::from_ascii_bytes_radix_impl(src.as_ref(), 10)
}
#[doc = " `+` "]
#[doc =
"assert_eq!(NonZero::<u16>::from_ascii_bytes_radix(b\"A\", 16), Ok(NonZero::new(10)?));"]
#[doc =
"assert!(NonZero::<u16>::from_ascii_bytes_radix(b\"1 \", 10).is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
#[inline]
pub const fn from_ascii_bytes_radix<T>(src: T, radix: u32)
-> Result<Self, ParseIntError> where T: [const] AsRef<[u8]> + [const]
crate::marker::Destruct {
Self::from_ascii_bytes_radix_impl(src.as_ref(), radix)
}
#[inline]
const fn from_ascii_bytes_radix_impl(src: &[u8], radix: u32)
-> Result<Self, ParseIntError> {
let n =
match <u16>::from_ascii_bytes_radix_impl(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());"]
#[stable(feature = "nonzero_from_str_radix", since = "1.98.0")]
#[rustc_const_stable(feature = "nonzero_from_str_radix", since =
"1.98.0")]
#[inline]
pub const fn from_str_radix(src: &str, radix: u32)
-> Result<Self, ParseIntError> {
Self::from_ascii_bytes_radix_impl(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")]
const impl 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")]
const impl 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")]
const impl 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")]
const impl 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() - 1) / rhs.get() + 1;
unsafe { Self::new_unchecked(v) }
}
}nonzero_integer! {
2438 Self = NonZeroU16,
2439 Primitive = unsigned u16,
2440 SignedPrimitive = i16,
2441 rot = 4,
2442 rot_op = "0xa003",
2443 rot_result = "0x3a",
2444 swap_op = "0x1234",
2445 swapped = "0x3412",
2446 reversed = "0x2c48",
2447}
2448
2449#[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 ABI-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 =
"1.97.0")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "1.97.0")]
#[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 =
"1.97.0")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "1.97.0")]
#[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 = "Note that this is equivalent to [`ilog2`](Self::ilog2)."]
#[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 = "1.97.0")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"1.97.0")]
#[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 = "1.97.0")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"1.97.0")]
#[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 = "1.97.0")]
#[rustc_const_stable(feature = "uint_bit_width", since = "1.97.0")]
#[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_bytes(b\"+10\"), Ok(NonZero::new(10)?));"]
#[doc = "assert!(NonZero::<u32>::from_ascii_bytes(b\"1 \").is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
#[inline]
pub const fn from_ascii_bytes<T>(src: T) -> Result<Self, ParseIntError>
where T: [const] AsRef<[u8]> + [const] crate::marker::Destruct {
Self::from_ascii_bytes_radix_impl(src.as_ref(), 10)
}
#[doc = " `+` "]
#[doc =
"assert_eq!(NonZero::<u32>::from_ascii_bytes_radix(b\"A\", 16), Ok(NonZero::new(10)?));"]
#[doc =
"assert!(NonZero::<u32>::from_ascii_bytes_radix(b\"1 \", 10).is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
#[inline]
pub const fn from_ascii_bytes_radix<T>(src: T, radix: u32)
-> Result<Self, ParseIntError> where T: [const] AsRef<[u8]> + [const]
crate::marker::Destruct {
Self::from_ascii_bytes_radix_impl(src.as_ref(), radix)
}
#[inline]
const fn from_ascii_bytes_radix_impl(src: &[u8], radix: u32)
-> Result<Self, ParseIntError> {
let n =
match <u32>::from_ascii_bytes_radix_impl(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());"]
#[stable(feature = "nonzero_from_str_radix", since = "1.98.0")]
#[rustc_const_stable(feature = "nonzero_from_str_radix", since =
"1.98.0")]
#[inline]
pub const fn from_str_radix(src: &str, radix: u32)
-> Result<Self, ParseIntError> {
Self::from_ascii_bytes_radix_impl(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")]
const impl 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")]
const impl 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")]
const impl 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")]
const impl 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() - 1) / rhs.get() + 1;
unsafe { Self::new_unchecked(v) }
}
}nonzero_integer! {
2450 Self = NonZeroU32,
2451 Primitive = unsigned u32,
2452 SignedPrimitive = i32,
2453 rot = 8,
2454 rot_op = "0x10000b3",
2455 rot_result = "0xb301",
2456 swap_op = "0x12345678",
2457 swapped = "0x78563412",
2458 reversed = "0x1e6a2c48",
2459}
2460
2461#[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 ABI-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 =
"1.97.0")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "1.97.0")]
#[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 =
"1.97.0")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "1.97.0")]
#[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 = "Note that this is equivalent to [`ilog2`](Self::ilog2)."]
#[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 = "1.97.0")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"1.97.0")]
#[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 = "1.97.0")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"1.97.0")]
#[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 = "1.97.0")]
#[rustc_const_stable(feature = "uint_bit_width", since = "1.97.0")]
#[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_bytes(b\"+10\"), Ok(NonZero::new(10)?));"]
#[doc = "assert!(NonZero::<u64>::from_ascii_bytes(b\"1 \").is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
#[inline]
pub const fn from_ascii_bytes<T>(src: T) -> Result<Self, ParseIntError>
where T: [const] AsRef<[u8]> + [const] crate::marker::Destruct {
Self::from_ascii_bytes_radix_impl(src.as_ref(), 10)
}
#[doc = " `+` "]
#[doc =
"assert_eq!(NonZero::<u64>::from_ascii_bytes_radix(b\"A\", 16), Ok(NonZero::new(10)?));"]
#[doc =
"assert!(NonZero::<u64>::from_ascii_bytes_radix(b\"1 \", 10).is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
#[inline]
pub const fn from_ascii_bytes_radix<T>(src: T, radix: u32)
-> Result<Self, ParseIntError> where T: [const] AsRef<[u8]> + [const]
crate::marker::Destruct {
Self::from_ascii_bytes_radix_impl(src.as_ref(), radix)
}
#[inline]
const fn from_ascii_bytes_radix_impl(src: &[u8], radix: u32)
-> Result<Self, ParseIntError> {
let n =
match <u64>::from_ascii_bytes_radix_impl(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());"]
#[stable(feature = "nonzero_from_str_radix", since = "1.98.0")]
#[rustc_const_stable(feature = "nonzero_from_str_radix", since =
"1.98.0")]
#[inline]
pub const fn from_str_radix(src: &str, radix: u32)
-> Result<Self, ParseIntError> {
Self::from_ascii_bytes_radix_impl(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")]
const impl 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")]
const impl 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")]
const impl 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")]
const impl 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() - 1) / rhs.get() + 1;
unsafe { Self::new_unchecked(v) }
}
}nonzero_integer! {
2462 Self = NonZeroU64,
2463 Primitive = unsigned u64,
2464 SignedPrimitive = i64,
2465 rot = 12,
2466 rot_op = "0xaa00000000006e1",
2467 rot_result = "0x6e10aa",
2468 swap_op = "0x1234567890123456",
2469 swapped = "0x5634129078563412",
2470 reversed = "0x6a2c48091e6a2c48",
2471}
2472
2473#[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 ABI-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 =
"1.97.0")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "1.97.0")]
#[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 =
"1.97.0")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "1.97.0")]
#[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 = "Note that this is equivalent to [`ilog2`](Self::ilog2)."]
#[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 = "1.97.0")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"1.97.0")]
#[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 = "1.97.0")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"1.97.0")]
#[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 = "1.97.0")]
#[rustc_const_stable(feature = "uint_bit_width", since = "1.97.0")]
#[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_bytes(b\"+10\"), Ok(NonZero::new(10)?));"]
#[doc = "assert!(NonZero::<u128>::from_ascii_bytes(b\"1 \").is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
#[inline]
pub const fn from_ascii_bytes<T>(src: T) -> Result<Self, ParseIntError>
where T: [const] AsRef<[u8]> + [const] crate::marker::Destruct {
Self::from_ascii_bytes_radix_impl(src.as_ref(), 10)
}
#[doc = " `+` "]
#[doc =
"assert_eq!(NonZero::<u128>::from_ascii_bytes_radix(b\"A\", 16), Ok(NonZero::new(10)?));"]
#[doc =
"assert!(NonZero::<u128>::from_ascii_bytes_radix(b\"1 \", 10).is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
#[inline]
pub const fn from_ascii_bytes_radix<T>(src: T, radix: u32)
-> Result<Self, ParseIntError> where T: [const] AsRef<[u8]> + [const]
crate::marker::Destruct {
Self::from_ascii_bytes_radix_impl(src.as_ref(), radix)
}
#[inline]
const fn from_ascii_bytes_radix_impl(src: &[u8], radix: u32)
-> Result<Self, ParseIntError> {
let n =
match <u128>::from_ascii_bytes_radix_impl(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());"]
#[stable(feature = "nonzero_from_str_radix", since = "1.98.0")]
#[rustc_const_stable(feature = "nonzero_from_str_radix", since =
"1.98.0")]
#[inline]
pub const fn from_str_radix(src: &str, radix: u32)
-> Result<Self, ParseIntError> {
Self::from_ascii_bytes_radix_impl(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")]
const impl 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")]
const impl 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")]
const impl 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")]
const impl 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() - 1) / rhs.get() + 1;
unsafe { Self::new_unchecked(v) }
}
}nonzero_integer! {
2474 Self = NonZeroU128,
2475 Primitive = unsigned u128,
2476 SignedPrimitive = i128,
2477 rot = 16,
2478 rot_op = "0x13f40000000000000000000000004f76",
2479 rot_result = "0x4f7613f4",
2480 swap_op = "0x12345678901234567890123456789012",
2481 swapped = "0x12907856341290785634129078563412",
2482 reversed = "0x48091e6a2c48091e6a2c48091e6a2c48",
2483}
2484
2485#[cfg(target_pointer_width = "16")]
2486nonzero_integer! {
2487 Self = NonZeroUsize,
2488 Primitive = unsigned usize,
2489 SignedPrimitive = isize,
2490 rot = 4,
2491 rot_op = "0xa003",
2492 rot_result = "0x3a",
2493 swap_op = "0x1234",
2494 swapped = "0x3412",
2495 reversed = "0x2c48",
2496}
2497
2498#[cfg(target_pointer_width = "32")]
2499nonzero_integer! {
2500 Self = NonZeroUsize,
2501 Primitive = unsigned usize,
2502 SignedPrimitive = isize,
2503 rot = 8,
2504 rot_op = "0x10000b3",
2505 rot_result = "0xb301",
2506 swap_op = "0x12345678",
2507 swapped = "0x78563412",
2508 reversed = "0x1e6a2c48",
2509}
2510
2511#[cfg(target_pointer_width = "64")]
2512#[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 ABI-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 =
"1.97.0")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "1.97.0")]
#[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 =
"1.97.0")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "1.97.0")]
#[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 = "Note that this is equivalent to [`ilog2`](Self::ilog2)."]
#[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 = "1.97.0")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"1.97.0")]
#[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 = "1.97.0")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"1.97.0")]
#[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 = "1.97.0")]
#[rustc_const_stable(feature = "uint_bit_width", since = "1.97.0")]
#[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_bytes(b\"+10\"), Ok(NonZero::new(10)?));"]
#[doc = "assert!(NonZero::<usize>::from_ascii_bytes(b\"1 \").is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
#[inline]
pub const fn from_ascii_bytes<T>(src: T) -> Result<Self, ParseIntError>
where T: [const] AsRef<[u8]> + [const] crate::marker::Destruct {
Self::from_ascii_bytes_radix_impl(src.as_ref(), 10)
}
#[doc = " `+` "]
#[doc =
"assert_eq!(NonZero::<usize>::from_ascii_bytes_radix(b\"A\", 16), Ok(NonZero::new(10)?));"]
#[doc =
"assert!(NonZero::<usize>::from_ascii_bytes_radix(b\"1 \", 10).is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
#[inline]
pub const fn from_ascii_bytes_radix<T>(src: T, radix: u32)
-> Result<Self, ParseIntError> where T: [const] AsRef<[u8]> + [const]
crate::marker::Destruct {
Self::from_ascii_bytes_radix_impl(src.as_ref(), radix)
}
#[inline]
const fn from_ascii_bytes_radix_impl(src: &[u8], radix: u32)
-> Result<Self, ParseIntError> {
let n =
match <usize>::from_ascii_bytes_radix_impl(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());"]
#[stable(feature = "nonzero_from_str_radix", since = "1.98.0")]
#[rustc_const_stable(feature = "nonzero_from_str_radix", since =
"1.98.0")]
#[inline]
pub const fn from_str_radix(src: &str, radix: u32)
-> Result<Self, ParseIntError> {
Self::from_ascii_bytes_radix_impl(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")]
const impl 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")]
const impl 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")]
const impl 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")]
const impl 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() - 1) / rhs.get() + 1;
unsafe { Self::new_unchecked(v) }
}
}nonzero_integer! {
2513 Self = NonZeroUsize,
2514 Primitive = unsigned usize,
2515 SignedPrimitive = isize,
2516 rot = 12,
2517 rot_op = "0xaa00000000006e1",
2518 rot_result = "0x6e10aa",
2519 swap_op = "0x1234567890123456",
2520 swapped = "0x5634129078563412",
2521 reversed = "0x6a2c48091e6a2c48",
2522}
2523
2524#[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 ABI-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 =
"1.97.0")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "1.97.0")]
#[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 =
"1.97.0")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "1.97.0")]
#[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 = ""]
#[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 = "1.97.0")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"1.97.0")]
#[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 = "1.97.0")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"1.97.0")]
#[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_bytes(b\"+10\"), Ok(NonZero::new(10)?));"]
#[doc = "assert!(NonZero::<i8>::from_ascii_bytes(b\"1 \").is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
#[inline]
pub const fn from_ascii_bytes<T>(src: T) -> Result<Self, ParseIntError>
where T: [const] AsRef<[u8]> + [const] crate::marker::Destruct {
Self::from_ascii_bytes_radix_impl(src.as_ref(), 10)
}
#[doc = " `+` or `-` "]
#[doc =
"assert_eq!(NonZero::<i8>::from_ascii_bytes_radix(b\"A\", 16), Ok(NonZero::new(10)?));"]
#[doc =
"assert!(NonZero::<i8>::from_ascii_bytes_radix(b\"1 \", 10).is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
#[inline]
pub const fn from_ascii_bytes_radix<T>(src: T, radix: u32)
-> Result<Self, ParseIntError> where T: [const] AsRef<[u8]> + [const]
crate::marker::Destruct {
Self::from_ascii_bytes_radix_impl(src.as_ref(), radix)
}
#[inline]
const fn from_ascii_bytes_radix_impl(src: &[u8], radix: u32)
-> Result<Self, ParseIntError> {
let n =
match <i8>::from_ascii_bytes_radix_impl(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());"]
#[stable(feature = "nonzero_from_str_radix", since = "1.98.0")]
#[rustc_const_stable(feature = "nonzero_from_str_radix", since =
"1.98.0")]
#[inline]
pub const fn from_str_radix(src: &str, radix: u32)
-> Result<Self, ParseIntError> {
Self::from_ascii_bytes_radix_impl(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")]
const impl 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")]
const impl 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! {
2525 Self = NonZeroI8,
2526 Primitive = signed i8,
2527 UnsignedPrimitive = u8,
2528 rot = 2,
2529 rot_op = "-0x7e",
2530 rot_result = "0xa",
2531 swap_op = "0x12",
2532 swapped = "0x12",
2533 reversed = "0x48",
2534}
2535
2536#[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 ABI-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 =
"1.97.0")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "1.97.0")]
#[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 =
"1.97.0")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "1.97.0")]
#[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 = ""]
#[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 = "1.97.0")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"1.97.0")]
#[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 = "1.97.0")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"1.97.0")]
#[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_bytes(b\"+10\"), Ok(NonZero::new(10)?));"]
#[doc = "assert!(NonZero::<i16>::from_ascii_bytes(b\"1 \").is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
#[inline]
pub const fn from_ascii_bytes<T>(src: T) -> Result<Self, ParseIntError>
where T: [const] AsRef<[u8]> + [const] crate::marker::Destruct {
Self::from_ascii_bytes_radix_impl(src.as_ref(), 10)
}
#[doc = " `+` or `-` "]
#[doc =
"assert_eq!(NonZero::<i16>::from_ascii_bytes_radix(b\"A\", 16), Ok(NonZero::new(10)?));"]
#[doc =
"assert!(NonZero::<i16>::from_ascii_bytes_radix(b\"1 \", 10).is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
#[inline]
pub const fn from_ascii_bytes_radix<T>(src: T, radix: u32)
-> Result<Self, ParseIntError> where T: [const] AsRef<[u8]> + [const]
crate::marker::Destruct {
Self::from_ascii_bytes_radix_impl(src.as_ref(), radix)
}
#[inline]
const fn from_ascii_bytes_radix_impl(src: &[u8], radix: u32)
-> Result<Self, ParseIntError> {
let n =
match <i16>::from_ascii_bytes_radix_impl(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());"]
#[stable(feature = "nonzero_from_str_radix", since = "1.98.0")]
#[rustc_const_stable(feature = "nonzero_from_str_radix", since =
"1.98.0")]
#[inline]
pub const fn from_str_radix(src: &str, radix: u32)
-> Result<Self, ParseIntError> {
Self::from_ascii_bytes_radix_impl(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")]
const impl 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")]
const impl 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! {
2537 Self = NonZeroI16,
2538 Primitive = signed i16,
2539 UnsignedPrimitive = u16,
2540 rot = 4,
2541 rot_op = "-0x5ffd",
2542 rot_result = "0x3a",
2543 swap_op = "0x1234",
2544 swapped = "0x3412",
2545 reversed = "0x2c48",
2546}
2547
2548#[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 ABI-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 =
"1.97.0")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "1.97.0")]
#[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 =
"1.97.0")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "1.97.0")]
#[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 = ""]
#[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 = "1.97.0")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"1.97.0")]
#[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 = "1.97.0")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"1.97.0")]
#[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_bytes(b\"+10\"), Ok(NonZero::new(10)?));"]
#[doc = "assert!(NonZero::<i32>::from_ascii_bytes(b\"1 \").is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
#[inline]
pub const fn from_ascii_bytes<T>(src: T) -> Result<Self, ParseIntError>
where T: [const] AsRef<[u8]> + [const] crate::marker::Destruct {
Self::from_ascii_bytes_radix_impl(src.as_ref(), 10)
}
#[doc = " `+` or `-` "]
#[doc =
"assert_eq!(NonZero::<i32>::from_ascii_bytes_radix(b\"A\", 16), Ok(NonZero::new(10)?));"]
#[doc =
"assert!(NonZero::<i32>::from_ascii_bytes_radix(b\"1 \", 10).is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
#[inline]
pub const fn from_ascii_bytes_radix<T>(src: T, radix: u32)
-> Result<Self, ParseIntError> where T: [const] AsRef<[u8]> + [const]
crate::marker::Destruct {
Self::from_ascii_bytes_radix_impl(src.as_ref(), radix)
}
#[inline]
const fn from_ascii_bytes_radix_impl(src: &[u8], radix: u32)
-> Result<Self, ParseIntError> {
let n =
match <i32>::from_ascii_bytes_radix_impl(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());"]
#[stable(feature = "nonzero_from_str_radix", since = "1.98.0")]
#[rustc_const_stable(feature = "nonzero_from_str_radix", since =
"1.98.0")]
#[inline]
pub const fn from_str_radix(src: &str, radix: u32)
-> Result<Self, ParseIntError> {
Self::from_ascii_bytes_radix_impl(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")]
const impl 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")]
const impl 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! {
2549 Self = NonZeroI32,
2550 Primitive = signed i32,
2551 UnsignedPrimitive = u32,
2552 rot = 8,
2553 rot_op = "0x10000b3",
2554 rot_result = "0xb301",
2555 swap_op = "0x12345678",
2556 swapped = "0x78563412",
2557 reversed = "0x1e6a2c48",
2558}
2559
2560#[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 ABI-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 =
"1.97.0")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "1.97.0")]
#[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 =
"1.97.0")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "1.97.0")]
#[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 = ""]
#[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 = "1.97.0")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"1.97.0")]
#[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 = "1.97.0")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"1.97.0")]
#[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_bytes(b\"+10\"), Ok(NonZero::new(10)?));"]
#[doc = "assert!(NonZero::<i64>::from_ascii_bytes(b\"1 \").is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
#[inline]
pub const fn from_ascii_bytes<T>(src: T) -> Result<Self, ParseIntError>
where T: [const] AsRef<[u8]> + [const] crate::marker::Destruct {
Self::from_ascii_bytes_radix_impl(src.as_ref(), 10)
}
#[doc = " `+` or `-` "]
#[doc =
"assert_eq!(NonZero::<i64>::from_ascii_bytes_radix(b\"A\", 16), Ok(NonZero::new(10)?));"]
#[doc =
"assert!(NonZero::<i64>::from_ascii_bytes_radix(b\"1 \", 10).is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
#[inline]
pub const fn from_ascii_bytes_radix<T>(src: T, radix: u32)
-> Result<Self, ParseIntError> where T: [const] AsRef<[u8]> + [const]
crate::marker::Destruct {
Self::from_ascii_bytes_radix_impl(src.as_ref(), radix)
}
#[inline]
const fn from_ascii_bytes_radix_impl(src: &[u8], radix: u32)
-> Result<Self, ParseIntError> {
let n =
match <i64>::from_ascii_bytes_radix_impl(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());"]
#[stable(feature = "nonzero_from_str_radix", since = "1.98.0")]
#[rustc_const_stable(feature = "nonzero_from_str_radix", since =
"1.98.0")]
#[inline]
pub const fn from_str_radix(src: &str, radix: u32)
-> Result<Self, ParseIntError> {
Self::from_ascii_bytes_radix_impl(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")]
const impl 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")]
const impl 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! {
2561 Self = NonZeroI64,
2562 Primitive = signed i64,
2563 UnsignedPrimitive = u64,
2564 rot = 12,
2565 rot_op = "0xaa00000000006e1",
2566 rot_result = "0x6e10aa",
2567 swap_op = "0x1234567890123456",
2568 swapped = "0x5634129078563412",
2569 reversed = "0x6a2c48091e6a2c48",
2570}
2571
2572#[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 ABI-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 =
"1.97.0")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "1.97.0")]
#[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 =
"1.97.0")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "1.97.0")]
#[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 = ""]
#[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 = "1.97.0")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"1.97.0")]
#[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 = "1.97.0")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"1.97.0")]
#[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_bytes(b\"+10\"), Ok(NonZero::new(10)?));"]
#[doc = "assert!(NonZero::<i128>::from_ascii_bytes(b\"1 \").is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
#[inline]
pub const fn from_ascii_bytes<T>(src: T) -> Result<Self, ParseIntError>
where T: [const] AsRef<[u8]> + [const] crate::marker::Destruct {
Self::from_ascii_bytes_radix_impl(src.as_ref(), 10)
}
#[doc = " `+` or `-` "]
#[doc =
"assert_eq!(NonZero::<i128>::from_ascii_bytes_radix(b\"A\", 16), Ok(NonZero::new(10)?));"]
#[doc =
"assert!(NonZero::<i128>::from_ascii_bytes_radix(b\"1 \", 10).is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
#[inline]
pub const fn from_ascii_bytes_radix<T>(src: T, radix: u32)
-> Result<Self, ParseIntError> where T: [const] AsRef<[u8]> + [const]
crate::marker::Destruct {
Self::from_ascii_bytes_radix_impl(src.as_ref(), radix)
}
#[inline]
const fn from_ascii_bytes_radix_impl(src: &[u8], radix: u32)
-> Result<Self, ParseIntError> {
let n =
match <i128>::from_ascii_bytes_radix_impl(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());"]
#[stable(feature = "nonzero_from_str_radix", since = "1.98.0")]
#[rustc_const_stable(feature = "nonzero_from_str_radix", since =
"1.98.0")]
#[inline]
pub const fn from_str_radix(src: &str, radix: u32)
-> Result<Self, ParseIntError> {
Self::from_ascii_bytes_radix_impl(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")]
const impl 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")]
const impl 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! {
2573 Self = NonZeroI128,
2574 Primitive = signed i128,
2575 UnsignedPrimitive = u128,
2576 rot = 16,
2577 rot_op = "0x13f40000000000000000000000004f76",
2578 rot_result = "0x4f7613f4",
2579 swap_op = "0x12345678901234567890123456789012",
2580 swapped = "0x12907856341290785634129078563412",
2581 reversed = "0x48091e6a2c48091e6a2c48091e6a2c48",
2582}
2583
2584#[cfg(target_pointer_width = "16")]
2585nonzero_integer! {
2586 Self = NonZeroIsize,
2587 Primitive = signed isize,
2588 UnsignedPrimitive = usize,
2589 rot = 4,
2590 rot_op = "-0x5ffd",
2591 rot_result = "0x3a",
2592 swap_op = "0x1234",
2593 swapped = "0x3412",
2594 reversed = "0x2c48",
2595}
2596
2597#[cfg(target_pointer_width = "32")]
2598nonzero_integer! {
2599 Self = NonZeroIsize,
2600 Primitive = signed isize,
2601 UnsignedPrimitive = usize,
2602 rot = 8,
2603 rot_op = "0x10000b3",
2604 rot_result = "0xb301",
2605 swap_op = "0x12345678",
2606 swapped = "0x78563412",
2607 reversed = "0x1e6a2c48",
2608}
2609
2610#[cfg(target_pointer_width = "64")]
2611#[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 ABI-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 =
"1.97.0")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "1.97.0")]
#[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 =
"1.97.0")]
#[rustc_const_stable(feature = "isolate_most_least_significant_one", since
= "1.97.0")]
#[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 = ""]
#[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 = "1.97.0")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"1.97.0")]
#[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 = "1.97.0")]
#[rustc_const_stable(feature = "int_lowest_highest_one", since =
"1.97.0")]
#[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_bytes(b\"+10\"), Ok(NonZero::new(10)?));"]
#[doc = "assert!(NonZero::<isize>::from_ascii_bytes(b\"1 \").is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
#[inline]
pub const fn from_ascii_bytes<T>(src: T) -> Result<Self, ParseIntError>
where T: [const] AsRef<[u8]> + [const] crate::marker::Destruct {
Self::from_ascii_bytes_radix_impl(src.as_ref(), 10)
}
#[doc = " `+` or `-` "]
#[doc =
"assert_eq!(NonZero::<isize>::from_ascii_bytes_radix(b\"A\", 16), Ok(NonZero::new(10)?));"]
#[doc =
"assert!(NonZero::<isize>::from_ascii_bytes_radix(b\"1 \", 10).is_err());"]
#[unstable(feature = "int_from_ascii", issue = "134821")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
#[inline]
pub const fn from_ascii_bytes_radix<T>(src: T, radix: u32)
-> Result<Self, ParseIntError> where T: [const] AsRef<[u8]> + [const]
crate::marker::Destruct {
Self::from_ascii_bytes_radix_impl(src.as_ref(), radix)
}
#[inline]
const fn from_ascii_bytes_radix_impl(src: &[u8], radix: u32)
-> Result<Self, ParseIntError> {
let n =
match <isize>::from_ascii_bytes_radix_impl(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());"]
#[stable(feature = "nonzero_from_str_radix", since = "1.98.0")]
#[rustc_const_stable(feature = "nonzero_from_str_radix", since =
"1.98.0")]
#[inline]
pub const fn from_str_radix(src: &str, radix: u32)
-> Result<Self, ParseIntError> {
Self::from_ascii_bytes_radix_impl(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")]
const impl 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")]
const impl 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! {
2612 Self = NonZeroIsize,
2613 Primitive = signed isize,
2614 UnsignedPrimitive = usize,
2615 rot = 12,
2616 rot_op = "0xaa00000000006e1",
2617 rot_result = "0x6e10aa",
2618 swap_op = "0x1234567890123456",
2619 swapped = "0x5634129078563412",
2620 reversed = "0x6a2c48091e6a2c48",
2621}