pub type NonZeroI64 = NonZero<i64>;
An i64 that is known not to equal zero.
This enables some memory layout optimization. For example, Option<NonZeroI64> is the same size as i64:
assert_eq!(size_of::<Option<core::num::NonZeroI64>>(), size_of::<i64>());
NonZeroI64 is guaranteed to have the same layout and bit validity as i64 with the exception that 0 is not a valid instance. Option<NonZeroI64> is guaranteed to be compatible with i64, including in FFI.
Thanks to the null pointer optimization, NonZeroI64 and Option<NonZeroI64> are guaranteed to have the same size and alignment:
use std::num::NonZeroI64; assert_eq!(size_of::<NonZeroI64>(), size_of::<Option<NonZeroI64>>()); assert_eq!(align_of::<NonZeroI64>(), align_of::<Option<NonZeroI64>>());
Since both Option::unwrap() and Option::expect() are const, it is possible to define a new NonZeroI64 at compile time via:
use std::num::NonZeroI64;
const TEN: NonZeroI64 = NonZeroI64::new(10).expect("ten is non-zero");pub struct NonZeroI64(/* 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.NonZeroI64.html