Scoped Reference

Crates.io Docs.rs

This crate provides runtime-checked borrow lifetimes. It allows one to store references of the form &'a T as structs with lifetime 'static. This is useful in situations where a reference with a shorter lifetime cannot be stored naturally.

The following example demonstrates the use of scoped references. Scoped references come in both mutable and immutable variants. If the underlying reference is dropped while scoped borrows to it still exist, then the program panics. Note that a panic will always cause an immediate abort - unwinding is not allowed - because allowing unwinding could lead to dangling references and undefined behavior.

```rust struct StaticBorrow(ScopedBorrow);

let mut x = 10; let borrowedx = &mut x; let mut scopedref = ScopedReference::newmut(borrowedx);

let mut mutreftox = scopedref.borrowmut(); *mutreftox = 9;

// Panic: mutreftox is still out! // drop(scopedref);

drop(mutrefto_x);

let staticborrow = StaticBorrow(scopedref.borrow()); asserteq!(*staticborrow.0, 9);

// Panic: staticborrow is still out! // drop(scopedref);

drop(staticborrow); drop(scopedref); ```