std_fork/
zeroable.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2// Copyright (c) 2019 Daniel "Lokathor" Gee
3//
4// This file is derived from `bytemuck` version 1.22.0,
5// available at https://crates.io/crates/bytemuck
6//
7// - https://docs.rs/bytemuck/1.22.0/src/bytemuck/zeroable.rs.html#25-35
8//
9// Copyright (c) 2019 Daniel "Lokathor" Gee
10// Licensed under either of Apache License, Version 2.0 or MIT license at your option.
11//
12// See the original license texts at:
13// - http://www.apache.org/licenses/LICENSE-2.0
14// - https://opensource.org/licenses/MIT
15
16/// Marker Trait for types that can be safely created with
17/// [`zeroed`](core::mem::zeroed).
18///
19/// An all-zeroes value may or may not be the same value as the
20/// [Default](core::default::Default) value of the type.
21///
22/// ## Safety
23///
24/// * Your type must be inhabited (eg: no
25///   [Infallible](core::convert::Infallible)).
26/// * Your type must be allowed to be an "all zeroes" bit pattern (eg: no
27///   [`NonNull<T>`](core::ptr::NonNull)).
28///
29pub unsafe trait Zeroable: Sized {}
30unsafe impl Zeroable for () {}
31unsafe impl Zeroable for bool {}
32unsafe impl Zeroable for char {}
33unsafe impl Zeroable for u8 {}
34unsafe impl Zeroable for i8 {}
35unsafe impl Zeroable for u16 {}
36unsafe impl Zeroable for i16 {}
37unsafe impl Zeroable for u32 {}
38unsafe impl Zeroable for i32 {}
39unsafe impl Zeroable for u64 {}
40unsafe impl Zeroable for i64 {}
41unsafe impl Zeroable for usize {}
42unsafe impl Zeroable for isize {}
43unsafe impl Zeroable for u128 {}
44unsafe impl Zeroable for i128 {}
45unsafe impl Zeroable for f32 {}
46unsafe impl Zeroable for f64 {}
47unsafe impl<T: Zeroable> Zeroable for core::num::Wrapping<T> {}
48unsafe impl<T: Zeroable> Zeroable for core::cmp::Reverse<T> {}
49unsafe impl<T: Zeroable> Zeroable for core::num::Saturating<T> {}
50
51// Note: we can't implement this for all `T: ?Sized` types because it would
52// create NULL pointers for vtables.
53// Maybe one day this could be changed to be implemented for
54// `T: ?Sized where <T as core::ptr::Pointee>::Metadata: Zeroable`.
55unsafe impl<T> Zeroable for *mut T {}
56unsafe impl<T> Zeroable for *const T {}
57unsafe impl<T> Zeroable for *mut [T] {}
58unsafe impl<T> Zeroable for *const [T] {}
59unsafe impl Zeroable for *mut str {}
60unsafe impl Zeroable for *const str {}
61
62unsafe impl<T: ?Sized> Zeroable for core::marker::PhantomData<T> {}
63unsafe impl Zeroable for core::marker::PhantomPinned {}
64unsafe impl<T: Zeroable> Zeroable for core::mem::ManuallyDrop<T> {}
65unsafe impl<T: Zeroable> Zeroable for core::cell::UnsafeCell<T> {}
66unsafe impl<T: Zeroable> Zeroable for core::cell::Cell<T> {}
67
68unsafe impl<A: Zeroable> Zeroable for (A,) {}
69unsafe impl<A: Zeroable, B: Zeroable> Zeroable for (A, B) {}
70unsafe impl<A: Zeroable, B: Zeroable, C: Zeroable> Zeroable for (A, B, C) {}
71unsafe impl<A: Zeroable, B: Zeroable, C: Zeroable, D: Zeroable> Zeroable for (A, B, C, D) {}
72unsafe impl<A: Zeroable, B: Zeroable, C: Zeroable, D: Zeroable, E: Zeroable> Zeroable
73    for (A, B, C, D, E)
74{
75}
76unsafe impl<A: Zeroable, B: Zeroable, C: Zeroable, D: Zeroable, E: Zeroable, F: Zeroable> Zeroable
77    for (A, B, C, D, E, F)
78{
79}
80unsafe impl<
81    A: Zeroable,
82    B: Zeroable,
83    C: Zeroable,
84    D: Zeroable,
85    E: Zeroable,
86    F: Zeroable,
87    G: Zeroable,
88> Zeroable for (A, B, C, D, E, F, G)
89{
90}
91unsafe impl<
92    A: Zeroable,
93    B: Zeroable,
94    C: Zeroable,
95    D: Zeroable,
96    E: Zeroable,
97    F: Zeroable,
98    G: Zeroable,
99    H: Zeroable,
100> Zeroable for (A, B, C, D, E, F, G, H)
101{
102}
103
104unsafe impl<T, const N: usize> Zeroable for [T; N] where T: Zeroable {}