[PATCH v4 2/2] arm64: rust: Enable Rust support for AArch64

Miguel Ojeda miguel.ojeda.sandonis at gmail.com
Sun Oct 22 05:57:23 PDT 2023


On Sat, Oct 21, 2023 at 6:03 PM Andrew Lunn <andrew at lunn.ch> wrote:
>
> Maybe at the netdev conference next month, how to handle endiannes can
> be part of the presentation? I'm guessing, but is it part of the type
> system? So long as a type is marked as 'cpu endian', little endian, or
> big endian, the compiler will take care of adding the needed swaps?

Yeah, the type system can take care of ensuring that one does not
forget which type of quantity one is handling.

For instance, in this out-of-tree commit [1] from Wedson a `LE`
generic is added, which allows you to use e.g. `LE<u32>` in your
structs:

    struct Example {
        a: LE<u32>,
        b: LE<u32>,
    }

    fn sum(e: &Example) -> u32 {
        // `value` extracts the value in cpu representation.
        e.a.value() + e.b.value()
    }

Relatedly, the primitive integer types also provide "raw"
functionality for endianness, e.g. the `to_{le,be}` and
`to_{le,be,ne}_bytes` methods [2][3], and there are third-party crates
on this, e.g. [4].

[1] https://github.com/wedsonaf/linux/commit/ca4a93caff8b96a54a68fb052959801468bce01a
[2] https://doc.rust-lang.org/std/primitive.u32.html#method.to_le
[3] https://doc.rust-lang.org/std/primitive.u32.html#method.to_be_bytes
[4] https://docs.rs/byteorder/latest/byteorder/

Cheers,
Miguel



More information about the linux-arm-kernel mailing list