Skip to content

Commit 04d34a9

Browse files
committedMay 27, 2021
Enable Vec's calloc optimization for Option<NonZero>
1 parent 3bcaeb0 commit 04d34a9

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
 

‎library/alloc/src/vec/is_zero.rs

+33
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,36 @@ unsafe impl<T: ?Sized> IsZero for Option<Box<T>> {
6969
self.is_none()
7070
}
7171
}
72+
73+
// `Option<num::NonZeroU32>` and similar have a representation guarantee that
74+
// they're the same size as the corresponding `u32` type, as well as a guarantee
75+
// that transmuting between `NonZeroU32` and `Option<num::NonZeroU32>` works.
76+
// While the documentation officially makes in UB to transmute from `None`,
77+
// we're the standard library so we can make extra inferences, and we know that
78+
// the only niche available to represent `None` is the one that's all zeros.
79+
80+
macro_rules! impl_is_zero_option_of_nonzero {
81+
($($t:ident,)+) => {$(
82+
unsafe impl IsZero for Option<core::num::$t> {
83+
#[inline]
84+
fn is_zero(&self) -> bool {
85+
self.is_none()
86+
}
87+
}
88+
)+};
89+
}
90+
91+
impl_is_zero_option_of_nonzero!(
92+
NonZeroU8,
93+
NonZeroU16,
94+
NonZeroU32,
95+
NonZeroU64,
96+
NonZeroU128,
97+
NonZeroI8,
98+
NonZeroI16,
99+
NonZeroI32,
100+
NonZeroI64,
101+
NonZeroI128,
102+
NonZeroUsize,
103+
NonZeroIsize,
104+
);

0 commit comments

Comments
 (0)
Please sign in to comment.