From 31cd0aa823a379b6c0d0f66ba4172585d1780e8b Mon Sep 17 00:00:00 2001 From: Scott McMurray Date: Sun, 2 Oct 2022 12:26:58 -0700 Subject: Do the `calloc` optimization for `Option` Inspired by . --- library/alloc/src/vec/is_zero.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'library/alloc/src') diff --git a/library/alloc/src/vec/is_zero.rs b/library/alloc/src/vec/is_zero.rs index 2e025c8a4a5..8e652d676dc 100644 --- a/library/alloc/src/vec/is_zero.rs +++ b/library/alloc/src/vec/is_zero.rs @@ -160,3 +160,25 @@ unsafe impl IsZero for Saturating { self.0.is_zero() } } + +macro_rules! impl_for_optional_bool { + ($($t:ty,)+) => {$( + unsafe impl IsZero for $t { + #[inline] + fn is_zero(&self) -> bool { + // SAFETY: This is *not* a stable layout guarantee, but + // inside `core` we're allowed to rely on the current rustc + // behaviour that options of bools will be one byte with + // no padding, so long as they're nested less than 254 deep. + let raw: u8 = unsafe { core::mem::transmute(*self) }; + raw == 0 + } + } + )+}; +} +impl_for_optional_bool! { + Option, + Option>, + Option>>, + // Could go further, but not worth the metadata overhead +} -- cgit 1.4.1-3-g733a5