@@ -35,6 +35,7 @@ pub struct DeviceTree {
3535 /// The root of the tree.
3636 root : Rc < Node > ,
3737 /// All of the labels.
38+ /// Note that this is a BTree so that the output will be deterministic.
3839 labels : BTreeMap < String , Rc < Node > > ,
3940}
4041
@@ -70,7 +71,7 @@ pub struct Property {
7071pub enum Value {
7172 Words ( Vec < Word > ) ,
7273 Bytes ( Vec < u8 > ) ,
73- Phandle ( Phandle ) , // TODO
74+ Phandle ( Phandle ) ,
7475 String ( String ) ,
7576}
7677
@@ -123,34 +124,24 @@ impl Node {
123124 }
124125
125126 fn parent_walk ( self : & Rc < Self > ) {
126- // *(self.parent.borrow_mut()) = Some(parent.clone());
127-
128127 for child in & self . children {
129128 * ( child. parent . borrow_mut ( ) ) = Some ( self . clone ( ) ) ;
130129 child. parent_walk ( )
131130 }
132131 }
133132
134133 fn is_compatible ( & self , name : & str ) -> bool {
135- if let Some ( prop) = self . properties . iter ( ) . find ( |p| p. name == "compatible" ) {
136- prop. value . iter ( ) . any ( |v| {
137- match v {
138- Value :: String ( vn) if name == vn => true ,
139- _ => false ,
140- }
141- } )
142- } else {
143- // If there is no compatible field, we are clearly not compatible.
144- false
145- }
134+ self . properties
135+ . iter ( )
136+ . filter ( |p| p. name == "compatible" )
137+ . flat_map ( |prop| prop. value . iter ( ) )
138+ . any ( |v| matches ! ( v, Value :: String ( vn) if name == vn) )
146139 }
147140
148141 /// A richer compatible test. Walks a series of names, in reverse. Any that are "Some(x)" must
149142 /// be compatible with "x" at that level.
150143 fn compatible_path ( & self , path : & [ Option < & str > ] ) -> bool {
151- let res = self . path_walk ( path, 0 ) ;
152- // println!("compatible? {}: {} {:?}", res, self.path, path);
153- res
144+ self . path_walk ( path, 0 )
154145 }
155146
156147 /// Recursive path walk, to make borrowing simpler.
@@ -161,10 +152,8 @@ impl Node {
161152 }
162153
163154 // Check the failure condition, where this node isn't compatible with this section of the path.
164- if let Some ( name) = path[ pos] {
165- if !self . is_compatible ( name) {
166- return false ;
167- }
155+ if matches ! ( path[ pos] , Some ( name) if !self . is_compatible( name) ) {
156+ return false ;
168157 }
169158
170159 // Walk down the tree. We have to check for None here, as we can't recurse on the none
@@ -177,19 +166,17 @@ impl Node {
177166 }
178167 }
179168
180- /// Is the named property present?
169+ /// Returns `true` if there is a property with this name.
181170 fn has_prop ( & self , name : & str ) -> bool {
182171 self . properties . iter ( ) . any ( |p| p. name == name)
183172 }
184173
185- /// Get this property in its entirety.
174+ /// Returns the slice of values of a property with this name as `Some` or `None` if the property
175+ /// does not exist.
186176 fn get_property ( & self , name : & str ) -> Option < & [ Value ] > {
187- for p in & self . properties {
188- if p. name == name {
189- return Some ( & p. value ) ;
190- }
191- }
192- return None ;
177+ self . properties
178+ . iter ( )
179+ . find_map ( |p| if p. name == name { Some ( p. value . as_slice ( ) ) } else { None } )
193180 }
194181
195182 /// Attempt to retrieve the named property, as a single entry of Words.
@@ -244,12 +231,11 @@ impl Node {
244231impl Value {
245232 fn phandle_walk ( & self , labels : & BTreeMap < String , Rc < Node > > ) {
246233 match self {
247- Value :: Phandle ( ph) => ph. phandle_resolve ( labels) ,
248- Value :: Words ( words) => {
234+ Self :: Phandle ( ph) => ph. phandle_resolve ( labels) ,
235+ Self :: Words ( words) => {
249236 for w in words {
250- match w {
251- Word :: Phandle ( ph) => ph. phandle_resolve ( labels) ,
252- _ => ( ) ,
237+ if let Word :: Phandle ( ph) = w {
238+ ph. phandle_resolve ( labels) ;
253239 }
254240 }
255241 }
@@ -260,8 +246,8 @@ impl Value {
260246
261247impl Phandle {
262248 /// Construct a phandle that is unresolved.
263- pub fn new ( name : String ) -> Phandle {
264- Phandle {
249+ pub fn new ( name : String ) -> Self {
250+ Self {
265251 name,
266252 node : RefCell :: new ( None ) ,
267253 }
@@ -286,9 +272,9 @@ impl Phandle {
286272}
287273
288274impl Word {
289- pub fn get_number ( & self ) -> Option < u32 > {
275+ pub fn as_number ( & self ) -> Option < u32 > {
290276 match self {
291- Word :: Number ( n) => Some ( * n) ,
277+ Self :: Number ( n) => Some ( * n) ,
292278 _ => None ,
293279 }
294280 }
@@ -297,6 +283,9 @@ impl Word {
297283// To avoid recursion, the debug printer for Phandle just prints the name.
298284impl std:: fmt:: Debug for Phandle {
299285 fn fmt ( & self , fmt : & mut std:: fmt:: Formatter ) -> std:: fmt:: Result {
300- write ! ( fmt, "Phandle({:?})" , self . name)
286+ fmt
287+ . debug_struct ( "Phandle" )
288+ . field ( "name" , & self . name )
289+ . finish_non_exhaustive ( )
301290 }
302291}
0 commit comments