Skip to content

Commit 289279d

Browse files
committed
Auto merge of rust-lang#93455 - asquared31415:vec-zero-opts, r=thomcc
Implement internal `IsZero` for Wrapping and Saturating for `Vec` optimizations This implements the `IsZero` trait for the `Wrapping` and `Saturating` types so that users of these types can get the improved performance from the specialization of creating a `Vec` from a single element repeated when it has a zero bit pattern (example `vec![0_i32; 500]`, or after this PR `vec![Wrapping(0_i32); 500]`) CC rust-lang#60978
2 parents 0ebd3ab + 80e035c commit 289279d

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

library/alloc/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
#![feature(ptr_metadata)]
135135
#![feature(ptr_sub_ptr)]
136136
#![feature(receiver_trait)]
137+
#![feature(saturating_int_impl)]
137138
#![feature(set_ptr_value)]
138139
#![feature(slice_from_ptr_range)]
139140
#![feature(slice_group_by)]

library/alloc/src/vec/is_zero.rs

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use core::num::{Saturating, Wrapping};
2+
13
use crate::boxed::Box;
24

35
#[rustc_specialization_trait]
@@ -144,3 +146,17 @@ impl_is_zero_option_of_nonzero!(
144146
NonZeroUsize,
145147
NonZeroIsize,
146148
);
149+
150+
unsafe impl<T: IsZero> IsZero for Wrapping<T> {
151+
#[inline]
152+
fn is_zero(&self) -> bool {
153+
self.0.is_zero()
154+
}
155+
}
156+
157+
unsafe impl<T: IsZero> IsZero for Saturating<T> {
158+
#[inline]
159+
fn is_zero(&self) -> bool {
160+
self.0.is_zero()
161+
}
162+
}

0 commit comments

Comments
 (0)