Skip to content

Commit 2cabe2c

Browse files
committed
glib: Make WeakRef cheaply cloneable
By using an `Arc` instead of a `Box`, we can make `WeakRef::clone` a cheap operation, compared to taking a global write lock for setting a `GWeakRef`. The extra space used for the refcounts should be a fine trade-off.
1 parent 6bdbfe0 commit 2cabe2c

File tree

1 file changed

+28
-30
lines changed

1 file changed

+28
-30
lines changed

glib/src/object.rs

+28-30
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@
33
// rustdoc-stripper-ignore-next
44
//! `IMPL` Object wrapper implementation and `Object` binding.
55
6-
use std::{cmp, fmt, hash, marker::PhantomData, mem, mem::ManuallyDrop, ops, pin::Pin, ptr};
6+
use std::{
7+
cmp, fmt, hash,
8+
marker::PhantomData,
9+
mem::{self, ManuallyDrop},
10+
ops,
11+
pin::Pin,
12+
ptr,
13+
sync::Arc,
14+
};
715

816
use crate::{
917
closure::TryFromClosureReturnValue,
@@ -3029,9 +3037,9 @@ impl<T: ObjectType> ObjectExt for T {
30293037
#[inline]
30303038
fn downgrade(&self) -> WeakRef<T> {
30313039
unsafe {
3032-
let w = WeakRef(Box::pin(mem::zeroed()), PhantomData);
3040+
let w = WeakRef(Arc::pin(WeakRefInner(mem::zeroed(), PhantomData)));
30333041
gobject_ffi::g_weak_ref_init(
3034-
mut_override(&*w.0),
3042+
mut_override(&(*w.0).0),
30353043
self.as_object_ref().to_glib_none().0,
30363044
);
30373045
w
@@ -3373,9 +3381,12 @@ impl<T: ObjectType> WeakRefNotify<T> {
33733381

33743382
// rustdoc-stripper-ignore-next
33753383
/// A weak reference to an object.
3376-
#[derive(Debug)]
3384+
#[derive(Debug, PartialEq, PartialOrd)]
33773385
#[doc(alias = "GWeakRef")]
3378-
pub struct WeakRef<T: ObjectType>(Pin<Box<gobject_ffi::GWeakRef>>, PhantomData<*mut T>);
3386+
pub struct WeakRef<T: ObjectType>(Pin<Arc<WeakRefInner<T>>>);
3387+
3388+
#[derive(Debug)]
3389+
struct WeakRefInner<T: ObjectType>(gobject_ffi::GWeakRef, PhantomData<*mut T>);
33793390

33803391
impl<T: ObjectType> WeakRef<T> {
33813392
// rustdoc-stripper-ignore-next
@@ -3385,11 +3396,8 @@ impl<T: ObjectType> WeakRef<T> {
33853396
#[inline]
33863397
pub fn new() -> WeakRef<T> {
33873398
unsafe {
3388-
let mut w = WeakRef(Box::pin(mem::zeroed()), PhantomData);
3389-
gobject_ffi::g_weak_ref_init(
3390-
Pin::as_mut(&mut w.0).get_unchecked_mut(),
3391-
ptr::null_mut(),
3392-
);
3399+
let w = WeakRef(Arc::pin(WeakRefInner(mem::zeroed(), PhantomData)));
3400+
gobject_ffi::g_weak_ref_init(mut_override(&(*w.0).0), ptr::null_mut());
33933401
w
33943402
}
33953403
}
@@ -3401,7 +3409,7 @@ impl<T: ObjectType> WeakRef<T> {
34013409
pub fn set(&self, obj: Option<&T>) {
34023410
unsafe {
34033411
gobject_ffi::g_weak_ref_set(
3404-
mut_override(Pin::as_ref(&self.0).get_ref()),
3412+
mut_override(&(*self.0).0),
34053413
obj.map_or(std::ptr::null_mut(), |obj| {
34063414
obj.as_object_ref().to_glib_none().0
34073415
}),
@@ -3417,7 +3425,7 @@ impl<T: ObjectType> WeakRef<T> {
34173425
#[inline]
34183426
pub fn upgrade(&self) -> Option<T> {
34193427
unsafe {
3420-
let ptr = gobject_ffi::g_weak_ref_get(mut_override(Pin::as_ref(&self.0).get_ref()));
3428+
let ptr = gobject_ffi::g_weak_ref_get(mut_override(&(*self.0).0));
34213429
if ptr.is_null() {
34223430
None
34233431
} else {
@@ -3428,29 +3436,19 @@ impl<T: ObjectType> WeakRef<T> {
34283436
}
34293437
}
34303438

3431-
impl<T: ObjectType> Drop for WeakRef<T> {
3439+
impl<T: ObjectType> Drop for WeakRefInner<T> {
34323440
#[inline]
34333441
fn drop(&mut self) {
34343442
unsafe {
3435-
gobject_ffi::g_weak_ref_clear(Pin::as_mut(&mut self.0).get_unchecked_mut());
3443+
gobject_ffi::g_weak_ref_clear(&mut self.0);
34363444
}
34373445
}
34383446
}
34393447

34403448
impl<T: ObjectType> Clone for WeakRef<T> {
34413449
#[inline]
34423450
fn clone(&self) -> Self {
3443-
unsafe {
3444-
let o = self.upgrade();
3445-
3446-
let mut c = WeakRef(Box::pin(mem::zeroed()), PhantomData);
3447-
gobject_ffi::g_weak_ref_init(
3448-
Pin::as_mut(&mut c.0).get_unchecked_mut(),
3449-
o.to_glib_none().0 as *mut gobject_ffi::GObject,
3450-
);
3451-
3452-
c
3453-
}
3451+
Self(self.0.clone())
34543452
}
34553453
}
34563454

@@ -3461,10 +3459,10 @@ impl<T: ObjectType> Default for WeakRef<T> {
34613459
}
34623460
}
34633461

3464-
unsafe impl<T: ObjectType + Sync + Sync> Sync for WeakRef<T> {}
3465-
unsafe impl<T: ObjectType + Send + Sync> Send for WeakRef<T> {}
3462+
unsafe impl<T: ObjectType + Sync + Sync> Sync for WeakRefInner<T> {}
3463+
unsafe impl<T: ObjectType + Send + Sync> Send for WeakRefInner<T> {}
34663464

3467-
impl<T: ObjectType> PartialEq for WeakRef<T> {
3465+
impl<T: ObjectType> PartialEq for WeakRefInner<T> {
34683466
#[inline]
34693467
fn eq(&self, other: &Self) -> bool {
34703468
unsafe { self.0.priv_.p == other.0.priv_.p }
@@ -3474,11 +3472,11 @@ impl<T: ObjectType> PartialEq for WeakRef<T> {
34743472
impl<T: ObjectType> PartialEq<T> for WeakRef<T> {
34753473
#[inline]
34763474
fn eq(&self, other: &T) -> bool {
3477-
unsafe { self.0.priv_.p == other.as_ptr() as *mut std::os::raw::c_void }
3475+
unsafe { (*self.0).0.priv_.p == other.as_ptr() as *mut std::os::raw::c_void }
34783476
}
34793477
}
34803478

3481-
impl<T: ObjectType> PartialOrd for WeakRef<T> {
3479+
impl<T: ObjectType> PartialOrd for WeakRefInner<T> {
34823480
#[inline]
34833481
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
34843482
unsafe { self.0.priv_.p.partial_cmp(&other.0.priv_.p) }

0 commit comments

Comments
 (0)