@@ -266,6 +266,19 @@ impl PartialEq for DevicePathNode {
266
266
}
267
267
}
268
268
269
+ impl < ' a > TryFrom < & [ u8 ] > for & ' a DevicePathNode {
270
+ type Error = ByteConversionError ;
271
+
272
+ fn try_from ( bytes : & [ u8 ] ) -> Result < Self , Self :: Error > {
273
+ let dp = <& DevicePathHeader >:: try_from ( bytes) ?;
274
+ if usize:: from ( dp. length ) <= bytes. len ( ) {
275
+ unsafe { Ok ( DevicePathNode :: from_ffi_ptr ( bytes. as_ptr ( ) . cast ( ) ) ) }
276
+ } else {
277
+ Err ( ByteConversionError :: InvalidLength )
278
+ }
279
+ }
280
+ }
281
+
269
282
/// A single device path instance that ends with either an [`END_INSTANCE`]
270
283
/// or [`END_ENTIRE`] node. Use [`DevicePath::instance_iter`] to get the
271
284
/// path instances in a [`DevicePath`].
@@ -975,4 +988,33 @@ mod tests {
975
988
let owned_dp_ref = & * owned_dp;
976
989
assert_eq ! ( owned_dp_ref, dp)
977
990
}
991
+
992
+ #[ test]
993
+ fn test_device_path_node_from_bytes ( ) {
994
+ let mut raw_data = Vec :: new ( ) ;
995
+ let node = [ 0xa0 , 0xb0 ] ;
996
+ let node_data = & [ 10 , 11 ] ;
997
+
998
+ // Raw data is less than size of a [`DevicePathNode`].
999
+ raw_data. push ( node[ 0 ] ) ;
1000
+ assert ! ( <& DevicePathNode >:: try_from( raw_data. as_slice( ) ) . is_err( ) ) ;
1001
+
1002
+ // Raw data is long enough to hold a [`DevicePathNode`].
1003
+ raw_data. push ( node[ 1 ] ) ;
1004
+ raw_data. extend (
1005
+ u16:: try_from ( mem:: size_of :: < DevicePathHeader > ( ) + node_data. len ( ) )
1006
+ . unwrap ( )
1007
+ . to_le_bytes ( ) ,
1008
+ ) ;
1009
+ raw_data. extend ( node_data) ;
1010
+ let dp = <& DevicePathNode >:: try_from ( raw_data. as_slice ( ) ) . unwrap ( ) ;
1011
+
1012
+ // Relevant assertions to verify the conversion is fine.
1013
+ assert_eq ! ( mem:: size_of_val( dp) , 6 ) ;
1014
+ check_node ( dp, 0xa0 , 0xb0 , & [ 10 , 11 ] ) ;
1015
+
1016
+ // [`DevicePathNode`] data length exceeds the raw_data slice.
1017
+ raw_data[ 2 ] += 1 ;
1018
+ assert ! ( <& DevicePathNode >:: try_from( raw_data. as_slice( ) ) . is_err( ) ) ;
1019
+ }
978
1020
}
0 commit comments