3 ms·
Bitfields are hard! They are potentially unaligned, thus interact poorly with some language features, like pointers and references. And their size isn't a multi
by nextaccountic 5y ago
Bitfields are hard! They are potentially unaligned, thus interact poorly with some language features, like pointers and references. And their size isn't a multiple of bytes, unlike any other value, which causes problems as well. They interact poorly with padding and the general idea of Rust ABIs that enables optimizations like reordering fields and using "niches" (invalid values) to find a more compact layout for structs and enums.
I agree Rust should eventually have some native support for bitfields, but only if it's a high quality spec that addresses the outstanding concerns - Rust should specifically avoid the C mess, which isn't usable for most projects.
But this doesn't mean bitfields can't be done in Rust; it's just not in the stdlib (yet). See this post[0] which contains a survey of various libraries (and their respective limitations), and this slightly older article [1]. Those articles somehow missed the modular-bitfield crate [2], which is perhaps the nicest to use, and is what I would recommend you.
If you don't care about the specifics you can get any crate discussed in the above articles and use them.
About interoperability with C (and C++), I really like bindgen, cbindgen and cxx. The bindgen book [3] is straighforward. This section of cxx documentation [4] also describes the tradeoffs between the different FFI approaches. I don't know Ada though, so I must ask: what does Ada do?
About this,
> I just don't see what Rust has to offer that Ada didn't already address.
Off the top of my head, the crates ecosystem, the borrow checker, sum types with pattern matching [5], a stellar story for error handling (beats any language I know hands down - that is, if you use crates like thiserror and anyhow).
[0] https://immunant.com/blog/2020/01/bitfields/ https://immunant.com/blog/2020/01/bitfields/
[1] https://myrrlyn.net/blog/misc/bitfields-in-rust https://myrrlyn.net/blog/misc/bitfields-in-rust
[2] https://crates.io/crates/modular-bitfield https://crates.io/crates/modular-bitfield
[3] https://rust-lang.github.io/rust-bindgen/ https://rust-lang.github.io/rust-bindgen/
[4] https://cxx.rs/context.html https://cxx.rs/context.html
[5] https://chadaustin.me/2015/07/sum-types/ https://chadaustin.me/2015/07/sum-types/
- ajxs 5y ago> Bitfields are hard! They are... Again, like so many others in this thread, you're talking about C's problems. C is not the only language to implement this feature. I keep bringing up Ada because it's a language that features the ability to define structures at the bit level in its core language semantics. It manages to do this in a portable way, with a much more sophisticated, not to mention sound type system. > But this doesn't mean bitfields can't be done in Rust... Why should users need to rely on installing third-party additions in order to do something so basic like this? With the addition of third-party frameworks, and tooling you can make C as safe as Rust too, so I don't think the argument that third-party dependencies can solve this problem are relevant. I'm judging these languages simply on how well they allow me to express the semantics required for low-level programming. > About interoperability with C (and C++), I really like bindgen, cbindgen and cxx. The bindgen book [3] is straighforward... Ada comes out of the box with this functionality. It doesn't require reading a 'book' to understand it either, it's very straightforward. > beats any language I know hands down - that is, if you use crates like thiserror and anyhow... Again, why should someone need to install third-party tooling and dependencies for such basic features? The fact that so many responses list a litany of external dependencies to address Rust's shortcomings does not lend much credibility to Rust's design.