Skip to content

Commit 8aad570

Browse files
committed
rust/kernel: Move PointerWrapper trait to types.rs
Since usage of PointerWrapper is not limited only to file operations, move it out from file_operations.rs to a more suitable place, types.rs. Fixes #144 Signed-off-by: Sumera Priyadarsini <[email protected]>
1 parent 1fa10de commit 8aad570

File tree

2 files changed

+68
-67
lines changed

2 files changed

+68
-67
lines changed

rust/kernel/file_operations.rs

+3-66
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
//! C header: [`include/linux/fs.h`](../../../../include/linux/fs.h)
66
77
use core::convert::{TryFrom, TryInto};
8-
use core::{marker, mem, ops::Deref, pin::Pin, ptr};
9-
10-
use alloc::boxed::Box;
11-
use alloc::sync::Arc;
8+
use core::{marker, mem, ptr};
129

1310
use crate::bindings;
1411
use crate::c_types;
1512
use crate::error::{Error, KernelResult};
16-
use crate::sync::{CondVar, Ref, RefCounted};
13+
use crate::sync::CondVar;
14+
use crate::types::PointerWrapper;
1715
use crate::user_ptr::{UserSlicePtr, UserSlicePtrReader, UserSlicePtrWriter};
1816

1917
/// Wraps the kernel's `struct file`.
@@ -605,64 +603,3 @@ pub trait FileOperations: Send + Sync + Sized {
605603
Ok(bindings::POLLIN | bindings::POLLOUT | bindings::POLLRDNORM | bindings::POLLWRNORM)
606604
}
607605
}
608-
609-
/// Used to convert an object into a raw pointer that represents it.
610-
///
611-
/// It can eventually be converted back into the object. This is used to store objects as pointers
612-
/// in kernel data structures, for example, an implementation of [`FileOperations`] in `struct
613-
/// file::private_data`.
614-
pub trait PointerWrapper<T> {
615-
/// Returns the raw pointer.
616-
fn into_pointer(self) -> *const T;
617-
618-
/// Returns the instance back from the raw pointer.
619-
///
620-
/// # Safety
621-
///
622-
/// The passed pointer must come from a previous call to [`PointerWrapper::into_pointer()`].
623-
unsafe fn from_pointer(ptr: *const T) -> Self;
624-
}
625-
626-
impl<T> PointerWrapper<T> for Box<T> {
627-
fn into_pointer(self) -> *const T {
628-
Box::into_raw(self)
629-
}
630-
631-
unsafe fn from_pointer(ptr: *const T) -> Self {
632-
Box::from_raw(ptr as _)
633-
}
634-
}
635-
636-
impl<T: RefCounted> PointerWrapper<T> for Ref<T> {
637-
fn into_pointer(self) -> *const T {
638-
Ref::into_raw(self)
639-
}
640-
641-
unsafe fn from_pointer(ptr: *const T) -> Self {
642-
Ref::from_raw(ptr as _)
643-
}
644-
}
645-
646-
impl<T> PointerWrapper<T> for Arc<T> {
647-
fn into_pointer(self) -> *const T {
648-
Arc::into_raw(self)
649-
}
650-
651-
unsafe fn from_pointer(ptr: *const T) -> Self {
652-
Arc::from_raw(ptr)
653-
}
654-
}
655-
656-
impl<T, W: PointerWrapper<T> + Deref> PointerWrapper<T> for Pin<W> {
657-
fn into_pointer(self) -> *const T {
658-
// SAFETY: We continue to treat the pointer as pinned by returning just a pointer to it to
659-
// the caller.
660-
let inner = unsafe { Pin::into_inner_unchecked(self) };
661-
inner.into_pointer()
662-
}
663-
664-
unsafe fn from_pointer(p: *const T) -> Self {
665-
// SAFETY: The object was originally pinned.
666-
Pin::new_unchecked(W::from_pointer(p))
667-
}
668-
}

rust/kernel/types.rs

+65-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
//!
55
//! C header: [`include/linux/types.h`](../../../../include/linux/types.h)
66
7-
use core::ops::Deref;
7+
use core::{ops::Deref, pin::Pin};
8+
9+
use alloc::{boxed::Box, sync::Arc};
810

911
use crate::bindings;
12+
use crate::sync::{Ref, RefCounted};
1013

1114
/// Permissions.
1215
///
@@ -71,3 +74,64 @@ macro_rules! cstr {
7174
unsafe { $crate::CStr::new_unchecked(s) }
7275
}};
7376
}
77+
78+
/// Used to convert an object into a raw pointer that represents it.
79+
///
80+
/// It can eventually be converted back into the object. This is used to store objects as pointers
81+
/// in kernel data structures, for example, an implementation of [`FileOperations`] in `struct
82+
/// file::private_data`.
83+
pub trait PointerWrapper<T> {
84+
/// Returns the raw pointer.
85+
fn into_pointer(self) -> *const T;
86+
87+
/// Returns the instance back from the raw pointer.
88+
///
89+
/// # Safety
90+
///
91+
/// The passed pointer must come from a previous call to [`PointerWrapper::into_pointer()`].
92+
unsafe fn from_pointer(ptr: *const T) -> Self;
93+
}
94+
95+
impl<T> PointerWrapper<T> for Box<T> {
96+
fn into_pointer(self) -> *const T {
97+
Box::into_raw(self)
98+
}
99+
100+
unsafe fn from_pointer(ptr: *const T) -> Self {
101+
Box::from_raw(ptr as _)
102+
}
103+
}
104+
105+
impl<T: RefCounted> PointerWrapper<T> for Ref<T> {
106+
fn into_pointer(self) -> *const T {
107+
Ref::into_raw(self)
108+
}
109+
110+
unsafe fn from_pointer(ptr: *const T) -> Self {
111+
Ref::from_raw(ptr as _)
112+
}
113+
}
114+
115+
impl<T> PointerWrapper<T> for Arc<T> {
116+
fn into_pointer(self) -> *const T {
117+
Arc::into_raw(self)
118+
}
119+
120+
unsafe fn from_pointer(ptr: *const T) -> Self {
121+
Arc::from_raw(ptr)
122+
}
123+
}
124+
125+
impl<T, W: PointerWrapper<T> + Deref> PointerWrapper<T> for Pin<W> {
126+
fn into_pointer(self) -> *const T {
127+
// SAFETY: We continue to treat the pointer as pinned by returning just a pointer to it to
128+
// the caller.
129+
let inner = unsafe { Pin::into_inner_unchecked(self) };
130+
inner.into_pointer()
131+
}
132+
133+
unsafe fn from_pointer(p: *const T) -> Self {
134+
// SAFETY: The object was originally pinned.
135+
Pin::new_unchecked(W::from_pointer(p))
136+
}
137+
}

0 commit comments

Comments
 (0)