File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed
Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -76,6 +76,11 @@ impl<'de> Node<'de> {
7676 i : 0 ,
7777 }
7878 }
79+
80+ /// 尝试获得指定属性
81+ pub fn get_prop < ' b > ( & ' b self , name : & str ) -> Option < PropItem < ' b > > {
82+ self . props ( ) . find ( |prop| prop. get_name ( ) == name)
83+ }
7984}
8085
8186impl Debug for Node < ' _ > {
@@ -283,3 +288,28 @@ impl<'de> PropItem<'de> {
283288 . unwrap ( )
284289 }
285290}
291+
292+ #[ cfg( test) ]
293+ mod tests {
294+ use crate :: { buildin:: Node , from_raw_mut, Dtb , DtbPtr } ;
295+ const RAW_DEVICE_TREE : & [ u8 ] = include_bytes ! ( "../../examples/hifive-unmatched-a00.dtb" ) ;
296+ const BUFFER_SIZE : usize = RAW_DEVICE_TREE . len ( ) ;
297+ #[ repr( align( 8 ) ) ]
298+ struct AlignedBuffer {
299+ pub data : [ u8 ; RAW_DEVICE_TREE . len ( ) ] ,
300+ }
301+ #[ test]
302+ fn test_find_prop ( ) {
303+ let mut aligned_data: Box < AlignedBuffer > = Box :: new ( AlignedBuffer {
304+ data : [ 0 ; BUFFER_SIZE ] ,
305+ } ) ;
306+ aligned_data. data [ ..BUFFER_SIZE ] . clone_from_slice ( RAW_DEVICE_TREE ) ;
307+ let mut slice = aligned_data. data . to_vec ( ) ;
308+ let ptr = DtbPtr :: from_raw ( slice. as_mut_ptr ( ) ) . unwrap ( ) ;
309+ let dtb = Dtb :: from ( ptr) . share ( ) ;
310+
311+ let node: Node = from_raw_mut ( & dtb) . unwrap ( ) ;
312+ let prop = node. get_prop ( "compatible" ) ;
313+ assert ! ( prop. is_some( ) ) ;
314+ }
315+ }
You can’t perform that action at this time.
0 commit comments