34个版本 (13个破坏性版本)
0.17.0 | 2022年10月19日 |
---|---|
0.16.0-rkyv.3 | 2022年6月8日 |
0.15.0-rc.1 | 2022年2月24日 |
0.12.0-rc.5 |
|
0.5.3 | 2020年11月17日 |
#365 在 数据结构
每月92次下载
在 5 个crate中使用了(4个直接使用)
34KB
801 行
微开尔文
用于创建和遍历递归注释结构的crate。
复合特质
/// A type that can recursively contain itself and leaves.
pub trait Compound<A>: Sized {
/// The leaf type of the compound collection
type Leaf;
/// Returns a reference to a possible child at specified index
fn child(&self, index: usize) -> Child<Self, A>;
/// Returns a mutable reference to a possible child at specified index
fn child_mut(&mut self, index: usize) -> ChildMut<Self, A>;
}
Compound
特质定义了一个类型为集合类型。这意味着它可以被搜索,并且可以构建指向其元素的分支。
分支遍历
可以为以用户定义的方式遍历树实现Walker
特质。以下是一个示例,即内部使用的AllLeaves
- 一个实现
/// Walker that visits all leaves
pub struct AllLeaves;
impl<C, A> Walker<C, A> for AllLeaves
where
C: Compound<A>,
{
fn walk(&mut self, walk: Walk<C, A>) -> Step {
for i in 0.. {
match walk.child(i) {
Child::Leaf(_) => return Step::Found(i),
Child::Node(_) => return Step::Into(i),
Child::Empty => (),
Child::EndOfNode => return Step::Advance,
}
}
unreachable!()
}
}
用法
请查看nstack对栈/向量类型的实现,以获取更高级的示例。
依赖
~10KB