在Rust中,我有很多要管理的接收器对象,但是使用Select遇到生命周期问题

如何解决在Rust中,我有很多要管理的接收器对象,但是使用Select遇到生命周期问题

由于存在大量对象的可能性,我希望有一种方法可以将它们添加到选择列表中,将其删除以进行处理,然后再添加回去。全部,而不必每次重新添加对象以等待时都重新构建选择列表。看起来像这样:

dragStart()

随着时间的推移,选择列表将包含更多无效条目,然后生效,我将在那时重建它。但是,我不想每次都重建它。这是详细的错误消息:

use std::collections::HashMap;
use crossbeam::{Select,Sender,Receiver};

struct WaitList <'a> {
    sel: Select<'a>,objects: HashMap<u128,Object>,sel_index: HashMap<usize,u128>,}
impl<'a> WaitList<'a> {
    fn new () -> Self { Self { sel: Select::new(),objects: HashMap::new(),sel_index: HashMap::new() } }
    fn select(&self) -> &Object {
        let oper = self.sel.select();
        let index = oper.index();
        let id = self.sel_index.get(&index).unwrap();
        let obj = self.objects.get(&id).unwrap();
        obj.cmd = oper.recv(&obj.receiver).unwrap();
        self.sel.remove(index);
        obj
    }

    fn add_object(&self,object: Object) {
        let id = object.id;
        self.objects.insert(id,object);
        self.add_select(id);
    }

    fn add_select(&self,id: u128) {
        let idx = self.sel.recv(&self.objects.get(&id).unwrap().receiver);
        self.sel_index.insert(idx,id);
    }
}

虽然我相信我理解问题,但哈希表中的接收者借用期限不够长,但我很难找到一个替代方案-而且我没有看到一种干净的借阅信息的方法。我考虑过创建一个包含借用的结构,并在等待sel_index中使用它来代替普通ID,但这会遇到相同的生命周期问题。

    Checking test-select v0.1.0 (/Users/bruce/Projects/rust/examples/test-select)
error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
  --> src/main.rs:28:47
   |
28 |         let idx = self.sel.recv(&self.objects.get(&id).unwrap().receiver);
   |                                               ^^^
   |
note: first,the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 27:5...
  --> src/main.rs:27:5
   |
27 | /     fn add_select(&self,id: u128) {
28 | |         let idx = self.sel.recv(&self.objects.get(&id).unwrap().receiver);
29 | |         self.sel_index.insert(idx,id);
30 | |     }
   | |_____^
note: ...so that reference does not outlive borrowed content
  --> src/main.rs:28:34
   |
28 |         let idx = self.sel.recv(&self.objects.get(&id).unwrap().receiver);
   |                                  ^^^^^^^^^^^^
note: but,the lifetime must be valid for the lifetime `'a` as defined on the impl at 9:6...
  --> src/main.rs:9:6
   |
9  | impl<'a> WaitList<'a> {
   |      ^^
note: ...so that the types are compatible
  --> src/main.rs:28:28
   |
28 |         let idx = self.sel.recv(&self.objects.get(&id).unwrap().receiver);
   |                            ^^^^
   = note: expected `&mut crossbeam::Select<'_>`
              found `&mut crossbeam::Select<'a>`

我觉得我缺少某些东西或不了解某些东西,因为似乎要做我想做的事情并不那么困难。我可以想象,选择HashMap来保存对象可能是个问题,在我添加和插入时,Vec感到不对。顺便说一句,HashMap通常不是等待列表的一部分。它是其他事情的一部分,但是无论HashMap居住在哪里,问题仍然存在。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?