Debian Codegraph @

third_party/rust/pulse/src/mainloop_api.rs

raw
1 // Copyright © 2017 Mozilla Foundation
2 //
3 // This program is made available under an ISC-style license. See the
4 // accompanying file LICENSE for details.
5
6 use ffi;
7 use std::mem;
8 use std::os::raw::c_void;
9
10 #[allow(non_camel_case_types)]
11 type pa_once_cb_t =
12 Option<unsafe extern "C" fn(m: *mut ffi::pa_mainloop_api, userdata: *mut c_void)>;
13 fn wrap_once_cb<F>(_: F) -> pa_once_cb_t
14 where
15 F: Fn(&MainloopApi, *mut c_void),
16 {
17 assert!(mem::size_of::<F>() == 0);
18
19 unsafe extern "C" fn wrapped<F>(m: *mut ffi::pa_mainloop_api, userdata: *mut c_void)
20 where
21 F: Fn(&MainloopApi, *mut c_void),
22 {
23 let api = from_raw_ptr(m);
24 let result = mem::transmute::<_, &F>(&())(&api, userdata);
25 mem::forget(api);
26 result
27 }
28
29 Some(wrapped::<F>)
30 }
31
32 pub struct MainloopApi(*mut ffi::pa_mainloop_api);
33
34 impl MainloopApi {
35 pub fn raw_mut(&self) -> &mut ffi::pa_mainloop_api {
36 unsafe { &mut *self.0 }
37 }
38
39 pub fn once<CB>(&self, cb: CB, userdata: *mut c_void)
40 where
41 CB: Fn(&MainloopApi, *mut c_void),
42 {
43 let wrapped = wrap_once_cb(cb);
44 unsafe {
45 ffi::pa_mainloop_api_once(self.raw_mut(), wrapped, userdata);
46 }
47 }
48
49 pub fn time_free(&self, e: *mut ffi::pa_time_event) {
50 unsafe {
51 if let Some(f) = self.raw_mut().time_free {
52 f(e);
53 }
54 }
55 }
56 }
57
58 pub unsafe fn from_raw_ptr(raw: *mut ffi::pa_mainloop_api) -> MainloopApi {
59 MainloopApi(raw)
60 }