Macro to attach OnceCell to a Type using getter/setter methods. This is mainly useful for variables which will be set at the start and be accessed read-only. Initially developed for use with the Bevy-Engine to easily access Handles (smart pointers to assets) globally.
$typeOn > $typeStore: $name_1, $name_n;
rust
use type_cell::*;
type_cell!{
// #clone // the getters return the values cloned
// store a vec of bools on the bool type
bool > Vec<bool>: bools;
// store a u8 on the u8 type
u8 > u8: app_id, seed;
}
fn main () {
// set global on startup
bool::set_bools(vec![true,false]);
u8::set_app_id(100);
u8::set_seed(111);
// get anywhere
assert_eq!(&vec![true,false], bool::get_bools());
assert_eq!(&100, u8::get_app_id());
assert_eq!(&111, u8::get_seed());
}
Queries are the base of this crate, they consist of the following parts and are separated by | :
on $typeOn > store $typeStore | set $typeIn | get $typeOut | $name
- on $typeOn > store $typeStore
$typeOn: The Type to implement the getter/setter methods in.
$typeStore: The Type, stored in the cell.
- set $typeIn[.methods(val:type)]
$typeIn: Input-Parameter of the setter method.
.methods(val:type): Methods applied on $typeIn to fit $typeStore. Parameters of those will be added to the setter method and forwarded.
- get $paramOut[.methods(val:type)]
$typeOut: Output-Type of the getter method.
.methods(val:type): Methods applied on $typeStore to fit $typeOut. Parameters of those will be added to the getter method and forwarded.
- $name
$name: Name of the value, method names will be: get_$name
and set_$name
.
Store bool on bool, set bool directly and get a reference to it.
rust
use type_cell::*;
type_cell!(on bool > store bool | set bool | get &'static bool | test);
fn main () {
bool::set_test(true);
assert_eq!(&true,bool::get_test());
}
Store bool on bool, set bool directly and get a clone of it.
rust
use type_cell::*;
type_cell!(on bool > store bool | set bool | get bool.clone() | test);
fn main () {
bool::set_test(true);
assert_eq!(true,bool::get_test());
}
Currently there are simplifications for Vec and HashMap indicated by a @:
rust
use type_cell::*;
type_cell!{
@Vec #unwrap
u8: vec;
}
fn main () {
u8::set_vec(vec![50,100,150,200]);
assert_eq!(&150, u8::get_vec(2));
}
rust
use type_cell::*;
use std::collections::HashMap;
type_cell!{
@HashMap<usize> #unwrap #clone
bool: map1, map2;
u8: map3, map4;
}
fn main () {
u8::set_map3(HashMap::from([
(11,50), (22,100), (33,150), (44,200)
]));
assert_eq!(150, u8::get_map3(&33));
}
Mutability can be achieved using Mutex, RwLock or other locks, just make sure you know what you're doing!
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 crate by you, as defined in the Apache-2.0 license, shall
be dual licensed as above, without any additional terms or conditions.