简体   繁体   中英

Why doesn't a trait implementation compile for a struct, but it compiles for a reference to the struct?

To answer one of my previous questions ( How to implement a generic trait with a generic type implementing Iterator? ), this chunk of code was given to me:

pub trait Vector {
    type Item;
    type Iter: Iterator<Item = Self::Item>;

    // several functions
    fn iter(&self) -> Self::Iter;
}

pub struct VectorImplementation1<T> {
    numbers: Vec<T>,
}

impl<'a, T> Vector for &'a VectorImplementation1<T> {
    type Item = &'a T;
    type Iter = std::slice::Iter<'a, T>;

    fn iter(&self) -> Self::Iter {
        self.numbers.iter()
    }
}

fn main() {}

I see that the trait is implemented for a reference of struct, and it doesn't compile if I use only a struct. Can someone explain why?

The problem here, as mentioned by the compiler error, is that the lifetime 'a has no reason to be there if it is implemented like so:

impl<'a, T> Vector for VectorImplementation1<T> {
    /**/
}
error[E0207]: the lifetime parameter `'a` is not constrained by the impl 
trait, self type, or predicates
  --> src/main.rs:13:6
   |
13 | impl<'a, T> Vector for VectorImplementation1<T> {
   |      ^^ unconstrained lifetime parameter

Because the compiler only looks at the definition, and not the body in this case. A different approach, which probably wasn't mentioned earlier for simplicity, is the following:

pub trait Vector<'a> {
    type Item: 'a;
    type Iter: Iterator<Item = Self::Item> + 'a;

    // several functions
    fn iter(&'a self) -> Self::Iter;
}

pub struct VectorImplementation1<T> {
    numbers: Vec<T>,
}

impl<'a, T: 'a> Vector<'a> for VectorImplementation1<T> {
    type Item = &'a T;
    type Iter = std::slice::Iter<'a, T>;

    fn iter(&'a self) -> Self::Iter {
        self.numbers.iter()
    }
}

impl<'a, T: 'a> Vector<'a> for &'a VectorImplementation1<T> {
    type Item = &'a T;
    type Iter = std::slice::Iter<'a, T>;

    fn iter(&'a self) -> Self::Iter {
        self.numbers.iter()
    }
}

In this case, we move the lifetime to the trait, so that the trait can "use" the lifetime, and therefore validate our use of the trait implementation. But as I mentioned earlier, this has the added complexity of needing to understand the lifetimes attached to this trait, reducing readability.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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