@@ -35,6 +35,7 @@ pub struct DeviceTree {
35
35
/// The root of the tree.
36
36
root : Rc < Node > ,
37
37
/// All of the labels.
38
+ /// Note that this is a BTree so that the output will be deterministic.
38
39
labels : BTreeMap < String , Rc < Node > > ,
39
40
}
40
41
@@ -70,7 +71,7 @@ pub struct Property {
70
71
pub enum Value {
71
72
Words ( Vec < Word > ) ,
72
73
Bytes ( Vec < u8 > ) ,
73
- Phandle ( Phandle ) , // TODO
74
+ Phandle ( Phandle ) ,
74
75
String ( String ) ,
75
76
}
76
77
@@ -123,34 +124,24 @@ impl Node {
123
124
}
124
125
125
126
fn parent_walk ( self : & Rc < Self > ) {
126
- // *(self.parent.borrow_mut()) = Some(parent.clone());
127
-
128
127
for child in & self . children {
129
128
* ( child. parent . borrow_mut ( ) ) = Some ( self . clone ( ) ) ;
130
129
child. parent_walk ( )
131
130
}
132
131
}
133
132
134
133
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) )
146
139
}
147
140
148
141
/// A richer compatible test. Walks a series of names, in reverse. Any that are "Some(x)" must
149
142
/// be compatible with "x" at that level.
150
143
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 )
154
145
}
155
146
156
147
/// Recursive path walk, to make borrowing simpler.
@@ -161,10 +152,8 @@ impl Node {
161
152
}
162
153
163
154
// 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 ;
168
157
}
169
158
170
159
// 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 {
177
166
}
178
167
}
179
168
180
- /// Is the named property present?
169
+ /// Returns `true` if there is a property with this name.
181
170
fn has_prop ( & self , name : & str ) -> bool {
182
171
self . properties . iter ( ) . any ( |p| p. name == name)
183
172
}
184
173
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.
186
176
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 } )
193
180
}
194
181
195
182
/// Attempt to retrieve the named property, as a single entry of Words.
@@ -244,12 +231,11 @@ impl Node {
244
231
impl Value {
245
232
fn phandle_walk ( & self , labels : & BTreeMap < String , Rc < Node > > ) {
246
233
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) => {
249
236
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) ;
253
239
}
254
240
}
255
241
}
@@ -260,8 +246,8 @@ impl Value {
260
246
261
247
impl Phandle {
262
248
/// Construct a phandle that is unresolved.
263
- pub fn new ( name : String ) -> Phandle {
264
- Phandle {
249
+ pub fn new ( name : String ) -> Self {
250
+ Self {
265
251
name,
266
252
node : RefCell :: new ( None ) ,
267
253
}
@@ -286,9 +272,9 @@ impl Phandle {
286
272
}
287
273
288
274
impl Word {
289
- pub fn get_number ( & self ) -> Option < u32 > {
275
+ pub fn as_number ( & self ) -> Option < u32 > {
290
276
match self {
291
- Word :: Number ( n) => Some ( * n) ,
277
+ Self :: Number ( n) => Some ( * n) ,
292
278
_ => None ,
293
279
}
294
280
}
@@ -297,6 +283,9 @@ impl Word {
297
283
// To avoid recursion, the debug printer for Phandle just prints the name.
298
284
impl std:: fmt:: Debug for Phandle {
299
285
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 ( )
301
290
}
302
291
}
0 commit comments