Re: [TECH TOPIC] Rust for Linux

Geert Uytterhoeven
2021-07-19T14:43:55Z

On Mon, Jul 19, 2021 at 4:03 PM Arnd Bergmann <arnd@arndb.de> wrote:
> On Mon, Jul 19, 2021 at 3:15 PM Wedson Almeida Filho
> <wedsonaf@google.com> wrote:
> > On Mon, Jul 19, 2021 at 01:24:49PM +0100, Wedson Almeida Filho wrote:
> > > On Fri, Jul 09, 2021 at 12:13:25AM +0200, Linus Walleij wrote:
> > > > I have seen that QEMU has a piece of code for the Arm PrimeCell
> > > > PL061 GPIO block which corresponds to drivers/gpio/gpio-pl061.c
> > > > Note that this hardware apart from being used in all Arm reference
> > > > designs is used on ARMv4T systems that are not supported by
> > > > LLVM but only GCC, which might complicate things.
> > >
> > > Here is a working PL061 driver in Rust (converted form the C one):
> > > https://raw.githubusercontent.com/wedsonaf/linux/pl061/drivers/gpio/gpio_pl061_rust.rs
> >
> > I'm also attaching an html rending of the C and Rust versions side by side where
> > I try to line the definitions up to make it easier to contrast the two
> > implementations.
>
> Thanks a lot, this looks extremely helpful!

+1

For a moment I thought I found an off-by-one bug:

for offset in 0..PL061_GPIO_NR {
if pending & bit(offset) != 0 {
router.deliver(offset.into());
}
}

Turns out "a..b" in Rust does mean "range from a to b-1".
That's gonna be hard to (un)learn...

https://doc.rust-lang.org/reference/expressions/range-expr.html

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds