From 7ec260fd4f4c93c9740845c8d3dc713699d1c929 Mon Sep 17 00:00:00 2001 From: Tyler Ruckinger Date: Mon, 13 May 2024 08:03:26 -0400 Subject: [PATCH] support const zeroed() --- Cargo.toml | 4 ++++ src/allocation.rs | 7 ++++--- src/lib.rs | 17 ++++++++++++++++- src/pod.rs | 2 +- src/zeroable.rs | 2 +- 5 files changed, 26 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1180a90..2428d59 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,6 +34,8 @@ aarch64_simd = [] # MSRV 1.59.0: support aarch64 simd types must_cast = [] # MSRV 1.57.0: support the `must` module. +const_zeroed = [] # MSRV 1.75.0: support const `zeroed()` + # Do not use if you can avoid it, because this is **unsound**!!!! unsound_ptr_pod_impl = [] @@ -59,6 +61,7 @@ features = [ "min_const_generics", "wasm_simd", "must_cast", + "const_zeroed", ] [package.metadata.playground] @@ -72,4 +75,5 @@ features = [ "min_const_generics", "wasm_simd", "must_cast", + "const_zeroed", ] diff --git a/src/allocation.rs b/src/allocation.rs index 047f396..83f11b8 100644 --- a/src/allocation.rs +++ b/src/allocation.rs @@ -19,6 +19,7 @@ use alloc::{ vec, vec::Vec, }; +use core::mem::ManuallyDrop; use core::ops::{Deref, DerefMut}; /// As [`try_cast_box`](try_cast_box), but unwraps for you. @@ -286,7 +287,7 @@ pub fn try_cast_vec( pub fn pod_collect_to_vec( src: &[A], ) -> Vec { - let src_size = size_of_val(src); + let src_size = core::mem::size_of_val(src); // Note(Lokathor): dst_count is rounded up so that the dest will always be at // least as many bytes as the src. let dst_count = src_size / size_of::() @@ -512,7 +513,7 @@ pub trait TransparentWrapperAlloc: Self: Sized, Inner: Sized, { - let mut s = core::mem::ManuallyDrop::new(s); + let mut s = ManuallyDrop::new(s); let length = s.len(); let capacity = s.capacity(); @@ -617,7 +618,7 @@ pub trait TransparentWrapperAlloc: Self: Sized, Inner: Sized, { - let mut s = core::mem::ManuallyDrop::new(s); + let mut s = ManuallyDrop::new(s); let length = s.len(); let capacity = s.capacity(); diff --git a/src/lib.rs b/src/lib.rs index a810bb4..07995c1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -83,6 +83,7 @@ //! instead of just for a select list of array lengths. //! * `must_cast`: Provides the `must_` functions, which will compile error if //! the requested cast can't be statically verified. +//! * `const_zeroed`: Provides the const [`zeroed`] function. #[cfg(all(target_arch = "aarch64", feature = "aarch64_simd"))] use core::arch::aarch64; @@ -93,7 +94,12 @@ use core::arch::x86; #[cfg(target_arch = "x86_64")] use core::arch::x86_64; // -use core::{marker::*, mem::*, num::*, ptr::*}; +use core::{ + marker::*, + mem::{align_of, size_of}, + num::*, + ptr::*, +}; // Used from macros to ensure we aren't using some locally defined name and // actually are referencing libcore. This also would allow pre-2018 edition @@ -506,3 +512,12 @@ pub fn fill_zeroes(slice: &mut [T]) { unsafe { core::ptr::write_bytes(slice.as_mut_ptr() as *mut u8, 0u8, len) } } } + +/// Initialize a zeroed `T`. +/// +/// Like [`Zeroable::zeroed`], but supports const. +#[cfg(feature = "const_zeroed")] +#[inline] +pub const fn zeroed() -> T { + unsafe { core::mem::zeroed() } +} diff --git a/src/pod.rs b/src/pod.rs index 2cec1c2..724c0a0 100644 --- a/src/pod.rs +++ b/src/pod.rs @@ -74,7 +74,7 @@ unsafe impl PodInOption for NonNull {} unsafe impl Pod for PhantomData {} unsafe impl Pod for PhantomPinned {} -unsafe impl Pod for ManuallyDrop {} +unsafe impl Pod for core::mem::ManuallyDrop {} // Note(Lokathor): MaybeUninit can NEVER be Pod. diff --git a/src/zeroable.rs b/src/zeroable.rs index b64a9bf..f7d7ed8 100644 --- a/src/zeroable.rs +++ b/src/zeroable.rs @@ -66,7 +66,7 @@ unsafe impl Zeroable for *const str {} unsafe impl Zeroable for PhantomData {} unsafe impl Zeroable for PhantomPinned {} -unsafe impl Zeroable for ManuallyDrop {} +unsafe impl Zeroable for core::mem::ManuallyDrop {} unsafe impl Zeroable for core::cell::UnsafeCell {} unsafe impl Zeroable for core::cell::Cell {}