|
| 1 | +use glib_ffi; |
| 2 | +use gobject_ffi; |
| 3 | + |
| 4 | +use std::ffi::CString; |
| 5 | +use std::sync::{Once, ONCE_INIT}; |
| 6 | +use std::mem; |
| 7 | +use std::sync::Arc; |
| 8 | + |
| 9 | +use glib::translate::{from_glib_none, ToGlibPtr}; |
| 10 | + |
| 11 | +use libc::{c_char, c_void}; |
| 12 | + |
| 13 | +// No #[repr(C)] here as we export it as an opaque struct |
| 14 | +// If it was not opaque, it must be #[repr(C)] |
| 15 | +pub struct SharedRString(Option<String>); |
| 16 | + |
| 17 | +impl SharedRString { |
| 18 | + fn new(s: Option<String>) -> Arc<SharedRString> { |
| 19 | + Arc::new(SharedRString(s)) |
| 20 | + } |
| 21 | + |
| 22 | + // FIXME: This could borrow the &str in theory! |
| 23 | + fn get(&self) -> Option<String> { |
| 24 | + self.0.clone() |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +// |
| 29 | +// Public C functions below |
| 30 | +// |
| 31 | +#[no_mangle] |
| 32 | +pub unsafe extern "C" fn ex_shared_rstring_new(s: *const c_char) -> *mut SharedRString { |
| 33 | + callback_guard!(); |
| 34 | + |
| 35 | + let s = SharedRString::new(from_glib_none(s)); |
| 36 | + Arc::into_raw(s) as *mut _ |
| 37 | +} |
| 38 | + |
| 39 | +#[no_mangle] |
| 40 | +pub unsafe extern "C" fn ex_shared_rstring_ref( |
| 41 | + shared_rstring: *mut SharedRString, |
| 42 | +) -> *mut SharedRString { |
| 43 | + callback_guard!(); |
| 44 | + |
| 45 | + let shared_rstring = Arc::from_raw(shared_rstring); |
| 46 | + let s = shared_rstring.clone(); |
| 47 | + |
| 48 | + // Forget it and keep it alive, we will still need it later |
| 49 | + let _ = Arc::into_raw(shared_rstring); |
| 50 | + |
| 51 | + Arc::into_raw(s) as *mut _ |
| 52 | +} |
| 53 | + |
| 54 | +#[no_mangle] |
| 55 | +pub unsafe extern "C" fn ex_shared_rstring_unref(shared_rstring: *mut SharedRString) { |
| 56 | + callback_guard!(); |
| 57 | + |
| 58 | + let _ = Arc::from_raw(shared_rstring); |
| 59 | +} |
| 60 | + |
| 61 | +#[no_mangle] |
| 62 | +pub unsafe extern "C" fn ex_shared_rstring_get(shared_rstring: *mut SharedRString) -> *mut c_char { |
| 63 | + callback_guard!(); |
| 64 | + |
| 65 | + let shared_rstring = &*shared_rstring; |
| 66 | + // FIXME: This could borrow the &str in theory! |
| 67 | + shared_rstring.get().to_glib_full() |
| 68 | +} |
| 69 | + |
| 70 | +// GObject glue |
| 71 | +#[no_mangle] |
| 72 | +pub unsafe extern "C" fn ex_shared_rstring_get_type() -> glib_ffi::GType { |
| 73 | + callback_guard!(); |
| 74 | + |
| 75 | + static mut TYPE: glib_ffi::GType = gobject_ffi::G_TYPE_INVALID; |
| 76 | + static ONCE: Once = ONCE_INIT; |
| 77 | + |
| 78 | + ONCE.call_once(|| { |
| 79 | + let type_name = CString::new("ExSharedRString").unwrap(); |
| 80 | + |
| 81 | + TYPE = gobject_ffi::g_boxed_type_register_static( |
| 82 | + type_name.as_ptr(), |
| 83 | + Some(mem::transmute(ex_shared_rstring_ref as *const c_void)), |
| 84 | + Some(mem::transmute(ex_shared_rstring_ref as *const c_void)), |
| 85 | + ); |
| 86 | + |
| 87 | + }); |
| 88 | + |
| 89 | + TYPE |
| 90 | +} |
0 commit comments