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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#![warn(missing_docs)]
use alloc::boxed::Box;
use core::cell::Cell;
#[repr(C)]
pub struct Callback<Arg: ?Sized, Ret = ()> {
handler: Cell<Option<Box<dyn FnMut(&Arg, &mut Ret)>>>,
}
impl<Arg: ?Sized, Ret> Default for Callback<Arg, Ret> {
fn default() -> Self {
Self { handler: Default::default() }
}
}
impl<Arg: ?Sized, Ret: Default> Callback<Arg, Ret> {
pub fn call(&self, a: &Arg) -> Ret {
let mut r = Ret::default();
if let Some(mut h) = self.handler.take() {
h(a, &mut r);
assert!(self.handler.take().is_none(), "Callback Handler set while called");
self.handler.set(Some(h));
}
r
}
pub fn set_handler(&self, mut f: impl FnMut(&Arg) -> Ret + 'static) {
self.handler.set(Some(Box::new(move |a: &Arg, r: &mut Ret| *r = f(a))));
}
}
#[test]
fn callback_simple_test() {
use std::rc::Rc;
#[derive(Default)]
struct Component {
pressed: core::cell::Cell<bool>,
clicked: Callback<()>,
}
let c = Rc::new(Component::default());
let weak = Rc::downgrade(&c);
c.clicked.set_handler(move |()| weak.upgrade().unwrap().pressed.set(true));
c.clicked.call(&());
assert!(c.pressed.get());
}
#[cfg(feature = "ffi")]
pub(crate) mod ffi {
#![allow(unsafe_code)]
use super::*;
#[allow(non_camel_case_types)]
type c_void = ();
#[repr(C)]
pub struct CallbackOpaque(*const c_void, *const c_void);
static_assertions::assert_eq_align!(CallbackOpaque, Callback<()>);
static_assertions::assert_eq_size!(CallbackOpaque, Callback<()>);
static_assertions::assert_eq_align!(CallbackOpaque, Callback<(alloc::string::String,)>);
static_assertions::assert_eq_size!(CallbackOpaque, Callback<(alloc::string::String,)>);
#[no_mangle]
pub unsafe extern "C" fn slint_callback_init(out: *mut CallbackOpaque) {
assert_eq!(core::mem::size_of::<CallbackOpaque>(), core::mem::size_of::<Callback<()>>());
core::ptr::write(out as *mut Callback<()>, Default::default());
}
#[no_mangle]
pub unsafe extern "C" fn slint_callback_call(
sig: *const CallbackOpaque,
arg: *const c_void,
ret: *mut c_void,
) {
let sig = &*(sig as *const Callback<c_void>);
if let Some(mut h) = sig.handler.take() {
h(&*arg, &mut *ret);
assert!(sig.handler.take().is_none(), "Callback Handler set while called");
sig.handler.set(Some(h));
}
}
#[no_mangle]
pub unsafe extern "C" fn slint_callback_set_handler(
sig: *const CallbackOpaque,
binding: extern "C" fn(user_data: *mut c_void, arg: *const c_void, ret: *mut c_void),
user_data: *mut c_void,
drop_user_data: Option<extern "C" fn(*mut c_void)>,
) {
let sig = &mut *(sig as *mut Callback<c_void>);
struct UserData {
user_data: *mut c_void,
drop_user_data: Option<extern "C" fn(*mut c_void)>,
binding: extern "C" fn(user_data: *mut c_void, arg: *const c_void, ret: *mut c_void),
}
impl Drop for UserData {
fn drop(&mut self) {
if let Some(x) = self.drop_user_data {
x(self.user_data)
}
}
}
impl UserData {
unsafe fn call(&self, arg: *const c_void, ret: *mut c_void) {
(self.binding)(self.user_data, arg, ret)
}
}
let ud = UserData { user_data, drop_user_data, binding };
sig.handler.set(Some(Box::new(move |a: &(), r: &mut ()| {
ud.call(a as *const c_void, r as *mut c_void)
})));
}
#[no_mangle]
pub unsafe extern "C" fn slint_callback_drop(handle: *mut CallbackOpaque) {
core::ptr::drop_in_place(handle as *mut Callback<()>);
}
}