Introduction
This crate provides safe references with runtime-checked lifetimes. While a reference is in use, the owner cannot drop the referenced value.
The main entry-point for this library is the [Scope] struct.
Features
This crate has no feature flags.
Example Use Cases
Spawning In a smol::Executor<'a>
A smol::Executor<'a> is an async executor that owns tasks with a lifetime of 'a. To spawn a sub-task from a task in a smol executor, you need something like:
async
async
Unfortunately, it is not possible to create a &'a Executor<'a> since Executor<'a> is invariant in 'a and implements [Drop]. With this library, you can instead use WeakRef<'a, Executor<'a>:
use ;
async
async
let scope = new;
let executor = new;
let result = scope
.assign
.unwrap;
assert_eq!;
Similar Crates
- The lien crate provides a similar construction without weak references but with a more flexible API and
no_stdsupport. - The scoped_reference crate provides runtime checked references that aborts the program (without first running panic handlers) on violations. This is preferable to deadlocking, especially in single-threaded use cases.