diff options
| author | bors <bors@rust-lang.org> | 2023-01-19 08:04:26 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2023-01-19 08:04:26 +0000 |
| commit | 705a96d39bbd97ac0cc8ae119b0f3c097ab62fc0 (patch) | |
| tree | e150d8f5821c55d5d84905ee20560b9aba175264 /library/alloc/src/vec | |
| parent | 65d2f2a5f9c323c88d1068e8e90d0b47a20d491c (diff) | |
| parent | 50e9f2e6e87911659b6ae63230a5797f4cdf28e1 (diff) | |
| download | rust-705a96d39bbd97ac0cc8ae119b0f3c097ab62fc0.tar.gz rust-705a96d39bbd97ac0cc8ae119b0f3c097ab62fc0.zip | |
Auto merge of #106989 - clubby789:is-zero-num, r=scottmcm
Implement `alloc::vec::IsZero` for `Option<$NUM>` types Fixes #106911 Mirrors the `NonZero$NUM` implementations with an additional `assert_zero_valid`. `None::<i32>` doesn't stricly satisfy `IsZero` but for the purpose of allocating we can produce more efficient codegen.
Diffstat (limited to 'library/alloc/src/vec')
| -rw-r--r-- | library/alloc/src/vec/is_zero.rs | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/library/alloc/src/vec/is_zero.rs b/library/alloc/src/vec/is_zero.rs index 26120270c0c..cb9adf05c25 100644 --- a/library/alloc/src/vec/is_zero.rs +++ b/library/alloc/src/vec/is_zero.rs @@ -4,7 +4,8 @@ use crate::boxed::Box; #[rustc_specialization_trait] pub(super) unsafe trait IsZero { - /// Whether this value's representation is all zeros + /// Whether this value's representation is all zeros, + /// or can be represented with all zeroes. fn is_zero(&self) -> bool; } @@ -147,6 +148,23 @@ impl_is_zero_option_of_nonzero!( NonZeroIsize, ); +macro_rules! impl_is_zero_option_of_num { + ($($t:ty,)+) => {$( + unsafe impl IsZero for Option<$t> { + #[inline] + fn is_zero(&self) -> bool { + const { + let none: Self = unsafe { core::mem::MaybeUninit::zeroed().assume_init() }; + assert!(none.is_none()); + } + self.is_none() + } + } + )+}; +} + +impl_is_zero_option_of_num!(u8, u16, u32, u64, u128, i8, i16, i32, i64, i128, usize, isize,); + unsafe impl<T: IsZero> IsZero for Wrapping<T> { #[inline] fn is_zero(&self) -> bool { |
