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

Cannot infer lifetime for autoref when calling mutable method from closure

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

我有一个特征 FooTraitMut 可以访问 BarStruct 中特定范围的数据,我想推广这个特征,以便它可以访问多个 [=] 中的相同范围13=]s 步调一致。所以我有一个 MutChannels 特征,它像类型级函数一样产生访问者需要的引用元组,例如(T, U) --> (&mut T, &mut U).

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

在 playground 中,对不可变特征 FooTraitRef 进行了同样的操作,它按预期工作。但是由于 autoref 生命周期问题,可变的被破坏了。我认为 self 的生命周期正在发生某种隐式转换,因为我可以内联 indexer 函数并且它工作正常。

如有任何帮助,我们将不胜感激。

有问题的代码:

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。根据我的理解,FnMuts 保留外部生命周期是不合理的,因为该函数能够将引用泄漏到其范围之外,并可能违反 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 不同的错误,我不确定如何处理。