Skip to content

Commit a8fc80c

Browse files
committed
uefi: helpers: Introduce OwnedDevicePath
This PR is split off from rust-lang#135368 to reduce noise. Also implement Debug for OwnedDevicePath for some quality of life improvements. Signed-off-by: Ayush Singh <[email protected]>
1 parent 12445e0 commit a8fc80c

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

Diff for: library/std/src/sys/pal/uefi/helpers.rs

+14-6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use r_efi::protocols::{device_path, device_path_to_text, shell};
1414

1515
use crate::ffi::{OsStr, OsString};
1616
use crate::io::{self, const_error};
17+
use crate::iter::Iterator;
1718
use crate::mem::{MaybeUninit, size_of};
1819
use crate::os::uefi::env::boot_services;
1920
use crate::os::uefi::ffi::{OsStrExt, OsStringExt};
@@ -222,14 +223,14 @@ pub(crate) fn runtime_services() -> Option<NonNull<r_efi::efi::RuntimeServices>>
222223
NonNull::new(runtime_services)
223224
}
224225

225-
pub(crate) struct DevicePath(NonNull<r_efi::protocols::device_path::Protocol>);
226+
pub(crate) struct OwnedDevicePath(pub(crate) NonNull<r_efi::protocols::device_path::Protocol>);
226227

227-
impl DevicePath {
228+
impl OwnedDevicePath {
228229
pub(crate) fn from_text(p: &OsStr) -> io::Result<Self> {
229230
fn inner(
230231
p: &OsStr,
231232
protocol: NonNull<r_efi::protocols::device_path_from_text::Protocol>,
232-
) -> io::Result<DevicePath> {
233+
) -> io::Result<OwnedDevicePath> {
233234
let path_vec = p.encode_wide().chain(Some(0)).collect::<Vec<u16>>();
234235
if path_vec[..path_vec.len() - 1].contains(&0) {
235236
return Err(const_error!(
@@ -242,7 +243,7 @@ impl DevicePath {
242243
unsafe { ((*protocol.as_ptr()).convert_text_to_device_path)(path_vec.as_ptr()) };
243244

244245
NonNull::new(path)
245-
.map(DevicePath)
246+
.map(OwnedDevicePath)
246247
.ok_or_else(|| const_error!(io::ErrorKind::InvalidFilename, "Invalid Device Path"))
247248
}
248249

@@ -275,12 +276,12 @@ impl DevicePath {
275276
))
276277
}
277278

278-
pub(crate) fn as_ptr(&self) -> *mut r_efi::protocols::device_path::Protocol {
279+
pub(crate) const fn as_ptr(&self) -> *mut r_efi::protocols::device_path::Protocol {
279280
self.0.as_ptr()
280281
}
281282
}
282283

283-
impl Drop for DevicePath {
284+
impl Drop for OwnedDevicePath {
284285
fn drop(&mut self) {
285286
if let Some(bt) = boot_services() {
286287
let bt: NonNull<r_efi::efi::BootServices> = bt.cast();
@@ -291,6 +292,13 @@ impl Drop for DevicePath {
291292
}
292293
}
293294

295+
impl crate::fmt::Debug for OwnedDevicePath {
296+
fn fmt(&self, f: &mut crate::fmt::Formatter<'_>) -> crate::fmt::Result {
297+
let p = device_path_to_text(self.0).unwrap();
298+
p.fmt(f)
299+
}
300+
}
301+
294302
pub(crate) struct OwnedProtocol<T> {
295303
guid: r_efi::efi::Guid,
296304
handle: NonNull<crate::ffi::c_void>,

Diff for: library/std/src/sys/pal/uefi/process.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ mod uefi_command_internal {
326326

327327
impl Image {
328328
pub fn load_image(p: &OsStr) -> io::Result<Self> {
329-
let path = helpers::DevicePath::from_text(p)?;
329+
let path = helpers::OwnedDevicePath::from_text(p)?;
330330
let boot_services: NonNull<r_efi::efi::BootServices> = boot_services()
331331
.ok_or_else(|| const_error!(io::ErrorKind::NotFound, "Boot Services not found"))?
332332
.cast();

0 commit comments

Comments
 (0)