使用traits作为类型参数时,借用检查器失败

Vae*_*den 5 rust borrow-checker

在结构中使用traits作为类型参数时,我遇到了借用检查器的问题:

trait Trait {}

struct FooBar;
impl Trait for FooBar{}

struct Observer<Arg> {
    action: Box<Fn(Arg) + Send>,
    // Other fields
}

impl <Arg> Observer<Arg> {
    fn new(action: Box<Fn(Arg) + Send>) -> Observer<Arg> {
        Observer{action: action}
    }

    fn execute(&self, arg: Arg) {
        (*self.action)(arg);
    }
}

fn test() {
    let mut foobar = FooBar;
    {
        let mut observer = Observer::new(Box::new(|&: param: &mut Trait| {
            // do something with param here
        }));
        observer.execute(&mut foobar);   // First borrow passes ...
        observer.execute(&mut foobar);   // This fails as "foobar" is already borrowed
    }   // The previous borrow ends here (lifetime of "observer")
}
Run Code Online (Sandbox Code Playgroud)

输出是:

error: cannot borrow `foobar` as mutable more than once at a time
    observer.execute(&mut foobar);   // This fails as "foobar" is already borrowed
                          ^~~~~~
note: previous borrow of `foobar` occurs here; the mutable borrow prevents subsequent moves, borrows, or modification of `foobar` until the borrow ends
    observer.execute(&mut foobar);   // First borrow passes ...
                          ^~~~~~
note: previous borrow ends here
 {
...
 }   // The previous borrow ends here (lifetime of "observer")
 ^
Run Code Online (Sandbox Code Playgroud)

然而以下示例有效:

trait Trait {}

struct FooBar;
impl Trait for FooBar{}

struct Observer {
    action: Box<Fn(&mut Trait) + Send>,
    // Other fields
}

impl Observer {
    fn new(action: Box<Fn(&mut Trait) + Send>) -> Observer {
        Observer{action: action}
    }

    fn execute(&self, arg: &mut Trait) {
        (*self.action)(arg);
    }
}

fn test() {
    let mut foobar = FooBar;
    {
        let mut observer = Observer::new(Box::new(|&: param: &mut Trait| {
            // do something with param here
        }));
        observer.execute(&mut foobar);
        observer.execute(&mut foobar);
    }
}
Run Code Online (Sandbox Code Playgroud)

这看起来很奇怪对我来说,作为第二个例子就是第一个例子中的一个实例,我大概可以(痛苦)实现与宏同样的事情.

我想这是相当棘手,因为我需要知道封闭拍摄参数的类型,但我并不需要存储这个参考...

这是借用检查器中的错误吗?或者我做错了什么?

rustc 1.0.0-nightly (44a287e6e 2015-01-08 17:03:40 -0800)
Run Code Online (Sandbox Code Playgroud)

编辑1:精确用例

编辑2:由于在下面的答案解释的,问题是,借检查强制的寿命Observer<&mut Type>是一样的&mut Type,所以其实这个问题是不相关的事实,我们用一个特质作为一个类型参数(其与实际结构相同).
所以在我的情况下,我可以通过定义Observer<Arg>这样一个解决方法:

struct Observer<Arg> {
    action: Box<Fn(&mut Arg) + Send>,
}
Run Code Online (Sandbox Code Playgroud)

所以类型参数Arg本身不是引用,但这使得代码不那么通用.有人有更好的解决方案吗?

Ren*_*non 3

这里的问题是借用检查器强制引用的生命周期&mut Trait与整体相同GenericStruct。我相信这是因为引用是结构本身的类型参数。

由于您的结构没有存储引用的字段(如果您需要在原始代码中执行此操作,请更新您的问题),那么您可以将类型参数移动到方法本身,而不是结构:

trait Trait{}

struct FooBar;
impl Trait for FooBar{}

struct GenericStruct;

impl GenericStruct {
    fn bar<T>(&self, _: T) {}
}

fn main() {
    let mut foobar = FooBar;

    {
        let foo = GenericStruct;
        foo.bar(&mut foobar);
        foo.bar(&mut foobar);
    }
}
Run Code Online (Sandbox Code Playgroud)

这将使借用仅在调用 时持续foo.bar()