On Sat Nov 14, 2026 at 8:07 PM GMT, Ivan Tverdokhleb wrote:
> Currently Lock::new() only accepts an infallible initializer for the
> protected data, because impl PinInit<T> defaults the error type to
> Infallible. This prevents wrapping a fallible initializer, such as
> Opaque::try_ffi_init(), in a Mutex or SpinLock.
>
> Make Lock::new() generic over the error type E, so that it accepts any
> impl PinInit<T, E> and returns impl PinInit<Self, E>. E must implement
> From<Infallible>, since initializing the lock state itself cannot fail.
>
> This is needed, for example, by the GPU buddy allocator, which has a
> TODO to move its gpu_buddy structure inside the mutex.
>
> Signed-off-by: Ivan Tverdokhleb <[email protected]>

See 
https://lore.kernel.org/rust-for-linux/[email protected]/
which is already in linux-next.

Best,
Gary

> ---
>  rust/kernel/sync/lock.rs | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs
> index 10b6b5e9b024..26c071088b02 100644
> --- a/rust/kernel/sync/lock.rs
> +++ b/rust/kernel/sync/lock.rs
> @@ -128,11 +128,11 @@ unsafe impl<T: ?Sized + Send, B: Backend> Sync for 
> Lock<T, B> {}
>  
>  impl<T, B: Backend> Lock<T, B> {
>      /// Constructs a new lock initialiser.
> -    pub fn new(
> -        t: impl PinInit<T>,
> +    pub fn new<E: core::convert::From<core::convert::Infallible>>(
> +        t: impl PinInit<T, E>,
>          name: &'static CStr,
>          key: Pin<&'static LockClassKey>,
> -    ) -> impl PinInit<Self> {
> +    ) -> impl PinInit<Self, E> {
>          pin_init!(Self {
>              data <- UnsafeCell::pin_init(t),
>              _pin: PhantomPinned,
> @@ -141,7 +141,7 @@ pub fn new(
>              state <- Opaque::ffi_init(|slot| unsafe {
>                  B::init(slot, name.as_char_ptr(), key.as_ptr())
>              }),
> -        })
> +        }? E)
>      }
>  }
>  


Reply via email to