kyren / gc-arena

Incremental garbage collection from safe Rust
Creative Commons Zero v1.0 Universal
436 stars 36 forks source link

Support getting the value out of a _unique_ `Gc<T>`? #88

Closed dragazo closed 7 months ago

dragazo commented 7 months ago

This is just a wild feature request that would be useful for a project I'm working on.

Would it be possible to support something like Rc::into_inner for Gc<T> to get the value out of a Gc<T> if it's a unique pointer to it? I realize this would require reference counting for Gc<T>, which would break Copy (and maybe be a total deal breaker?), but we'd still get to keep Clone and it would only cost an integer increment (and a decrement in Drop).

Also, by doing reference counting, in the future we could possibly do eager collection of objects whose reference count falls to zero so that the mark-and-sweep collector has less work to do for use cases where cycles are possible but somewhat rare (as they are in my case). Although that's a totally separate extra feature and there might be some nuances of the safety proof that would forbid this anyway - I'm just throwing the idea out there lol

If something like this does get added, other useful things might be Rc::get_mut and Rc::make_mut.

kyren commented 7 months ago

I think adding reference counting is a deal breaker, sorry.

It might be worth it to explore a general reference-counting-with-cycle-detection garbage collector design though? I think such a garbage collector wouldn't require branding for memory safety (but might end up requiring something similar for correctness?) and thus might fit situations where gc-arena is currently kind of annoying. I wonder if one exists already (for Rust, obviously)? I haven't looked very hard.

Anyway, gc-arena exists in the design space where you don't want reference counting, and I think you could (should) do an entirely different design for a collector with reference counting.