Are casts encouraged in Rust?
Are casts encouraged in Rust?
Posted Jun 29, 2025 20:44 UTC (Sun) by excors (subscriber, #95769)In reply to: Are casts encouraged in Rust? by alx.manpages
Parent article: How to write Rust in the kernel: part 2
The Rust driver's dev.read() returns a semantically-appropriate Result<u16>, which becomes u16 after handling the error case. The uapi module is mechanically generated from the C code, and I think it emits the constants as (probably?) i32 because it doesn't know how the constants are going to be used so it'll pick a default. So you need a cast from i32 to u16, which is ugly. The nice solution would be to get uapi to define the constants as u16, but those definitions need to be shared between C and Rust, and the C code doesn't use the correct types.
