gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 08:36:16 25 4
gpt4 key购买 nike

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

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

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

运行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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com