我正在尝试将工具存放在工具带中并将工具带放在工具箱中。我也试图将工具箱中的工具编入索引以快速抓取它们。这只是一个抽象,因为我的项目中没有工具等。
在工具带中存放工具时,我会返回工具的侵入参考(光标)。我试图将此引用存储在工具箱索引中,但这似乎不起作用!
这似乎是引用兄弟字段的情况。
运行cargo init --bin toolbox
,在依赖项下添加intrusive-collections = "0.3"
,然后将main.rs
替换为以下代码。
运行时会出现此错误:
Compiling toolbox v0.1.0 (file:///Users/joelr/Work/rust/node)
src/main.rs:67:31: 67:36 error: cannot infer an appropriate lifetime for autoref due to conflicting requirements [E0495]
src/main.rs:67 let belt = self.belts.entry(belt_id).or_insert_with(|| Belt::new());
^~~~~
src/main.rs:65:5: 71:6 help: consider using an explicit lifetime parameter as shown: fn add(&'a mut self, belt_id: u64, tool_id: u64, size: u64)
src/main.rs:65 fn add<'b>(&'b mut self, belt_id: u64, tool_id: u64, size: u64) {
我可以吃蛋糕吗?
#[macro_use]
extern crate intrusive_collections;
use std::collections::HashMap;
use std::convert::{AsRef, AsMut};
use intrusive_collections::{IntrusiveRef, LinkedList, LinkedListLink, linked_list};
#[derive(Debug)]
struct Tool {
id: u64,
size: u64,
link: LinkedListLink,
}
impl Tool {
fn new(id: u64, size: u64) -> Self {
Tool {
id: id,
size: size,
link: LinkedListLink::new(),
}
}
}
intrusive_adaptor!(ToolAdaptor = Tool { link: LinkedListLink });
#[derive(Debug)]
struct Belt {
total_size: u64,
tools: LinkedList<ToolAdaptor>,
}
impl Belt {
fn new() -> Self {
Belt {
total_size: 0,
tools: LinkedList::new(ToolAdaptor),
}
}
fn add(&mut self, tool: Tool) -> CursorMut {
let r = IntrusiveRef::from_box(Box::new(tool));
self.tools.push_back(r);
self.tools.back_mut()
}
}
type CursorMut<'a> = linked_list::CursorMut<'a, ToolAdaptor>;
type Belts = HashMap<u64, Belt>;
type Tools<'a> = HashMap<u64, CursorMut<'a>>;
struct ToolBox<'a> {
tools: Tools<'a>,
belts: Belts,
}
impl<'a> ToolBox<'a> {
fn new() -> Self {
ToolBox {
tools: HashMap::new(),
belts: HashMap::new(),
}
}
fn add<'b>(&'b mut self, belt_id: u64, tool_id: u64, size: u64) {
let tool = Tool::new(tool_id, size);
let belt = self.belts.entry(belt_id).or_insert_with(|| Belt::new());
let cursor = belt.add(tool);
self.tools.insert(tool_id, cursor);
()
}
fn belts(&self) -> &Belts {
&self.belts
}
}
impl<'a> AsRef<ToolBox<'a>> for ToolBox<'a> {
fn as_ref(&self) -> &Self {
self
}
}
impl<'a> AsMut<ToolBox<'a>> for ToolBox<'a> {
fn as_mut(&mut self) -> &mut Self {
self
}
}
fn main() {
let mut toolbox = ToolBox::new();
toolbox.add(1, 1, 1);
println!("ToolBox belts = {:?}", toolbox.as_ref().belts())
}
答案 0 :(得分:0)
这不能像我上面写的那样工作。例如,可以从ToolBelt
中的链接列表中删除会使Tool
中存储的ToolBox
引用无效的项目。
我最终使用linked hash map重写了代码。它按照插入顺序保存项目和允许我按键删除它们。