4 ms·
I write a lot of code using the #[rustc_layout_scalar_valid_range_start(x)] #[rustc_layout_sclaar_valid_range_end(y)] struct MyInt(i32); to create
by volta83 6y ago
I write a lot of code using the
#[rustc_layout_scalar_valid_range_start(x)]
#[rustc_layout_sclaar_valid_range_end(y)]
struct MyInt(i32);
to create integers with only valid values in range [x, y) that benefit from the niche optimizations.
Like, have literally a project with almost 100k LOC which is all built on top of this.
- brundolf 6y agoWow, I did not know about those! I'm surprised how little a Google search turns up for them; the best I found was a mention (but no description or examples) in docs.rs: https://docs.rs/rustc-ap-syntax_pos/634.0.0/rustc_ap_syntax_pos/symbol/sym/constant.rustc_layout_scalar_valid_range_start.html https://docs.rs/rustc-ap-syntax_pos/634.0.0/rustc_ap_syntax_... Do these enforce against out-of-bounds values by panicking, the way arrays do? Or are they unsafe?
- jleahy 6y agoThey're not only unsafe, but as far as I'm aware they're a compiler implementation detail that isn't really supposed to be used in user code (along with rustc_nonnull_optimization_guaranteed). It's actually how NonZero is implemented. See https://github.com/rust-lang/rust/blob/master/library/core/src/num/nonzero.rs https://github.com/rust-lang/rust/blob/master/library/core/s...
- brundolf 6y agoAh, that explains things
- tmzt 6y agoIt would be nice to have a #[range(a..b)] version of this that could be standardized.
- volta83 6y ago> Or are they unsafe? They are unsafe. The safe API of your type must prevent constructing invalid values.
- oblio 6y agoThe revenge of Pascal integer ranges! :-D
- johnisgood 6y agoIt makes me chuckle when they perceive it to be something of an innovation when it has been in languages long before Rust, in a much better way; I mean, just look at Ada, really. You even have formal verification in Ada/SPARK.
- volta83 6y agoI don't use these hints for verification; in fact, these hints don't verify anything. It is trivial to create an integer type in Rust that only accepts certain values, and doing so doesn't require these hints, e.g., #[bounded_int(start, end)] struct MyBoundedInt(i32); creates a type called MyBoundedInt that never will take a value outside the range [start, end); this can be proved formally, i.e., there is no safe Rust program that can violate that. No need to run any kind of verification on top. The place where Rust differs from Ada, and the reason I use these attributes, is to enable the layout optimizations that Rust performs (e.g. Option<NonZero> having the same size in bytes as NonZero, and many others).
- bogeholm 6y agoRust doesn’t claim to have invented this, or many of the other aspect of the language at all really. From the Rust reference (https://doc.rust-lang.org/reference/influences.html https://doc.rust-lang.org/reference/influences.html): > Rust is not a particularly original language, with design elements coming from a wide range of sources. Some of these are listed below ...
- johnisgood 6y ago> Rust doesn’t claim to have invented this, or many of the other aspect of the language at all really. Oh, I did not mean to suggest that Rust does, just some people do, because they are not familiar with other programming languages and whatnot.