diff options
author | Benno Lossin <benno.lossin@proton.me> | 2023-04-08 14:26:01 +0200 |
---|---|---|
committer | Miguel Ojeda <ojeda@kernel.org> | 2023-04-12 18:41:05 +0200 |
commit | d0fdc3961270617826e4794fca1d092853847707 (patch) | |
tree | 297fd0bcde51e9b9d7fef7c1bc7e12e0bf0ea155 /rust/kernel/init/__internal.rs | |
parent | rust: init/sync: add `InPlaceInit` trait to pin-initialize smart pointers (diff) | |
download | linux-d0fdc3961270617826e4794fca1d092853847707.tar.xz linux-d0fdc3961270617826e4794fca1d092853847707.zip |
rust: init: add `PinnedDrop` trait and macros
The `PinnedDrop` trait that facilitates destruction of pinned types.
It has to be implemented via the `#[pinned_drop]` macro, since the
`drop` function should not be called by normal code, only by other
destructors. It also only works on structs that are annotated with
`#[pin_data(PinnedDrop)]`.
Co-developed-by: Gary Guo <gary@garyguo.net>
Signed-off-by: Gary Guo <gary@garyguo.net>
Signed-off-by: Benno Lossin <benno.lossin@proton.me>
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Reviewed-by: Andreas Hindborg <a.hindborg@samsung.com>
Link: https://lore.kernel.org/r/20230408122429.1103522-10-y86-dev@protonmail.com
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
Diffstat (limited to 'rust/kernel/init/__internal.rs')
-rw-r--r-- | rust/kernel/init/__internal.rs | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/rust/kernel/init/__internal.rs b/rust/kernel/init/__internal.rs index a3389a684296..774cb620afa7 100644 --- a/rust/kernel/init/__internal.rs +++ b/rust/kernel/init/__internal.rs @@ -161,3 +161,18 @@ impl<T: ?Sized> Drop for DropGuard<T> { } } } + +/// Token used by `PinnedDrop` to prevent calling the function without creating this unsafely +/// created struct. This is needed, because the `drop` function is safe, but should not be called +/// manually. +pub struct OnlyCallFromDrop(()); + +impl OnlyCallFromDrop { + /// # Safety + /// + /// This function should only be called from the [`Drop::drop`] function and only be used to + /// delegate the destruction to the pinned destructor [`PinnedDrop::drop`] of the same type. + pub unsafe fn new() -> Self { + Self(()) + } +} |