@@ -9,6 +9,7 @@ use core::{
marker::PhantomData,
mem::MaybeUninit,
ops::{Deref, DerefMut},
+ pin::Pin,
ptr::NonNull,
};
@@ -100,6 +101,29 @@ impl ForeignOwnable for () {
unsafe fn from_foreign(_: *const core::ffi::c_void) -> Self {}
}
+impl<T: ForeignOwnable + Deref> ForeignOwnable for Pin<T> {
+ type Borrowed<'a> = T::Borrowed<'a>;
+
+ fn into_foreign(self) -> *const core::ffi::c_void {
+ // SAFETY: We continue to treat the pointer as pinned by returning just a pointer to it to
+ // the caller.
+ let inner = unsafe { Pin::into_inner_unchecked(self) };
+ inner.into_foreign()
+ }
+
+ unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> Self::Borrowed<'a> {
+ // SAFETY: The safety requirements for this function are the same as the ones for
+ // `T::borrow`.
+ unsafe { T::borrow(ptr) }
+ }
+
+ unsafe fn from_foreign(p: *const core::ffi::c_void) -> Self {
+ // SAFETY: The object was originally pinned.
+ // The passed pointer comes from a previous call to `T::into_foreign`.
+ unsafe { Pin::new_unchecked(T::from_foreign(p)) }
+ }
+}
+
/// Runs a cleanup function/closure when dropped.
///
/// The [`ScopeGuard::dismiss`] function prevents the cleanup function from running.
From: Andreas Hindborg <a.hindborg@samsung.com> Implement `ForeignOwnable for Pin<T> where T: ForeignOwnable + Deref`. Imported from rust tree [1] [1] https://github.com/Rust-for-Linux/linux/tree/bc22545f38d74473cfef3e9fd65432733435b79f Cc: Wedson Almeida Filho <wedsonaf@gmail.com> --- rust/kernel/types.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)