type_cell 0.1.1

Attach single assignment cells to types using get/set methods.
Documentation
  • The value can only be set once.
  • The value has to be set before getting.

Simplified Usage

  • $typeOn > $typeStore: $name_1, $name_n; $typeOn: The Type to implement the getter/setter methods in. $typeStore: The Type, stored in the cell. $names: The Type, stored in the cell.
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());
}

Query 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.

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.

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 @:

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