简体   繁体   English

我怎样才能让 impl Trait 使用适当的生命周期来对具有另一个生命周期的值进行可变引用?

[英]How can I get impl Trait to use the appropriate lifetime for a mutable reference to a value with another lifetime in it?

I have a struct with a lifetime:我有一个终生的结构:

struct HasLifetime<'a>( /* ... */ );

There is there is an implementation of the trait Foo :有一个特性Foo的实现:

impl<'a, 'b: 'a> Foo for &'a mut HasLifetime<'b> { }

I want to implement the following function:我想实现以下功能:

fn bar_to_foo<'a, 'b: 'a>(bar: &'a mut Lifetime<'b>) -> impl Foo {
    bar
}

This won't compile because the returned impl is only valid for 'a .这不会编译,因为返回的impl仅对'a有效。 However, specifying impl Foo + 'a results in:但是,指定impl Foo + 'a导致:

error[E0909]: hidden type for `impl Trait` captures lifetime that does not appear in bounds
 --> src/main.rs:7:60
  |
7 | fn bar_to_foo<'a, 'b: 'a>(bar: &'a mut HasLifetime<'b>) -> impl Trait + 'a {
  |                                                            ^^^^^^^^^^^^^^^
  |
note: hidden type `&'a mut HasLifetime<'b>` captures the lifetime 'b as defined on the function body at 7:1
 --> src/main.rs:7:1
  |
7 | fn bar_to_foo<'a, 'b: 'a>(bar: &'a mut HasLifetime<'b>) -> impl Trait + 'a {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The seemingly equivalent function with a boxed trait object compiles:带有装箱 trait 对象的看似等效的函数编译:

fn bar_to_foo<'a, 'b: 'a>(bar: &'a mut Lifetime<'b>) -> Box<Foo + 'a> {
    Box::new(bar)
}

How can I define bar_to_foo with impl Trait ?如何使用impl Trait定义bar_to_foo

Playground link 游乐场链接

You need to indicate that the returned value is built upon multiple lifetimes.您需要指出返回的值是建立在多个生命周期之上的。 However, you can't use multiple lifetime bounds with impl Trait , and attempting to do so doesn't have a useful error message .但是,您不能对impl Trait使用多个生命周期边界,并且尝试这样做不会产生有用的错误消息

There's a trick you can use that involves creating a dummy trait that has a lifetime parameter: 您可以使用一个技巧来创建一个具有生命周期参数的虚拟特征:

trait Captures<'a> {}
impl<'a, T: ?Sized> Captures<'a> for T {}

fn bar_to_foo<'a, 'b: 'a>(bar: &'a mut HasLifetime<'b>) -> impl Trait + Captures<'b> + 'a {
    bar
}

Thankfully, this only occurs when the "hidden" lifetime is invariant , which occurs because the reference is mutable.值得庆幸的是,这仅在“隐藏”生命周期不变时才会发生,这是因为引用是可变的。

暂无
暂无

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

相关问题 从 trait 方法返回对结构字段的可变引用时,如何修复生命周期不匹配? - How do I fix the lifetime mismatch when returning a mutable reference to the struct's field from a trait method? 在实现返回可变引用的迭代器时,如何解决“无法为 autoref 推断适当的生命周期”? - How can I fix “cannot infer an appropriate lifetime for autoref” when implementing an iterator that returns mutable references? 当我在结构中使用可变引用而不是不可变引用时,为什么会出现生命周期错误? - Why do I get a lifetime error when I use a mutable reference in a struct instead of an immutable reference? 为什么我可以使用可变变量使其生命周期与不可变引用重叠,但我不能以相同的方式使用可变引用? - Why can I use a mutable variable such that its lifetime overlaps with an immutable reference, but I can't use a mutable reference in the same way? 如何获得结构(受生命周期约束)字段的可变引用? - How to get mutable reference of struct(constrained with lifetime) field? 如何使用生命周期来解决“引用必须对静态生命周期有效” - How can I use lifetime bounds to solve “reference must be valid for the static lifetime” 为什么在使用嵌套的可变引用时,我会收到错误“无法推断泛型参数的适当生命周期”? - Why do I get the error “cannot infer an appropriate lifetime for lifetime parameter in generic type” when using nested mutable references? 如何解决生存期错误以在Rust中进行可变引用? - How to resolve lifetime error for mutable reference in Rust? 如何使用 structs 和 impl 的生命周期来推断实现的适当生命周期? - How to infer an appropriate lifetime for an implementation using lifetimes for structs and impl? 我如何在 rust 实现中为具有 static 生命周期的特征指定结构生命周期? - How can i specify struct lifetime in rust implementation for trait with static lifetime?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM