@@ -388,11 +388,9 @@ impl<T> Opaque<T> {
388388 }
389389}
390390
391- /// Types that are _always_ reference counted.
391+ /// Types that are internally reference counted.
392392///
393393/// It allows such types to define their own custom ref increment and decrement functions.
394- /// Additionally, it allows users to convert from a shared reference `&T` to an owned reference
395- /// [`ARef<T>`].
396394///
397395/// This is usually implemented by wrappers to existing structures on the C side of the code. For
398396/// Rust code, the recommendation is to use [`Arc`](crate::sync::Arc) to create reference-counted
@@ -404,9 +402,8 @@ impl<T> Opaque<T> {
404402/// at least until matching decrements are performed.
405403///
406404/// Implementers must also ensure that all instances are reference-counted. (Otherwise they
407- /// won't be able to honour the requirement that [`AlwaysRefCounted::inc_ref`] keep the object
408- /// alive.)
409- pub unsafe trait AlwaysRefCounted {
405+ /// won't be able to honour the requirement that [`RefCounted::inc_ref`] keep the object alive.)
406+ pub unsafe trait RefCounted {
410407 /// Increments the reference count on the object.
411408 fn inc_ref ( & self ) ;
412409
@@ -419,11 +416,21 @@ pub unsafe trait AlwaysRefCounted {
419416 /// Callers must ensure that there was a previous matching increment to the reference count,
420417 /// and that the object is no longer used after its reference count is decremented (as it may
421418 /// result in the object being freed), unless the caller owns another increment on the refcount
422- /// (e.g., it calls [`AlwaysRefCounted::inc_ref`] twice, then calls
423- /// [`AlwaysRefCounted::dec_ref`] once).
419+ /// (e.g., it calls [`RefCounted::inc_ref`] twice, then calls [`RefCounted::dec_ref`] once).
424420 unsafe fn dec_ref ( obj : NonNull < Self > ) ;
425421}
426422
423+ /// An extension to RefCounted, which declares that it is allowed to convert
424+ /// from a shared reference `&T` to an owned reference [`ARef<T>`].
425+ ///
426+ /// # Safety
427+ ///
428+ /// Implementers must ensure that no safety invariants are violated by upgrading an `&T`
429+ /// to an [`ARef<T>`]. In particular that implies [`AlwaysRefCounted`] and [`Ownable`]
430+ /// cannot be implemented for the same type, as this would allow to violate the uniqueness
431+ /// guarantee of [`Owned<T>`] by derefencing it into an `&T` and obtaining an [`ARef`] from that.
432+ pub unsafe trait AlwaysRefCounted : RefCounted { }
433+
427434/// An owned reference to an always-reference-counted object.
428435///
429436/// The object's reference count is automatically decremented when an instance of [`ARef`] is
@@ -434,7 +441,7 @@ pub unsafe trait AlwaysRefCounted {
434441///
435442/// The pointer stored in `ptr` is non-null and valid for the lifetime of the [`ARef`] instance. In
436443/// particular, the [`ARef`] instance owns an increment on the underlying object's reference count.
437- pub struct ARef < T : AlwaysRefCounted > {
444+ pub struct ARef < T : RefCounted > {
438445 ptr : NonNull < T > ,
439446 _p : PhantomData < T > ,
440447}
@@ -443,16 +450,16 @@ pub struct ARef<T: AlwaysRefCounted> {
443450// it effectively means sharing `&T` (which is safe because `T` is `Sync`); additionally, it needs
444451// `T` to be `Send` because any thread that has an `ARef<T>` may ultimately access `T` using a
445452// mutable reference, for example, when the reference count reaches zero and `T` is dropped.
446- unsafe impl < T : AlwaysRefCounted + Sync + Send > Send for ARef < T > { }
453+ unsafe impl < T : RefCounted + Sync + Send > Send for ARef < T > { }
447454
448455// SAFETY: It is safe to send `&ARef<T>` to another thread when the underlying `T` is `Sync`
449456// because it effectively means sharing `&T` (which is safe because `T` is `Sync`); additionally,
450457// it needs `T` to be `Send` because any thread that has a `&ARef<T>` may clone it and get an
451458// `ARef<T>` on that thread, so the thread may ultimately access `T` using a mutable reference, for
452459// example, when the reference count reaches zero and `T` is dropped.
453- unsafe impl < T : AlwaysRefCounted + Sync + Send > Sync for ARef < T > { }
460+ unsafe impl < T : RefCounted + Sync + Send > Sync for ARef < T > { }
454461
455- impl < T : AlwaysRefCounted > ARef < T > {
462+ impl < T : RefCounted > ARef < T > {
456463 /// Creates a new instance of [`ARef`].
457464 ///
458465 /// It takes over an increment of the reference count on the underlying object.
@@ -481,12 +488,12 @@ impl<T: AlwaysRefCounted> ARef<T> {
481488 ///
482489 /// ```
483490 /// use core::ptr::NonNull;
484- /// use kernel::types::{ARef, AlwaysRefCounted };
491+ /// use kernel::types::{ARef, RefCounted };
485492 ///
486493 /// struct Empty {}
487494 ///
488495 /// # // SAFETY: TODO.
489- /// unsafe impl AlwaysRefCounted for Empty {
496+ /// unsafe impl RefCounted for Empty {
490497 /// fn inc_ref(&self) {}
491498 /// unsafe fn dec_ref(_obj: NonNull<Self>) {}
492499 /// }
@@ -504,15 +511,15 @@ impl<T: AlwaysRefCounted> ARef<T> {
504511 }
505512}
506513
507- impl < T : AlwaysRefCounted > Clone for ARef < T > {
514+ impl < T : RefCounted > Clone for ARef < T > {
508515 fn clone ( & self ) -> Self {
509516 self . inc_ref ( ) ;
510517 // SAFETY: We just incremented the refcount above.
511518 unsafe { Self :: from_raw ( self . ptr ) }
512519 }
513520}
514521
515- impl < T : AlwaysRefCounted > Deref for ARef < T > {
522+ impl < T : RefCounted > Deref for ARef < T > {
516523 type Target = T ;
517524
518525 fn deref ( & self ) -> & Self :: Target {
@@ -529,7 +536,7 @@ impl<T: AlwaysRefCounted> From<&T> for ARef<T> {
529536 }
530537}
531538
532- impl < T : AlwaysRefCounted > Drop for ARef < T > {
539+ impl < T : RefCounted > Drop for ARef < T > {
533540 fn drop ( & mut self ) {
534541 // SAFETY: The type invariants guarantee that the `ARef` owns the reference we're about to
535542 // decrement.
0 commit comments