@@ -394,11 +394,9 @@ impl<T> Opaque<T> {
394
394
}
395
395
}
396
396
397
- /// Types that are _always_ reference counted.
397
+ /// Types that are internally reference counted.
398
398
///
399
399
/// 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>`].
402
400
///
403
401
/// This is usually implemented by wrappers to existing structures on the C side of the code. For
404
402
/// Rust code, the recommendation is to use [`Arc`](crate::sync::Arc) to create reference-counted
@@ -410,9 +408,8 @@ impl<T> Opaque<T> {
410
408
/// at least until matching decrements are performed.
411
409
///
412
410
/// 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 {
416
413
/// Increments the reference count on the object.
417
414
fn inc_ref ( & self ) ;
418
415
@@ -425,11 +422,21 @@ pub unsafe trait AlwaysRefCounted {
425
422
/// Callers must ensure that there was a previous matching increment to the reference count,
426
423
/// and that the object is no longer used after its reference count is decremented (as it may
427
424
/// 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).
430
426
unsafe fn dec_ref ( obj : NonNull < Self > ) ;
431
427
}
432
428
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
+
433
440
/// An owned reference to an always-reference-counted object.
434
441
///
435
442
/// The object's reference count is automatically decremented when an instance of [`ARef`] is
@@ -440,7 +447,7 @@ pub unsafe trait AlwaysRefCounted {
440
447
///
441
448
/// The pointer stored in `ptr` is non-null and valid for the lifetime of the [`ARef`] instance. In
442
449
/// 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 > {
444
451
ptr : NonNull < T > ,
445
452
_p : PhantomData < T > ,
446
453
}
@@ -449,16 +456,16 @@ pub struct ARef<T: AlwaysRefCounted> {
449
456
// it effectively means sharing `&T` (which is safe because `T` is `Sync`); additionally, it needs
450
457
// `T` to be `Send` because any thread that has an `ARef<T>` may ultimately access `T` using a
451
458
// 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 > { }
453
460
454
461
// SAFETY: It is safe to send `&ARef<T>` to another thread when the underlying `T` is `Sync`
455
462
// because it effectively means sharing `&T` (which is safe because `T` is `Sync`); additionally,
456
463
// it needs `T` to be `Send` because any thread that has a `&ARef<T>` may clone it and get an
457
464
// `ARef<T>` on that thread, so the thread may ultimately access `T` using a mutable reference, for
458
465
// 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 > { }
460
467
461
- impl < T : AlwaysRefCounted > ARef < T > {
468
+ impl < T : RefCounted > ARef < T > {
462
469
/// Creates a new instance of [`ARef`].
463
470
///
464
471
/// It takes over an increment of the reference count on the underlying object.
@@ -487,12 +494,12 @@ impl<T: AlwaysRefCounted> ARef<T> {
487
494
///
488
495
/// ```
489
496
/// use core::ptr::NonNull;
490
- /// use kernel::types::{ARef, AlwaysRefCounted };
497
+ /// use kernel::types::{ARef, RefCounted };
491
498
///
492
499
/// struct Empty {}
493
500
///
494
501
/// # // SAFETY: TODO.
495
- /// unsafe impl AlwaysRefCounted for Empty {
502
+ /// unsafe impl RefCounted for Empty {
496
503
/// fn inc_ref(&self) {}
497
504
/// unsafe fn dec_ref(_obj: NonNull<Self>) {}
498
505
/// }
@@ -510,15 +517,15 @@ impl<T: AlwaysRefCounted> ARef<T> {
510
517
}
511
518
}
512
519
513
- impl < T : AlwaysRefCounted > Clone for ARef < T > {
520
+ impl < T : RefCounted > Clone for ARef < T > {
514
521
fn clone ( & self ) -> Self {
515
522
self . inc_ref ( ) ;
516
523
// SAFETY: We just incremented the refcount above.
517
524
unsafe { Self :: from_raw ( self . ptr ) }
518
525
}
519
526
}
520
527
521
- impl < T : AlwaysRefCounted > Deref for ARef < T > {
528
+ impl < T : RefCounted > Deref for ARef < T > {
522
529
type Target = T ;
523
530
524
531
fn deref ( & self ) -> & Self :: Target {
@@ -535,7 +542,7 @@ impl<T: AlwaysRefCounted> From<&T> for ARef<T> {
535
542
}
536
543
}
537
544
538
- impl < T : AlwaysRefCounted > Drop for ARef < T > {
545
+ impl < T : RefCounted > Drop for ARef < T > {
539
546
fn drop ( & mut self ) {
540
547
// SAFETY: The type invariants guarantee that the `ARef` owns the reference we're about to
541
548
// decrement.
0 commit comments