0

I have the following code:

struct BufferPool {}

struct Buffer<'a> {
    pool: &'a BufferPool,
}

impl BufferPool {
    pub fn alloc(&self) -> Buffer<'_> {
        Buffer { pool: self }
    }
}

struct User {
    pool: BufferPool,
    buffer: Buffer<'what_lifetime_here?>,
}

The buffer object in the User struct is guaranteed to be allocated by the pool object in the same struct, as long as the pool object in the User struct is valid, buffer is valid too. But buffer requires a lifetime specifier, I am wondering if I can borrow the lifetime of the pool object for buffer, or do I have to lift the lifetime of the object that buffer depends on from the pool object to the User object like: struct User<'a>? Or there's another way around?

neevek
  • 11,760
  • 8
  • 55
  • 73
  • @cafce25 Actually I just read that post before posting the question, it's not the same, my question is about how to specify lifetime in this specific case. – neevek Apr 02 '23 at 16:24
  • 2
    The linked question is about what you try to do is not possible and why. So asking about how to do it can be answered with you can't. – cafce25 Apr 02 '23 at 16:25

0 Answers0