[PATCH v3 11/13] rust: init: make `PinInit<T, E>` a supertrait of `Init<T, E>`

Benno Lossin posted 13 patches 2 years, 6 months ago
There is a newer version of this series
[PATCH v3 11/13] rust: init: make `PinInit<T, E>` a supertrait of `Init<T, E>`
Posted by Benno Lossin 2 years, 6 months ago
Remove the blanket implementation of `PinInit<T, E> for I where I:
Init<T, E>`. This blanket implementation prevented custom types that
implement `PinInit`.

Reviewed-by: Martin Rodriguez Reboredo <yakoyoku@gmail.com>
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Signed-off-by: Benno Lossin <benno.lossin@proton.me>
---
v2 -> v3:
- add blank missing line,
- added Reviewed-by's from Martin and Alice.

 rust/kernel/init.rs            | 21 ++++++++-------------
 rust/kernel/init/__internal.rs | 12 ++++++++++++
 2 files changed, 20 insertions(+), 13 deletions(-)

diff --git a/rust/kernel/init.rs b/rust/kernel/init.rs
index 06ecab4901f2..040dc9a5f9fd 100644
--- a/rust/kernel/init.rs
+++ b/rust/kernel/init.rs
@@ -807,7 +807,7 @@ pub unsafe trait PinInit<T: ?Sized, E = Infallible>: Sized {
 ///
 /// [`Arc<T>`]: crate::sync::Arc
 #[must_use = "An initializer must be used in order to create its value."]
-pub unsafe trait Init<T: ?Sized, E = Infallible>: Sized {
+pub unsafe trait Init<T: ?Sized, E = Infallible>: PinInit<T, E> {
     /// Initializes `slot`.
     ///
     /// # Safety
@@ -818,18 +818,6 @@ pub unsafe trait Init<T: ?Sized, E = Infallible>: Sized {
     unsafe fn __init(self, slot: *mut T) -> Result<(), E>;
 }
 
-// SAFETY: Every in-place initializer can also be used as a pin-initializer.
-unsafe impl<T: ?Sized, E, I> PinInit<T, E> for I
-where
-    I: Init<T, E>,
-{
-    unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E> {
-        // SAFETY: `__init` meets the same requirements as `__pinned_init`, except that it does not
-        // require `slot` to not move after init.
-        unsafe { self.__init(slot) }
-    }
-}
-
 /// Creates a new [`PinInit<T, E>`] from the given closure.
 ///
 /// # Safety
@@ -971,6 +959,13 @@ unsafe fn __init(self, slot: *mut T) -> Result<(), E> {
     }
 }
 
+// SAFETY: Every type can be initialized by-value. `__pinned_init` calls `__init`.
+unsafe impl<T, E> PinInit<T, E> for T {
+    unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E> {
+        unsafe { self.__init(slot) }
+    }
+}
+
 /// Smart pointer that can initialize memory in-place.
 pub trait InPlaceInit<T>: Sized {
     /// Use the given pin-initializer to pin-initialize a `T` inside of a new smart pointer of this
diff --git a/rust/kernel/init/__internal.rs b/rust/kernel/init/__internal.rs
index 7abd1fb65e41..12e195061525 100644
--- a/rust/kernel/init/__internal.rs
+++ b/rust/kernel/init/__internal.rs
@@ -32,6 +32,18 @@ unsafe fn __init(self, slot: *mut T) -> Result<(), E> {
     }
 }
 
+// SAFETY: While constructing the `InitClosure`, the user promised that it upholds the
+// `__pinned_init` invariants.
+unsafe impl<T: ?Sized, F, E> PinInit<T, E> for InitClosure<F, T, E>
+where
+    F: FnOnce(*mut T) -> Result<(), E>,
+{
+    #[inline]
+    unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E> {
+        (self.0)(slot)
+    }
+}
+
 /// This trait is only implemented via the `#[pin_data]` proc-macro. It is used to facilitate
 /// the pin projections within the initializers.
 ///
-- 
2.41.0
Re: [PATCH v3 11/13] rust: init: make `PinInit<T, E>` a supertrait of `Init<T, E>`
Posted by Gary Guo 2 years, 6 months ago
On Sat, 29 Jul 2023 09:10:22 +0000
Benno Lossin <benno.lossin@proton.me> wrote:

> Remove the blanket implementation of `PinInit<T, E> for I where I:
> Init<T, E>`. This blanket implementation prevented custom types that
> implement `PinInit`.
> 
> Reviewed-by: Martin Rodriguez Reboredo <yakoyoku@gmail.com>
> Reviewed-by: Alice Ryhl <aliceryhl@google.com>
> Signed-off-by: Benno Lossin <benno.lossin@proton.me>

Reviewed-by: Gary Guo <gary@garyguo.net>

> ---
> v2 -> v3:
> - add blank missing line,
> - added Reviewed-by's from Martin and Alice.
> 
>  rust/kernel/init.rs            | 21 ++++++++-------------
>  rust/kernel/init/__internal.rs | 12 ++++++++++++
>  2 files changed, 20 insertions(+), 13 deletions(-)
> 
> diff --git a/rust/kernel/init.rs b/rust/kernel/init.rs
> index 06ecab4901f2..040dc9a5f9fd 100644
> --- a/rust/kernel/init.rs
> +++ b/rust/kernel/init.rs
> @@ -807,7 +807,7 @@ pub unsafe trait PinInit<T: ?Sized, E = Infallible>: Sized {
>  ///
>  /// [`Arc<T>`]: crate::sync::Arc
>  #[must_use = "An initializer must be used in order to create its value."]
> -pub unsafe trait Init<T: ?Sized, E = Infallible>: Sized {
> +pub unsafe trait Init<T: ?Sized, E = Infallible>: PinInit<T, E> {
>      /// Initializes `slot`.
>      ///
>      /// # Safety
> @@ -818,18 +818,6 @@ pub unsafe trait Init<T: ?Sized, E = Infallible>: Sized {
>      unsafe fn __init(self, slot: *mut T) -> Result<(), E>;
>  }
>  
> -// SAFETY: Every in-place initializer can also be used as a pin-initializer.
> -unsafe impl<T: ?Sized, E, I> PinInit<T, E> for I
> -where
> -    I: Init<T, E>,
> -{
> -    unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E> {
> -        // SAFETY: `__init` meets the same requirements as `__pinned_init`, except that it does not
> -        // require `slot` to not move after init.
> -        unsafe { self.__init(slot) }
> -    }
> -}
> -
>  /// Creates a new [`PinInit<T, E>`] from the given closure.
>  ///
>  /// # Safety
> @@ -971,6 +959,13 @@ unsafe fn __init(self, slot: *mut T) -> Result<(), E> {
>      }
>  }
>  
> +// SAFETY: Every type can be initialized by-value. `__pinned_init` calls `__init`.
> +unsafe impl<T, E> PinInit<T, E> for T {
> +    unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E> {
> +        unsafe { self.__init(slot) }
> +    }
> +}
> +
>  /// Smart pointer that can initialize memory in-place.
>  pub trait InPlaceInit<T>: Sized {
>      /// Use the given pin-initializer to pin-initialize a `T` inside of a new smart pointer of this
> diff --git a/rust/kernel/init/__internal.rs b/rust/kernel/init/__internal.rs
> index 7abd1fb65e41..12e195061525 100644
> --- a/rust/kernel/init/__internal.rs
> +++ b/rust/kernel/init/__internal.rs
> @@ -32,6 +32,18 @@ unsafe fn __init(self, slot: *mut T) -> Result<(), E> {
>      }
>  }
>  
> +// SAFETY: While constructing the `InitClosure`, the user promised that it upholds the
> +// `__pinned_init` invariants.
> +unsafe impl<T: ?Sized, F, E> PinInit<T, E> for InitClosure<F, T, E>
> +where
> +    F: FnOnce(*mut T) -> Result<(), E>,
> +{
> +    #[inline]
> +    unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E> {
> +        (self.0)(slot)
> +    }
> +}
> +
>  /// This trait is only implemented via the `#[pin_data]` proc-macro. It is used to facilitate
>  /// the pin projections within the initializers.
>  ///