Crates.io | restor |
lib.rs | restor |
version | 2.0.2 |
source | src |
created_at | 2019-04-14 22:24:25.495459 |
updated_at | 2019-06-16 14:07:06.181953 |
description | A dynamic resource storage system in rust. |
homepage | |
repository | https://github.com/OptimisticPeach/restor |
max_upload_size | |
id | 127975 |
size | 99,409 |
A dyamic resource storage written in rust. It supports storage of multiple types and multiple entries and dynamic borrow checking with the help of RefCell
s, Mutex
s and RwLock
s from parking_lot
. It also supports extracting and aqcuiring multiple types at once.
use restor::{DynamicStorage, make_storage};
fn main() {
// Use the shorthand for creating storage with preallocated types
let x = make_storage!(DynamicStorage: usize, String);
// Insert some data into the storage, either many at once, or one
x.insert_many((0..10).collect::<Vec<usize>>()).unwrap();
x.insert("abc".to_string()).unwrap();
create_string(&x);
println!("{}", &*x.get::<&String>().unwrap());
}
fn create_string(x: &DynamicStorage) {
let mut mystring = x.get::<&mut String>().unwrap();
for i in x.get::<&[usize]>().unwrap().iter() {
*mystring = format!("{}, {}", &*mystring, i);
}
}
BlackBox
(Or DynamicStorage
) is defined as so (More or less):
struct BlackBox {
data: HashMap<TypeId, Box<dyn Unit>>
}
The Unit
trait allows us to abstract over the generic type of the container (Referred to as UnitStorage
in the code), so we can pass data in and out of it by using the seemingly magical Any
trait. When you insert something into the storage it goes through these stages:
BlackBox::insert<T>
Box<dyn Any>
StorageUnit as dyn Unit
T
or a Vec<T>
Vec