Skip to content

Commit b94a29a

Browse files
committed
Implement alloc::vec::IsZero for Option<$NUM> types
1 parent f34cc65 commit b94a29a

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

library/alloc/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
#![feature(const_size_of_val)]
107107
#![feature(const_align_of_val)]
108108
#![feature(const_ptr_read)]
109+
#![feature(const_maybe_uninit_zeroed)]
109110
#![feature(const_maybe_uninit_write)]
110111
#![feature(const_maybe_uninit_as_mut_ptr)]
111112
#![feature(const_refs_to_cell)]

library/alloc/src/vec/is_zero.rs

+17
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,23 @@ impl_is_zero_option_of_nonzero!(
147147
NonZeroIsize,
148148
);
149149

150+
macro_rules! impl_is_zero_option_of_num {
151+
($($t:ty,)+) => {$(
152+
unsafe impl IsZero for Option<$t> {
153+
#[inline]
154+
fn is_zero(&self) -> bool {
155+
const {
156+
let none: Self = unsafe { core::mem::MaybeUninit::zeroed().assume_init() };
157+
assert!(none.is_none());
158+
}
159+
self.is_none()
160+
}
161+
}
162+
)+};
163+
}
164+
165+
impl_is_zero_option_of_num!(u8, u16, u32, u64, u128, i8, i16, i32, i64, i128, usize, isize,);
166+
150167
unsafe impl<T: IsZero> IsZero for Wrapping<T> {
151168
#[inline]
152169
fn is_zero(&self) -> bool {

tests/codegen/vec-calloc.rs

+17
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,23 @@ pub fn vec_option_bool(n: usize) -> Vec<Option<bool>> {
161161
vec![Some(false); n]
162162
}
163163

164+
// CHECK-LABEL: @vec_option_i32
165+
#[no_mangle]
166+
pub fn vec_option_i32(n: usize) -> Vec<Option<i32>> {
167+
// CHECK-NOT: call {{.*}}alloc::vec::from_elem
168+
// CHECK-NOT: call {{.*}}reserve
169+
// CHECK-NOT: call {{.*}}__rust_alloc(
170+
171+
// CHECK: call {{.*}}__rust_alloc_zeroed(
172+
173+
// CHECK-NOT: call {{.*}}alloc::vec::from_elem
174+
// CHECK-NOT: call {{.*}}reserve
175+
// CHECK-NOT: call {{.*}}__rust_alloc(
176+
177+
// CHECK: ret void
178+
vec![None; n]
179+
}
180+
164181
// Ensure that __rust_alloc_zeroed gets the right attributes for LLVM to optimize it away.
165182
// CHECK: declare noalias ptr @__rust_alloc_zeroed(i64, i64 allocalign) unnamed_addr [[RUST_ALLOC_ZEROED_ATTRS:#[0-9]+]]
166183

0 commit comments

Comments
 (0)