@@ -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,21 @@ 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
+ #[ must_use]
241
+ pub fn to_boxed ( & self ) -> Box < Self > {
242
+ let data = self . data . to_owned ( ) ;
243
+ let data = data. into_boxed_slice ( ) ;
244
+ unsafe { mem:: transmute ( data) }
245
+ }
229
246
}
230
247
231
248
impl Debug for DevicePathInstance {
@@ -242,6 +259,15 @@ impl PartialEq for DevicePathInstance {
242
259
}
243
260
}
244
261
262
+ #[ cfg( feature = "alloc" ) ]
263
+ impl ToOwned for DevicePathInstance {
264
+ type Owned = Box < DevicePathInstance > ;
265
+
266
+ fn to_owned ( & self ) -> Self :: Owned {
267
+ self . to_boxed ( )
268
+ }
269
+ }
270
+
245
271
/// Device path protocol.
246
272
///
247
273
/// A device path contains one or more device path instances made of up
@@ -326,6 +352,21 @@ impl DevicePath {
326
352
stop_condition : StopCondition :: EndEntireNode ,
327
353
}
328
354
}
355
+
356
+ /// Returns a copy to the underlying slice of bytes.
357
+ #[ must_use]
358
+ pub const fn raw ( & self ) -> & [ u8 ] {
359
+ & self . data
360
+ }
361
+
362
+ /// Returns a boxed copy of that value.
363
+ #[ cfg( feature = "alloc" ) ]
364
+ #[ must_use]
365
+ pub fn to_boxed ( & self ) -> Box < Self > {
366
+ let data = self . data . to_owned ( ) ;
367
+ let data = data. into_boxed_slice ( ) ;
368
+ unsafe { mem:: transmute ( data) }
369
+ }
329
370
}
330
371
331
372
impl Debug for DevicePath {
@@ -342,6 +383,15 @@ impl PartialEq for DevicePath {
342
383
}
343
384
}
344
385
386
+ #[ cfg( feature = "alloc" ) ]
387
+ impl ToOwned for DevicePath {
388
+ type Owned = Box < DevicePath > ;
389
+
390
+ fn to_owned ( & self ) -> Self :: Owned {
391
+ self . to_boxed ( )
392
+ }
393
+ }
394
+
345
395
/// Iterator over the [`DevicePathInstance`]s in a [`DevicePath`].
346
396
///
347
397
/// This struct is returned by [`DevicePath::instance_iter`].
@@ -644,6 +694,7 @@ impl Deref for LoadedImageDevicePath {
644
694
mod tests {
645
695
use super :: * ;
646
696
use alloc:: vec:: Vec ;
697
+ use core:: mem:: { size_of, size_of_val} ;
647
698
648
699
/// Create a node to `path` from raw data.
649
700
fn add_node ( path : & mut Vec < u8 > , device_type : u8 , sub_type : u8 , node_data : & [ u8 ] ) {
@@ -748,4 +799,19 @@ mod tests {
748
799
// Only two instances.
749
800
assert ! ( iter. next( ) . is_none( ) ) ;
750
801
}
802
+
803
+ #[ test]
804
+ fn test_to_owned ( ) {
805
+ // Relevant assertion to verify the transmute is fine.
806
+ assert_eq ! ( size_of:: <& DevicePath >( ) , size_of:: <& [ u8 ] >( ) ) ;
807
+
808
+ let raw_data = create_raw_device_path ( ) ;
809
+ let dp = unsafe { DevicePath :: from_ffi_ptr ( raw_data. as_ptr ( ) . cast ( ) ) } ;
810
+ // Relevant assertion to verify the transmute is fine.
811
+ assert_eq ! ( size_of_val( & dp) , size_of_val( & db. data) ) ;
812
+
813
+ let owned_dp = dp. to_owned ( ) ;
814
+ let owned_dp_ref = & * owned_dp;
815
+ assert_eq ! ( owned_dp_ref, dp)
816
+ }
751
817
}
0 commit comments