bytemuck/
zeroable_in_option.rs

1use super::*;
2
3// Note(Lokathor): This is the neat part!!
4unsafe impl<T: ZeroableInOption> Zeroable for Option<T> {}
5
6/// Trait for types which are [Zeroable](Zeroable) when wrapped in
7/// [Option](core::option::Option).
8///
9/// ## Safety
10///
11/// * `Option<YourType>` must uphold the same invariants as
12///   [Zeroable](Zeroable).
13pub unsafe trait ZeroableInOption: Sized {}
14
15unsafe impl ZeroableInOption for NonZeroI8 {}
16unsafe impl ZeroableInOption for NonZeroI16 {}
17unsafe impl ZeroableInOption for NonZeroI32 {}
18unsafe impl ZeroableInOption for NonZeroI64 {}
19unsafe impl ZeroableInOption for NonZeroI128 {}
20unsafe impl ZeroableInOption for NonZeroIsize {}
21unsafe impl ZeroableInOption for NonZeroU8 {}
22unsafe impl ZeroableInOption for NonZeroU16 {}
23unsafe impl ZeroableInOption for NonZeroU32 {}
24unsafe impl ZeroableInOption for NonZeroU64 {}
25unsafe impl ZeroableInOption for NonZeroU128 {}
26unsafe impl ZeroableInOption for NonZeroUsize {}
27
28// Note: this does not create NULL vtable because we get `None` anyway.
29unsafe impl<T: ?Sized> ZeroableInOption for NonNull<T> {}
30unsafe impl<T: ?Sized> ZeroableInOption for &'_ T {}
31unsafe impl<T: ?Sized> ZeroableInOption for &'_ mut T {}
32
33#[cfg(feature = "extern_crate_alloc")]
34#[cfg_attr(feature = "nightly_docs", doc(cfg(feature = "extern_crate_alloc")))]
35unsafe impl<T: ?Sized> ZeroableInOption for alloc::boxed::Box<T> {}
36
37#[cfg(feature = "zeroable_unwind_fn")]
38macro_rules! impl_for_unwind_fn {
39    ($($ArgTy:ident),* $(,)?) => {
40        unsafe impl<$($ArgTy,)* R> ZeroableInOption for extern "C-unwind" fn($($ArgTy,)*) -> R {}
41        unsafe impl<$($ArgTy,)* R> ZeroableInOption for unsafe extern "C-unwind" fn($($ArgTy,)*) -> R {}
42        unsafe impl<$($ArgTy,)* R> ZeroableInOption for extern "system-unwind" fn($($ArgTy,)*) -> R {}
43        unsafe impl<$($ArgTy,)* R> ZeroableInOption for unsafe extern "system-unwind" fn($($ArgTy,)*) -> R {}
44    };
45}
46
47macro_rules! impl_for_fn {
48    ($($ArgTy:ident),* $(,)?) => {
49        unsafe impl<$($ArgTy,)* R> ZeroableInOption for fn($($ArgTy,)*) -> R {}
50        unsafe impl<$($ArgTy,)* R> ZeroableInOption for unsafe fn($($ArgTy,)*) -> R {}
51        unsafe impl<$($ArgTy,)* R> ZeroableInOption for extern "C" fn($($ArgTy,)*) -> R {}
52        unsafe impl<$($ArgTy,)* R> ZeroableInOption for unsafe extern "C" fn($($ArgTy,)*) -> R {}
53        unsafe impl<$($ArgTy,)* R> ZeroableInOption for extern "system" fn($($ArgTy,)*) -> R {}
54        unsafe impl<$($ArgTy,)* R> ZeroableInOption for unsafe extern "system" fn($($ArgTy,)*) -> R {}
55        #[cfg(target_os="windows")]
56        unsafe impl<$($ArgTy,)* R> ZeroableInOption for extern "stdcall" fn($($ArgTy,)*) -> R {}
57        #[cfg(target_os="windows")]
58        unsafe impl<$($ArgTy,)* R> ZeroableInOption for unsafe extern "stdcall" fn($($ArgTy,)*) -> R {}
59        #[cfg(feature = "zeroable_unwind_fn")]
60        impl_for_unwind_fn!($($ArgTy),*);
61    };
62}
63
64impl_for_fn!();
65impl_for_fn!(A);
66impl_for_fn!(A, B);
67impl_for_fn!(A, B, C);
68impl_for_fn!(A, B, C, D);
69impl_for_fn!(A, B, C, D, E);
70impl_for_fn!(A, B, C, D, E, F);
71impl_for_fn!(A, B, C, D, E, F, G);
72impl_for_fn!(A, B, C, D, E, F, G, H);
73impl_for_fn!(A, B, C, D, E, F, G, H, I);
74impl_for_fn!(A, B, C, D, E, F, G, H, I, J);
75impl_for_fn!(A, B, C, D, E, F, G, H, I, J, K);
76impl_for_fn!(A, B, C, D, E, F, G, H, I, J, K, L);
77impl_for_fn!(A, B, C, D, E, F, G, H, I, J, K, L, M);