Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 840c4e2

Browse files
committed
refactor: moved IsZero into is_zero.rs
1 parent 2a12489 commit 840c4e2

File tree

2 files changed

+75
-70
lines changed

2 files changed

+75
-70
lines changed

library/alloc/src/vec/is_zero.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
use crate::boxed::Box;
2+
3+
#[rustc_specialization_trait]
4+
pub(super) unsafe trait IsZero {
5+
/// Whether this value is zero
6+
fn is_zero(&self) -> bool;
7+
}
8+
9+
macro_rules! impl_is_zero {
10+
($t:ty, $is_zero:expr) => {
11+
unsafe impl IsZero for $t {
12+
#[inline]
13+
fn is_zero(&self) -> bool {
14+
$is_zero(*self)
15+
}
16+
}
17+
};
18+
}
19+
20+
impl_is_zero!(i16, |x| x == 0);
21+
impl_is_zero!(i32, |x| x == 0);
22+
impl_is_zero!(i64, |x| x == 0);
23+
impl_is_zero!(i128, |x| x == 0);
24+
impl_is_zero!(isize, |x| x == 0);
25+
26+
impl_is_zero!(u16, |x| x == 0);
27+
impl_is_zero!(u32, |x| x == 0);
28+
impl_is_zero!(u64, |x| x == 0);
29+
impl_is_zero!(u128, |x| x == 0);
30+
impl_is_zero!(usize, |x| x == 0);
31+
32+
impl_is_zero!(bool, |x| x == false);
33+
impl_is_zero!(char, |x| x == '\0');
34+
35+
impl_is_zero!(f32, |x: f32| x.to_bits() == 0);
36+
impl_is_zero!(f64, |x: f64| x.to_bits() == 0);
37+
38+
unsafe impl<T> IsZero for *const T {
39+
#[inline]
40+
fn is_zero(&self) -> bool {
41+
(*self).is_null()
42+
}
43+
}
44+
45+
unsafe impl<T> IsZero for *mut T {
46+
#[inline]
47+
fn is_zero(&self) -> bool {
48+
(*self).is_null()
49+
}
50+
}
51+
52+
// `Option<&T>` and `Option<Box<T>>` are guaranteed to represent `None` as null.
53+
// For fat pointers, the bytes that would be the pointer metadata in the `Some`
54+
// variant are padding in the `None` variant, so ignoring them and
55+
// zero-initializing instead is ok.
56+
// `Option<&mut T>` never implements `Clone`, so there's no need for an impl of
57+
// `SpecFromElem`.
58+
59+
unsafe impl<T: ?Sized> IsZero for Option<&T> {
60+
#[inline]
61+
fn is_zero(&self) -> bool {
62+
self.is_none()
63+
}
64+
}
65+
66+
unsafe impl<T: ?Sized> IsZero for Option<Box<T>> {
67+
#[inline]
68+
fn is_zero(&self) -> bool {
69+
self.is_none()
70+
}
71+
}

library/alloc/src/vec/mod.rs

Lines changed: 4 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ pub (crate) use self::into_iter::AsIntoIter;
9797

9898
mod into_iter;
9999

100+
use self::is_zero::IsZero;
101+
102+
mod is_zero;
103+
100104
/// A contiguous growable array type, written `Vec<T>` but pronounced 'vector'.
101105
///
102106
/// # Examples
@@ -2042,76 +2046,6 @@ impl<T: Clone + IsZero> SpecFromElem for T {
20422046
}
20432047
}
20442048

2045-
#[rustc_specialization_trait]
2046-
unsafe trait IsZero {
2047-
/// Whether this value is zero
2048-
fn is_zero(&self) -> bool;
2049-
}
2050-
2051-
macro_rules! impl_is_zero {
2052-
($t:ty, $is_zero:expr) => {
2053-
unsafe impl IsZero for $t {
2054-
#[inline]
2055-
fn is_zero(&self) -> bool {
2056-
$is_zero(*self)
2057-
}
2058-
}
2059-
};
2060-
}
2061-
2062-
impl_is_zero!(i16, |x| x == 0);
2063-
impl_is_zero!(i32, |x| x == 0);
2064-
impl_is_zero!(i64, |x| x == 0);
2065-
impl_is_zero!(i128, |x| x == 0);
2066-
impl_is_zero!(isize, |x| x == 0);
2067-
2068-
impl_is_zero!(u16, |x| x == 0);
2069-
impl_is_zero!(u32, |x| x == 0);
2070-
impl_is_zero!(u64, |x| x == 0);
2071-
impl_is_zero!(u128, |x| x == 0);
2072-
impl_is_zero!(usize, |x| x == 0);
2073-
2074-
impl_is_zero!(bool, |x| x == false);
2075-
impl_is_zero!(char, |x| x == '\0');
2076-
2077-
impl_is_zero!(f32, |x: f32| x.to_bits() == 0);
2078-
impl_is_zero!(f64, |x: f64| x.to_bits() == 0);
2079-
2080-
unsafe impl<T> IsZero for *const T {
2081-
#[inline]
2082-
fn is_zero(&self) -> bool {
2083-
(*self).is_null()
2084-
}
2085-
}
2086-
2087-
unsafe impl<T> IsZero for *mut T {
2088-
#[inline]
2089-
fn is_zero(&self) -> bool {
2090-
(*self).is_null()
2091-
}
2092-
}
2093-
2094-
// `Option<&T>` and `Option<Box<T>>` are guaranteed to represent `None` as null.
2095-
// For fat pointers, the bytes that would be the pointer metadata in the `Some`
2096-
// variant are padding in the `None` variant, so ignoring them and
2097-
// zero-initializing instead is ok.
2098-
// `Option<&mut T>` never implements `Clone`, so there's no need for an impl of
2099-
// `SpecFromElem`.
2100-
2101-
unsafe impl<T: ?Sized> IsZero for Option<&T> {
2102-
#[inline]
2103-
fn is_zero(&self) -> bool {
2104-
self.is_none()
2105-
}
2106-
}
2107-
2108-
unsafe impl<T: ?Sized> IsZero for Option<Box<T>> {
2109-
#[inline]
2110-
fn is_zero(&self) -> bool {
2111-
self.is_none()
2112-
}
2113-
}
2114-
21152049
////////////////////////////////////////////////////////////////////////////////
21162050
// Common trait implementations for Vec
21172051
////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)