简体   繁体   English

自我嵌入自我的一生,而不是自我

[英]Lifetime of self embedded in Self, not &'a self

Below I have下面我有

impl<'a, T> AsPolyIterator<'a, T> for MyPolynomial<'a, T> {
    fn as_poly_iterator(
        &self
    ) -> Result<

which shouldn't need &'a self because the 'a already comes from MyPolynomial<'a, T> .这不应该需要&'a self因为'a已经来自MyPolynomial<'a, T> Also, I should pass &'a[T] in PolyIterator::new in the line另外,我应该在PolyIterator::new中通过&'a[T]

let iterator = PolyIterator::new(self.coefficients.as_life_ref());

but I'm getting an error saying that it wants a &'a (dyn Mem<'a, T> + 'a) which I have no idea why.但我收到一个错误,说它想要一个&'a (dyn Mem<'a, T> + 'a)我不知道为什么。

pub trait Mem<'r, T>
{
    fn as_life_ref(&'r self) -> &'r [T];
}

pub struct PolyIterator<'a, T> {
    coefficients:   &'a [T]
}

impl<'a, T> PolyIterator<'a, T> {
    pub fn new(coefficients: &'a[T]) -> PolyIterator<'a, T> {
        todo!()
    }
}

pub struct MyPolynomial<'a, T> {
    coefficients:   Box<dyn Mem<'a, T> + 'a>,
}

pub trait AsPolyIterator<'a, T> {
    fn as_poly_iterator(&'a self) -> Result<PolyIterator<'a, T>, ()>;
}

impl<'a, T> AsPolyIterator<'a, T> for MyPolynomial<'a, T> {
    fn as_poly_iterator(
        &self
    ) -> Result<
        PolyIterator<'a, T>,
        (),
    > {
        let iterator = PolyIterator::new(self.coefficients.as_life_ref());
        todo!()
    }
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=c3e657136252b96cd5aca91efeaea56f https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=c3e657136252b96cd5aca91efeaea56f

error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
  --> src/lib.rs:31:54
   |
31 |         let iterator = PolyIterator::new(self.coefficients.as_life_ref());
   |                                                            ^^^^^^^^^^^
   |
note: first, the lifetime cannot outlive the anonymous lifetime defined here...
  --> src/lib.rs:26:3
   |
26 |         &self
   |         ^^^^^
note: ...so that reference does not outlive borrowed content
  --> src/lib.rs:31:36
   |
31 |         let iterator = PolyIterator::new(self.coefficients.as_life_ref());
   |                                          ^^^^^^^^^^^^^^^^^
note: but, the lifetime must be valid for the lifetime `'a` as defined here...
  --> src/lib.rs:24:6
   |
24 | impl<'a, T> AsPolyIterator<'a, T> for MyPolynomial<'a, T> {
   |      ^^
note: ...so that the types are compatible
  --> src/lib.rs:31:54
   |
31 |         let iterator = PolyIterator::new(self.coefficients.as_life_ref());
   |                                                            ^^^^^^^^^^^
   = note: expected `&'a (dyn Mem<'a, T> + 'a)`
              found `&(dyn Mem<'a, T> + 'a)`

Your trait definition and implementation do not line up:您的特征定义和实现不对齐:

pub trait AsPolyIterator<'a, T> {
    fn as_poly_iterator(&'a self) -> Result<PolyIterator<'a, T>, ()>;
}

impl<'a, T> AsPolyIterator<'a, T> for MyPolynomial<'a, T> {
    fn as_poly_iterator(&self) -> Result<PolyIterator<'a, T>, ()> { ... }
}

The trait has PolyIterator bound to self , while the implementation has it bound elsewhere, these are not compatible.该特征将PolyIterator绑定到self ,而实现将其绑定到其他地方,这些是不兼容的。


It should definitely be &'a self since the PolyIterator is derived from self.coefficients , but you should use a different lifetime 'b for MyPolynomial to avoid over-constraining:它绝对应该是&'a self因为PolyIterator是从self.coefficients派生的,但是您应该为MyPolynomial使用不同的生命周期'b以避免过度约束:

impl<'a, 'b, T> AsPolyIterator<'a, T> for MyPolynomial<'b, T> where 'a: 'b {
    fn as_poly_iterator(&'a self) -> Result<PolyIterator<'a, T>, ()> { ... }
}

Mind the where 'a: 'b since that ensures the lifetime 'b is larger than 'a .注意where 'a: 'b ,因为这确保了生命周期'b大于'a See it working on the playground .看到它在操场上工作。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM