In Brief
- A
Soul<T, B>wraps a given valueT. When pinned (either withcore::pin::pin!orBox/Arc/Rc::pin), it can produceLich<dyn Trait>es that are bound to it (whereTraitis a trait implemented byT). On drop, it will guarantee that the valueTbecomes unreachable (the behavior varies based on theB: Binding). - A
Lich<T, B>is a handle to the value inside theSoul. It may have any lifetime (including'static), thus it is allowed to cross'staticboundaries (such as whenstd::thread::spawning a thread or when storing a value in astaticvariable).
Two B: Binding implementations are currently supported and offer different tradeoffs:
phylactery::cell::Cell:- Uses a
core::cell::Cell<u32>internally for reference counting. - Can not be sent to other threads.
- Create a
Soulusingphylactery::cell::Soul::new(..) - When the
Soulis dropped, the thread will panic unless allLiches are dropped.
- Uses a
phylactery::atomic::Atomic:- Uses a
core::sync::atomic::AtomicU32for reference counting. - Can be sent to other threads.
- Create a
Soulusingphylactery::atomic::Soul::new(..) - When the
Soulis dropped, the thread will block until allLiches are dropped.
- Uses a
Since this library makes use of some unsafe code, all tests are run with miri to try to catch any unsoundness.
This library supports #[no_std] (use default-features = false in your 'Cargo.toml').
Examples
/// Trivially reimplement [`thread::scope`] in a more powerful way.
///
/// Contrary to other `scope` solutions, here, the captured reference can be
/// returned (as a [`Soul<T>`]) while the threads continue to execute.
/// Implements a thread local scoped logger available from anywhere that can
/// borrow values that live on the stack.
See the examples and tests folder for more detailed examples.
Contribute
- If you find a bug or have a feature request, please open an issues.
phylacteryis actively maintained and pull requests are welcome.- If
phylacterywas useful to you, please consider leaving a star!