rust - 拥有兄弟领域和生命周期

标签 rust

我正在尝试将工具存放在工具带中,并将工具带放在工具箱中。我还尝试为工具箱本身中的工具编制索引以快速获取它们。这只是一个抽象,因为我的项目中没有工具等。

将工具存储在工具带中时,我取回了工具的侵入式引用(光标)。我试图将此引用存储在工具箱索引中,但这似乎不起作用!

这似乎是引用同级字段的情况。

运行cargo init --bin toolbox,在dependencies下添加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())
}

最佳答案

这不能像我上面写的那样工作。例如,可以从 ToolBelt 中的链接列表中删除项目,这会使存储在 ToolBox 中的 Tool 引用无效。

我最终使用 linked hash map 重写了代码.它按插入顺序保存项目并且让我可以按键删除它们。

关于rust - 拥有兄弟领域和生命周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38312922/

相关文章:

rust - 内联 Rust 中的常量值是什么意思?

pointers - 内部可变结构的原始指针类型

rust - 如何使 Rust Game of Life WebAssembly 作为静态网站工作?

rust - 不比较 Rust 中的字符串

c - 使用类型 `[my_struct]` 是将 C 结构数组传递给 Rust 函数的正确方法吗?

string - 如何在 Rust 中获得 float 的紧凑但无损的字符串表示?

rust - 如何为所有平台生成 cargo 文档?

rust - 编译为 WebAssembly 时无法使 image::load_from_memory() 工作

rust - 为什么 Rust 创建的 f32 小于 f32::MIN_POSITIVE?

rust - syn::parse::ParseBuffer::peek() 如何接受类型作为参数?