【问题标题】:conflicting lifetimes in rust生锈的冲突寿命
【发布时间】:2020-08-11 07:46:00
【问题描述】:

代码是让score_handler在operate()方法中处理某个学校的分数,handle()只是做一些计算而不是保留self.school的引用:

trait Class{
    fn student_count(&self, )->usize;
    fn student_score(&self, i: usize) ->u64;
}

trait School<'a>{
    fn class_count(&self)->usize;
    fn class(&'a self, i:usize)->&'a dyn Class;
}

trait ScoreHandler<'a> {
    fn handle(&'a mut self, school: &'a dyn School<'a>);
}

struct Coordinator<'a>{
    some_value: u64,
    school: &'a dyn School<'a>,
    score_handler: &'a mut dyn ScoreHandler<'a>
}

impl Coordinator<'_>{
    pub fn main(&mut self){
        self.operate();
        if self.some_value == 0 {
            println!("0");
        }
    }
    fn operate(&mut self){
        self.score_handler.handle(self.school);
    }
}

我遇到了错误:

error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
  -> mytest/main/src/main.rs:29:28
   |
29 |         self.score_handler.handle(self.school);
   |                            ^^^^^^
   |
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 28:5...
  -> mytest/main/src/main.rs:28:5
   |
28 | /     fn operate(&mut self){
29 | |         self.score_handler.handle(self.school);
30 | |     }
   | |_____^
note: ...so that reference does not outlive borrowed content
  -> mytest/main/src/main.rs:29:9
   |
29 |         self.score_handler.handle(self.school);
   |         ^^^^^^^^^^^^^^^^^^
note: but, the lifetime must be valid for the lifetime `'_` as defined on the impl at 21:18...
  --> mytest/main/src/main.rs:21:18
   |
21 | impl Coordinator<'_>{
   |                  ^^
note: ...so that the types are compatible
  --> mytest/main/src/main.rs:29:28
   |
29 |         self.score_handler.handle(self.school);
   |                            ^^^^^^
   = note: expected `&mut dyn ScoreHandler<'_>`
              found `&mut dyn ScoreHandler<'_>`

...

所以我把改成了,像这样:

trait Class{
    fn student_count(&self, )->usize;
    fn student_score(&self, i: usize) ->u64;
}

trait School<'a>{
    fn class_count(&self)->usize;
    fn class(&'a self, i:usize)->&'a dyn Class;
}

trait ScoreHandler<'a> {
    fn handle(&'a mut self, school: &'a dyn School<'a>);
}

struct Coordinator<'a>{
    some_value: u64,
    school: &'a dyn School<'a>,
    score_handler: &'a mut dyn ScoreHandler<'a>
}

impl<'a> Coordinator<'a>{
    pub fn main(&'a mut self){
        self.operate();
        if self.some_value == 0 {
            println!("0");
        }
    }
    fn operate(&'a mut self){
        self.score_handler.handle(self.school);
    }
}

我得到了错误:

error[E0503]: cannot use `self.some_value` because it was mutably borrowed
  --> mytest/main/src/main.rs:24:12
   |
21 | impl<'a> Coordinator<'a>{
   |      -- lifetime `'a` defined here
22 |     pub fn main(&'a mut self){
23 |         self.operate();
   |         --------------
   |         |
   |         borrow of `*self` occurs here
   |         argument requires that `*self` is borrowed for `'a`
24 |         if self.some_value == 0 {
   |            ^^^^^^^^^^^^^^^ use of borrowed `*self`

error[E0503]: cannot use `self.some_value` because it was mutably borrowed
  --> mytest/main/src/main.rs:24:12
   |
21 | impl<'a> Coordinator<'a>{
   |      -- lifetime `'a` defined here
22 |     pub fn main(&'a mut self){
23 |         self.operate();
   |         --------------
   |         |
   |         borrow of `*self` occurs here
   |         argument requires that `*self` is borrowed for `'a`
24 |         if self.some_value == 0 {
   |            ^^^^^^^^^^^^^^^ use of borrowed `*self`
...

我想知道是否有人可以帮助我解决这个问题,谢谢!

【问题讨论】:

  • 如果你删除除了Coordinator&lt;'a&gt; it compiles之外的所有。这对你有用吗?
  • @rodrigo,它有效,谢谢!但是我会检查为什么我将生命周期用于学校等,因为这是一个简化的演示,与我的原始代码完全不同,我使用生命周期来解决另一个问题。很快回来。

标签: rust lifetime


【解决方案1】:

首先:我强烈建议您重新考虑您的类型架构。为什么是ClassSchool 特征?你的不同类是否有不同的实现细节,比如一个在内部使用Vec,另一个在内部使用HashMap

正如 rodrigo 在 cmets 中已经指出的那样,您可以简单地删除一些 'as,然后它就会编译。原因是,用你的代码你字面上说,Coordinator::operate() 借用的可变自我将持续与Coordinator::main() 一样长,但是你在那里重新借用它。

然后看起来像这样:

trait Class {
    fn student_count(&self) -> usize;
    fn student_score(&self, i: usize) -> u64;
}

trait School<'a> {
    fn class_count(&self) -> usize;
    fn class(&'a self, i: usize) -> &'a dyn Class;
}

trait ScoreHandler<'a> {
    fn handle(&mut self, school: &'a dyn School<'a>);
}
struct Coordinator<'a> {
    some_value: u64,
    school: &'a dyn School<'a>,
    score_handler: &'a mut dyn ScoreHandler<'a>,
}

impl<'a> Coordinator<'a> {
    pub fn main(&mut self) {
        self.operate();
        if self.some_value == 0 {
            println!("0");
        }
    }
    fn operate(&mut self) {
        self.score_handler.handle(self.school);
    }
}

当然,你还必须定义一些实际的结构来实现这些特征,我举个例子:

struct ClassA {
    student_scores: Vec<u64>,
}

impl Class for ClassA {
    fn student_count(&self) -> usize {
        self.student_scores.len()
    }
    fn student_score(&self, i: usize) -> u64 {
        self.student_scores[i]
    }
}

struct SchoolA<'a> {
    classes: Vec<&'a dyn Class>,
}

impl<'a> School<'a> for SchoolA<'a> {
    fn class_count(&self) -> usize {
        self.classes.len()
    }
    fn class(&'a self, i: usize) -> &'a dyn Class {
        self.classes[i]
    }
}

struct ScoreHandlerA {
    some_internal_state: u64,
}

impl<'a> ScoreHandler<'a> for ScoreHandlerA {
    fn handle(&mut self, school: &'a dyn School<'a>) {
        for i in 0..school.class_count() {
            println!("Handling a class in a school");
        }
    }
}

然后,你的 main 可能看起来像这样:

fn main() {
    let classes = vec![ClassA {
        student_scores: vec![13, 14, 15],
    }];
    let school = SchoolA {
        classes: classes.iter().map(|c| c as &dyn Class).collect(),
    };
    let mut coordinator = Coordinator {
        some_value: 13,
        school: &school,
        score_handler: &mut ScoreHandlerA {
            some_internal_state: 0,
        },
    };

    coordinator.main();
}

【讨论】:

  • 谢谢!这是一个简化的演示,而我的原始代码更复杂。实际上在我的原始代码中,类对应的是一些复杂的缓存,它是可变的,但接口简单而固定,学校是一个缓存列表,而实际上它是一个双端队列,所以我只想隐藏实现。无论如何,这个问题帮助我更好地了解生命,因为我认为我对它仍然很不了解。
  • 您可以使用结构很好地隐藏实现,只需将公共部分设为pub。好吧,无论如何,你会知道你在做什么,只是我的一个友好的指针。生命周期真的没有那么复杂,如果你尝试编写一个所有生命周期都是显式的玩具程序,你可能会受到启发。非常冗长,但它可能会对您有所帮助。
  • 我将它们作为接口的原因是尝试解耦实现。好的,我要检查我的代码。如果没有疑问,我愿意接受您的回答。非常感谢您提供非常详细的代码。
  • 如果出现其他问题,请随时更新您的问题 :)
猜你喜欢
  • 1970-01-01
  • 2014-10-25
  • 1970-01-01
  • 2019-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-25
相关资源
最近更新 更多