Skip to content

Instantly share code, notes, and snippets.

@mendes5
Created January 27, 2023 02:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mendes5/1086363c6433f087ed43992d0857edf1 to your computer and use it in GitHub Desktop.
Save mendes5/1086363c6433f087ed43992d0857edf1 to your computer and use it in GitHub Desktop.
fn mount<T, P, B: FnMut(&mut P)>(
component: fn(this: Option<T>, props: P, block: B) -> T,
props: P,
block: B,
) {
component(None, props, block);
}
struct Props<T: FnMut()> {
on_click: T,
}
fn component<B: FnMut(&mut Props<T>), T: FnMut()>(a: Option<u32>, mut props: Props<T>, mut block: B) -> u32 {
block(&mut props);
a.or_else(|| Some(0)).unwrap()
}
fn main() {
let mut x = 0;
mount(
component,
Props {
on_click: || x += 10,
},
|props| {
(props.on_click)();
print!("A");
},
);
mount(
component,
Props {
on_click: || x += 10,
},
|props| {
(props.on_click)();
print!("A");
},
);
println!("{}", x);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment