简体   繁体   English

将结构转换为具有生存期的特征得到“由于需求冲突,无法为生存期参数'a'推断适当的生存期”

[英]casting struct to trait with lifetime got “cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements”

trait BT {
    fn get_a(&self) -> &A;
}

#[derive(Debug)]
struct A {
    v: i32,
}

impl A {
    fn nb(&self) -> Box<BT> {
        Box::new(B { a: self })
    }
}

#[derive(Debug)]
struct B<'a> {
    a: &'a A,
}

impl<'a> BT for B<'a> {
    fn get_a(&self) -> &A {
        return self.a;
    }
}

fn main() {
    println!("{:?}", A { v: 32 }.nb().get_a());
}

A has a method to generate a B instance with a reference of A , and B might have many methods access Ba (A's reference in B). A必须产生的方法B与参考实例AB可能有许多方法来访问Ba (A在B参考)。 If let A.nb() return B instead of BT , the code would work well. 如果让A.nb()返回B而不是BT ,则代码将运行良好。

I'm new to Rust. 我是Rust的新手。 This problem has troubled me all day. 这个问题整日困扰着我。 What should I do to make this code work? 我应该怎么做才能使此代码起作用? Thanks! 谢谢!

The whole error report: 整个错误报告:

error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
  --> src\socket\msg\message.rs:53:26
   |
53 |                 Box::new(B{a: self})
   |                          ^
   |
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 52:13...
  --> src\socket\msg\message.rs:52:13
   |
52 | /             fn nb(&self) -> Box<BT> {
53 | |                 Box::new(B{a: self})
54 | |             }
   | |_____________^
note: ...so that reference does not outlive borrowed content
  --> src\socket\msg\message.rs:53:31
   |
53 |                 Box::new(B{a: self})
   |                               ^^^^
   = note: but, the lifetime must be valid for the static lifetime...
   = note: ...so that the expression is assignable:
           expected std::boxed::Box<socket::msg::message::test::test::BT + 'static>
              found std::boxed::Box<socket::msg::message::test::test::BT>

The default lifetime of a trait object is 'static . 特征对象的默认生存期为'static You need to add an explicit lifetime bound to the trait object returned by nb() function: 您需要添加绑定到nb()函数返回的trait对象的显式生命周期:

impl A {
    fn nb<'s>(&'s self) -> Box<BT+'s> {
        Box::new(B{a: self})
    }
}

Inference of Trait Object Lifetimes 特性对象寿命推断

暂无
暂无

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

相关问题 由于对由具有 Serde Deserialize 的枚举组成的结构的要求相互冲突,因此无法为生命周期参数“de”推断合适的生命周期 - cannot infer an appropriate lifetime for lifetime parameter `'de` due to conflicting requirements for struct made of enums with Serde Deserialize 由于需求冲突,无法推断出合适的生命周期 - Cannot infer an appropriate lifetime due to conflicting requirements 由于递归结构中的冲突要求,无法推断出适当的生命周期 - Cannot infer an appropriate lifetime due to conflicting requirements in a recursive struct 作为函数参数的闭包“由于需求冲突而无法推断出适当的寿命” - Closure as function parameter “cannot infer an appropriate lifetime due to conflicting requirements” 尝试将具有生命周期的返回值设置为结构时,由于存在冲突的要求,无法推断出 autoref 的适当生命周期 - Cannot infer an appropriate lifetime for autoref due to conflicting requirements when tried to set a return value with lifetime to a struct "由于要求冲突,无法推断函数调用中生命周期参数 &#39;_ 的适当生命周期" - cannot infer an appropriate lifetime for lifetime parameter '_ in function call due to conflicting requirements `由于需求冲突,无法为 autoref 推断适当的生命周期`,但由于特征定义约束,无法更改任何内容 - `cannot infer an appropriate lifetime for autoref due to conflicting requirements` but can't change anything due to trait definition constraints 尝试实现迭代器:由于需求冲突而无法推断出适当的生存期 - Trying to implement an iterator: cannot infer an appropriate lifetime due to conflicting requirements 由于需求冲突,无法推断出自动强制的适当寿命 - cannot infer an appropriate lifetime for automatic coercion due to conflicting requirements 由于需求冲突,无法为借用表达式推断出适当的生命周期 - cannot infer an appropriate lifetime for borrow expression due to conflicting requirements
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM