Skip to content

Commit 8ee112f

Browse files
committed
refactor: 重构上下文管理,合并父节点压入和弹出逻辑,简化节点迭代器实现
1 parent 616ae44 commit 8ee112f

File tree

2 files changed

+33
-36
lines changed

2 files changed

+33
-36
lines changed

fdt-edit/src/ctx.rs

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -123,32 +123,11 @@ impl<'a> Context<'a> {
123123
parent.ranges(grandparent_address_cells)
124124
}
125125

126-
/// 压入父节点,进入子节点前调用
127-
pub fn push_parent(&mut self, parent: &'a Node) {
128-
if let Some(ph) = parent.phandle() {
129-
self.phandle_map.insert(ph, parent);
126+
pub fn push(&mut self, node: &'a Node) {
127+
if let Some(ph) = node.phandle() {
128+
self.phandle_map.insert(ph, node);
130129
}
131-
132-
// 压入父节点栈
133-
self.parents.push(parent);
134-
}
135-
136-
/// 弹出父节点,离开子节点后调用
137-
pub fn pop_parent(&mut self) -> Option<&'a Node> {
138-
let node = self.parents.pop()?;
139-
140-
Some(node)
141-
}
142-
143-
/// 为进入指定子节点创建新的上下文
144-
/// 当前节点成为新上下文的父节点
145-
pub fn for_child(&self, current_node: &'a Node) -> Self {
146-
let mut child_ctx = Self {
147-
parents: self.parents.clone(),
148-
phandle_map: self.phandle_map.clone(),
149-
};
150-
child_ctx.push_parent(current_node);
151-
child_ctx
130+
self.parents.push(node);
152131
}
153132

154133
/// 通过 phandle 查找节点

fdt-edit/src/node/iter.rs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use core::{
22
fmt::Debug,
33
ops::{Deref, DerefMut},
4-
slice::IterMut,
4+
slice::{Iter, IterMut},
55
};
66

77
use alloc::{string::String, vec::Vec};
88

9-
use crate::{Context, Node, Property, ctx};
9+
use crate::{Context, Node, Property};
1010

1111
#[derive(Clone, Debug)]
1212
pub enum NodeRef<'a> {
@@ -127,13 +127,19 @@ impl<'a> DerefMut for NodeMut<'a> {
127127
}
128128

129129
pub struct NodeIter<'a> {
130-
stack: Vec<(&'a Node, Context<'a>)>,
130+
ctx: Context<'a>,
131+
node: Option<&'a Node>,
132+
stack: Vec<Iter<'a, Node>>,
131133
}
132134

133135
impl<'a> NodeIter<'a> {
134136
pub fn new(root: &'a Node) -> Self {
137+
let ctx = Context::new();
138+
135139
Self {
136-
stack: vec![(root, Context::new())],
140+
ctx,
141+
node: Some(root),
142+
stack: vec![],
137143
}
138144
}
139145
}
@@ -142,16 +148,28 @@ impl<'a> Iterator for NodeIter<'a> {
142148
type Item = NodeRef<'a>;
143149

144150
fn next(&mut self) -> Option<Self::Item> {
145-
let (node, ctx) = self.stack.pop()?;
151+
if let Some(n) = self.node.take() {
152+
// 返回当前节点,并将其子节点压入栈中
153+
let ctx = self.ctx.clone();
154+
self.ctx.push(n);
155+
self.stack.push(n.children.iter());
156+
return Some(NodeRef::new(n, ctx));
157+
}
158+
159+
let iter = self.stack.last_mut()?;
146160

147-
// 使用栈实现前序深度优先,保持原始子节点顺序
148-
for child in node.children.iter().rev() {
149-
// 为子节点创建新的上下文,当前节点成为父节点
150-
let child_ctx = ctx.for_child(node);
151-
self.stack.push((child, child_ctx));
161+
if let Some(child) = iter.next() {
162+
// 返回子节点,并将其子节点压入栈中
163+
let ctx = self.ctx.clone();
164+
self.ctx.push(child);
165+
self.stack.push(child.children.iter());
166+
return Some(NodeRef::new(child, ctx));
152167
}
153168

154-
Some(NodeRef::new(node, ctx))
169+
// 当前迭代器耗尽,弹出栈顶
170+
self.stack.pop();
171+
self.ctx.parents.pop();
172+
self.next()
155173
}
156174
}
157175

0 commit comments

Comments
 (0)