Trying to do something like Cell::Ref, with safe code

⚓ Rust    📅 2025-10-11    👤 surdeus    👁️ 5      

surdeus

Ref: Rust Playground

This is a set of types to implement single ownership with multiple weak pointers on top of safe Rc<RefCell>. OwnerCell owns T, via a Rc<RefCell>. OwnerCell is not cloneable, so it's single ownership. But you can get weak pointers from it. The weak references, if borrowed, do an upgrade and a borrow. Neither the borrow nor the upgrade can fail, because there's a check when OwnerCell is dropped for any outstanding borrows or weak references.

I modeled this after the way Cell::Ref works. But Cell::Ref uses a raw pointer to avoid problems with destruction order. So my example won't compile. Is it possible to do this entirely in safe Rust?

2 posts - 2 participants

Read full topic

🏷️ Rust_feed