Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Make WeakRef cheaply cloneable #1045

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 36 additions & 29 deletions glib/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@
// rustdoc-stripper-ignore-next
//! `IMPL` Object wrapper implementation and `Object` binding.

use std::{cmp, fmt, hash, marker::PhantomData, mem, mem::ManuallyDrop, ops, pin::Pin, ptr};
use std::{
cmp, fmt, hash,
marker::PhantomData,
mem::{self, ManuallyDrop},
ops,
pin::Pin,
ptr,
sync::Arc,
};

use crate::{
closure::TryFromClosureReturnValue,
Expand Down Expand Up @@ -3029,9 +3037,12 @@ impl<T: ObjectType> ObjectExt for T {
#[inline]
fn downgrade(&self) -> WeakRef<T> {
unsafe {
let w = WeakRef(Box::pin(mem::zeroed()), PhantomData);
let w = WeakRef(Arc::pin(WeakRefInner {
ffi: mem::zeroed(),
phantom: PhantomData,
}));
gobject_ffi::g_weak_ref_init(
mut_override(&*w.0),
mut_override(&w.0.ffi),
self.as_object_ref().to_glib_none().0,
);
w
Expand Down Expand Up @@ -3375,7 +3386,13 @@ impl<T: ObjectType> WeakRefNotify<T> {
/// A weak reference to an object.
#[derive(Debug)]
#[doc(alias = "GWeakRef")]
pub struct WeakRef<T: ObjectType>(Pin<Box<gobject_ffi::GWeakRef>>, PhantomData<*mut T>);
pub struct WeakRef<T: ObjectType>(Pin<Arc<WeakRefInner<T>>>);

#[derive(Debug)]
struct WeakRefInner<T: ObjectType> {
ffi: gobject_ffi::GWeakRef,
phantom: PhantomData<*mut T>,
}

impl<T: ObjectType> WeakRef<T> {
// rustdoc-stripper-ignore-next
Expand All @@ -3385,11 +3402,11 @@ impl<T: ObjectType> WeakRef<T> {
#[inline]
pub fn new() -> WeakRef<T> {
unsafe {
let mut w = WeakRef(Box::pin(mem::zeroed()), PhantomData);
gobject_ffi::g_weak_ref_init(
Pin::as_mut(&mut w.0).get_unchecked_mut(),
ptr::null_mut(),
);
let w = WeakRef(Arc::pin(WeakRefInner {
ffi: mem::zeroed(),
phantom: PhantomData,
}));
gobject_ffi::g_weak_ref_init(mut_override(&w.0.ffi), ptr::null_mut());
w
}
}
Expand All @@ -3401,7 +3418,7 @@ impl<T: ObjectType> WeakRef<T> {
pub fn set(&self, obj: Option<&T>) {
unsafe {
gobject_ffi::g_weak_ref_set(
mut_override(Pin::as_ref(&self.0).get_ref()),
mut_override(&self.0.ffi),
obj.map_or(std::ptr::null_mut(), |obj| {
obj.as_object_ref().to_glib_none().0
}),
Expand All @@ -3417,7 +3434,7 @@ impl<T: ObjectType> WeakRef<T> {
#[inline]
pub fn upgrade(&self) -> Option<T> {
unsafe {
let ptr = gobject_ffi::g_weak_ref_get(mut_override(Pin::as_ref(&self.0).get_ref()));
let ptr = gobject_ffi::g_weak_ref_get(mut_override(&self.0.ffi));
if ptr.is_null() {
None
} else {
Expand All @@ -3428,29 +3445,19 @@ impl<T: ObjectType> WeakRef<T> {
}
}

impl<T: ObjectType> Drop for WeakRef<T> {
impl<T: ObjectType> Drop for WeakRefInner<T> {
#[inline]
fn drop(&mut self) {
unsafe {
gobject_ffi::g_weak_ref_clear(Pin::as_mut(&mut self.0).get_unchecked_mut());
gobject_ffi::g_weak_ref_clear(&mut self.ffi);
}
}
}

impl<T: ObjectType> Clone for WeakRef<T> {
#[inline]
fn clone(&self) -> Self {
unsafe {
let o = self.upgrade();

let mut c = WeakRef(Box::pin(mem::zeroed()), PhantomData);
gobject_ffi::g_weak_ref_init(
Pin::as_mut(&mut c.0).get_unchecked_mut(),
o.to_glib_none().0 as *mut gobject_ffi::GObject,
);

c
}
Self(self.0.clone())
}
}

Expand All @@ -3461,27 +3468,27 @@ impl<T: ObjectType> Default for WeakRef<T> {
}
}

unsafe impl<T: ObjectType + Sync + Sync> Sync for WeakRef<T> {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops. Can you make a separate PR for this?

unsafe impl<T: ObjectType + Send + Sync> Send for WeakRef<T> {}
unsafe impl<T: ObjectType + Send + Sync> Sync for WeakRefInner<T> {}
unsafe impl<T: ObjectType + Send + Sync> Send for WeakRefInner<T> {}

impl<T: ObjectType> PartialEq for WeakRef<T> {
#[inline]
fn eq(&self, other: &Self) -> bool {
unsafe { self.0.priv_.p == other.0.priv_.p }
unsafe { self.0.ffi.priv_.p == other.0.ffi.priv_.p }
}
}

impl<T: ObjectType> PartialEq<T> for WeakRef<T> {
#[inline]
fn eq(&self, other: &T) -> bool {
unsafe { self.0.priv_.p == other.as_ptr() as *mut std::os::raw::c_void }
unsafe { self.0.ffi.priv_.p == other.as_ptr() as *mut std::os::raw::c_void }
}
}

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

Expand Down