From b94a29a25ff9ca4a8edfc0b6c7d1d309dc03d5cd Mon Sep 17 00:00:00 2001 From: clubby789 Date: Tue, 17 Jan 2023 15:42:53 +0000 Subject: Implement `alloc::vec::IsZero` for `Option<$NUM>` types --- library/alloc/src/vec/is_zero.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'library/alloc/src/vec') diff --git a/library/alloc/src/vec/is_zero.rs b/library/alloc/src/vec/is_zero.rs index 26120270c0c..2b6be14de79 100644 --- a/library/alloc/src/vec/is_zero.rs +++ b/library/alloc/src/vec/is_zero.rs @@ -147,6 +147,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 IsZero for Wrapping { #[inline] fn is_zero(&self) -> bool { -- cgit 1.4.1-3-g733a5 From 50e9f2e6e87911659b6ae63230a5797f4cdf28e1 Mon Sep 17 00:00:00 2001 From: clubby789 Date: Wed, 18 Jan 2023 15:48:53 +0000 Subject: Update `IsZero` documentation --- library/alloc/src/vec/is_zero.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'library/alloc/src/vec') diff --git a/library/alloc/src/vec/is_zero.rs b/library/alloc/src/vec/is_zero.rs index 2b6be14de79..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; } -- cgit 1.4.1-3-g733a5