I would like to store the reference to an object within the object itself. Using Arc is too much heavy lifting for my use-case. And as far as I know annotating Composite with 'object lifetime will just make it spread, but I don't really want the user of my API to deal with lifetime, it's an implementation detail and not necessary to expose to the user. Is there some good way to express what I want without spreading lifetime annotations everywhere?
pub struct Member<'object, T : 'object>
{
value: T,
parent: Option<&'object Composite>,
}
impl<'object, T : 'object> Member<'object, T>
{
pub fn new(value: T) -> Member<'object, T>
{
Member {value, parent: None}
}
}
pub struct Composite
{
member: Member<'static, usize>,
}
impl Composite
{
pub fn new(value: usize) -> Composite
{
Composite { member: Member::new(value) }
}
pub fn set_parent<'object>(&'object mut self)
{
self.member.parent = Some(self);
}
}
fn main()
{
let item: Composite = Composite::new(1);
item.set_parent();
}