Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a const zeroed() #240

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Expand All @@ -59,6 +61,7 @@ features = [
"min_const_generics",
"wasm_simd",
"must_cast",
"const_zeroed",
]

[package.metadata.playground]
Expand All @@ -72,4 +75,5 @@ features = [
"min_const_generics",
"wasm_simd",
"must_cast",
"const_zeroed",
]
7 changes: 4 additions & 3 deletions src/allocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -286,7 +287,7 @@ pub fn try_cast_vec<A: NoUninit, B: AnyBitPattern>(
pub fn pod_collect_to_vec<A: NoUninit, B: NoUninit + AnyBitPattern>(
src: &[A],
) -> Vec<B> {
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::<B>()
Expand Down Expand Up @@ -512,7 +513,7 @@ pub trait TransparentWrapperAlloc<Inner: ?Sized>:
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();
Expand Down Expand Up @@ -617,7 +618,7 @@ pub trait TransparentWrapperAlloc<Inner: ?Sized>:
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();
Expand Down
17 changes: 16 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -93,7 +94,12 @@
#[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
Expand Down Expand Up @@ -208,7 +214,7 @@
/// exact.
AlignmentMismatch,
}
#[cfg(not(target_arch = "spirv"))]

Check warning on line 217 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Rust nightly on ubuntu-latest

unexpected `cfg` condition value: `spirv`

Check warning on line 217 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Rust nightly on ubuntu-latest

unexpected `cfg` condition value: `spirv`

Check warning on line 217 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Rust nightly on ubuntu-latest

unexpected `cfg` condition value: `spirv`

Check warning on line 217 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Rust nightly on ubuntu-latest

unexpected `cfg` condition value: `spirv`

Check warning on line 217 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Rust nightly on ubuntu-latest

unexpected `cfg` condition value: `spirv`

Check warning on line 217 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Test with -Zsanitizer=leak

unexpected `cfg` condition value: `spirv`

Check warning on line 217 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Test with -Zsanitizer=leak

unexpected `cfg` condition value: `spirv`

Check warning on line 217 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Test with -Zsanitizer=memory

unexpected `cfg` condition value: `spirv`

Check warning on line 217 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Test with -Zsanitizer=memory

unexpected `cfg` condition value: `spirv`

Check warning on line 217 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Test with -Zsanitizer=address

unexpected `cfg` condition value: `spirv`

Check warning on line 217 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Test with -Zsanitizer=address

unexpected `cfg` condition value: `spirv`

Check warning on line 217 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Test with miri

unexpected `cfg` condition value: `spirv`

Check warning on line 217 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Test with miri

unexpected `cfg` condition value: `spirv`

Check warning on line 217 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Test with miri

unexpected `cfg` condition value: `spirv`

Check warning on line 217 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Test with miri

unexpected `cfg` condition value: `spirv`

Check warning on line 217 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Test with miri

unexpected `cfg` condition value: `spirv`

Check warning on line 217 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Test on i686-unknown-linux-gnu with cross

unexpected `cfg` condition value: `spirv`

Check warning on line 217 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Test on i686-unknown-linux-gnu with cross

unexpected `cfg` condition value: `spirv`
impl core::fmt::Display for PodCastError {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(f, "{:?}", self)
Expand Down Expand Up @@ -506,3 +512,12 @@
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: Zeroable>() -> T {
unsafe { core::mem::zeroed() }
}
2 changes: 1 addition & 1 deletion src/pod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ unsafe impl<T: 'static> PodInOption for NonNull<T> {}

unsafe impl<T: ?Sized + 'static> Pod for PhantomData<T> {}
unsafe impl Pod for PhantomPinned {}
unsafe impl<T: Pod> Pod for ManuallyDrop<T> {}
unsafe impl<T: Pod> Pod for core::mem::ManuallyDrop<T> {}

// Note(Lokathor): MaybeUninit can NEVER be Pod.

Expand Down
2 changes: 1 addition & 1 deletion src/zeroable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ unsafe impl Zeroable for *const str {}

unsafe impl<T: ?Sized> Zeroable for PhantomData<T> {}
unsafe impl Zeroable for PhantomPinned {}
unsafe impl<T: Zeroable> Zeroable for ManuallyDrop<T> {}
unsafe impl<T: Zeroable> Zeroable for core::mem::ManuallyDrop<T> {}
unsafe impl<T: Zeroable> Zeroable for core::cell::UnsafeCell<T> {}
unsafe impl<T: Zeroable> Zeroable for core::cell::Cell<T> {}

Expand Down
Loading