pub type NonZeroI8 = NonZero<i8>;
An i8 that is known not to equal zero.
This enables some memory layout optimization. For example, Option<NonZeroI8> is the same size as i8:
assert_eq!(size_of::<Option<core::num::NonZeroI8>>(), size_of::<i8>());
NonZeroI8 is guaranteed to have the same layout and bit validity as i8 with the exception that 0 is not a valid instance. Option<NonZeroI8> is guaranteed to be compatible with i8, including in FFI.
Thanks to the null pointer optimization, NonZeroI8 and Option<NonZeroI8> are guaranteed to have the same size and alignment:
use std::num::NonZeroI8; assert_eq!(size_of::<NonZeroI8>(), size_of::<Option<NonZeroI8>>()); assert_eq!(align_of::<NonZeroI8>(), align_of::<Option<NonZeroI8>>());
Since both Option::unwrap() and Option::expect() are const, it is possible to define a new NonZeroI8 at compile time via:
use std::num::NonZeroI8;
const TEN: NonZeroI8 = NonZeroI8::new(10).expect("ten is non-zero");pub struct NonZeroI8(/* private fields */);
© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/num/type.NonZeroI8.html