rune icon indicating copy to clipboard operation
rune copied to clipboard

objects in rooted structs

Open CeleritasCelery opened this issue 2 years ago • 0 comments

Normally an object has a lifetime parameter like object<'a>. However When an object is rooted in a struct there is no good value to set this parameter to. Really it should be something like 'self but that doesn't exist yet. So we store objects in a RawObj form that has no lifetime. It is similar to storing a raw pointer. The only way to safely get a normal object back out is if RootObj is wrapped in RootRef. This ensures that the object is rooted and safe to use.

https://github.com/CeleritasCelery/rune/blob/7136b74386a4586537ffa59c3be4849cb76354fe/src/arena/root.rs#L200-L204

Why this is sound

We don't have any methods with RootRef that expose the RawObj directly. We only create them from objects with lifetimes since those are guaranteed to be valid. So long as we can use root projection to get a raw object, we know the base struct it is part of is rooted and will be traced. Therefore it is safe to "dereference".

CeleritasCelery avatar Apr 11 '22 21:04 CeleritasCelery