Skip to content

Commit a7653d2

Browse files
authored
Merge pull request #458 from wedsonaf/ref-trait-objects
rust: add support for trait objects in `Ref`s.
2 parents 85f97a8 + 99c380f commit a7653d2

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

rust/kernel/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
const_unreachable_unchecked,
2323
doc_cfg,
2424
receiver_trait,
25+
coerce_unsized,
26+
dispatch_from_dyn,
27+
unsize,
2528
try_reserve
2629
)]
2730

rust/kernel/sync/arc.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@
1818
use crate::{bindings, Result};
1919
use alloc::boxed::Box;
2020
use core::{
21-
cell::UnsafeCell, convert::AsRef, marker::PhantomData, mem::ManuallyDrop, ops::Deref, pin::Pin,
21+
cell::UnsafeCell,
22+
convert::AsRef,
23+
marker::{PhantomData, Unsize},
24+
mem::ManuallyDrop,
25+
ops::Deref,
26+
pin::Pin,
2227
ptr::NonNull,
2328
};
2429

@@ -50,6 +55,13 @@ struct RefInner<T: ?Sized> {
5055
// This is to allow [`Ref`] (and variants) to be used as the type of `self`.
5156
impl<T: ?Sized> core::ops::Receiver for Ref<T> {}
5257

58+
// This is to allow coercion from `Ref<T>` to `Ref<U>` if `T` can be converted to the
59+
// dynamically-sized type (DST) `U`.
60+
impl<T: ?Sized + Unsize<U>, U: ?Sized> core::ops::CoerceUnsized<Ref<U>> for Ref<T> {}
61+
62+
// This is to allow `Ref<U>` to be dispatched on when `Ref<T>` can be coerced into `Ref<U>`.
63+
impl<T: ?Sized + Unsize<U>, U: ?Sized> core::ops::DispatchFromDyn<Ref<U>> for Ref<T> {}
64+
5365
// SAFETY: It is safe to send `Ref<T>` to another thread when the underlying `T` is `Sync` because
5466
// it effectively means sharing `&T` (which is safe because `T` is `Sync`); additionally, it needs
5567
// `T` to be `Send` because any thread that has a `Ref<T>` may ultimately access `T` directly, for

0 commit comments

Comments
 (0)