From 70da6d7a772b54d151bef11494a64cbf96cae1d6 Mon Sep 17 00:00:00 2001 From: Lokathor Date: Thu, 19 Sep 2019 19:23:24 -0600 Subject: [PATCH] zeroable --- src/lib.rs | 21 +++++++ src/zeroable.rs | 151 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 172 insertions(+) create mode 100644 src/lib.rs create mode 100644 src/zeroable.rs diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..7d0ae09 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,21 @@ +#[cfg(target_arch = "x86")] +pub(crate) use core::arch::x86; +#[cfg(target_arch = "x86_64")] +pub(crate) use core::arch::x86_64; +// +pub(crate) use core::{marker::*, num::*, ptr::*}; + +macro_rules! impl_unsafe_marker_for_array { + ( $marker:ident , $( $n:expr ),* ) => { + $(unsafe impl $marker for [T; $n] where T: $marker {})* + } +} + +macro_rules! impl_unsafe_marker_for_type { + ( $marker:ident , $( $t:ty ),* ) => { + $(unsafe impl $marker for $t {})* + } +} + +mod zeroable; +pub use zeroable::*; diff --git a/src/zeroable.rs b/src/zeroable.rs new file mode 100644 index 0000000..dd810d8 --- /dev/null +++ b/src/zeroable.rs @@ -0,0 +1,151 @@ +use super::*; + +/// Trait for types that can be safely created with [`zeroed`](core::mem::zeroed). +/// +/// ## Safety +/// +/// * Your type must be _inhabited_ (eg: no +/// [Infallible](core::convert::Infallible)). +/// * Your type must be allowed to be an "all zeroes" bit pattern (eg: no +/// [`NonNull`](core::ptr::NonNull)). +pub unsafe trait Zeroable: Sized { + /// Calls [`zeroed`](core::mem::zeroed). + /// + /// This is a trait method so that you can write `MyType::zeroed()` in your + /// code. It is a contract of this trait that if you implement it on your type + /// you **must not** override this method. + fn zeroed() -> Self { + unsafe { core::mem::zeroed() } + } +} +unsafe impl Zeroable for () {} +unsafe impl Zeroable for bool {} +unsafe impl Zeroable for char {} +unsafe impl Zeroable for u8 {} +unsafe impl Zeroable for i8 {} +unsafe impl Zeroable for u16 {} +unsafe impl Zeroable for i16 {} +unsafe impl Zeroable for u32 {} +unsafe impl Zeroable for i32 {} +unsafe impl Zeroable for u64 {} +unsafe impl Zeroable for i64 {} +unsafe impl Zeroable for usize {} +unsafe impl Zeroable for isize {} +unsafe impl Zeroable for u128 {} +unsafe impl Zeroable for i128 {} +unsafe impl Zeroable for f32 {} +unsafe impl Zeroable for f64 {} + +unsafe impl Zeroable for Option {} +unsafe impl Zeroable for Option {} +unsafe impl Zeroable for Option {} +unsafe impl Zeroable for Option {} +unsafe impl Zeroable for Option {} +unsafe impl Zeroable for Option {} +unsafe impl Zeroable for Option {} +unsafe impl Zeroable for Option {} +unsafe impl Zeroable for Option {} +unsafe impl Zeroable for Option {} +unsafe impl Zeroable for Option {} +unsafe impl Zeroable for Option {} + +unsafe impl Zeroable for *mut T {} +unsafe impl Zeroable for *const T {} +unsafe impl Zeroable for Option> {} +unsafe impl Zeroable for PhantomData where T: Zeroable {} + +unsafe impl Zeroable for (A,) where A: Zeroable {} +unsafe impl Zeroable for (A, B) +where + A: Zeroable, + B: Zeroable, +{ +} +unsafe impl Zeroable for (A, B, C) +where + A: Zeroable, + B: Zeroable, + C: Zeroable, +{ +} +unsafe impl Zeroable for (A, B, C, D) +where + A: Zeroable, + B: Zeroable, + C: Zeroable, + D: Zeroable, +{ +} +unsafe impl Zeroable for (A, B, C, D, E) +where + A: Zeroable, + B: Zeroable, + C: Zeroable, + D: Zeroable, + E: Zeroable, +{ +} +unsafe impl Zeroable for (A, B, C, D, E, F) +where + A: Zeroable, + B: Zeroable, + C: Zeroable, + D: Zeroable, + E: Zeroable, + F: Zeroable, +{ +} +unsafe impl Zeroable for (A, B, C, D, E, F, G) +where + A: Zeroable, + B: Zeroable, + C: Zeroable, + D: Zeroable, + E: Zeroable, + F: Zeroable, + G: Zeroable, +{ +} +unsafe impl Zeroable for (A, B, C, D, E, F, G, H) +where + A: Zeroable, + B: Zeroable, + C: Zeroable, + D: Zeroable, + E: Zeroable, + F: Zeroable, + G: Zeroable, + H: Zeroable, +{ +} + +impl_unsafe_marker_for_array!( + Zeroable, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, + 24, 25, 26, 27, 28, 29, 30, 31, 32, 48, 64, 96, 128, 256, 512, 1024 +); + +#[cfg(target_arch = "x86")] +unsafe impl Zeroable for x86::__m128i {} +#[cfg(target_arch = "x86")] +unsafe impl Zeroable for x86::__m128 {} +#[cfg(target_arch = "x86")] +unsafe impl Zeroable for x86::__m128d {} +#[cfg(target_arch = "x86")] +unsafe impl Zeroable for x86::__m256i {} +#[cfg(target_arch = "x86")] +unsafe impl Zeroable for x86::__m256 {} +#[cfg(target_arch = "x86")] +unsafe impl Zeroable for x86::__m256d {} + +#[cfg(target_arch = "x86_64")] +unsafe impl Zeroable for x86_64::__m128i {} +#[cfg(target_arch = "x86_64")] +unsafe impl Zeroable for x86_64::__m128 {} +#[cfg(target_arch = "x86_64")] +unsafe impl Zeroable for x86_64::__m128d {} +#[cfg(target_arch = "x86_64")] +unsafe impl Zeroable for x86_64::__m256i {} +#[cfg(target_arch = "x86_64")] +unsafe impl Zeroable for x86_64::__m256 {} +#[cfg(target_arch = "x86_64")] +unsafe impl Zeroable for x86_64::__m256d {}