Skip to content
This repository has been archived by the owner on Jun 8, 2021. It is now read-only.

Commit

Permalink
Deprecate CallbackGuard
Browse files Browse the repository at this point in the history
  • Loading branch information
EPashkin committed Jun 23, 2018
1 parent f016524 commit 7729b8b
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 25 deletions.
4 changes: 1 addition & 3 deletions src/main_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use MainContext;
use Source;
use SourceId;

use source::{CallbackGuard, Priority};
use source::Priority;

impl MainContext {
pub fn prepare(&self) -> (bool, i32) {
Expand Down Expand Up @@ -64,15 +64,13 @@ impl MainContext {

#[cfg_attr(feature = "cargo-clippy", allow(transmute_ptr_to_ref))]
unsafe extern "C" fn trampoline<F: FnOnce() + Send + 'static>(func: gpointer) -> gboolean {
let _guard = CallbackGuard::new();
let func: &mut Option<Box<F>> = transmute(func);
let func = func.take().expect("MainContext::invoke() closure called multiple times");
func();
glib_ffi::G_SOURCE_REMOVE
}

unsafe extern "C" fn destroy_closure<F: FnOnce() + Send + 'static>(ptr: gpointer) {
let _guard = CallbackGuard::new();
Box::<Option<Box<F>>>::from_raw(ptr as *mut _);
}

Expand Down
9 changes: 0 additions & 9 deletions src/main_context_futures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use MainContext;
use MainLoop;
use Source;
use Priority;
use ::source::CallbackGuard;

#[cfg(feature = "futures-nightly")]
type StoredFutureBox<T> = PinBox<T>;
Expand Down Expand Up @@ -68,8 +67,6 @@ unsafe extern "C" fn prepare(
source: *mut glib_ffi::GSource,
timeout: *mut i32,
) -> glib_ffi::gboolean {
let _guard = CallbackGuard::new();

let source = &mut *(source as *mut TaskSource);

*timeout = -1;
Expand Down Expand Up @@ -97,8 +94,6 @@ unsafe extern "C" fn prepare(
}

unsafe extern "C" fn check(source: *mut glib_ffi::GSource) -> glib_ffi::gboolean {
let _guard = CallbackGuard::new();

let source = &mut *(source as *mut TaskSource);

let cur = source.state.load(Ordering::SeqCst);
Expand All @@ -114,8 +109,6 @@ unsafe extern "C" fn dispatch(
callback: glib_ffi::GSourceFunc,
_user_data: glib_ffi::gpointer,
) -> glib_ffi::gboolean {
let _guard = CallbackGuard::new();

let source = &mut *(source as *mut TaskSource);
assert!(callback.is_none());

Expand All @@ -140,8 +133,6 @@ unsafe extern "C" fn dispatch(
}

unsafe extern "C" fn finalize(source: *mut glib_ffi::GSource) {
let _guard = CallbackGuard::new();

let source = source as *mut TaskSource;
let _ = (*source).future.take();
}
Expand Down
2 changes: 0 additions & 2 deletions src/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use libc::{c_void, c_uint, c_ulong};
use gobject_ffi::{self, GCallback};
use ffi::{gboolean, GQuark};
use object::{IsA, Object};
use source::CallbackGuard;
use translate::{from_glib, FromGlib, ToGlib, ToGlibPtr};

/// The id of a signal that is returned by `connect`.
Expand Down Expand Up @@ -88,7 +87,6 @@ pub fn signal_stop_emission_by_name<T: IsA<Object>>(instance: &T, signal_name: &
}

unsafe extern "C" fn destroy_closure(ptr: *mut c_void, _: *mut gobject_ffi::GClosure) {
let _guard = CallbackGuard::new();
// destroy
Box::<Box<Fn()>>::from_raw(ptr as *mut _);
}
10 changes: 4 additions & 6 deletions src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,24 @@ impl ToGlib for Continue {

/// Unwinding propagation guard. Aborts the process if destroyed while
/// panicking.
#[deprecated(note="Rustc has this functionality built-in since 1.26.0")]
pub struct CallbackGuard(());

#[allow(deprecated)]
impl CallbackGuard {
pub fn new() -> CallbackGuard {
CallbackGuard(())
}
}

#[allow(deprecated)]
impl Default for CallbackGuard {
fn default() -> Self {
Self::new()
}
}

#[allow(deprecated)]
impl Drop for CallbackGuard {
fn drop(&mut self) {
use std::io::stderr;
Expand All @@ -95,13 +99,11 @@ impl Drop for CallbackGuard {

#[cfg_attr(feature = "cargo-clippy", allow(transmute_ptr_to_ref))]
unsafe extern "C" fn trampoline(func: gpointer) -> gboolean {
let _guard = CallbackGuard::new();
let func: &RefCell<Box<FnMut() -> Continue + 'static>> = transmute(func);
(&mut *func.borrow_mut())().to_glib()
}

unsafe extern "C" fn destroy_closure(ptr: gpointer) {
let _guard = CallbackGuard::new();
Box::<RefCell<Box<FnMut() -> Continue + 'static>>>::from_raw(ptr as *mut _);
}

Expand All @@ -113,13 +115,11 @@ fn into_raw<F: FnMut() -> Continue + Send + 'static>(func: F) -> gpointer {

#[cfg_attr(feature = "cargo-clippy", allow(transmute_ptr_to_ref))]
unsafe extern "C" fn trampoline_child_watch(pid: glib_ffi::GPid, status: i32, func: gpointer) {
let _guard = CallbackGuard::new();
let func: &RefCell<Box<FnMut(Pid, i32) + 'static>> = transmute(func);
(&mut *func.borrow_mut())(Pid(pid), status)
}

unsafe extern "C" fn destroy_closure_child_watch(ptr: gpointer) {
let _guard = CallbackGuard::new();
Box::<RefCell<Box<FnMut(Pid, i32) + 'static>>>::from_raw(ptr as *mut _);
}

Expand All @@ -132,14 +132,12 @@ fn into_raw_child_watch<F: FnMut(Pid, i32) + Send + 'static>(func: F) -> gpointe
#[cfg(any(all(feature = "v2_36", unix), feature = "dox"))]
#[cfg_attr(feature = "cargo-clippy", allow(transmute_ptr_to_ref))]
unsafe extern "C" fn trampoline_unix_fd(fd: i32, condition: glib_ffi::GIOCondition, func: gpointer) -> gboolean {
let _guard = CallbackGuard::new();
let func: &RefCell<Box<FnMut(RawFd, IOCondition) -> Continue + 'static>> = transmute(func);
(&mut *func.borrow_mut())(fd, from_glib(condition)).to_glib()
}

#[cfg(any(all(feature = "v2_36", unix), feature = "dox"))]
unsafe extern "C" fn destroy_closure_unix_fd(ptr: gpointer) {
let _guard = CallbackGuard::new();
Box::<RefCell<Box<FnMut(RawFd, IOCondition) + 'static>>>::from_raw(ptr as *mut _);
}

Expand Down
4 changes: 0 additions & 4 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -941,13 +941,11 @@ impl AnyValue {
}

unsafe extern "C" fn copy(v: *mut c_void) -> *mut c_void {
let _guard = ::source::CallbackGuard::new();
let v = &*(v as *mut AnyValue);
Box::into_raw(Box::new(v.clone())) as *mut c_void
}

unsafe extern "C" fn free(v: *mut c_void) {
let _guard = ::source::CallbackGuard::new();
let _ = Box::from_raw(v as *mut AnyValue);
}
}
Expand Down Expand Up @@ -1018,13 +1016,11 @@ impl AnySendValue {
}

unsafe extern "C" fn copy(v: *mut c_void) -> *mut c_void {
let _guard = ::source::CallbackGuard::new();
let v = &*(v as *mut AnySendValue);
Box::into_raw(Box::new(v.clone())) as *mut c_void
}

unsafe extern "C" fn free(v: *mut c_void) {
let _guard = ::source::CallbackGuard::new();
let _ = Box::from_raw(v as *mut AnySendValue);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/variant_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::fmt;
use std::hash::{Hash, Hasher};
use std::ops::Deref;
use std::slice;
use value::{Value, ToValue, SetValue, FromValueOptional, SetValueOptional};
use value::{Value, SetValue, FromValueOptional, SetValueOptional};
use gobject_ffi;

/// Describes `Variant` types.
Expand Down Expand Up @@ -339,6 +339,7 @@ mod tests {
use ffi as glib_ffi;
use translate::*;
use super::*;
use value::ToValue;

unsafe fn equal<T, U>(ptr1: *const T, ptr2: *const U) -> bool {
from_glib(glib_ffi::g_variant_type_equal(ptr1 as *const _, ptr2 as *const _))
Expand Down

0 comments on commit 7729b8b

Please sign in to comment.