Store allocated data alongside allocator on struct

I want to use the memory allocator M to allocate an Allocated which is gonna live together with M inside a struct. However, I don't want to pollute UseM with its lifetime. Is there a way to store Allocated inside UseM?

use std::marker::PhantomData;

pub struct Allocated<'r, T> {
    _phantom: PhantomData<&'r T>
}

pub trait MemPool<'s, 'r, T> {
	fn allocate_default(
		&'s self,
		size: usize,
	) -> Allocated<'r, T>;
}

struct M<T> {
    _phantom: PhantomData<T>
}

impl<'s, T> MemPool<'s, 's, T> for M<T>
{
    fn allocate_default(
		&'s self,
		size: usize,
	) -> Allocated<'s, T>{
	    todo!()
	}
}

struct UseM<T> {
    //I wanted to allocated with m
    m: M<T>,
    //and store here, withotu `UseM` having to have a lifetime
    allocated: Allocated<'r, T>
}
error[E0261]: use of undeclared lifetime name `'r`
  --> src/lib.rs:32:26
   |
28 | struct UseM<T> {
   |             - help: consider introducing lifetime `'r` here: `'r,`
...
32 |     allocated: Allocated<'r, T>
   |                          ^^ undeclared lifetime

If you store something with a lifetime, you're infected with the lifetime. Moreover, if Allocated<'r, T> includes references into M<T>, that would be a self-referential struct.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.