Created
January 27, 2023 02:21
-
-
Save mendes5/1086363c6433f087ed43992d0857edf1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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