-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
54 lines (51 loc) · 1.24 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use std::collections::HashMap;
struct Widget {}
impl Widget {
pub(crate) fn set_label(&self, format: &str) -> () {
println!("label: {}", format);
}
}
impl WidgetExt for Widget {
fn set_callback<F: FnMut(&mut Self) + 'static>(&mut self, _cb: F)
where
Self: Sized,
{
// nothing
}
}
pub trait WidgetExt {
fn set_callback<F: FnMut(&mut Self) + 'static>(&mut self, cb: F)
where
Self: Sized;
}
pub trait Callbacks {
fn add_callback<F: FnMut(&mut Self) + 'static>(&mut self, cb: F)
where
Self: Sized;
}
impl<T> Callbacks for T
where
T: WidgetExt,
T: Sized,
{
fn add_callback<F: FnMut(&mut Self) + 'static>(&mut self, mut callback: F)
where
Self: Sized,
Self: WidgetExt,
{
CALLBACKS.insert(self as *const dyn WidgetExt, callback);
self.set_callback(move |widget| CALLBACKS.get(widget)());
}
}
static CALLBACKS: HashMap<*const dyn WidgetExt, dyn FnMut(&mut _) + 'static> = HashMap::new();
pub fn main() {
let mut b = Widget {};
let mut click = 0;
b.add_callback(move |b| {
b.set_label(&format!("click {}", click));
click += 1;
});
b.add_callback(move |_b| {
println!("click {}", click);
});
}