@@ -394,11 +394,9 @@ impl<T> Opaque<T> {
394394 }
395395}
396396
397- /// Types that are _always_ reference counted.
397+ /// Types that are internally reference counted.
398398///
399399/// It allows such types to define their own custom ref increment and decrement functions.
400- /// Additionally, it allows users to convert from a shared reference `&T` to an owned reference
401- /// [`ARef<T>`].
402400///
403401/// This is usually implemented by wrappers to existing structures on the C side of the code. For
404402/// Rust code, the recommendation is to use [`Arc`](crate::sync::Arc) to create reference-counted
@@ -410,9 +408,8 @@ impl<T> Opaque<T> {
410408/// at least until matching decrements are performed.
411409///
412410/// Implementers must also ensure that all instances are reference-counted. (Otherwise they
413- /// won't be able to honour the requirement that [`AlwaysRefCounted::inc_ref`] keep the object
414- /// alive.)
415- pub unsafe trait AlwaysRefCounted {
411+ /// won't be able to honour the requirement that [`RefCounted::inc_ref`] keep the object alive.)
412+ pub unsafe trait RefCounted {
416413 /// Increments the reference count on the object.
417414 fn inc_ref ( & self ) ;
418415
@@ -425,11 +422,21 @@ pub unsafe trait AlwaysRefCounted {
425422 /// Callers must ensure that there was a previous matching increment to the reference count,
426423 /// and that the object is no longer used after its reference count is decremented (as it may
427424 /// result in the object being freed), unless the caller owns another increment on the refcount
428- /// (e.g., it calls [`AlwaysRefCounted::inc_ref`] twice, then calls
429- /// [`AlwaysRefCounted::dec_ref`] once).
425+ /// (e.g., it calls [`RefCounted::inc_ref`] twice, then calls [`RefCounted::dec_ref`] once).
430426 unsafe fn dec_ref ( obj : NonNull < Self > ) ;
431427}
432428
429+ /// An extension to RefCounted, which declares that it is allowed to convert
430+ /// from a shared reference `&T` to an owned reference [`ARef<T>`].
431+ ///
432+ /// # Safety
433+ ///
434+ /// Implementers must ensure that no safety invariants are violated by upgrading an `&T`
435+ /// to an [`ARef<T>`]. In particular that implies [`AlwaysRefCounted`] and [`Ownable`]
436+ /// cannot be implemented for the same type, as this would allow to violate the uniqueness
437+ /// guarantee of [`Owned<T>`] by derefencing it into an `&T` and obtaining an [`ARef`] from that.
438+ pub unsafe trait AlwaysRefCounted : RefCounted { }
439+
433440/// An owned reference to an always-reference-counted object.
434441///
435442/// The object's reference count is automatically decremented when an instance of [`ARef`] is
@@ -440,7 +447,7 @@ pub unsafe trait AlwaysRefCounted {
440447///
441448/// The pointer stored in `ptr` is non-null and valid for the lifetime of the [`ARef`] instance. In
442449/// particular, the [`ARef`] instance owns an increment on the underlying object's reference count.
443- pub struct ARef < T : AlwaysRefCounted > {
450+ pub struct ARef < T : RefCounted > {
444451 ptr : NonNull < T > ,
445452 _p : PhantomData < T > ,
446453}
@@ -449,16 +456,16 @@ pub struct ARef<T: AlwaysRefCounted> {
449456// it effectively means sharing `&T` (which is safe because `T` is `Sync`); additionally, it needs
450457// `T` to be `Send` because any thread that has an `ARef<T>` may ultimately access `T` using a
451458// mutable reference, for example, when the reference count reaches zero and `T` is dropped.
452- unsafe impl < T : AlwaysRefCounted + Sync + Send > Send for ARef < T > { }
459+ unsafe impl < T : RefCounted + Sync + Send > Send for ARef < T > { }
453460
454461// SAFETY: It is safe to send `&ARef<T>` to another thread when the underlying `T` is `Sync`
455462// because it effectively means sharing `&T` (which is safe because `T` is `Sync`); additionally,
456463// it needs `T` to be `Send` because any thread that has a `&ARef<T>` may clone it and get an
457464// `ARef<T>` on that thread, so the thread may ultimately access `T` using a mutable reference, for
458465// example, when the reference count reaches zero and `T` is dropped.
459- unsafe impl < T : AlwaysRefCounted + Sync + Send > Sync for ARef < T > { }
466+ unsafe impl < T : RefCounted + Sync + Send > Sync for ARef < T > { }
460467
461- impl < T : AlwaysRefCounted > ARef < T > {
468+ impl < T : RefCounted > ARef < T > {
462469 /// Creates a new instance of [`ARef`].
463470 ///
464471 /// It takes over an increment of the reference count on the underlying object.
@@ -487,12 +494,12 @@ impl<T: AlwaysRefCounted> ARef<T> {
487494 ///
488495 /// ```
489496 /// use core::ptr::NonNull;
490- /// use kernel::types::{ARef, AlwaysRefCounted };
497+ /// use kernel::types::{ARef, RefCounted };
491498 ///
492499 /// struct Empty {}
493500 ///
494501 /// # // SAFETY: TODO.
495- /// unsafe impl AlwaysRefCounted for Empty {
502+ /// unsafe impl RefCounted for Empty {
496503 /// fn inc_ref(&self) {}
497504 /// unsafe fn dec_ref(_obj: NonNull<Self>) {}
498505 /// }
@@ -510,15 +517,15 @@ impl<T: AlwaysRefCounted> ARef<T> {
510517 }
511518}
512519
513- impl < T : AlwaysRefCounted > Clone for ARef < T > {
520+ impl < T : RefCounted > Clone for ARef < T > {
514521 fn clone ( & self ) -> Self {
515522 self . inc_ref ( ) ;
516523 // SAFETY: We just incremented the refcount above.
517524 unsafe { Self :: from_raw ( self . ptr ) }
518525 }
519526}
520527
521- impl < T : AlwaysRefCounted > Deref for ARef < T > {
528+ impl < T : RefCounted > Deref for ARef < T > {
522529 type Target = T ;
523530
524531 fn deref ( & self ) -> & Self :: Target {
@@ -535,7 +542,7 @@ impl<T: AlwaysRefCounted> From<&T> for ARef<T> {
535542 }
536543}
537544
538- impl < T : AlwaysRefCounted > Drop for ARef < T > {
545+ impl < T : RefCounted > Drop for ARef < T > {
539546 fn drop ( & mut self ) {
540547 // SAFETY: The type invariants guarantee that the `ARef` owns the reference we're about to
541548 // decrement.
0 commit comments