1
- use alloc:: borrow;
2
1
use core:: fmt;
3
- use core:: hash;
4
- use core:: iter:: FusedIterator ;
5
2
use core:: marker:: PhantomData ;
6
3
use core:: mem:: ManuallyDrop ;
7
4
use core:: ops:: { Deref , DerefMut } ;
8
5
use core:: ptr:: NonNull ;
6
+ use std:: panic:: { RefUnwindSafe , UnwindSafe } ;
9
7
10
8
use super :: AutoreleasePool ;
11
9
use super :: { Owned , Ownership , Shared } ;
@@ -273,9 +271,11 @@ impl<T: Message> Id<T, Owned> {
273
271
///
274
272
/// # Safety
275
273
///
276
- /// The caller must ensure that there are no other pointers to the same
277
- /// object (which also means that the given [`Id`] should have a retain
278
- /// count of exactly 1 all cases, except when autoreleases are involved).
274
+ /// The caller must ensure that there are no other pointers (including
275
+ /// [`WeakId`][`super::WeakId`] pointers) to the same object.
276
+ ///
277
+ /// This also means that the given [`Id`] should have a retain count of
278
+ /// exactly 1 (except when autoreleases are involved).
279
279
#[ inline]
280
280
pub unsafe fn from_shared ( obj : Id < T , Shared > ) -> Self {
281
281
// Note: We can't debug_assert retainCount because of autoreleases
@@ -379,6 +379,8 @@ impl<T, O: Ownership> Deref for Id<T, O> {
379
379
type Target = T ;
380
380
381
381
/// Obtain an immutable reference to the object.
382
+ // Box doesn't inline, but that's because it's a compiler built-in
383
+ #[ inline]
382
384
fn deref ( & self ) -> & T {
383
385
// SAFETY: The pointer's validity is verified when the type is created
384
386
unsafe { self . ptr . as_ref ( ) }
@@ -387,6 +389,7 @@ impl<T, O: Ownership> Deref for Id<T, O> {
387
389
388
390
impl < T > DerefMut for Id < T , Owned > {
389
391
/// Obtain a mutable reference to the object.
392
+ #[ inline]
390
393
fn deref_mut ( & mut self ) -> & mut T {
391
394
// SAFETY: The pointer's validity is verified when the type is created
392
395
// Additionally, the owned `Id` is the unique owner of the object, so
@@ -395,142 +398,25 @@ impl<T> DerefMut for Id<T, Owned> {
395
398
}
396
399
}
397
400
398
- impl < T : PartialEq , O : Ownership > PartialEq for Id < T , O > {
399
- #[ inline]
400
- fn eq ( & self , other : & Self ) -> bool {
401
- ( * * self ) . eq ( & * * other)
402
- }
403
-
404
- #[ inline]
405
- fn ne ( & self , other : & Self ) -> bool {
406
- ( * * self ) . ne ( & * * other)
407
- }
408
- }
409
-
410
- impl < T : Eq , O : Ownership > Eq for Id < T , O > { }
411
-
412
- impl < T : PartialOrd , O : Ownership > PartialOrd for Id < T , O > {
413
- #[ inline]
414
- fn partial_cmp ( & self , other : & Self ) -> Option < core:: cmp:: Ordering > {
415
- ( * * self ) . partial_cmp ( & * * other)
416
- }
417
- #[ inline]
418
- fn lt ( & self , other : & Self ) -> bool {
419
- ( * * self ) . lt ( & * * other)
420
- }
421
- #[ inline]
422
- fn le ( & self , other : & Self ) -> bool {
423
- ( * * self ) . le ( & * * other)
424
- }
425
- #[ inline]
426
- fn ge ( & self , other : & Self ) -> bool {
427
- ( * * self ) . ge ( & * * other)
428
- }
429
- #[ inline]
430
- fn gt ( & self , other : & Self ) -> bool {
431
- ( * * self ) . gt ( & * * other)
432
- }
433
- }
434
-
435
- impl < T : Ord , O : Ownership > Ord for Id < T , O > {
436
- #[ inline]
437
- fn cmp ( & self , other : & Self ) -> core:: cmp:: Ordering {
438
- ( * * self ) . cmp ( & * * other)
439
- }
440
- }
441
-
442
- impl < T : hash:: Hash , O : Ownership > hash:: Hash for Id < T , O > {
443
- fn hash < H : hash:: Hasher > ( & self , state : & mut H ) {
444
- ( * * self ) . hash ( state)
445
- }
446
- }
447
-
448
- impl < T : fmt:: Display , O : Ownership > fmt:: Display for Id < T , O > {
449
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
450
- ( * * self ) . fmt ( f)
451
- }
452
- }
453
-
454
- impl < T : fmt:: Debug , O : Ownership > fmt:: Debug for Id < T , O > {
455
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
456
- ( * * self ) . fmt ( f)
457
- }
458
- }
459
-
460
401
impl < T , O : Ownership > fmt:: Pointer for Id < T , O > {
461
402
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
462
403
fmt:: Pointer :: fmt ( & self . ptr . as_ptr ( ) , f)
463
404
}
464
405
}
465
406
466
- impl < I : Iterator > Iterator for Id < I , Owned > {
467
- type Item = I :: Item ;
468
- fn next ( & mut self ) -> Option < I :: Item > {
469
- ( * * self ) . next ( )
470
- }
471
- fn size_hint ( & self ) -> ( usize , Option < usize > ) {
472
- ( * * self ) . size_hint ( )
473
- }
474
- fn nth ( & mut self , n : usize ) -> Option < I :: Item > {
475
- ( * * self ) . nth ( n)
476
- }
477
- }
478
-
479
- impl < I : DoubleEndedIterator > DoubleEndedIterator for Id < I , Owned > {
480
- fn next_back ( & mut self ) -> Option < I :: Item > {
481
- ( * * self ) . next_back ( )
482
- }
483
- fn nth_back ( & mut self , n : usize ) -> Option < I :: Item > {
484
- ( * * self ) . nth_back ( n)
485
- }
486
- }
487
-
488
- impl < I : ExactSizeIterator > ExactSizeIterator for Id < I , Owned > {
489
- fn len ( & self ) -> usize {
490
- ( * * self ) . len ( )
491
- }
492
- }
493
-
494
- impl < I : FusedIterator > FusedIterator for Id < I , Owned > { }
495
-
496
- impl < T , O : Ownership > borrow:: Borrow < T > for Id < T , O > {
497
- fn borrow ( & self ) -> & T {
498
- & * * self
499
- }
500
- }
501
-
502
- impl < T > borrow:: BorrowMut < T > for Id < T , Owned > {
503
- fn borrow_mut ( & mut self ) -> & mut T {
504
- & mut * * self
505
- }
506
- }
507
-
508
- impl < T , O : Ownership > AsRef < T > for Id < T , O > {
509
- fn as_ref ( & self ) -> & T {
510
- & * * self
511
- }
512
- }
513
-
514
- impl < T > AsMut < T > for Id < T , Owned > {
515
- fn as_mut ( & mut self ) -> & mut T {
516
- & mut * * self
517
- }
518
- }
519
-
520
- // impl<F: Future + Unpin> Future for Id<F, Owned> {
521
- // type Output = F::Output;
522
- // fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
523
- // F::poll(Pin::new(&mut *self), cx)
524
- // }
525
- // }
526
-
527
407
// This is valid without `T: Unpin` because we don't implement any projection.
528
408
//
529
409
// See https://doc.rust-lang.org/1.54.0/src/alloc/boxed.rs.html#1652-1675
530
410
// and the `Arc` implementation.
531
411
impl < T , O : Ownership > Unpin for Id < T , O > { }
532
412
533
- // TODO: When stabilized impl Fn traits & CoerceUnsized
413
+ impl < T : RefUnwindSafe , O : Ownership > RefUnwindSafe for Id < T , O > { }
414
+
415
+ // Same as `Arc<T>`.
416
+ impl < T : RefUnwindSafe > UnwindSafe for Id < T , Shared > { }
417
+
418
+ // Same as `Box<T>`.
419
+ impl < T : UnwindSafe > UnwindSafe for Id < T , Owned > { }
534
420
535
421
#[ cfg( test) ]
536
422
mod tests {
0 commit comments