Add methods to 'leak' RefCell borrows as references with the lifetime of the original reference by 197g · Pull Request #68712 · rust-lang/rust (original) (raw)
Usually, references to the interior are only created by the Deref andDerefMut impl of the guards Ref and RefMut. Note that RefCell
already has to cope with leaks of such guards which, when it occurs,
effectively makes it impossible to ever acquire a mutable guard or any
guard for Ref and RefMut respectively. It is already safe to use
this to create a reference to the inner of the ref cell that lives as
long as the reference to the RefCell itself, e.g.
fn leak(r: &RefCell) -> Option<&usize> { let guard = r.try_borrow().ok()?; let leaked = Box::leak(Box::new(guard)); Some(&*leaked) }
The newly added methods allow the same reference conversion without an
indirection over a leaked allocation. It's placed on the Ref/RefMut to
compose with both borrow and try_borrow directly.