Skip to content

Commit 6033132

Browse files
committed
rust: implement Wrapper<T> for Opaque<T>
Moves the implementation for `pin-init` from an associated function to the trait function of the `Wrapper` trait and extends the implementation to support pin-initializers with error types. This allows to declare functions that are generic over `Wrapper` types. Adds a use for the `Wrapper` trait in `revocable.rs`, to use the new `pin-init` function. This is currently the only usage in the kernel. Reviewed-by: Gerald Wisböck <gerald.wisboeck@feather.ink> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Benno Lossin <lossin@kernel.org> Signed-off-by: Christian Schrefl <chrisi.schrefl@gmail.com>
1 parent 239229b commit 6033132

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

rust/kernel/revocable.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
//! The [`Revocable`] type wraps other types and allows access to them to be revoked. The existence
66
//! of a [`RevocableGuard`] ensures that objects remain valid.
77
8+
use pin_init::Wrapper;
9+
810
use crate::{bindings, prelude::*, sync::rcu, types::Opaque};
911
use core::{
1012
marker::PhantomData,

rust/kernel/types.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use core::{
99
ops::{Deref, DerefMut},
1010
ptr::NonNull,
1111
};
12-
use pin_init::{PinInit, Zeroable};
12+
use pin_init::{PinInit, Wrapper, Zeroable};
1313

1414
/// Used to transfer ownership to and from foreign (non-Rust) languages.
1515
///
@@ -332,17 +332,6 @@ impl<T> Opaque<T> {
332332
}
333333
}
334334

335-
/// Create an opaque pin-initializer from the given pin-initializer.
336-
pub fn pin_init(slot: impl PinInit<T>) -> impl PinInit<Self> {
337-
Self::ffi_init(|ptr: *mut T| {
338-
// SAFETY:
339-
// - `ptr` is a valid pointer to uninitialized memory,
340-
// - `slot` is not accessed on error; the call is infallible,
341-
// - `slot` is pinned in memory.
342-
let _ = unsafe { PinInit::<T>::__pinned_init(slot, ptr) };
343-
})
344-
}
345-
346335
/// Creates a pin-initializer from the given initializer closure.
347336
///
348337
/// The returned initializer calls the given closure with the pointer to the inner `T` of this
@@ -393,6 +382,18 @@ impl<T> Opaque<T> {
393382
UnsafeCell::raw_get(this.cast::<UnsafeCell<MaybeUninit<T>>>()).cast::<T>()
394383
}
395384
}
385+
impl<T> Wrapper<T> for Opaque<T> {
386+
/// Create an opaque pin-initializer from the given pin-initializer.
387+
fn pin_init<E>(slot: impl PinInit<T, E>) -> impl PinInit<Self, E> {
388+
Self::try_ffi_init(|ptr: *mut T| {
389+
// SAFETY:
390+
// - `ptr` is a valid pointer to uninitialized memory,
391+
// - `slot` is not accessed on error; the call is infallible,
392+
// - `slot` is pinned in memory.
393+
unsafe { PinInit::<T, E>::__pinned_init(slot, ptr) }
394+
})
395+
}
396+
}
396397

397398
/// Types that are _always_ reference counted.
398399
///

0 commit comments

Comments
 (0)