Macro to 'attach' values statically to a type using static getter and setter methods.
[]
= "0.2"
use *;
// Simple Preview
type_cell!
u32set_a_number;
assert_eq!;
There are different settings available:
🌟 once! Set it once. Get it read-only! (combine with Mutex/RwLock/... for mutability)
🦥 lazy! Lazily access a value, set within the macro!
🏁 risky! Set it once. Get it mutable, but risk race conditions! (be sure you win the race!)
👹 unsafe! Same as risky!, but setters and getters are unsafe!
🧱 Basic Usage
- Use the macro:
type_cell!{...} - Which type should the value be 'attached' on?
u32 {...} - Which type does the value have?
static u32:- Which settings will it use?
once!,lazy!,riskyorunsafe! - examples:
static u32: once!orstatic String: lazy!
- 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
type_cell!
// 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
type_cell!
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
type_cell!
// Sets value to 1000.clamp(0,123) = 123
u32set_pass;
// Gets 123.add(5) = 128
assert_eq!;
👹 As Risky/Unsafe Mutable
⚠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 risky! or unsafe!.
// Risky Mutable
type_cell!
// 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! option and use a block instead of the setter function!
// Lazy Static
type_cell!
// Gets Some("3":&String)
assert_eq!;
🗺 Simple Mapping
If you only need the default getter and setters, there is a shortcut included for once!, risky! and unsafe!:
// Simple Usage
type_cell!
boolset_bools;
boolset_more_bools;
u8set_id;
u8set_seed;
unsafe;
If you only attach values of the same type as their parent:
// Simplest Usage
type_cell!
🔗 Related Projects
- bevy_cell - Attach bevy Handle and Entity to types.