@@ -53,6 +53,9 @@ use crate::Message;
53
53
/// do so. If you need to run some code when the object is destroyed,
54
54
/// implement the `dealloc` method instead.
55
55
///
56
+ /// This allows `?Sized` types `T`, but the intention is to only support when
57
+ /// `T` is an `extern type` (yet unstable).
58
+ ///
56
59
/// # Examples
57
60
///
58
61
/// ```no_run
@@ -95,9 +98,9 @@ use crate::Message;
95
98
/// ```
96
99
#[ repr( transparent) ]
97
100
// TODO: Figure out if `Message` bound on `T` would be better here?
98
- // TODO: Add `?Sized + ptr::Thin` bound on `T` to allow for extern types
101
+ // TODO: Add `ptr::Thin` bound on `T` to allow for only extern types
99
102
// TODO: Consider changing the name of Id -> Retain
100
- pub struct Id < T , O : Ownership > {
103
+ pub struct Id < T : ? Sized , O : Ownership > {
101
104
/// A pointer to the contained object. The pointer is always retained.
102
105
///
103
106
/// It is important that this is `NonNull`, since we want to dereference
@@ -116,8 +119,7 @@ pub struct Id<T, O: Ownership> {
116
119
own : PhantomData < O > ,
117
120
}
118
121
119
- // TODO: Maybe make most of these functions "associated" functions instead?
120
- impl < T : Message , O : Ownership > Id < T , O > {
122
+ impl < T : Message + ?Sized , O : Ownership > Id < T , O > {
121
123
/// Constructs an [`Id`] to an object that already has +1 retain count.
122
124
///
123
125
/// This is useful when you have a retain count that has been handed off
@@ -171,7 +173,10 @@ impl<T: Message, O: Ownership> Id<T, O> {
171
173
own : PhantomData ,
172
174
}
173
175
}
176
+ }
174
177
178
+ // TODO: Add ?Sized bound
179
+ impl < T : Message , O : Ownership > Id < T , O > {
175
180
/// Retains the given object pointer.
176
181
///
177
182
/// This is useful when you have been given a pointer to an object from
@@ -254,6 +259,7 @@ impl<T: Message, O: Ownership> Id<T, O> {
254
259
// }
255
260
// }
256
261
262
+ // TODO: Add ?Sized bound
257
263
impl < T : Message > Id < T , Owned > {
258
264
/// Autoreleases the owned [`Id`], returning a mutable reference bound to
259
265
/// the pool.
@@ -291,6 +297,7 @@ impl<T: Message> Id<T, Owned> {
291
297
}
292
298
}
293
299
300
+ // TODO: Add ?Sized bound
294
301
impl < T : Message > Id < T , Shared > {
295
302
/// Autoreleases the shared [`Id`], returning an aliased reference bound
296
303
/// to the pool.
@@ -308,7 +315,7 @@ impl<T: Message> Id<T, Shared> {
308
315
}
309
316
}
310
317
311
- impl < T : Message > From < Id < T , Owned > > for Id < T , Shared > {
318
+ impl < T : Message + ? Sized > From < Id < T , Owned > > for Id < T , Shared > {
312
319
/// Downgrade from an owned to a shared [`Id`], allowing it to be cloned.
313
320
#[ inline]
314
321
fn from ( obj : Id < T , Owned > ) -> Self {
@@ -318,6 +325,7 @@ impl<T: Message> From<Id<T, Owned>> for Id<T, Shared> {
318
325
}
319
326
}
320
327
328
+ // TODO: Add ?Sized bound
321
329
impl < T : Message > Clone for Id < T , Shared > {
322
330
/// Makes a clone of the shared object.
323
331
///
@@ -338,7 +346,7 @@ impl<T: Message> Clone for Id<T, Shared> {
338
346
/// borrowed data.
339
347
///
340
348
/// [dropck_eyepatch]: https://doc.rust-lang.org/nightly/nomicon/dropck.html#an-escape-hatch
341
- impl < T , O : Ownership > Drop for Id < T , O > {
349
+ impl < T : ? Sized , O : Ownership > Drop for Id < T , O > {
342
350
/// Releases the retained object.
343
351
///
344
352
/// The contained object's destructor (if it has one) is never run!
@@ -363,25 +371,25 @@ impl<T, O: Ownership> Drop for Id<T, O> {
363
371
/// clone a `Id<T, Shared>`, send it to another thread, and drop the clone
364
372
/// last, making `dealloc` get called on the other thread, and violate
365
373
/// `T: !Send`.
366
- unsafe impl < T : Sync + Send > Send for Id < T , Shared > { }
374
+ unsafe impl < T : Sync + Send + ? Sized > Send for Id < T , Shared > { }
367
375
368
376
/// The `Sync` implementation requires `T: Sync` because `&Id<T, Shared>` give
369
377
/// access to `&T`.
370
378
///
371
379
/// Additiontally, it requires `T: Send`, because if `T: !Send`, you could
372
380
/// clone a `&Id<T, Shared>` from another thread, and drop the clone last,
373
381
/// making `dealloc` get called on the other thread, and violate `T: !Send`.
374
- unsafe impl < T : Sync + Send > Sync for Id < T , Shared > { }
382
+ unsafe impl < T : Sync + Send + ? Sized > Sync for Id < T , Shared > { }
375
383
376
384
/// `Id<T, Owned>` are `Send` if `T` is `Send` because they give the same
377
385
/// access as having a T directly.
378
- unsafe impl < T : Send > Send for Id < T , Owned > { }
386
+ unsafe impl < T : Send + ? Sized > Send for Id < T , Owned > { }
379
387
380
388
/// `Id<T, Owned>` are `Sync` if `T` is `Sync` because they give the same
381
389
/// access as having a `T` directly.
382
- unsafe impl < T : Sync > Sync for Id < T , Owned > { }
390
+ unsafe impl < T : Sync + ? Sized > Sync for Id < T , Owned > { }
383
391
384
- impl < T , O : Ownership > Deref for Id < T , O > {
392
+ impl < T : ? Sized , O : Ownership > Deref for Id < T , O > {
385
393
type Target = T ;
386
394
387
395
/// Obtain an immutable reference to the object.
@@ -393,7 +401,7 @@ impl<T, O: Ownership> Deref for Id<T, O> {
393
401
}
394
402
}
395
403
396
- impl < T > DerefMut for Id < T , Owned > {
404
+ impl < T : ? Sized > DerefMut for Id < T , Owned > {
397
405
/// Obtain a mutable reference to the object.
398
406
#[ inline]
399
407
fn deref_mut ( & mut self ) -> & mut T {
@@ -404,7 +412,7 @@ impl<T> DerefMut for Id<T, Owned> {
404
412
}
405
413
}
406
414
407
- impl < T , O : Ownership > fmt:: Pointer for Id < T , O > {
415
+ impl < T : ? Sized , O : Ownership > fmt:: Pointer for Id < T , O > {
408
416
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
409
417
fmt:: Pointer :: fmt ( & self . ptr . as_ptr ( ) , f)
410
418
}
@@ -414,15 +422,15 @@ impl<T, O: Ownership> fmt::Pointer for Id<T, O> {
414
422
//
415
423
// See https://doc.rust-lang.org/1.54.0/src/alloc/boxed.rs.html#1652-1675
416
424
// and the `Arc` implementation.
417
- impl < T , O : Ownership > Unpin for Id < T , O > { }
425
+ impl < T : ? Sized , O : Ownership > Unpin for Id < T , O > { }
418
426
419
- impl < T : RefUnwindSafe , O : Ownership > RefUnwindSafe for Id < T , O > { }
427
+ impl < T : RefUnwindSafe + ? Sized , O : Ownership > RefUnwindSafe for Id < T , O > { }
420
428
421
429
// Same as `Arc<T>`.
422
- impl < T : RefUnwindSafe > UnwindSafe for Id < T , Shared > { }
430
+ impl < T : RefUnwindSafe + ? Sized > UnwindSafe for Id < T , Shared > { }
423
431
424
432
// Same as `Box<T>`.
425
- impl < T : UnwindSafe > UnwindSafe for Id < T , Owned > { }
433
+ impl < T : UnwindSafe + ? Sized > UnwindSafe for Id < T , Owned > { }
426
434
427
435
#[ cfg( test) ]
428
436
mod tests {
0 commit comments