Skip to content

Commit

Permalink
Merge pull request torvalds#434 from wedsonaf/ref-list
Browse files Browse the repository at this point in the history
Implement `GetLinksWrapped` for `Ref` objects.
  • Loading branch information
wedsonaf authored Jul 31, 2021
2 parents a033825 + 2b10d1b commit 38512f6
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion rust/kernel/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use alloc::{boxed::Box, sync::Arc};
use core::ptr::NonNull;

pub use crate::raw_list::{Cursor, GetLinks, Links};
use crate::{raw_list, raw_list::RawList};
use crate::{raw_list, raw_list::RawList, sync::Ref};

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

impl<T: ?Sized> Wrapper<T> for Ref<T> {
fn into_pointer(self) -> NonNull<T> {
NonNull::new(Ref::into_raw(self) as _).unwrap()
}

unsafe fn from_pointer(ptr: NonNull<T>) -> Self {
// SAFETY: The safety requirements of `from_pointer` satisfy the ones from `Ref::from_raw`.
unsafe { Ref::from_raw(ptr.as_ptr() as _) }
}

fn as_ref(&self) -> &T {
AsRef::as_ref(self)
}
}

impl<T: ?Sized> Wrapper<T> for &T {
fn into_pointer(self) -> NonNull<T> {
NonNull::from(self)
Expand Down Expand Up @@ -103,6 +118,21 @@ impl<T: GetLinks + ?Sized> GetLinks for Arc<T> {
}
}

impl<T: ?Sized> GetLinksWrapped for Ref<T>
where
Ref<T>: GetLinks,
{
type Wrapped = Ref<<Ref<T> as GetLinks>::EntryType>;
}

impl<T: GetLinks + ?Sized> GetLinks for Ref<T> {
type EntryType = T::EntryType;

fn get_links(data: &Self::EntryType) -> &Links<Self::EntryType> {
<T as GetLinks>::get_links(data)
}
}

/// A linked list.
///
/// Elements in the list are wrapped and ownership is transferred to the list while the element is
Expand Down

0 comments on commit 38512f6

Please sign in to comment.