Macro dioxus_shareables::shareable_struct
source · [−]macro_rules! shareable_struct {
(
$vis:vis struct $Struct:ident {
$($field:ident: $T:ty = $init:expr),*$(,)?
}
actions for $_:ident {
$(
$action_vis:vis $ACTION:ident$(: $trait_vis:vis $Trait:ident)?
= $(W[$($w:ident),*])?$(,)?$(RW[$($rw:ident),*])?;
)*
}
) => { ... };
}Expand description
Create a struct definition for a global.
The idea is that each field of the struct will be stored in a separate global, and loaded only
when requested. The actions block describes possible ways of using the struct in terms of what
type of access (W or RW) they need to fields of the struct.
The struct can then be initialized using an “action” which describes which fields we need which type of access to.
dioxus_shareables::shareable_struct! {
pub struct Fuzzy {
wuzzy: u8 = 17,
was_a: u16 = 59,
was_he: &'static str = "bear?",
}
actions for Puzzle {
pub WAS: pub WasTrait = W[was_a, was_he]; // declares an WAS constant, as well an
// equivalent trait.
INIT = W[wuzzy, was_a], RW[was_he]; // declares the INIT constant, but no
// equivalent trait.
}
};
impl<A: FuzzyActions> Fuzzy<A> {
pub fn method(&self) where A: WasTrait {
let me = self.with_actions(WAS); // Pending updates to the rust trait system, we
// have to typecast here to get a Fuzzy<WAS>.
*me.was_he().write() = "bare!"; // We have write access to was_he
// self.wuzzy(); // but this would fail because we don't have access to wuzzy.
// ...
}
}
// ...
fn component(cx: Scope) -> Element {
let fuzzy = Fuzzy::use_(&cx, INIT); // This creates the hooks for the struct and initializes it
// from the necessary globals.
// ...
fuzzy.method(); // This is ok, since the INIT action includes everything the WAS action does.
// ...
}