1//! Polyfills for functions in the standard library that are currently gated
2//! behind unstable features.
3
4use core::mem::MaybeUninit;
5#[cfg(feature = "alloc")]
6use {alloc::vec::Vec, core::mem::ManuallyDrop};
7
8/// Polyfill for the unstable `MaybeUninit::slice_assume_init_ref` function.
9///
10/// See <https://github.com/rust-lang/rust/issues/63569>.
11pub const unsafe fn maybe_uninit_slice_assume_init_ref<T>(s: &[MaybeUninit<T>]) -> &[T] {
12 unsafe { &*(s as *const [MaybeUninit<T>] as *const [T]) }
13}
14
15/// Polyfill for the unstable `MaybeUninit::slice_as_mut_ptr` function.
16///
17/// See <https://github.com/rust-lang/rust/issues/63569>.
18pub fn maybe_uninit_slice_as_mut_ptr<T>(s: &mut [MaybeUninit<T>]) -> *mut T {
19 s.as_mut_ptr().cast::<T>()
20}
21
22/// Polyfill for the unstable `Vec::into_raw_parts` function.
23///
24/// See <https://github.com/rust-lang/rust/issues/65816>.
25#[cfg(feature = "alloc")]
26pub fn vec_into_raw_parts<T>(v: Vec<T>) -> (*mut T, usize, usize) {
27 let mut v = ManuallyDrop::new(v);
28 (v.as_mut_ptr(), v.len(), v.capacity())
29}
30