Как исправить несоответствие времени жизни при возврате изменяемой ссылки на поле структуры из типаж-метода?

Это моя попытка вернуть изменяемую ссылку на поле структуры.

pub trait Objective {
    fn get_children<'a>(&'a mut self) -> &'_ mut Vec<&'_ mut Box<dyn Objective>>;
    fn get_parent(&'_ mut self) -> &'_ mut Box<dyn Objective>;
    fn update(&'_ self) -> ();
}

// #[derive(Objective)]
pub struct Object<'a> {
    children: Vec<&'a mut Box<dyn Objective>>,
    parent: &'a mut Box<dyn Objective>,
}

impl<'a> Objective for Object<'a> {
    fn get_children(&'a mut self) -> &'_ mut Vec<&'_ mut Box<dyn Objective>> {
        return &mut self.children;
    }

    fn get_parent(&'_ mut self) -> &'_ mut Box<dyn Objective> {
        return self.parent;
    }

    fn update(&'_ self) -> () {}
}

Есть проблема со сроком службы. Вот ошибка, которую выдает компилятор:

error[E0308]: method not compatible with trait
  --> src/lib.rs:14:5
   |
14 |     fn get_children(&'a mut self) -> &'_ mut Vec<&'_ mut Box<dyn Objective>> {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
   |
   = note: expected fn pointer `fn(&'a mut Object<'a>) -> &'a mut std::vec::Vec<&'a mut std::boxed::Box<(dyn Objective + 'static)>>`
              found fn pointer `fn(&'a mut Object<'a>) -> &'a mut std::vec::Vec<&'a mut std::boxed::Box<(dyn Objective + 'static)>>`
note: the lifetime `'a` as defined on the method body at 14:5...
  --> src/lib.rs:14:5
   |
14 |     fn get_children(&'a mut self) -> &'_ mut Vec<&'_ mut Box<dyn Objective>> {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...does not necessarily outlive the lifetime `'a` as defined on the impl at 13:6
  --> src/lib.rs:13:6
   |
13 | impl<'a> Objective for Object<'a> {
   |      ^^

error[E0308]: method not compatible with trait
  --> src/lib.rs:14:5
   |
14 |     fn get_children(&'a mut self) -> &'_ mut Vec<&'_ mut Box<dyn Objective>> {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
   |
   = note: expected fn pointer `fn(&'a mut Object<'a>) -> &'a mut std::vec::Vec<&'a mut std::boxed::Box<(dyn Objective + 'static)>>`
              found fn pointer `fn(&'a mut Object<'a>) -> &'a mut std::vec::Vec<&'a mut std::boxed::Box<(dyn Objective + 'static)>>`
note: the lifetime `'a` as defined on the impl at 13:6...
  --> src/lib.rs:13:6
   |
13 | impl<'a> Objective for Object<'a> {
   |      ^^
note: ...does not necessarily outlive the lifetime `'a` as defined on the method body at 14:5
  --> src/lib.rs:14:5
   |
14 |     fn get_children(&'a mut self) -> &'_ mut Vec<&'_ mut Box<dyn Objective>> {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Вот моя более ранняя попытка, в которой я явно не указывал время жизни:

pub trait Objective {
    fn get_children(&'_ mut self) -> &'_ mut Vec<&'_ mut Box<dyn Objective>>;
    fn get_parent(&'_ mut self) -> &'_ mut Box<dyn Objective>;
    fn update(&'_ self) -> ();
}

// #[derive(Objective)]
pub struct Object<'a> {
    children: Vec<&'a mut Box<dyn Objective>>,
    parent: &'a mut Box<dyn Objective>,
}

impl Objective for Object<'_> {
    fn get_children(&'_ mut self) -> &'_ mut Vec<&'_ mut Box<dyn Objective>> {
        return &mut self.children;
    }

    fn get_parent(&'_ mut self) -> &'_ mut Box<dyn Objective> {
        return self.parent;
    }

    fn update(&'_ self) -> () {}
}

И я получил аналогичную ошибку:

error[E0308]: mismatched types
  --> src/lib.rs:15:16
   |
15 |         return &mut self.children;
   |                ^^^^^^^^^^^^^^^^^^ lifetime mismatch
   |
   = note: expected mutable reference `&mut std::vec::Vec<&mut std::boxed::Box<(dyn Objective + 'static)>>`
              found mutable reference `&mut std::vec::Vec<&mut std::boxed::Box<(dyn Objective + 'static)>>`
note: the anonymous lifetime #1 defined on the method body at 14:5...
  --> src/lib.rs:14:5
   |
14 | /     fn get_children(&'_ mut self) -> &'_ mut Vec<&'_ mut Box<dyn Objective>> {
15 | |         return &mut self.children;
16 | |     }
   | |_____^
note: ...does not necessarily outlive the lifetime `'_` as defined on the impl at 13:27
  --> src/lib.rs:13:27
   |
13 | impl Objective for Object<'_> {
   |                           ^^

Как это исправить?


person Tr3nchHvlk    schedule 21.07.2020    source источник
comment
Вы можете заставить его скомпилироваться, как этим, но это на практике, скорее всего, будет неприменим. Если вы хотите представить дерево с обратными ссылками, вы, вероятно, захотите Rc< /a> и Weak. См. также это.   -  person Jmb    schedule 21.07.2020


Ответы (1)


Укажите время жизни черты, и она сработает:

pub trait Objective<'a> {
    fn get_children(&mut self) -> &mut Vec<&'a mut Box<dyn Objective<'a>>>;
    fn get_parent(&mut self) -> &mut Box<dyn Objective<'a>>;
    fn update(&self) -> ();
}

// #[derive(Objective)]
pub struct Object<'a> {
    children: Vec<&'a mut Box<dyn Objective<'a>>>,
    parent: Box<dyn Objective<'a>>,
}

impl<'a> Objective<'a> for Object<'a> {
    fn get_children(&mut self) -> &mut Vec<&'a mut Box<dyn Objective<'a>>> {
        &mut self.children
    }

    fn get_parent(&'_ mut self) -> &mut Box<dyn Objective<'a>> {
        &mut self.parent
    }

    fn update(&'_ self) -> () {}
}

Указав время жизни в Objective, мы можем быть уверены, что разработчики вернут Vec изменяемых ссылок, которые удовлетворяют тем же требованиям к времени жизни, что и требуемое трейтом.

person richerarc    schedule 21.07.2020