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.

Simplified Usage

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.

Example 1:

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()); }

Example 2:

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()); }

Prefabs

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

Mutability can be achieved using Mutex, RwLock or other locks, just make sure you know what you're doing!


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 crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.