Skip to content

Commit a691731

Browse files
Drop unstable maybe_uninit_slice and vec_into_raw_parts features
The functionality we need from those features can be provided with a few pretty simple one-liner functions. Add those functions to the uefi crate in a new polyfill module.
1 parent 761bed9 commit a691731

File tree

9 files changed

+99
-65
lines changed

9 files changed

+99
-65
lines changed

uefi/src/data_types/owned_strs.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use super::strs::{CStr16, FromSliceWithNulError};
33
use crate::alloc::vec::Vec;
44
use crate::data_types::strs::EqStrUntilNul;
55
use crate::data_types::UnalignedSlice;
6+
use crate::polyfill::vec_into_raw_parts;
67
use core::fmt;
78
use core::ops;
89

@@ -92,7 +93,7 @@ impl TryFrom<Vec<u16>> for CString16 {
9293
// Safety: `Char16` is a transparent struct wrapping `u16`, so
9394
// the types are compatible. The pattern used here matches the
9495
// example in the docs for `into_raw_parts`.
95-
let (ptr, len, cap) = input.into_raw_parts();
96+
let (ptr, len, cap) = vec_into_raw_parts(input);
9697
let rebuilt = unsafe {
9798
let ptr = ptr.cast::<Char16>();
9899
Vec::from_raw_parts(ptr, len, cap)

uefi/src/data_types/strs.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::chars::{Char16, Char8, NUL_16, NUL_8};
22
use super::UnalignedSlice;
3+
use crate::polyfill::maybe_uninit_slice_assume_init_ref;
34
use core::ffi::CStr;
45
use core::fmt;
56
use core::iter::Iterator;
@@ -300,7 +301,7 @@ impl CStr16 {
300301
src.copy_to_maybe_uninit(buf);
301302
let buf = unsafe {
302303
// Safety: `copy_buf` fully initializes the slice.
303-
MaybeUninit::slice_assume_init_ref(buf)
304+
maybe_uninit_slice_assume_init_ref(buf)
304305
};
305306
CStr16::from_u16_with_nul(buf).map_err(|e| match e {
306307
FromSliceWithNulError::InvalidChar(v) => UnalignedCStr16Error::InvalidChar(v),

uefi/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@
5959
//! [unstable features]: https://doc.rust-lang.org/unstable-book/
6060
6161
#![feature(abi_efiapi)]
62-
#![feature(maybe_uninit_slice)]
63-
#![cfg_attr(feature = "alloc", feature(vec_into_raw_parts))]
6462
#![cfg_attr(feature = "unstable", feature(error_in_core))]
6563
#![cfg_attr(all(feature = "unstable", feature = "alloc"), feature(allocator_api))]
6664
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
@@ -105,3 +103,5 @@ pub mod logger;
105103
// As long as this is behind "alloc", we can simplify cfg-feature attributes in this module.
106104
#[cfg(feature = "alloc")]
107105
pub(crate) mod mem;
106+
107+
pub(crate) mod polyfill;

uefi/src/polyfill.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//! Polyfills for functions in the standard library that are currently gated
2+
//! behind unstable features.
3+
4+
use core::mem::MaybeUninit;
5+
#[cfg(feature = "alloc")]
6+
use {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>.
11+
pub 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>.
18+
pub 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")]
26+
pub 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+
}

uefi/src/proto/device_path/build.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
88
pub use uefi::proto::device_path::device_path_gen::build::*;
99

10+
use crate::polyfill::{maybe_uninit_slice_as_mut_ptr, maybe_uninit_slice_assume_init_ref};
1011
use crate::proto::device_path::{DevicePath, DevicePathNode};
1112
use core::mem::MaybeUninit;
1213

@@ -131,7 +132,7 @@ impl<'a> DevicePathBuilder<'a> {
131132

132133
let data: &[u8] = match &this.storage {
133134
BuilderStorage::Buf { buf, offset } => unsafe {
134-
MaybeUninit::slice_assume_init_ref(&buf[..*offset])
135+
maybe_uninit_slice_assume_init_ref(&buf[..*offset])
135136
},
136137
#[cfg(feature = "alloc")]
137138
BuilderStorage::Vec(vec) => vec,
@@ -206,7 +207,7 @@ unsafe impl BuildNode for &DevicePathNode {
206207
fn write_data(&self, out: &mut [MaybeUninit<u8>]) {
207208
let src: *const u8 = self.as_ffi_ptr().cast();
208209

209-
let dst: *mut u8 = MaybeUninit::slice_as_mut_ptr(out);
210+
let dst: *mut u8 = maybe_uninit_slice_as_mut_ptr(out);
210211
unsafe {
211212
dst.copy_from_nonoverlapping(src, out.len());
212213
}

0 commit comments

Comments
 (0)