Re: [PATCH v4 1/2] rust: sync: Add Lock::from_raw() for Lock<(), B>
From: Alice Ryhl
Date: Wed Nov 20 2024 - 03:58:39 EST
On Wed, Nov 20, 2024 at 12:11 AM Lyude Paul <lyude@xxxxxxxxxx> wrote:
>
> The KMS bindings ( https://patchwork.freedesktop.org/series/131522/ ) have
> a few bindings that require manually acquiring specific locks before
> calling certain functions. At the moment though, the only way of acquiring
> these locks in bindings is to simply call the C locking functions directly
> - since said locks are not acquired on the rust side of things.
>
> However - if we add `#[repr(C)]` to `Lock<(), B>`, then given `()` is a ZST
> - `Lock<(), B>` becomes equivalent in data layout to its inner `B::State`
> type. Since locks in C don't have data explicitly associated with them
> anyway, we can take advantage of this to add a `Lock::from_raw()` function
> that can translate a raw pointer to `B::State` into its proper `Lock<(),
> B>` equivalent. This lets us simply acquire a reference to the lock in
> question and work with it like it was initialized on the rust side of
> things, allowing us to use less unsafe code to implement bindings with lock
> requirements.
>
> Signed-off-by: Lyude Paul <lyude@xxxxxxxxxx>
Reviewed-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>