Skip to content

Commit 38512f6

Browse files
authored
Merge pull request #434 from wedsonaf/ref-list
Implement `GetLinksWrapped` for `Ref` objects.
2 parents a033825 + 2b10d1b commit 38512f6

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

rust/kernel/linked_list.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use alloc::{boxed::Box, sync::Arc};
88
use core::ptr::NonNull;
99

1010
pub use crate::raw_list::{Cursor, GetLinks, Links};
11-
use crate::{raw_list, raw_list::RawList};
11+
use crate::{raw_list, raw_list::RawList, sync::Ref};
1212

1313
// TODO: Use the one from `kernel::file_operations::PointerWrapper` instead.
1414
/// Wraps an object to be inserted in a linked list.
@@ -55,6 +55,21 @@ impl<T: ?Sized> Wrapper<T> for Arc<T> {
5555
}
5656
}
5757

58+
impl<T: ?Sized> Wrapper<T> for Ref<T> {
59+
fn into_pointer(self) -> NonNull<T> {
60+
NonNull::new(Ref::into_raw(self) as _).unwrap()
61+
}
62+
63+
unsafe fn from_pointer(ptr: NonNull<T>) -> Self {
64+
// SAFETY: The safety requirements of `from_pointer` satisfy the ones from `Ref::from_raw`.
65+
unsafe { Ref::from_raw(ptr.as_ptr() as _) }
66+
}
67+
68+
fn as_ref(&self) -> &T {
69+
AsRef::as_ref(self)
70+
}
71+
}
72+
5873
impl<T: ?Sized> Wrapper<T> for &T {
5974
fn into_pointer(self) -> NonNull<T> {
6075
NonNull::from(self)
@@ -103,6 +118,21 @@ impl<T: GetLinks + ?Sized> GetLinks for Arc<T> {
103118
}
104119
}
105120

121+
impl<T: ?Sized> GetLinksWrapped for Ref<T>
122+
where
123+
Ref<T>: GetLinks,
124+
{
125+
type Wrapped = Ref<<Ref<T> as GetLinks>::EntryType>;
126+
}
127+
128+
impl<T: GetLinks + ?Sized> GetLinks for Ref<T> {
129+
type EntryType = T::EntryType;
130+
131+
fn get_links(data: &Self::EntryType) -> &Links<Self::EntryType> {
132+
<T as GetLinks>::get_links(data)
133+
}
134+
}
135+
106136
/// A linked list.
107137
///
108138
/// Elements in the list are wrapped and ownership is transferred to the list while the element is

0 commit comments

Comments
 (0)