Macro to 'attach' values statically to a type using static getter and setter methods.
[]
= "0.3"
use *;
tycell!
🧱 Basic Usage
- Use the macro:
tycell!{...} - Which type should the value be 'attached' on?
u32 {...} - Which type does the value have?
static u32:- Which settings will it use?
🌟
once_readSet it once. Get it read-only! (combine with Mutex/RwLock/... for mutability) 🏁once_writeSet it once. Get it mutable, but risk race conditions! (be sure you win the race!) 🦥lazy_readLikeonce_readbut set lazy inside the macro! 👹lazy_writeLikeonce_writebut set lazy inside the macro!! - examples:
static u32: once_read;orstatic String: lazy_read;
- Which settings will it use?
🌟
- What's the name of the default setter method?
set_type() - What's the name of the default getter method?
get_type()
// Basic Usage
tycell!
// Set it somewhere once:
boolset_vec;
// Get it anywhere afterwards:
assert_eq!;
The default setter parameter is a dynamic Into<..> and will use .into().
This means in this example you could also set it like this:
boolset_vec;
assert_eq!;
⚗ Advanced Usage
Multiple Setter and Getter with different parameters and return types can be defined! There are two ways of doing it:
- Methods:
- Use inline methods for simple conversions!
set set_bool(Option<usize>): do.is_some();get get_bool() -> bool: static.clone();
- Function:
- Use a function with correct parameters/return types and is accessible in the same file!
- Use
=before the function meta! set =set_base_fn(a:Option<usize>);get =get_base_fn() -> bool;
// Advanced Usage
tycell!
boolset_by_methods;
assert_eq!;
Methods with parameters are supported in two different ways:
- Constants:
- Using
=before a constant value! set set_number(u32): do.clamp(=0,=100);get get_number() -> bool: static.clamp(=0,=100);
- Using
- Pass Through:
- Naming the values with its types will pass it into the function!
set set_number(u32): do.clamp(min:u32,max:u32);get get_number() -> bool: static.clamp(min:u32,max:u32);
// Advanced Usage
tycell!
// Sets value to 1000.clamp(0,123) = 123
u32set_pass;
// Gets 123.add(5) = 128
assert_eq!;
🧊 Constant
You can also set const values!
// Constant
tycell!
// Gets 10!
assert_eq!;
👹 Risky Mutable Options
⚠Only use this if you're sure there are no race conditions (or they don't matter) or for debug purposes!
To make the static value mutable, use once_write or lazy_write.
// Risky Mutable
tycell!
// Set it somewhere once:
u32set_number;
// Default getter is mutable already
*u32number = 10;
// Gets 10!
assert_eq!;
🦥 As Lazy Static
To create a lazy static value, use the lazy_read option and use a block instead of the setter function!
// Lazy Static
tycell!
// Gets Some("3":&String)
assert_eq!;
➡ Simple Mapping
If you only need the default getter and setters, there is a short form:
// Simple Usage
tycell!
boolset_bools;
boolset_more_bools;
If you only attach values of the same type as their parent:
// Simplest Usage
tycell!
If you want to attach a type to its single generic type, e.g. u32 > Vec<u32> you can use !Vec<u32>.
Increase the number of ! to set the level, e.g. u32 > Vec<Vec<u32>> <=> !!Vec<Vec<u32>>.
tycell!
You can't mix different types of left-handed syntax, unless wrapped in {}
// working
tycell!
// NOT working
tycell!
You can also chain methods for the getter and adjust its return type.
tycell!
➡ Simple (Hash)Maps and Vecs
Ease up getting values from a HasmMap-esque types, by using after the name. if no key is provided, the type is set to a Vec<..> instead.
// uses anythng named TyMap for flaxibility
use HashMap as TyMap;
tycell!
boolset_bools;
boolset_more_bools;
🔗 Related Projects
- bevy_cell - Attach bevy Handle and Entity to types.