Skip to content

Commit 527cd52

Browse files
committed
device_path: make device paths suck less (part 2)
1 parent 66babb2 commit 527cd52

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Changelog
22

33
## uefi - [Unreleased]
4+
- added `DevicePath::to_boxed`, `DevicePath::to_owned`, and `DevicePath::raw`
5+
- added `DevicePathInstance::to_boxed`, `DevicePathInstance::to_owned`, and `DevicePathInstance::raw`
46

57
## uefi-macros - [Unreleased]
68

uefi/src/fs/file_system/error.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::fs::{PathBuf, PathError};
22
use alloc::string::FromUtf8Error;
3-
use core::fmt::Debug;
4-
use core::fmt::{self, Display, Formatter};
3+
use core::fmt::{self, Debug, Display, Formatter};
54

65
/// All errors that can happen when working with the [`FileSystem`].
76
///

uefi/src/proto/device_path/mod.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ mod device_path_gen;
8080
pub use device_path_gen::{
8181
acpi, bios_boot_spec, end, hardware, media, messaging, DevicePathNodeEnum,
8282
};
83+
#[cfg(feature = "alloc")]
84+
use {alloc::borrow::ToOwned, alloc::boxed::Box};
8385

8486
use crate::proto::{unsafe_protocol, ProtocolPointer};
8587
use core::ffi::c_void;
@@ -226,6 +228,20 @@ impl DevicePathInstance {
226228
stop_condition: StopCondition::AnyEndNode,
227229
}
228230
}
231+
232+
/// Returns a copy to the underlying slice of bytes.
233+
#[must_use]
234+
pub const fn raw(&self) -> &[u8] {
235+
&self.data
236+
}
237+
238+
/// Returns a boxed copy of that value.
239+
#[cfg(feature = "alloc")]
240+
pub fn to_boxed(&self) -> Box<Self> {
241+
let data = self.data.to_owned();
242+
let data = data.into_boxed_slice();
243+
unsafe { mem::transmute(data) }
244+
}
229245
}
230246

231247
impl Debug for DevicePathInstance {
@@ -242,6 +258,15 @@ impl PartialEq for DevicePathInstance {
242258
}
243259
}
244260

261+
#[cfg(feature = "alloc")]
262+
impl ToOwned for DevicePathInstance {
263+
type Owned = Box<DevicePathInstance>;
264+
265+
fn to_owned(&self) -> Self::Owned {
266+
self.to_boxed()
267+
}
268+
}
269+
245270
/// Device path protocol.
246271
///
247272
/// A device path contains one or more device path instances made of up
@@ -326,6 +351,20 @@ impl DevicePath {
326351
stop_condition: StopCondition::EndEntireNode,
327352
}
328353
}
354+
355+
/// Returns a copy to the underlying slice of bytes.
356+
#[must_use]
357+
pub const fn raw(&self) -> &[u8] {
358+
&self.data
359+
}
360+
361+
/// Returns a boxed copy of that value.
362+
#[cfg(feature = "alloc")]
363+
pub fn to_boxed(&self) -> Box<Self> {
364+
let data = self.data.to_owned();
365+
let data = data.into_boxed_slice();
366+
unsafe { mem::transmute(data) }
367+
}
329368
}
330369

331370
impl Debug for DevicePath {
@@ -342,6 +381,15 @@ impl PartialEq for DevicePath {
342381
}
343382
}
344383

384+
#[cfg(feature = "alloc")]
385+
impl ToOwned for DevicePath {
386+
type Owned = Box<DevicePath>;
387+
388+
fn to_owned(&self) -> Self::Owned {
389+
self.to_boxed()
390+
}
391+
}
392+
345393
/// Iterator over the [`DevicePathInstance`]s in a [`DevicePath`].
346394
///
347395
/// This struct is returned by [`DevicePath::instance_iter`].
@@ -644,6 +692,7 @@ impl Deref for LoadedImageDevicePath {
644692
mod tests {
645693
use super::*;
646694
use alloc::vec::Vec;
695+
use core::mem::size_of;
647696

648697
/// Create a node to `path` from raw data.
649698
fn add_node(path: &mut Vec<u8>, device_type: u8, sub_type: u8, node_data: &[u8]) {
@@ -748,4 +797,16 @@ mod tests {
748797
// Only two instances.
749798
assert!(iter.next().is_none());
750799
}
800+
801+
#[test]
802+
fn test_to_owned() {
803+
assert_eq!(size_of::<&DevicePath>(), size_of::<&[u8]>());
804+
805+
let raw_data = create_raw_device_path();
806+
let dp = unsafe { DevicePath::from_ffi_ptr(raw_data.as_ptr().cast()) };
807+
808+
let owned_dp = dp.to_owned();
809+
let owned_dp_ref = &*owned_dp;
810+
assert_eq!(owned_dp_ref, dp)
811+
}
751812
}

0 commit comments

Comments
 (0)