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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use crate::prelude::*;
use crate::Cancellable;
use futures_channel::oneshot;
use futures_core::Future;
use glib::{translate::*, IsA};
use std::num::NonZeroU64;
#[derive(Debug, Eq, PartialEq)]
#[repr(transparent)]
pub struct CancelledHandlerId(NonZeroU64);
impl CancelledHandlerId {
pub unsafe fn as_raw(&self) -> libc::c_ulong {
self.0.get() as libc::c_ulong
}
}
impl TryFromGlib<libc::c_ulong> for CancelledHandlerId {
type Error = GlibNoneError;
#[inline]
unsafe fn try_from_glib(val: libc::c_ulong) -> Result<Self, GlibNoneError> {
NonZeroU64::new(val as u64).map(Self).ok_or(GlibNoneError)
}
}
pub trait CancellableExtManual {
#[doc(alias = "g_cancellable_connect")]
fn connect_cancelled<F: FnOnce(&Self) + Send + 'static>(
&self,
callback: F,
) -> Option<CancelledHandlerId>;
#[doc(alias = "g_cancellable_connect")]
fn connect_cancelled_local<F: FnOnce(&Self) + 'static>(
&self,
callback: F,
) -> Option<CancelledHandlerId>;
#[doc(alias = "g_cancellable_disconnect")]
fn disconnect_cancelled(&self, id: CancelledHandlerId);
fn future(&self) -> std::pin::Pin<Box<dyn Future<Output = ()> + Send + Sync + 'static>>;
}
impl<O: IsA<Cancellable>> CancellableExtManual for O {
#[doc(alias = "g_cancellable_connect")]
fn connect_cancelled<F: FnOnce(&Self) + Send + 'static>(
&self,
callback: F,
) -> Option<CancelledHandlerId> {
unsafe extern "C" fn connect_trampoline<P: IsA<Cancellable>, F: FnOnce(&P)>(
this: *mut ffi::GCancellable,
callback: glib::ffi::gpointer,
) {
let callback: &mut Option<F> = &mut *(callback as *mut Option<F>);
let callback = callback
.take()
.expect("Cancellable::cancel() closure called multiple times");
callback(Cancellable::from_glib_borrow(this).unsafe_cast_ref())
}
unsafe extern "C" fn destroy_closure<F>(ptr: glib::ffi::gpointer) {
Box::<Option<F>>::from_raw(ptr as *mut _);
}
let callback: Box<Option<F>> = Box::new(Some(callback));
unsafe {
from_glib(ffi::g_cancellable_connect(
self.as_ptr() as *mut _,
Some(std::mem::transmute::<_, unsafe extern "C" fn()>(
connect_trampoline::<Self, F> as *const (),
)),
Box::into_raw(callback) as *mut _,
Some(destroy_closure::<F>),
))
}
}
#[doc(alias = "g_cancellable_connect")]
fn connect_cancelled_local<F: FnOnce(&Self) + 'static>(
&self,
callback: F,
) -> Option<CancelledHandlerId> {
let callback = glib::thread_guard::ThreadGuard::new(callback);
self.connect_cancelled(move |obj| (callback.into_inner())(obj))
}
#[doc(alias = "g_cancellable_disconnect")]
fn disconnect_cancelled(&self, id: CancelledHandlerId) {
unsafe { ffi::g_cancellable_disconnect(self.as_ptr() as *mut _, id.as_raw()) };
}
fn future(&self) -> std::pin::Pin<Box<dyn Future<Output = ()> + Send + Sync + 'static>> {
let cancellable = self.as_ref().clone();
let (tx, rx) = oneshot::channel();
let id = cancellable.connect_cancelled(move |_| {
let _ = tx.send(());
});
Box::pin(async move {
rx.await.unwrap();
if let Some(id) = id {
cancellable.disconnect_cancelled(id);
}
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn cancellable_callback() {
let c = Cancellable::new();
let id = c.connect_cancelled(|_| {});
c.cancel(); c.disconnect_cancelled(id.unwrap());
}
#[test]
fn cancellable_callback_local() {
let c = Cancellable::new();
let id = c.connect_cancelled_local(|_| {});
c.cancel(); c.disconnect_cancelled(id.unwrap());
}
#[test]
fn cancellable_future() {
let c = Cancellable::new();
c.cancel();
glib::MainContext::new().block_on(c.future());
}
#[test]
fn cancellable_future_thread() {
let cancellable = Cancellable::new();
let c = cancellable.clone();
std::thread::spawn(move || c.cancel()).join().unwrap();
glib::MainContext::new().block_on(cancellable.future());
}
#[test]
fn cancellable_future_delayed() {
let ctx = glib::MainContext::new();
let c = Cancellable::new();
let (tx, rx) = oneshot::channel();
{
let c = c.clone();
ctx.spawn_local(async move {
c.future().await;
tx.send(()).unwrap();
});
}
std::thread::spawn(move || c.cancel()).join().unwrap();
ctx.block_on(rx).unwrap();
}
}