@@ -53,6 +53,9 @@ use crate::Message;
5353/// do so. If you need to run some code when the object is destroyed,
5454/// implement the `dealloc` method instead.
5555///
56+ /// This allows `?Sized` types `T`, but the intention is to only support when
57+ /// `T` is an `extern type` (yet unstable).
58+ ///
5659/// # Examples
5760///
5861/// ```no_run
@@ -95,9 +98,9 @@ use crate::Message;
9598/// ```
9699#[ repr( transparent) ]
97100// 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
99102// TODO: Consider changing the name of Id -> Retain
100- pub struct Id < T , O : Ownership > {
103+ pub struct Id < T : ? Sized , O : Ownership > {
101104 /// A pointer to the contained object. The pointer is always retained.
102105 ///
103106 /// It is important that this is `NonNull`, since we want to dereference
@@ -116,8 +119,7 @@ pub struct Id<T, O: Ownership> {
116119 own : PhantomData < O > ,
117120}
118121
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 > {
121123 /// Constructs an [`Id`] to an object that already has +1 retain count.
122124 ///
123125 /// 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> {
171173 own : PhantomData ,
172174 }
173175 }
176+ }
174177
178+ // TODO: Add ?Sized bound
179+ impl < T : Message , O : Ownership > Id < T , O > {
175180 /// Retains the given object pointer.
176181 ///
177182 /// 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> {
254259// }
255260// }
256261
262+ // TODO: Add ?Sized bound
257263impl < T : Message > Id < T , Owned > {
258264 /// Autoreleases the owned [`Id`], returning a mutable reference bound to
259265 /// the pool.
@@ -291,6 +297,7 @@ impl<T: Message> Id<T, Owned> {
291297 }
292298}
293299
300+ // TODO: Add ?Sized bound
294301impl < T : Message > Id < T , Shared > {
295302 /// Autoreleases the shared [`Id`], returning an aliased reference bound
296303 /// to the pool.
@@ -308,7 +315,7 @@ impl<T: Message> Id<T, Shared> {
308315 }
309316}
310317
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 > {
312319 /// Downgrade from an owned to a shared [`Id`], allowing it to be cloned.
313320 #[ inline]
314321 fn from ( obj : Id < T , Owned > ) -> Self {
@@ -318,6 +325,7 @@ impl<T: Message> From<Id<T, Owned>> for Id<T, Shared> {
318325 }
319326}
320327
328+ // TODO: Add ?Sized bound
321329impl < T : Message > Clone for Id < T , Shared > {
322330 /// Makes a clone of the shared object.
323331 ///
@@ -338,7 +346,7 @@ impl<T: Message> Clone for Id<T, Shared> {
338346/// borrowed data.
339347///
340348/// [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 > {
342350 /// Releases the retained object.
343351 ///
344352 /// 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> {
363371/// clone a `Id<T, Shared>`, send it to another thread, and drop the clone
364372/// last, making `dealloc` get called on the other thread, and violate
365373/// `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 > { }
367375
368376/// The `Sync` implementation requires `T: Sync` because `&Id<T, Shared>` give
369377/// access to `&T`.
370378///
371379/// Additiontally, it requires `T: Send`, because if `T: !Send`, you could
372380/// clone a `&Id<T, Shared>` from another thread, and drop the clone last,
373381/// 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 > { }
375383
376384/// `Id<T, Owned>` are `Send` if `T` is `Send` because they give the same
377385/// 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 > { }
379387
380388/// `Id<T, Owned>` are `Sync` if `T` is `Sync` because they give the same
381389/// 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 > { }
383391
384- impl < T , O : Ownership > Deref for Id < T , O > {
392+ impl < T : ? Sized , O : Ownership > Deref for Id < T , O > {
385393 type Target = T ;
386394
387395 /// Obtain an immutable reference to the object.
@@ -393,7 +401,7 @@ impl<T, O: Ownership> Deref for Id<T, O> {
393401 }
394402}
395403
396- impl < T > DerefMut for Id < T , Owned > {
404+ impl < T : ? Sized > DerefMut for Id < T , Owned > {
397405 /// Obtain a mutable reference to the object.
398406 #[ inline]
399407 fn deref_mut ( & mut self ) -> & mut T {
@@ -404,7 +412,7 @@ impl<T> DerefMut for Id<T, Owned> {
404412 }
405413}
406414
407- impl < T , O : Ownership > fmt:: Pointer for Id < T , O > {
415+ impl < T : ? Sized , O : Ownership > fmt:: Pointer for Id < T , O > {
408416 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
409417 fmt:: Pointer :: fmt ( & self . ptr . as_ptr ( ) , f)
410418 }
@@ -414,15 +422,15 @@ impl<T, O: Ownership> fmt::Pointer for Id<T, O> {
414422//
415423// See https://doc.rust-lang.org/1.54.0/src/alloc/boxed.rs.html#1652-1675
416424// 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 > { }
418426
419- impl < T : RefUnwindSafe , O : Ownership > RefUnwindSafe for Id < T , O > { }
427+ impl < T : RefUnwindSafe + ? Sized , O : Ownership > RefUnwindSafe for Id < T , O > { }
420428
421429// Same as `Arc<T>`.
422- impl < T : RefUnwindSafe > UnwindSafe for Id < T , Shared > { }
430+ impl < T : RefUnwindSafe + ? Sized > UnwindSafe for Id < T , Shared > { }
423431
424432// Same as `Box<T>`.
425- impl < T : UnwindSafe > UnwindSafe for Id < T , Owned > { }
433+ impl < T : UnwindSafe + ? Sized > UnwindSafe for Id < T , Owned > { }
426434
427435#[ cfg( test) ]
428436mod tests {
0 commit comments