Open a scope and then freeze it in time for future access.
This crate allows constructing structs that contain references and keeping them alive alongside the data they reference, without a lifetime.
This is especially useful for zero-copy parsers that construct elaborate (and possibly costly) representations that borrow the source data.
This crate achieves that by leveraging async functions. At their core, async functions are self-referential structs. this crate simply provides a way to ex-filtrate references outside of the async function, in a controlled manner.
Using this crate
After you identified the data and its borrowed representation that you'd like to access without a lifetime, using this crate will typically encompass a few steps:
// Given the following types:
;
;
// 1. Define a helper type that will express where the lifetimes of the borrowed representation live.
; // empty type, no lifetime.
// 2. Define a function that setups the data and its borrowed representation:
// 3. Open a `BoxScope` using the previously written async function:
let mut scope = new_dyn;
// 4. Store the `BoxScope` anywhere you want
// 5. Lastly, enter the scope to retrieve access to the referenced value.
scope.enter;
Kinds of scopes
This crate only provide a single kind of scope at the moment
| Scope | Allocations | Moveable after opening | Thread-safe |
|---|---|---|---|
[BoxScope] |
1 (size of the contained Future + 1 pointer to the reference type) | Yes | No |
An RcScope or MutexScope could be future extensions
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
Alternative
yoke serves a similar use case as this crate, albeit it is expressed in terms of a self-referential struct rather than as an async scope, which is less natural if the intent is to borrow some data.