【问题标题】:Borrow checker failing when using traits as type parameter使用特征作为类型参数时借用检查器失败
【发布时间】:2015-01-13 20:40:16
【问题描述】:

在结构中使用特征作为类型参数时,借用检查器出现问题:

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")
}

输出是:

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")
 ^

以下示例仍然有效:

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);
    }
}

这对我来说看起来很奇怪,因为第二个示例只是第一个示例的实例化,我可能(痛苦地)用宏实现相同的东西。

我想这很棘手,因为我需要知道闭包所采用的参数的类型,但我不需要存储这个引用......

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

rustc 1.0.0-nightly (44a287e6e 2015-01-08 17:03:40 -0800)

编辑 1:精确用例

编辑 2:正如下面的答案所解释的,问题是借用检查器强制 Observer&lt;&amp;mut Type&gt; 的生命周期与 &amp;mut Type 相同,所以实际上这个问题与我们无关使用 trait 作为类型参数(对实际结构也是如此)。
因此,就我而言,我可以通过像这样定义Observer&lt;Arg&gt; 来解决问题:

struct Observer<Arg> {
    action: Box<Fn(&mut Arg) + Send>,
}

所以类型参数 Arg 本身不是一个引用,但这会降低代码的通用性。谁有更好的解决方案?

【问题讨论】:

    标签: rust borrow-checker


    【解决方案1】:

    这里的问题是借用检查器强制&amp;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);
        }
    }
    

    这将使借用只持续到调用foo.bar()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-22
      • 1970-01-01
      • 2011-12-12
      • 2020-09-01
      相关资源
      最近更新 更多