Re: [RFC PATCH v3 3/4] iio: position: add Rust driver for ams AS5600

From: Muchamad Coirul Anwar

Date: Wed Jun 03 2026 - 04:54:18 EST


On Mon, 2 Jun 2026 13:06:00 +0100
Jonathan Cameron <jic23@xxxxxxxxxx> wrote:

> > > + let angle = (angle_h << 8 | angle_l) & 0x0FFF;
> >
> > Switching to `try_read16()` (calls `i2c_smbus_read_word_data`) plus
> > `swap_bytes()` for the byte order, then mask:
> >
> > const AS5600_RAW_ANGLE_MASK: u16 = 0x0FFF;
> >
> > let raw = client.try_read16(AS5600_REG_RAW_ANGLE_H as usize)?;
> > let angle = raw.swap_bytes() & AS5600_RAW_ANGLE_MASK;
>
> That swap goes back to a pattern we ripped out of the C code years ago
> and why we have the smbus swapped functions and regmap support for that.
> If a given part always does the bytes in opposite byte order of smbus
> then it should be handled as part of the read function rather than every
> word read having to be followed by a swap.

Understood. I'll use i2c_smbus_read_word_swapped or a Rust wrapper
around it, so the byte order is handled at the transport level.

> > Rust doesn't have FIELD_GET yet, but a named constant serves the same
> > documentation purpose. Single call, no manual byte assembly.
>
> A named constant serves only part of the purpose. The main gain from
> FIELD_GET() is we don't have to go check if a shift is also needed.
> I'd strongly support work on getting something similar for rust as it
> makes for a lot more consistent and readable driver code.
>
> Basically I want all the useful helper stuff we've built up in C to be
> available in Rust. In cases like this one I would prefer there was never
> a legacy of doing it any other way!

Agreed, I'll look into what exists for a Rust FIELD_GET equivalent.

Thanks,
Coirul