gpt4 book ai didi

rust - 从闭包调用可变方法时,无法推断autoref的生存期

转载 作者:行者123 更新时间:2023-12-03 11:36:11 24 4
gpt4 key购买 nike

这是一个再现错误的游乐场链接:https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=86ec4f11f407f5d04a8653cc904f991b
我有一个特性FooTraitMut,可以访问BarStruct内部的特定范围的数据,并且我想对此特性进行概括,以便它可以在锁步中访问多个BarStruct上的相同范围。所以我有一个MutChannels特性,它像一个类型级别的函数一样来生成访问者需要的引用元组,例如(T, U) --> (&mut T, &mut U)
我实际上还没有到达使用Channels2的地步,因为我无法使用更简单的Channels1案例来工作。
在操场上,对不变特性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。根据我的理解, FnMut保留外部生存期是不合理的,因为该函数可能会泄漏超出其范围的引用,并且可能违反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不同的错误,我不确定该如何处理。

关于rust - 从闭包调用可变方法时,无法推断autoref的生存期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66309886/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com