簡體   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