Skip to content

Commit 89958c3

Browse files
committed
zephyr-build: devicetree: Clean up compatable check
Instead of a separate function which uses a `pos` argument, just make `compatible_path` itself recursive by using slice operations. Signed-off-by: David Brown <[email protected]>
1 parent 1b02946 commit 89958c3

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

zephyr-build/src/devicetree.rs

+13-20
Original file line numberDiff line numberDiff line change
@@ -141,28 +141,21 @@ impl Node {
141141
/// A richer compatible test. Walks a series of names, in reverse. Any that are "Some(x)" must
142142
/// be compatible with "x" at that level.
143143
fn compatible_path(&self, path: &[Option<&str>]) -> bool {
144-
self.path_walk(path, 0)
145-
}
146-
147-
/// Recursive path walk, to make borrowing simpler.
148-
fn path_walk(&self, path: &[Option<&str>], pos: usize) -> bool {
149-
if pos >= path.len() {
150-
// Once past the end, we consider everything a match.
151-
return true;
152-
}
153-
154-
// Check the failure condition, where this node isn't compatible with this section of the path.
155-
if matches!(path[pos], Some(name) if !self.is_compatible(name)) {
156-
return false;
157-
}
144+
if let Some(first) = path.first() {
145+
if !matches!(first, Some(name) if !self.is_compatible(name)) {
146+
return false;
147+
}
158148

159-
// Walk down the tree. We have to check for None here, as we can't recurse on the none
160-
// case.
161-
if let Some(child) = self.parent.borrow().as_ref() {
162-
child.path_walk(path, pos + 1)
149+
// Walk down the tree with the remainder of the path.
150+
if let Some(child) = self.parent.borrow().as_ref() {
151+
child.compatible_path(&path[1..])
152+
} else {
153+
// We've run out of nodes, so this is considered not matching.
154+
false
155+
}
163156
} else {
164-
// We've run out of nodes, so this is considered not matching.
165-
false
157+
// The empty path always matches.
158+
true
166159
}
167160
}
168161

0 commit comments

Comments
 (0)