[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <78ffebda-744b-4a1b-9676-82f46c2d29d1@samsung.com>
Date: Fri, 22 Aug 2025 00:35:38 +0200
From: Michal Wilczynski <m.wilczynski@...sung.com>
To: Elle Rhumsaa <elle@...thered-steel.dev>
Cc: Uwe Kleine-König <ukleinek@...nel.org>, Miguel Ojeda
<ojeda@...nel.org>, Alex Gaynor <alex.gaynor@...il.com>, Boqun Feng
<boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>, Andreas
Hindborg <a.hindborg@...nel.org>, Alice Ryhl <aliceryhl@...gle.com>, Trevor
Gross <tmgross@...ch.edu>, Danilo Krummrich <dakr@...nel.org>, Guo Ren
<guoren@...nel.org>, Fu Wei <wefu@...hat.com>, Rob Herring
<robh@...nel.org>, Krzysztof Kozlowski <krzk+dt@...nel.org>, Conor Dooley
<conor+dt@...nel.org>, Paul Walmsley <paul.walmsley@...ive.com>, Palmer
Dabbelt <palmer@...belt.com>, Albert Ou <aou@...s.berkeley.edu>, Alexandre
Ghiti <alex@...ti.fr>, Marek Szyprowski <m.szyprowski@...sung.com>, Benno
Lossin <lossin@...nel.org>, Michael Turquette <mturquette@...libre.com>,
Drew Fustini <fustini@...nel.org>, Daniel Almeida
<daniel.almeida@...labora.com>, linux-kernel@...r.kernel.org,
linux-pwm@...r.kernel.org, rust-for-linux@...r.kernel.org,
linux-riscv@...ts.infradead.org, devicetree@...r.kernel.org
Subject: Re: [PATCH v14 3/7] rust: pwm: Add complete abstraction layer
On 8/20/25 23:11, Elle Rhumsaa wrote:
>> +/// Wrapper for a PWM device [`struct pwm_device`](srctree/include/linux/pwm.h).
>> +#[repr(transparent)]
>> +pub struct Device(Opaque<bindings::pwm_device>);
>> +
>> +impl Device {
>> + /// Creates a reference to a [`Device`] from a valid C pointer.
>> + ///
>> + /// # Safety
>> + ///
>> + /// The caller must ensure that `ptr` is valid and remains valid for the lifetime of the
>> + /// returned [`Device`] reference.
>> + pub(crate) unsafe fn from_raw<'a>(ptr: *mut bindings::pwm_device) -> &'a Self {
>> + // SAFETY: The safety requirements guarantee the validity of the dereference, while the
>> + // `Device` type being transparent makes the cast ok.
>> + unsafe { &*ptr.cast::<Self>() }
>> + }
>
> This looks like the only constructor for `Device`, and it has restricted
> visibility.
>
> Would it be helpful to provide a doc-test here to show intended usage?
>
> After looking at the C code, it looks like the raw `pwm_device` pointer
> is extracted from a `device` struct, or stored in an array of
> `pwm_device` pointers.
>
> Is the intent that the `pwm_device` will always be allocated from C
> code? For example, is a `pub fn new() -> &'a Self`-style constructor
> not possible/desired?
>
You're correct the intent is for pwm_device to be allocated and managed
by the C core. The Rust code only ever receives a raw pointer to an
existing device during callbacks.
For this reason, a public new() constructor isn't appropriate, and the
from_raw function is pub(crate) for use only by the FFI adapter. A
doc-test is also impractical since we can't create a valid pwm_device in
a test environment.
> Reviewed-by: Elle Rhumsaa <elle@...thered-steel.dev>
>
Best regards,
--
Michal Wilczynski <m.wilczynski@...sung.com>
Powered by blists - more mailing lists