Expand description
§Generational Box
Generational Box is a runtime for Rust that allows any static type to implement Copy
. It can be combined with a global runtime to create an ergonomic state solution like dioxus-signals
. This crate doesn’t have any unsafe
code.
Three main types manage state in Generational Box:
Store
: Handles recycling generational boxes that have been dropped. Your application should have one store or one store per thread.Owner
: Handles dropping generational boxes. The owner acts like a runtime lifetime guard. Any states that you create with an owner will be dropped when that owner is dropped.GenerationalBox
: The core Copy state type. The generational box will be dropped when the owner is dropped.
Example:
use generational_box::{UnsyncStorage, AnyStorage};
// Create an owner for some state for a scope
let owner = UnsyncStorage::owner();
// Create some non-copy data, move it into a owner, and work with copy data
let data: String = "hello world".to_string();
let key = owner.insert(data);
// The generational box can be read from and written to like a RefCell
let value = key.read();
assert_eq!(*value, "hello world");
§How it works
Internally, generational-box
creates an arena of generational RefCell
s that are recycled when the owner is dropped. You can think of the cells as something like &'static RefCell<Box<dyn Any>>
with a generational check to make recycling a cell easier to debug. Then GenerationalBox
es are Copy
because the &'static
pointer is Copy
.
Structs§
- Already
Borrowed Error - An error that can occur when trying to borrow a value mutably that has already been borrowed immutably.
- Already
Borrowed MutError - An error that can occur when trying to borrow a value that has already been borrowed mutably.
- Generational
Box - The core Copy state type. The generational box will be dropped when the Owner is dropped.
- Generational
BoxId - The type erased id of a generational box.
- Generational
Pointer - A pointer to a specific generational box and generation in that box.
- Generational
Ref - A reference to a value in a generational box. This reference acts similarly to
std::cell::Ref
, but has extra debug information to track when all references to the value are created and dropped. - Generational
RefMut - A mutable reference to a value in a generational box. This reference acts similarly to
std::cell::RefMut
, but has extra debug information to track when all references to the value are created and dropped. - Owner
- Owner: Handles dropping generational boxes. The owner acts like a runtime lifetime guard. Any states that you create with an owner will be dropped when that owner is dropped.
- Sync
Storage - A thread safe storage. This is slower than the unsync storage, but allows you to share the value between threads.
- Unsync
Storage - A unsync storage. This is the default storage type.
- Value
Dropped Error - An error that can occur when trying to use a value that has been dropped.
Enums§
- Borrow
Error - An error that can occur when trying to borrow a value.
- Borrow
MutError - An error that can occur when trying to borrow a value mutably.
Traits§
- AnyStorage
- A trait for any storage backing type.
- Storage
- A trait for a storage backing type. (RefCell, RwLock, etc.)
Type Aliases§
- Borrow
MutResult - A result that can be returned from a borrow mut operation.
- Borrow
Result - A result that can be returned from a borrow operation.