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