Skip to content

Commit 2b05c1a

Browse files
committed
refactor: 优化节点迭代器和可变节点引用的构造逻辑,简化代码结构
1 parent 277a865 commit 2b05c1a

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

fdt-edit/src/fdt.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,20 @@ impl Fdt {
138138
.unwrap_or_else(|| path.to_string());
139139

140140
NodeIter::new(self.root()).filter_map(move |node_ref| {
141-
if node_ref.ctx.current_path == path {
142-
Some(node_ref)
143-
} else {
144-
None
141+
let want = path.split("/");
142+
let got = node_ref.ctx.current_path.split("/");
143+
let last_idx = core::cmp::min(want.clone().count(), got.clone().count());
144+
for (w, g) in want.zip(got).take(last_idx - 1) {
145+
if w != g {
146+
return None;
147+
}
148+
}
149+
let want_name = path.rsplit('/').next().unwrap_or("");
150+
let got_name = node_ref.node.name();
151+
if !got_name.starts_with(want_name) {
152+
return None;
145153
}
154+
Some(node_ref)
146155
})
147156
}
148157

fdt-edit/src/node/iter.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use core::{
22
fmt::Debug,
3-
ops::{Deref, DerefMut},
3+
ops::{Deref, DerefMut}, slice::IterMut,
44
};
55

66
use alloc::vec::Vec;
@@ -63,6 +63,12 @@ pub enum NodeMut<'a> {
6363
Gerneric(NodeMutGen<'a>),
6464
}
6565

66+
impl<'a> NodeMut<'a> {
67+
pub fn new(node: &'a mut Node, ctx: Context<'a>) -> Self {
68+
Self::Gerneric(NodeMutGen { node, ctx })
69+
}
70+
}
71+
6672
impl<'a> Deref for NodeMut<'a> {
6773
type Target = NodeMutGen<'a>;
6874

@@ -111,13 +117,15 @@ impl<'a> Iterator for NodeIter<'a> {
111117
}
112118

113119
pub struct NodeIterMut<'a> {
114-
stack: Vec<(&'a mut Node, Context<'a>)>,
120+
ctx: Context<'a>,
121+
iter: IterMut<'a, Node>,
115122
}
116123

117124
impl<'a> NodeIterMut<'a> {
118125
pub fn new(root: &'a mut Node) -> Self {
119126
Self {
120-
stack: vec![(root, Context::new())],
127+
ctx: Context::new(),
128+
iter: root.children.iter_mut(),
121129
}
122130
}
123131
}

0 commit comments

Comments
 (0)