Skip to content

Drop unstable maybe_uninit_slice and vec_into_raw_parts features #622

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

Merged
merged 1 commit into from
Dec 31, 2022
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
3 changes: 2 additions & 1 deletion uefi/src/data_types/owned_strs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use super::strs::{CStr16, FromSliceWithNulError};
use crate::alloc::vec::Vec;
use crate::data_types::strs::EqStrUntilNul;
use crate::data_types::UnalignedSlice;
use crate::polyfill::vec_into_raw_parts;
use core::fmt;
use core::ops;

Expand Down Expand Up @@ -92,7 +93,7 @@ impl TryFrom<Vec<u16>> for CString16 {
// Safety: `Char16` is a transparent struct wrapping `u16`, so
// the types are compatible. The pattern used here matches the
// example in the docs for `into_raw_parts`.
let (ptr, len, cap) = input.into_raw_parts();
let (ptr, len, cap) = vec_into_raw_parts(input);
let rebuilt = unsafe {
let ptr = ptr.cast::<Char16>();
Vec::from_raw_parts(ptr, len, cap)
Expand Down
3 changes: 2 additions & 1 deletion uefi/src/data_types/strs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::chars::{Char16, Char8, NUL_16, NUL_8};
use super::UnalignedSlice;
use crate::polyfill::maybe_uninit_slice_assume_init_ref;
use core::ffi::CStr;
use core::fmt;
use core::iter::Iterator;
Expand Down Expand Up @@ -300,7 +301,7 @@ impl CStr16 {
src.copy_to_maybe_uninit(buf);
let buf = unsafe {
// Safety: `copy_buf` fully initializes the slice.
MaybeUninit::slice_assume_init_ref(buf)
maybe_uninit_slice_assume_init_ref(buf)
};
CStr16::from_u16_with_nul(buf).map_err(|e| match e {
FromSliceWithNulError::InvalidChar(v) => UnalignedCStr16Error::InvalidChar(v),
Expand Down
4 changes: 2 additions & 2 deletions uefi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@
//! [unstable features]: https://doc.rust-lang.org/unstable-book/

#![feature(abi_efiapi)]
#![feature(maybe_uninit_slice)]
#![cfg_attr(feature = "alloc", feature(vec_into_raw_parts))]
#![cfg_attr(feature = "unstable", feature(error_in_core))]
#![cfg_attr(all(feature = "unstable", feature = "alloc"), feature(allocator_api))]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
Expand Down Expand Up @@ -105,3 +103,5 @@ pub mod logger;
// As long as this is behind "alloc", we can simplify cfg-feature attributes in this module.
#[cfg(feature = "alloc")]
pub(crate) mod mem;

pub(crate) mod polyfill;
29 changes: 29 additions & 0 deletions uefi/src/polyfill.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//! Polyfills for functions in the standard library that are currently gated
//! behind unstable features.

use core::mem::MaybeUninit;
#[cfg(feature = "alloc")]
use {alloc::vec::Vec, core::mem::ManuallyDrop};

/// Polyfill for the unstable `MaybeUninit::slice_assume_init_ref` function.
///
/// See <https://github.com/rust-lang/rust/issues/63569>.
pub const unsafe fn maybe_uninit_slice_assume_init_ref<T>(s: &[MaybeUninit<T>]) -> &[T] {
unsafe { &*(s as *const [MaybeUninit<T>] as *const [T]) }
}

/// Polyfill for the unstable `MaybeUninit::slice_as_mut_ptr` function.
///
/// See <https://github.com/rust-lang/rust/issues/63569>.
pub fn maybe_uninit_slice_as_mut_ptr<T>(s: &mut [MaybeUninit<T>]) -> *mut T {
s.as_mut_ptr().cast::<T>()
}

/// Polyfill for the unstable `Vec::into_raw_parts` function.
///
/// See <https://github.com/rust-lang/rust/issues/65816>.
#[cfg(feature = "alloc")]
pub fn vec_into_raw_parts<T>(v: Vec<T>) -> (*mut T, usize, usize) {
let mut v = ManuallyDrop::new(v);
(v.as_mut_ptr(), v.len(), v.capacity())
}
5 changes: 3 additions & 2 deletions uefi/src/proto/device_path/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

pub use uefi::proto::device_path::device_path_gen::build::*;

use crate::polyfill::{maybe_uninit_slice_as_mut_ptr, maybe_uninit_slice_assume_init_ref};
use crate::proto::device_path::{DevicePath, DevicePathNode};
use core::mem::MaybeUninit;

Expand Down Expand Up @@ -131,7 +132,7 @@ impl<'a> DevicePathBuilder<'a> {

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

let dst: *mut u8 = MaybeUninit::slice_as_mut_ptr(out);
let dst: *mut u8 = maybe_uninit_slice_as_mut_ptr(out);
unsafe {
dst.copy_from_nonoverlapping(src, out.len());
}
Expand Down
Loading