Rust for Linux List
 help / color / mirror / Atom feed
From: Markus Probst <markus.probst@posteo.de>
To: Muchamad Coirul Anwar <muchamadcoirulanwar@gmail.com>,
	Jonathan Cameron <jic23@kernel.org>,
	Miguel Ojeda <ojeda@kernel.org>,
	Igor Korotin <igor.korotin@linux.dev>,
	Danilo Krummrich <dakr@kernel.org>
Cc: "David Lechner" <dlechner@baylibre.com>,
	"Nuno Sá" <nuno.sa@analog.com>,
	"Andy Shevchenko" <andy@kernel.org>,
	"Andi Shyti" <andi.shyti@kernel.org>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Mark Brown" <broonie@kernel.org>,
	"Joshua Crofts" <crofts@kernel.org>,
	"Shuai Liu" <847807867@qq.com>,
	linux-iio@vger.kernel.org, linux-i2c@vger.kernel.org,
	rust-for-linux@vger.kernel.org, driver-core@lists.linux.dev,
	linux-kernel@vger.kernel.org
Subject: Re: [RFC PATCH v8 1/4] rust: io: add FallibleIoCapable
Date: Tue, 01 Dec 2026 17:40:58 +0000	[thread overview]
Message-ID: <11115845d6cf228b1d9cc5ae629ea3a27dfe0550.camel@posteo.de> (raw)
In-Reply-To: <20261201165508.39336-2-muchamadcoirulanwar@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 7521 bytes --]

On Tue, 2026-12-01 at 23:55 +0700, Muchamad Coirul Anwar wrote:
> Add a fallible I/O capability for backends whose accesses may fail
> independently of address validation.
> 
> Route try_read, try_write, try_write_reg, and try_update through the
> fallible capability. Provide a blanket implementation for infallible
> IoCapable backends so existing users retain their current behavior.
> 
> This allows transport-backed implementations such as I2C to expose only
> fallible access while keeping infallible operations unavailable.
> 
> Link: https://lore.kernel.org/all/DJVQ852J7SOH.26YBIJTQ9B66G@kernel.org/
> Suggested-by: Danilo Krummrich <dakr@kernel.org>
> Signed-off-by: Muchamad Coirul Anwar <muchamadcoirulanwar@gmail.com>
> ---
>  rust/kernel/io.rs | 62 ++++++++++++++++++++++++++++++++++-------------
>  1 file changed, 45 insertions(+), 17 deletions(-)
> 
> diff --git a/rust/kernel/io.rs b/rust/kernel/io.rs
> index a877df11a03e..b93a540c28da 100644
> --- a/rust/kernel/io.rs
> +++ b/rust/kernel/io.rs
> @@ -356,6 +356,31 @@ pub trait IoCapable<T>: IoBackend {
>      fn io_write<'a>(view: Self::View<'a, T>, value: T);
>  }
>  
> +/// I/O backend capability for operations that may fail independently of address validation.
> +///
> +/// Infallible backends implementing [`IoCapable`] also support fallible operations. Backends
> +/// implementing only this trait cannot use the infallible [`Io`] accessors.
> +pub trait FallibleIoCapable<T>: IoBackend {
> +    /// Read from `view`, returning a transport error if the operation fails.
> +    fn io_try_read<'a>(view: Self::View<'a, T>) -> Result<T>;
> +
> +    /// Write `value` to `view`, returning a transport error if the operation fails.
> +    fn io_try_write<'a>(view: Self::View<'a, T>, value: T) -> Result;
> +}
> +
> +impl<B: IoCapable<T>, T> FallibleIoCapable<T> for B {
> +    #[inline(always)]
> +    fn io_try_read<'a>(view: Self::View<'a, T>) -> Result<T> {
> +        Ok(Self::io_read(view))
> +    }
> +
> +    #[inline(always)]
> +    fn io_try_write<'a>(view: Self::View<'a, T>, value: T) -> Result {
> +        Self::io_write(view, value);
> +        Ok(())
> +    }
> +}
> +
>  /// Trait indicating that an I/O backend supports memory copy operations.
>  pub trait IoCopyable: IoBackend {
>      /// Copy contents of `view` to `buffer`.
> @@ -759,7 +784,7 @@ fn copy_to_slice(self, data: &mut [u8])
>      fn try_read8(self, offset: usize) -> Result<u8>
>      where
>          usize: IoLoc<Self::Target, u8>,
> -        Self::Backend: IoCapable<u8>,
> +        Self::Backend: FallibleIoCapable<u8>,
>      {
>          self.try_read(offset)
>      }
> @@ -769,7 +794,7 @@ fn try_read8(self, offset: usize) -> Result<u8>
>      fn try_read16(self, offset: usize) -> Result<u16>
>      where
>          usize: IoLoc<Self::Target, u16>,
> -        Self::Backend: IoCapable<u16>,
> +        Self::Backend: FallibleIoCapable<u16>,
>      {
>          self.try_read(offset)
>      }
> @@ -779,7 +804,7 @@ fn try_read16(self, offset: usize) -> Result<u16>
>      fn try_read32(self, offset: usize) -> Result<u32>
>      where
>          usize: IoLoc<Self::Target, u32>,
> -        Self::Backend: IoCapable<u32>,
> +        Self::Backend: FallibleIoCapable<u32>,
>      {
>          self.try_read(offset)
>      }
> @@ -789,7 +814,7 @@ fn try_read32(self, offset: usize) -> Result<u32>
>      fn try_read64(self, offset: usize) -> Result<u64>
>      where
>          usize: IoLoc<Self::Target, u64>,
> -        Self::Backend: IoCapable<u64>,
> +        Self::Backend: FallibleIoCapable<u64>,
>      {
>          self.try_read(offset)
>      }
> @@ -799,7 +824,7 @@ fn try_read64(self, offset: usize) -> Result<u64>
>      fn try_write8(self, value: u8, offset: usize) -> Result
>      where
>          usize: IoLoc<Self::Target, u8>,
> -        Self::Backend: IoCapable<u8>,
> +        Self::Backend: FallibleIoCapable<u8>,
>      {
>          self.try_write(offset, value)
>      }
> @@ -809,7 +834,7 @@ fn try_write8(self, value: u8, offset: usize) -> Result
>      fn try_write16(self, value: u16, offset: usize) -> Result
>      where
>          usize: IoLoc<Self::Target, u16>,
> -        Self::Backend: IoCapable<u16>,
> +        Self::Backend: FallibleIoCapable<u16>,
>      {
>          self.try_write(offset, value)
>      }
> @@ -819,7 +844,7 @@ fn try_write16(self, value: u16, offset: usize) -> Result
>      fn try_write32(self, value: u32, offset: usize) -> Result
>      where
>          usize: IoLoc<Self::Target, u32>,
> -        Self::Backend: IoCapable<u32>,
> +        Self::Backend: FallibleIoCapable<u32>,
>      {
>          self.try_write(offset, value)
>      }
> @@ -829,7 +854,7 @@ fn try_write32(self, value: u32, offset: usize) -> Result
>      fn try_write64(self, value: u64, offset: usize) -> Result
>      where
>          usize: IoLoc<Self::Target, u64>,
> -        Self::Backend: IoCapable<u64>,
> +        Self::Backend: FallibleIoCapable<u64>,
>      {
>          self.try_write(offset, value)
>      }
> @@ -958,9 +983,12 @@ fn try_read<T, L>(self, location: L) -> Result<T>
>      where
>          T: AsReprMut,
>          L: IoLoc<Self::Target, T>,
> -        Self::Backend: IoCapable<<T as AsRepr>::Repr>,
> +        Self::Backend: FallibleIoCapable<<T as AsRepr>::Repr>,
>      {
> -        Ok(io_read!(self, try: location))
> +        let view = io_project!(self, try: location);
> +        Ok(T::from_repr(Self::Backend::io_try_read(io_view_as_repr(
> +            view,
> +        ))?))
>      }
>  
>      /// Generic fallible write with runtime bounds check.
> @@ -991,10 +1019,10 @@ fn try_write<T, L>(self, location: L, value: T) -> Result
>      where
>          T: AsRepr,
>          L: IoLoc<Self::Target, T>,
> -        Self::Backend: IoCapable<<T as AsRepr>::Repr>,
> +        Self::Backend: FallibleIoCapable<<T as AsRepr>::Repr>,
>      {
> -        io_write!(self, try: location, value);
> -        Ok(())
> +        let view = io_project!(self, try: location);
> +        Self::Backend::io_try_write(io_view_as_repr(view), T::into_repr(value))
>      }
>  
>      /// Generic fallible write of a fully-located register value.
> @@ -1037,7 +1065,7 @@ fn try_write_reg<T, L, V>(self, value: V) -> Result
>          T: AsRepr,
>          L: IoLoc<Self::Target, T>,
>          V: LocatedRegister<Self::Target, Location = L, Value = T>,
> -        Self::Backend: IoCapable<<T as AsRepr>::Repr>,
> +        Self::Backend: FallibleIoCapable<<T as AsRepr>::Repr>,
>      {
>          let (location, value) = value.into_io_op();
>  
> @@ -1071,12 +1099,12 @@ fn try_update<T, L, F>(self, location: L, f: F) -> Result
>      where
>          T: AsReprMut,
>          L: IoLoc<Self::Target, T>,
> -        Self::Backend: IoCapable<<T as AsRepr>::Repr>,
> +        Self::Backend: FallibleIoCapable<<T as AsRepr>::Repr>,
>          F: FnOnce(T) -> T,
>      {
>          let view = io_project!(self, try: location);
> -        view.write_val(f(view.read_val()));
> -        Ok(())
> +        let value = T::from_repr(Self::Backend::io_try_read(io_view_as_repr(view))?);
> +        Self::Backend::io_try_write(io_view_as_repr(view), T::into_repr(f(value)))
>      }
>  
>      /// Generic infallible read with compile-time bounds check.

Tested-by: Markus Probst <markus.probst@posteo.de>

Thanks
- Markus Probst


[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 870 bytes --]

  reply	other threads:[~2026-12-01 17:41 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 16:55 [RFC PATCH v8 0/4] iio: position: add Rust driver for ams AS5600 Muchamad Coirul Anwar
2026-12-01 16:55 ` [RFC PATCH v8 1/4] rust: io: add FallibleIoCapable Muchamad Coirul Anwar
2026-12-01 17:40   ` Markus Probst [this message]
2026-12-01 16:55 ` [RFC PATCH v8 2/4] i2c: rust: implement SMBus access via IoBackend Muchamad Coirul Anwar
2026-12-01 17:41   ` Markus Probst
2026-12-01 16:55 ` [RFC PATCH v8 3/4] rust: add minimal IIO subsystem abstractions Muchamad Coirul Anwar
2026-12-01 16:55 ` [RFC PATCH v8 4/4] iio: position: add Rust driver for ams AS5600 Muchamad Coirul Anwar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=11115845d6cf228b1d9cc5ae629ea3a27dfe0550.camel@posteo.de \
    --to=markus.probst@posteo.de \
    --cc=847807867@qq.com \
    --cc=aliceryhl@google.com \
    --cc=andi.shyti@kernel.org \
    --cc=andy@kernel.org \
    --cc=broonie@kernel.org \
    --cc=crofts@kernel.org \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=dlechner@baylibre.com \
    --cc=driver-core@lists.linux.dev \
    --cc=igor.korotin@linux.dev \
    --cc=jic23@kernel.org \
    --cc=linux-i2c@vger.kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=muchamadcoirulanwar@gmail.com \
    --cc=nuno.sa@analog.com \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox