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