繁体   English   中英

从闭包调用可变方法时无法推断 autoref 的生命周期

[英]Cannot infer lifetime for autoref when calling mutable method from closure

这是重现错误的游乐场链接: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=86ec4f11f407f5d04a8653cc904f991b

我有一个特征FooTraitMut ,它提供对BarStruct内部特定数据范围的访问,我想概括这个特征,以便它可以同步访问多个BarStruct上的相同范围。 所以我有一个MutChannels特征,它的作用类似于类型级别的 function 来生成访问者需要的引用元组,例如(T, U) --> (&mut T, &mut U)

我实际上还没有达到使用Channels2的地步,因为我无法让更简单的Channels1案例工作。

在 Playground 中,对不可变特征FooTraitRef执行相同的操作,它可以按预期工作。 但是由于自动引用生命周期问题,可变的被破坏了。 我认为self的生命周期正在发生某种隐式转换,因为我可以内联indexer function 并且它工作正常。

任何帮助将不胜感激。

有问题的代码:

struct BarStruct<T> {
    data: [T; 1],
}

pub struct Channels1<T>(T);
pub struct Channels2<T, U>(T, U);

fn indexer(mut f: impl FnMut(usize)) {
    f(0)
}

trait FooMutTrait {
    type Data: for<'a> MutChannels<'a>;

    fn foo<'a, F>(&'a mut self, f: F)
    where
        F: FnMut(<Self::Data as MutChannels<'a>>::Mut);
}

trait MutChannels<'a> {
    type Mut;
}

impl<'a, T: 'a> MutChannels<'a> for Channels1<T> {
    type Mut = &'a mut T;
}
impl<'a, T: 'a, U: 'a> MutChannels<'a> for Channels2<T, U> {
    type Mut = (&'a mut T, &'a mut U);
}

impl<T> BarStruct<T> {
    fn get_data_mut<'a>(&'a mut self, i: usize) -> &'a mut T {
        &mut self.data[i]
    }
}

impl<T> FooMutTrait for BarStruct<T>
where
    T: 'static,
{
    type Data = Channels1<T>;

    #[inline]
    fn foo<'a, F>(&'a mut self, mut f: F)
    where
        F: FnMut(<Self::Data as MutChannels<'a>>::Mut),
    {
        indexer(|i| f(self.get_data_mut(i)))
        
        // This works.
        // f(self.get_data_mut(0))
    }
}

和错误:

error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
  --> src/lib.rs:85:28
   |
85 |         indexer(|i| f(self.get_data_mut(i)))
   |                            ^^^^^^^^^^^^
   |
note: first, the lifetime cannot outlive the lifetime `'_` as defined on the body at 85:17...
  --> src/lib.rs:85:17
   |
85 |         indexer(|i| f(self.get_data_mut(i)))
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...so that closure can access `self`
  --> src/lib.rs:85:23
   |
85 |         indexer(|i| f(self.get_data_mut(i)))
   |                       ^^^^
note: but, the lifetime must be valid for the lifetime `'a` as defined on the method body at 81:12...
  --> src/lib.rs:81:12
   |
81 |     fn foo<'a, F>(&'a mut self, mut f: F)
   |            ^^
note: ...so that reference does not outlive borrowed content
  --> src/lib.rs:85:23
   |
85 |         indexer(|i| f(self.get_data_mut(i)))
   |                       ^^^^^^^^^^^^^^^^^^^^

这个错误可以通过这个例子重现:

fn indexer(mut f: impl FnMut()) {}

fn foo<'a, F>(a: &'a mut String, mut f: F)
where
    F: FnMut(&'a mut str),
{
    indexer(|| f(a.as_mut_str()));
}
error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
 --> src/lib.rs:7:20
  |
7 |     indexer(|| f(a.as_mut_str()));
  |                    ^^^^^^^^^^
  |
note: first, the lifetime cannot outlive the lifetime `'_` as defined on the body at 7:13...
 --> src/lib.rs:7:13
  |
7 |     indexer(|| f(a.as_mut_str()));
  |             ^^^^^^^^^^^^^^^^^^^^
note: ...so that closure can access `a`
 --> src/lib.rs:7:18
  |
7 |     indexer(|| f(a.as_mut_str()));
  |                  ^
note: but, the lifetime must be valid for the lifetime `'a` as defined on the function body at 3:8...
 --> src/lib.rs:3:8
  |
3 | fn foo<'a, F>(a: &'a mut String, mut f: F)
  |        ^^
note: ...so that reference does not outlive borrowed content
 --> src/lib.rs:7:18
  |
7 |     indexer(|| f(a.as_mut_str()));
  |                  ^^^^^^^^^^^^^^

正在发生的事情是F被键入以期望一个'a引用,但这不是闭包可以提供的。 闭包将&'a mut T转换为更短的生命周期&'_ mut T 据我了解, FnMut保留外部生命周期是不合理的,因为 function 将能够泄漏其 scope 之外的引用,并且可能违反 Rust 的引用保证。 这个问题不会出现不可变借用,因为它们的限制较少并且关闭不会缩短它们的生命周期。

这可以通过允许F在任何生命周期内工作来解决:

fn indexer(mut f: impl FnMut()) {}

fn foo<'a, F>(a: &'a mut String, mut f: F)
where
    F: FnMut(&mut str), // <--------
{
    indexer(|| f(a.as_mut_str()));
}

或者使用FnOnce ,因为它的功能受到更多限制并且不需要缩短生命周期:

fn indexer(f: impl FnOnce()) {} // <--------

fn foo<'a, F>(a: &'a mut String, mut f: F)
where
    F: FnOnce(&'a mut str), // <--------
{
    indexer(move || f(a.as_mut_str())); // added move so that the reference isn't reborrowed
}

FnOnce更改适用于您的案例是微不足道的。 但是,放宽F在任何生命周期工作都会遇到有关MutChannels<'_>::Mut&'_ mut T不同的错误,我不确定如何处理。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM