From 2b10d1be818113836e72551d149f82db443694c0 Mon Sep 17 00:00:00 2001 From: Wedson Almeida Filho Date: Thu, 10 Jun 2021 21:34:28 +0100 Subject: [PATCH] Implement `GetLinksWrapped` for `Ref` objects. This allows users to have linked lists of `Ref` entries, similarly to how `Box` and `Arc` are currently supported. This will be used in Binder as it moves away from `Arc`. Signed-off-by: Wedson Almeida Filho --- rust/kernel/linked_list.rs | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/rust/kernel/linked_list.rs b/rust/kernel/linked_list.rs index d57bf1b881a355..48d93af8126181 100644 --- a/rust/kernel/linked_list.rs +++ b/rust/kernel/linked_list.rs @@ -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. @@ -55,6 +55,21 @@ impl Wrapper for Arc { } } +impl Wrapper for Ref { + fn into_pointer(self) -> NonNull { + NonNull::new(Ref::into_raw(self) as _).unwrap() + } + + unsafe fn from_pointer(ptr: NonNull) -> 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 Wrapper for &T { fn into_pointer(self) -> NonNull { NonNull::from(self) @@ -103,6 +118,21 @@ impl GetLinks for Arc { } } +impl GetLinksWrapped for Ref +where + Ref: GetLinks, +{ + type Wrapped = Ref< as GetLinks>::EntryType>; +} + +impl GetLinks for Ref { + type EntryType = T::EntryType; + + fn get_links(data: &Self::EntryType) -> &Links { + ::get_links(data) + } +} + /// A linked list. /// /// Elements in the list are wrapped and ownership is transferred to the list while the element is