|
|
Subscribe / Log in / New account

A GPIO driver in Rust

A GPIO driver in Rust

Posted Jul 19, 2021 17:08 UTC (Mon) by rvolgers (guest, #63218)
Parent article: A GPIO driver in Rust

It's funny, seeing them side by side like this seems to activate the "reading C" part of my brain while looking at the Rust code.

I notice I get annoyed at the lack of explicit types on variables and things like ".into()" (which likewise doesn't offer much clue as to typing, just that a conversion is happening). In C, it's relatively important to be aware of types. On one hand due to various traps related to UB and integer arithmetic, and on the other hand because there are no guard rails on most APIs. Rust really encourages you to embed restrictions in the API, so that the use site can be a lot more terse.

Also with "C brain" engaged, some of the nested control flow like in `Chip::get_direction` seems weird. But objectively, I think the Rust version is quicker to read.

I do notice that the indentation is 4 spaces in the Rust version. Pretty sure that's because they're using Rust defaults for formatting and also pretty sure there has been a lot of discussion about this already and I really don't want to start that again. But just mentioning it since it does make nesting look a little worse in the C case.


to post comments

A GPIO driver in Rust

Posted Jul 19, 2021 22:14 UTC (Mon) by roc (subscriber, #30627) [Link]

IDE support, e.g. rust-analyzer, will tell you the inferred type of a variable by hovering over it with your mouse. I find this incredibly useful. rust-analyzer with VSCode can also be configured to display the type annotations inline (non-editable) in the text, though I prefer not to do that. For me, this is basically the best of both worlds: I can read the types when I want to, but I don't have to write them and they're not cluttering up the code when I don't need them.

But I can see why the kernel might not want to assume code readers have such capabilities available.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds