pub struct TreeUtil;Expand description
Tree structure utility class
§Example
use mitoo::{TreeData, TreeNode, TreeUtil};
#[derive(Debug, Clone)]
struct Menu {
id: u64,
name: String,
pid: u64,
// 可添加其他自定义字段
}
// 为 Menu 实现 TreeData 约束
impl TreeData for Menu {
type Id = u64;
fn id(&self) -> Self::Id { self.id }
fn name(&self) -> &str { &self.name }
fn pid(&self) -> Self::Id { self.pid }
// 定义根节点的父 ID 为 0
fn root_pid() -> Self::Id { 0 }
}
mod tests {
use super::*;
#[test]
fn test01() {
// 准备扁平数据
let menus = vec![
Menu {
id: 1,
name: "系统管理".to_string(),
pid: 0,
},
Menu {
id: 2,
name: "用户管理".to_string(),
pid: 1,
},
Menu {
id: 3,
name: "角色管理".to_string(),
pid: 1,
},
Menu {
id: 4,
name: "菜单管理".to_string(),
pid: 2,
},
Menu {
id: 5,
name: "数据统计".to_string(),
pid: 0,
},
];
// 转换为树形结构
let trees = TreeUtil::to_trees(menus);
println!("{:#?}", trees);
}
}Implementations§
Auto Trait Implementations§
impl Freeze for TreeUtil
impl RefUnwindSafe for TreeUtil
impl Send for TreeUtil
impl Sync for TreeUtil
impl Unpin for TreeUtil
impl UnwindSafe for TreeUtil
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more