match + RefCell = X does not live long enough

2019-07-12 18:15发布

问题:

I need to initialize an item (fn init(&mut self) -> Option<&Error>), and use it if there's no errors.

pub fn add(&mut self, mut m: Box<Item>) {
    if let None = m.init() {
        self.items.push(m);
    }
}

This works unless I need to check the error if there's any:

pub fn add(&mut self, mut m: Box<Item>) {
    if let Some(e) = m.init() {
        //process error
    } else {
        self.items.push(m); //won't compile, m is borrowed
    }
}

Fair. Need to use RefCell. However, this

pub fn add(&mut self, mut m: Box<Item>) {
    let rc = RefCell::new(m);

    if let Some(e) = rc.borrow_mut().init() {           
        //process error         
    } else {
        self.items.push(rc.borrow_mut())
    }
}

ends with weird

error: `rc` does not live long enough
        if let Some(e) = rc.borrow_mut().init() {
                     ^~
note: reference must be valid for the destruction scope surrounding block at 75:60...
    pub fn add_module(&mut self, mut m: Box<RuntimeModule>) {
                                                        ^
note: ...but borrowed value is only valid for the block suffix following statement 0 at 76:30
        let rc = RefCell::new(m);

I tried nearly everything: plain box, Rc'ed box, RefCell'ed box, Rc'ed RefCell. Tried to adapt this answer to my case. No use.

Complete example:

use std::cell::RefCell;
use std::error::Error;

trait Item {
    fn init(&mut self) -> Option<&Error>;
}

struct ItemImpl {}

impl Item for ItemImpl {
    fn init(&mut self) -> Option<&Error> {
        None
    }
}

//===========================================

struct Storage {
    items: Vec<Box<Item>>,
}

impl Storage {
    fn new() -> Storage {
        Storage{
            items: Vec::new(),
        }
    }

    fn add(&mut self, mut m: Box<Item>) {
        let rc = RefCell::new(m);

        if let Some(e) = rc.borrow_mut().init() {           
            //process error         
        } else {
            self.items.push(*rc.borrow_mut())
        }
    }
}

fn main() {
    let mut s = Storage::new();
    let mut i = Box::new(ItemImpl{});
    s.add(i);
}

(Playground)

UPD: As suggested, this is a "family" of mistakes like I did, it is well explained here. However my case has easier solution.

回答1:

As krdln suggested, the simplest way to work around this is to return in the if block and thus scope the borrow:

fn add(&mut self, mut m: Box<Item>) {
    if let Some(e) = m.init() {
        //process error
        return;
    } 
    self.items.push(m);
}