-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
284 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
//! Modified implementations of unstable libcore functions. | ||
|
||
#![allow(dead_code)] | ||
|
||
use core::mem::{self, MaybeUninit}; | ||
|
||
/// [`MaybeUninit::slice_assume_init_mut`] | ||
#[inline(always)] | ||
pub(crate) unsafe fn slice_assume_init_mut<T>(slice: &mut [MaybeUninit<T>]) -> &mut [T] { | ||
// SAFETY: similar to safety notes for `slice_get_ref`, but we have a | ||
// mutable reference which is also guaranteed to be valid for writes. | ||
unsafe { &mut *(slice as *mut [MaybeUninit<T>] as *mut [T]) } | ||
} | ||
|
||
/// [`MaybeUninit::uninit_array`] | ||
#[inline] | ||
pub(crate) const fn uninit_array<T, const N: usize>() -> [MaybeUninit<T>; N] { | ||
// SAFETY: An uninitialized `[MaybeUninit<_>; N]` is valid. | ||
unsafe { MaybeUninit::<[MaybeUninit<T>; N]>::uninit().assume_init() } | ||
} | ||
|
||
/// [`MaybeUninit::array_assume_init`] | ||
#[inline] | ||
pub(crate) unsafe fn array_assume_init<T, const N: usize>(array: [MaybeUninit<T>; N]) -> [T; N] { | ||
// SAFETY: | ||
// * The caller guarantees that all elements of the array are initialized | ||
// * `MaybeUninit<T>` and T are guaranteed to have the same layout | ||
// * `MaybeUninit` does not drop, so there are no double-frees | ||
// And thus the conversion is safe | ||
unsafe { transpose(array).assume_init() } | ||
} | ||
|
||
/// [`MaybeUninit::transpose`] | ||
#[inline(always)] | ||
unsafe fn transpose<T, const N: usize>(array: [MaybeUninit<T>; N]) -> MaybeUninit<[T; N]> { | ||
mem::transmute_copy::<[MaybeUninit<T>; N], MaybeUninit<[T; N]>>(&mem::ManuallyDrop::new(&array)) | ||
} |
Oops, something went wrong.