Lifetime of self?


#1

I was wondering, we can refer to an object from within its own code using self and we can refer to that object’s type using Self but is there any way to refer to the lifetime of that object? I don’t mean the lifetime of a reference to that object, but to the lifetime of the object itself.

As an example of what I’m trying to do, is there a lifetime I can use to make the following code compile?

use std::slice::Iter;

trait MakeIntoIter {
    type Item;
    type IntoIter: Iterator<Item=Self::Item>;
    
    fn iter(&self) -> Self::IntoIter;
}

struct Thing (Vec<usize>);

impl MakeIntoIter for Thing {
    type Item = usize;
    type IntoIter = Iter<usize>;
    
    fn iter(&self) -> Iter<usize> {
        self.0.iter()
    }
}

#2

This cannot work, the type of items yielded by Iter is &usize.

With that fixed, you can do this: http://is.gd/4YK9FM


#3

Thanks for the example, it was something I was wondering too.

A smaller fix in Yiab’s API for the usize/&usize issue would probably be type Item = &'i usize; instead of changing the trait signature.


#4

That’s also possible, yes.