@@ -402,11 +402,9 @@ impl<T> Opaque<T> {
402
402
}
403
403
}
404
404
405
- /// Types that are _always_ reference counted.
405
+ /// Types that are internally reference counted.
406
406
///
407
407
/// It allows such types to define their own custom ref increment and decrement functions.
408
- /// Additionally, it allows users to convert from a shared reference `&T` to an owned reference
409
- /// [`ARef<T>`].
410
408
///
411
409
/// This is usually implemented by wrappers to existing structures on the C side of the code. For
412
410
/// Rust code, the recommendation is to use [`Arc`](crate::sync::Arc) to create reference-counted
@@ -418,9 +416,9 @@ impl<T> Opaque<T> {
418
416
/// at least until matching decrements are performed.
419
417
///
420
418
/// Implementers must also ensure that all instances are reference-counted. (Otherwise they
421
- /// won't be able to honour the requirement that [`AlwaysRefCounted ::inc_ref`] keep the object
419
+ /// won't be able to honour the requirement that [`RefCounted ::inc_ref`] keep the object
422
420
/// alive.)
423
- pub unsafe trait AlwaysRefCounted {
421
+ pub unsafe trait RefCounted {
424
422
/// Increments the reference count on the object.
425
423
fn inc_ref ( & self ) ;
426
424
@@ -433,11 +431,22 @@ pub unsafe trait AlwaysRefCounted {
433
431
/// Callers must ensure that there was a previous matching increment to the reference count,
434
432
/// and that the object is no longer used after its reference count is decremented (as it may
435
433
/// result in the object being freed), unless the caller owns another increment on the refcount
436
- /// (e.g., it calls [`AlwaysRefCounted ::inc_ref`] twice, then calls
437
- /// [`AlwaysRefCounted ::dec_ref`] once).
434
+ /// (e.g., it calls [`RefCounted ::inc_ref`] twice, then calls
435
+ /// [`RefCounted ::dec_ref`] once).
438
436
unsafe fn dec_ref ( obj : NonNull < Self > ) ;
439
437
}
440
438
439
+ /// An extension to RefCounted, which declares that it is allowed to convert
440
+ /// from a shared reference `&T` to an owned reference [`ARef<T>`].
441
+ ///
442
+ /// # Safety
443
+ ///
444
+ /// Implementers must ensure that no safety invariants are violated by upgrading an `&T`
445
+ /// to an [`ARef<T>`]. In particular that implies [`AlwaysRefCounted`] and [`Ownable`]
446
+ /// cannot be implemented for the same type, as this would allow to violate the uniqueness
447
+ /// guarantee of [`Owned<T>`] by derefencing it into an `&T` and obtaining an [`ARef`] from that.
448
+ pub unsafe trait AlwaysRefCounted : RefCounted { }
449
+
441
450
/// An owned reference to an always-reference-counted object.
442
451
///
443
452
/// The object's reference count is automatically decremented when an instance of [`ARef`] is
@@ -448,7 +457,7 @@ pub unsafe trait AlwaysRefCounted {
448
457
///
449
458
/// The pointer stored in `ptr` is non-null and valid for the lifetime of the [`ARef`] instance. In
450
459
/// particular, the [`ARef`] instance owns an increment on the underlying object's reference count.
451
- pub struct ARef < T : AlwaysRefCounted > {
460
+ pub struct ARef < T : RefCounted > {
452
461
ptr : NonNull < T > ,
453
462
_p : PhantomData < T > ,
454
463
}
@@ -457,16 +466,16 @@ pub struct ARef<T: AlwaysRefCounted> {
457
466
// it effectively means sharing `&T` (which is safe because `T` is `Sync`); additionally, it needs
458
467
// `T` to be `Send` because any thread that has an `ARef<T>` may ultimately access `T` using a
459
468
// mutable reference, for example, when the reference count reaches zero and `T` is dropped.
460
- unsafe impl < T : AlwaysRefCounted + Sync + Send > Send for ARef < T > { }
469
+ unsafe impl < T : RefCounted + Sync + Send > Send for ARef < T > { }
461
470
462
471
// SAFETY: It is safe to send `&ARef<T>` to another thread when the underlying `T` is `Sync`
463
472
// because it effectively means sharing `&T` (which is safe because `T` is `Sync`); additionally,
464
473
// it needs `T` to be `Send` because any thread that has a `&ARef<T>` may clone it and get an
465
474
// `ARef<T>` on that thread, so the thread may ultimately access `T` using a mutable reference, for
466
475
// example, when the reference count reaches zero and `T` is dropped.
467
- unsafe impl < T : AlwaysRefCounted + Sync + Send > Sync for ARef < T > { }
476
+ unsafe impl < T : RefCounted + Sync + Send > Sync for ARef < T > { }
468
477
469
- impl < T : AlwaysRefCounted > ARef < T > {
478
+ impl < T : RefCounted > ARef < T > {
470
479
/// Creates a new instance of [`ARef`].
471
480
///
472
481
/// It takes over an increment of the reference count on the underlying object.
@@ -495,12 +504,12 @@ impl<T: AlwaysRefCounted> ARef<T> {
495
504
///
496
505
/// ```
497
506
/// use core::ptr::NonNull;
498
- /// use kernel::types::{ARef, AlwaysRefCounted };
507
+ /// use kernel::types::{ARef, RefCounted };
499
508
///
500
509
/// struct Empty {}
501
510
///
502
511
/// # // SAFETY: TODO.
503
- /// unsafe impl AlwaysRefCounted for Empty {
512
+ /// unsafe impl RefCounted for Empty {
504
513
/// fn inc_ref(&self) {}
505
514
/// unsafe fn dec_ref(_obj: NonNull<Self>) {}
506
515
/// }
@@ -518,15 +527,15 @@ impl<T: AlwaysRefCounted> ARef<T> {
518
527
}
519
528
}
520
529
521
- impl < T : AlwaysRefCounted > Clone for ARef < T > {
530
+ impl < T : RefCounted > Clone for ARef < T > {
522
531
fn clone ( & self ) -> Self {
523
532
self . inc_ref ( ) ;
524
533
// SAFETY: We just incremented the refcount above.
525
534
unsafe { Self :: from_raw ( self . ptr ) }
526
535
}
527
536
}
528
537
529
- impl < T : AlwaysRefCounted > Deref for ARef < T > {
538
+ impl < T : RefCounted > Deref for ARef < T > {
530
539
type Target = T ;
531
540
532
541
fn deref ( & self ) -> & Self :: Target {
@@ -543,10 +552,10 @@ impl<T: AlwaysRefCounted> From<&T> for ARef<T> {
543
552
}
544
553
}
545
554
546
- impl < T : AlwaysRefCounted > Drop for ARef < T > {
555
+ impl < T : RefCounted > Drop for ARef < T > {
547
556
fn drop ( & mut self ) {
548
- // SAFETY: The type invariants guarantee that the `ARef` owns the reference we're about to
549
- // decrement.
557
+ // SAFETY: The type invariants guarantee that the `ARef` owns the reference
558
+ // we're about to decrement.
550
559
unsafe { T :: dec_ref ( self . ptr ) } ;
551
560
}
552
561
}
0 commit comments