@@ -199,24 +199,32 @@ macro_rules! make_base_ref {
199
199
#[ doc = concat!( "This can be used to call methods on the base object of a " , $object_name, " that takes `&self` as the receiver.\n \n " ) ]
200
200
#[ doc = concat!( "See [`" , stringify!( $doc_type) , "::base()`](" , stringify!( $doc_path) , "::base()) for usage." ) ]
201
201
pub struct $ident<' a, T : $bound> {
202
- gd: Gd <T :: Base >,
202
+ // Weak reference to the base object. Safe because 'a lifetime keeps the object (strong ref) alive.
203
+ // Option because Gd::drop_weak() takes ownership, thus can't use ManuallyDrop.
204
+ weak_gd: Option <Gd <T :: Base >>,
203
205
_instance: & ' a T ,
204
206
}
205
207
206
208
impl <' a, T : $bound> $ident<' a, T > {
207
- pub ( crate ) fn new( gd : Gd <T :: Base >, instance: & ' a T ) -> Self {
209
+ pub ( crate ) fn new( weak_gd : Gd <T :: Base >, instance: & ' a T ) -> Self {
208
210
Self {
209
- gd ,
211
+ weak_gd : Some ( weak_gd ) ,
210
212
_instance: instance,
211
213
}
212
214
}
213
215
}
214
216
217
+ impl <' a, T : $bound> Drop for $ident<' a, T > {
218
+ fn drop( & mut self ) {
219
+ self . weak_gd. take( ) . unwrap( ) . drop_weak( ) ;
220
+ }
221
+ }
222
+
215
223
impl <T : $bound> Deref for $ident<' _, T > {
216
224
type Target = Gd <T :: Base >;
217
225
218
226
fn deref( & self ) -> & Gd <T :: Base > {
219
- & self . gd
227
+ self . weak_gd . as_ref ( ) . unwrap ( )
220
228
}
221
229
}
222
230
} ;
@@ -232,33 +240,41 @@ macro_rules! make_base_mut {
232
240
///
233
241
#[ doc = concat!( "See [`" , stringify!( $doc_type) , "::base_mut()`](" , stringify!( $doc_path) , "::base_mut()) for usage.\n " ) ]
234
242
pub struct $ident<' a, T : $bound> {
235
- gd: Gd <T :: Base >,
243
+ // Weak reference to the base object. Safe because 'a lifetime keeps the object (strong ref) alive.
244
+ // Option because Gd::drop_weak() takes ownership, thus can't use ManuallyDrop.
245
+ weak_gd: Option <Gd <T :: Base >>,
236
246
_inaccessible_guard: InaccessibleGuard <' a, T >,
237
247
}
238
248
239
249
impl <' a, T : $bound> $ident<' a, T > {
240
250
pub ( crate ) fn new(
241
- gd : Gd <T :: Base >,
251
+ weak_gd : Gd <T :: Base >,
242
252
inaccessible_guard: InaccessibleGuard <' a, T >,
243
253
) -> Self {
244
254
Self {
245
- gd ,
255
+ weak_gd : Some ( weak_gd ) ,
246
256
_inaccessible_guard: inaccessible_guard,
247
257
}
248
258
}
249
259
}
250
260
261
+ impl <' a, T : $bound> Drop for $ident<' a, T > {
262
+ fn drop( & mut self ) {
263
+ self . weak_gd. take( ) . unwrap( ) . drop_weak( ) ;
264
+ }
265
+ }
266
+
251
267
impl <T : $bound> Deref for $ident<' _, T > {
252
268
type Target = Gd <T :: Base >;
253
269
254
270
fn deref( & self ) -> & Gd <T :: Base > {
255
- & self . gd
271
+ self . weak_gd . as_ref ( ) . unwrap ( )
256
272
}
257
273
}
258
274
259
275
impl <T : $bound> DerefMut for $ident<' _, T > {
260
276
fn deref_mut( & mut self ) -> & mut Gd <T :: Base > {
261
- & mut self . gd
277
+ self . weak_gd . as_mut ( ) . unwrap ( )
262
278
}
263
279
}
264
280
} ;
0 commit comments