@@ -80,6 +80,8 @@ mod device_path_gen;
80
80
pub use device_path_gen:: {
81
81
acpi, bios_boot_spec, end, hardware, media, messaging, DevicePathNodeEnum ,
82
82
} ;
83
+ #[ cfg( feature = "alloc" ) ]
84
+ use { alloc:: borrow:: ToOwned , alloc:: boxed:: Box } ;
83
85
84
86
use crate :: proto:: { unsafe_protocol, ProtocolPointer } ;
85
87
use core:: ffi:: c_void;
@@ -226,6 +228,20 @@ impl DevicePathInstance {
226
228
stop_condition : StopCondition :: AnyEndNode ,
227
229
}
228
230
}
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
+ }
229
245
}
230
246
231
247
impl Debug for DevicePathInstance {
@@ -242,6 +258,15 @@ impl PartialEq for DevicePathInstance {
242
258
}
243
259
}
244
260
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
+
245
270
/// Device path protocol.
246
271
///
247
272
/// A device path contains one or more device path instances made of up
@@ -326,6 +351,20 @@ impl DevicePath {
326
351
stop_condition : StopCondition :: EndEntireNode ,
327
352
}
328
353
}
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
+ }
329
368
}
330
369
331
370
impl Debug for DevicePath {
@@ -342,6 +381,15 @@ impl PartialEq for DevicePath {
342
381
}
343
382
}
344
383
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
+
345
393
/// Iterator over the [`DevicePathInstance`]s in a [`DevicePath`].
346
394
///
347
395
/// This struct is returned by [`DevicePath::instance_iter`].
@@ -644,6 +692,7 @@ impl Deref for LoadedImageDevicePath {
644
692
mod tests {
645
693
use super :: * ;
646
694
use alloc:: vec:: Vec ;
695
+ use core:: mem:: size_of;
647
696
648
697
/// Create a node to `path` from raw data.
649
698
fn add_node ( path : & mut Vec < u8 > , device_type : u8 , sub_type : u8 , node_data : & [ u8 ] ) {
@@ -748,4 +797,16 @@ mod tests {
748
797
// Only two instances.
749
798
assert ! ( iter. next( ) . is_none( ) ) ;
750
799
}
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
+ }
751
812
}
0 commit comments