3
3
// rustdoc-stripper-ignore-next
4
4
//! `IMPL` Object wrapper implementation and `Object` binding.
5
5
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
+ } ;
7
15
8
16
use crate :: {
9
17
closure:: TryFromClosureReturnValue ,
@@ -3029,9 +3037,12 @@ impl<T: ObjectType> ObjectExt for T {
3029
3037
#[ inline]
3030
3038
fn downgrade ( & self ) -> WeakRef < T > {
3031
3039
unsafe {
3032
- let w = WeakRef ( Box :: pin ( mem:: zeroed ( ) ) , PhantomData ) ;
3040
+ let w = WeakRef ( Arc :: pin ( WeakRefInner {
3041
+ ffi : mem:: zeroed ( ) ,
3042
+ phantom : PhantomData ,
3043
+ } ) ) ;
3033
3044
gobject_ffi:: g_weak_ref_init (
3034
- mut_override ( & * w. 0 ) ,
3045
+ mut_override ( & w. 0 . ffi ) ,
3035
3046
self . as_object_ref ( ) . to_glib_none ( ) . 0 ,
3036
3047
) ;
3037
3048
w
@@ -3373,9 +3384,15 @@ impl<T: ObjectType> WeakRefNotify<T> {
3373
3384
3374
3385
// rustdoc-stripper-ignore-next
3375
3386
/// A weak reference to an object.
3376
- #[ derive( Debug ) ]
3387
+ #[ derive( Debug , PartialEq , PartialOrd ) ]
3377
3388
#[ doc( alias = "GWeakRef" ) ]
3378
- pub struct WeakRef < T : ObjectType > ( Pin < Box < gobject_ffi:: GWeakRef > > , PhantomData < * mut T > ) ;
3389
+ pub struct WeakRef < T : ObjectType > ( Pin < Arc < WeakRefInner < T > > > ) ;
3390
+
3391
+ #[ derive( Debug ) ]
3392
+ struct WeakRefInner < T : ObjectType > {
3393
+ ffi : gobject_ffi:: GWeakRef ,
3394
+ phantom : PhantomData < * mut T > ,
3395
+ }
3379
3396
3380
3397
impl < T : ObjectType > WeakRef < T > {
3381
3398
// rustdoc-stripper-ignore-next
@@ -3385,11 +3402,11 @@ impl<T: ObjectType> WeakRef<T> {
3385
3402
#[ inline]
3386
3403
pub fn new ( ) -> WeakRef < T > {
3387
3404
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
- ) ;
3405
+ let w = WeakRef ( Arc :: pin ( WeakRefInner {
3406
+ ffi : mem :: zeroed ( ) ,
3407
+ phantom : PhantomData ,
3408
+ } ) ) ;
3409
+ gobject_ffi :: g_weak_ref_init ( mut_override ( & w . 0 . ffi ) , ptr :: null_mut ( ) ) ;
3393
3410
w
3394
3411
}
3395
3412
}
@@ -3401,7 +3418,7 @@ impl<T: ObjectType> WeakRef<T> {
3401
3418
pub fn set ( & self , obj : Option < & T > ) {
3402
3419
unsafe {
3403
3420
gobject_ffi:: g_weak_ref_set (
3404
- mut_override ( Pin :: as_ref ( & self . 0 ) . get_ref ( ) ) ,
3421
+ mut_override ( & self . 0 . ffi ) ,
3405
3422
obj. map_or ( std:: ptr:: null_mut ( ) , |obj| {
3406
3423
obj. as_object_ref ( ) . to_glib_none ( ) . 0
3407
3424
} ) ,
@@ -3417,7 +3434,7 @@ impl<T: ObjectType> WeakRef<T> {
3417
3434
#[ inline]
3418
3435
pub fn upgrade ( & self ) -> Option < T > {
3419
3436
unsafe {
3420
- let ptr = gobject_ffi:: g_weak_ref_get ( mut_override ( Pin :: as_ref ( & self . 0 ) . get_ref ( ) ) ) ;
3437
+ let ptr = gobject_ffi:: g_weak_ref_get ( mut_override ( & self . 0 . ffi ) ) ;
3421
3438
if ptr. is_null ( ) {
3422
3439
None
3423
3440
} else {
@@ -3428,29 +3445,19 @@ impl<T: ObjectType> WeakRef<T> {
3428
3445
}
3429
3446
}
3430
3447
3431
- impl < T : ObjectType > Drop for WeakRef < T > {
3448
+ impl < T : ObjectType > Drop for WeakRefInner < T > {
3432
3449
#[ inline]
3433
3450
fn drop ( & mut self ) {
3434
3451
unsafe {
3435
- gobject_ffi:: g_weak_ref_clear ( Pin :: as_mut ( & mut self . 0 ) . get_unchecked_mut ( ) ) ;
3452
+ gobject_ffi:: g_weak_ref_clear ( & mut self . ffi ) ;
3436
3453
}
3437
3454
}
3438
3455
}
3439
3456
3440
3457
impl < T : ObjectType > Clone for WeakRef < T > {
3441
3458
#[ inline]
3442
3459
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
- }
3460
+ Self ( self . 0 . clone ( ) )
3454
3461
}
3455
3462
}
3456
3463
@@ -3461,27 +3468,27 @@ impl<T: ObjectType> Default for WeakRef<T> {
3461
3468
}
3462
3469
}
3463
3470
3464
- unsafe impl < T : ObjectType + Sync + Sync > Sync for WeakRef < T > { }
3465
- unsafe impl < T : ObjectType + Send + Sync > Send for WeakRef < T > { }
3471
+ unsafe impl < T : ObjectType + Sync + Sync > Sync for WeakRefInner < T > { }
3472
+ unsafe impl < T : ObjectType + Send + Sync > Send for WeakRefInner < T > { }
3466
3473
3467
- impl < T : ObjectType > PartialEq for WeakRef < T > {
3474
+ impl < T : ObjectType > PartialEq for WeakRefInner < T > {
3468
3475
#[ inline]
3469
3476
fn eq ( & self , other : & Self ) -> bool {
3470
- unsafe { self . 0 . priv_ . p == other. 0 . priv_ . p }
3477
+ unsafe { self . ffi . priv_ . p == other. ffi . priv_ . p }
3471
3478
}
3472
3479
}
3473
3480
3474
3481
impl < T : ObjectType > PartialEq < T > for WeakRef < T > {
3475
3482
#[ inline]
3476
3483
fn eq ( & self , other : & T ) -> bool {
3477
- unsafe { self . 0 . priv_ . p == other. as_ptr ( ) as * mut std:: os:: raw:: c_void }
3484
+ unsafe { self . 0 . ffi . priv_ . p == other. as_ptr ( ) as * mut std:: os:: raw:: c_void }
3478
3485
}
3479
3486
}
3480
3487
3481
- impl < T : ObjectType > PartialOrd for WeakRef < T > {
3488
+ impl < T : ObjectType > PartialOrd for WeakRefInner < T > {
3482
3489
#[ inline]
3483
3490
fn partial_cmp ( & self , other : & Self ) -> Option < cmp:: Ordering > {
3484
- unsafe { self . 0 . priv_ . p . partial_cmp ( & other. 0 . priv_ . p ) }
3491
+ unsafe { self . ffi . priv_ . p . partial_cmp ( & other. ffi . priv_ . p ) }
3485
3492
}
3486
3493
}
3487
3494
0 commit comments