I didn't know about this, and it's super cool. It's part of a broader pattern where the rigid constraints Rust can impose on code allow both the compiler and the user to do things that would be wildly dangerous in a less-strict language.
At first, this was special cased in the compiler, but then generalized.
enum Foo {
A = 0,
B = 1,
C = 2,
Unknown(i32),
}
where Unknown field cannot contain values 0, 1, or 2, thus Rust compiler will be able to pack this enum into single i32, like in C.I'd represent it like this:
#[repr(transparent)]
struct Foo(i32);
impl Foo {
const A: Foo = Foo(0);
const B: Foo = Foo(1);
const C: Foo = Foo(2);
}I guess the compiler can't help you keep known and unknown values separate with that structure though, so maybe it's not enough.
Whereas the semantics in Rust are "we can do whatever we want unless you add an explicit #[repr] attribute, in which case you choose the semantics of layout and we don't mess with it."
It's not about the strength of the type system, it's about history.
However, because there are no type guards for this stuff in C/C++ you're generally constrained by convention/intuitiveness, when picking these special values, in order to (hopefully) guard against accidental misuse. So in the NonZero case above, you probably wouldn't do that because it's pretty unusual, and you'd therefore use more memory.
And then, even when the "special values" are chosen "reasonably", they sometimes get misused anyway and we end up with bugs like last year's trivial sudo exploit: https://bit-sentinel.com/to-sudo-or-not-to-sudo-demystifying...
> Reading the manual for those functions we learn that -1 is a special value
> Supplying a value of -1 for either the real or effective user ID forces the system to leave that ID unchanged.
> If one of the arguments equals -1, the corresponding value is not changed.
> So, what happens when we send -1 as a parameter? Well, somewhere along the underlying code lines is a convention that -1 is represented as 4294967295, thus either values will get the wanted result.
Edit: In practice the above may be more common in C than modern C++
It is used in some other places though. For instance, std::string implementations typically employ small-string optimization (SSO) which means that there might be a limit on string::size() since some of the bits inside that value are used for different purposes (example: https://akrzemi1.wordpress.com/2014/04/14/common-optimizatio...). The "niche" in this case is the realization that strings are unlikely to be exabytes in size, so there is no reason to always store zeros in high bits of _size.
You can catch a hell lot of things at compile-time. Correctness is huge in Ada/SPARK. You write safety-critical software in it. I have a comment on Ada/SPARK in great detail, but I do not have the time to find it sadly. :(
Consider this:
Total : Integer range 0 .. Integer'Last
"Any attempt to assign a negative value to variable Total results in raising an exception at run time. During analysis, GNATprove checks that all values assigned to Total are positive or null."Analysis here means static analysis.
You can do stuff like this, too, for example:
subtype Natural is Integer range 0 .. Integer'Last;
subtype Positive is Integer range 1 .. Integer'Last;
The above is actually declared in the visible part of package Standard. #[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.
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_...
Do these enforce against out-of-bounds values by panicking, the way arrays do? Or are they unsafe?
See https://github.com/rust-lang/rust/blob/master/library/core/s...
They are unsafe. The safe API of your type must prevent constructing invalid values.
I know that other languages have subranges or refinement types but niches seem to solve a different problem - namely to use the "holes" in a data type for optimization. Does any other language have some comparable concept to niches in Rust?
http://www.itu.dk/~sestoft/mosmllib/Int.html#precision-val
I recall that MLton would stash data in the lower log_2(alignment) bits of pointers for types with alignment requirements (or architectures with memory load alignment requirements), but can't find a reference. There was a paper (ICFP, I think?) about using these bits for reference-counting GC with a fallback to mark+sweep when the count hit the maximum value. Don't think it ever got implemented in a production compiler.
I'm pretty sure the "None:Option<T> as null pointer when T is represented as a pointer" optimization happens under the hood in a lot of Haskell+ML compilers.
Rust is the first language I've come across that managed to include pretty much all the cool features ML had.
I think Ocaml also does something interesting with integers, IIRC they’re 1 bit less than the normal range and a shift stores a flag in the freed low bit: if the bit is set it’s a shifted integer, otherwise it’s an unshifted pointer to an integer.
[target.'cfg(target_os = "macos")'.dependencies]
foo = { version = "1", features = ["x"] }
[target.'cfg(not(target_os = "macos"))'.dependencies]
foo = { version = "1", features = [] }
With the current resolver, the dependency `foo` will always be compiled with a feature `x`, no matter which target os you're using. Debugging this took me a while and I was surprised it's actually just how the current resolver works.if predicate() { Some(thing) } else { None }
After:
predicate().then(|| thing)
the second, that magic is not clear, i'll keep using the Before personally
edit: thanks all, should have known it was a lambda :D, in Swift we can omit this. My first thought seeing "||" was some type of OR logic since it's in the context of if/else
https://web.archive.org/web/20080218051638/http://www.nikhil...
This solves another of the ergonomic issues in Rust. It really feels like within 2021 Rust will hit a point where it feels totally consistent.
The current limitation I find the most annoying is that you can't use associated constants in generics.
The only thing left that I want with generics is variadics/tuples. The only way currently to implement a trait over a generic tuple is to write a macro and call it for however many sized tuples you want. I've not seen any convincing RFCs about it yet though so I'm not confident we'll get them any time soon
1. Some crates in the ecosystem have "extension traits" that add the same method to `f32`. Adding this into std will cause conflicts with those methods so users will need to disambiguate or remove their use of those extension traits. (This is allowed breakage under the stability guidelines)
2. Should this use this two arguments or a range (`x.clamp(1.0, 5.0)` or `x.clamp(1.0..5.0)`)?
I think part of the reason this took so long was that by having widely used crates like `numtools` in the ecosystem that provided this functionality, it took a lot of the pressure off having this in std/core.
Short summary:
First delay was ecosystem breakage; many people had defined their own functions with the same name. We're allowed to make these changes but tend to try not not unnecessarily break people.
It then sat for about a year. The original author was a bit tired from all of the work to get to that point, and reasonably let it sit.
There was then a small discussion about taking individual parameters vs ranges.
Six months after that, it finally actually landed, thanks to some other changes that would help mitigate the breakage.
It then sat for a while, until the libs team proposed merging. That brought up a lot of the previous design questions, which some people thought weren't resolved to their satisfaction.
It then finally got stabilized in October of last year, and then it had to wait for the release trains.
So, TL;DR: a surprising amount of details for such a small feature, which can lead to burnout, along with not enough people wanting to push it over the line.
lots of upvotes for them too since its for privacy in the compiler
Looks like someone who cares about this needs to step up! You can make it happen sooner than later. It is one of the best things about open source.
[0] https://tiny.cc/
It really depends on what you're actually asking for. Do you want, for example, a "slimmer" Rust compiler that jettisons all the stuff that supports older language editions? Do you want a "simpler" compiler that only uses straightforward algorithms at the expense of compiler speed? Or do you want a faster compiler that does less optimization at the expense of code generation quality? The latter, at least, is something that the Cranelift backend for rustc hopes to achieve.
The borrow checker is technically optional (as proven by mrustc), but if you wanted to implement it, it's no longer a set of simple scope rules, but more like a flavor of Prolog inside the compiler.
These things are awesome for the power and usability of the language, but aren't tiny.
https://github.com/thepowersgang/mrustc
But AIUI that is not aimed at being a Rust compiler you would actually use day-to-day, just at being a way to bootstrap a Rust toolchain without needing a Rust compiler.
I know Rust already has Tuples[0], so I'm assuming Const Generic Indexing For Arrays[1] is a happy path for the compiler to optimize what amounts to a finite sized Generic Tuple? (Finite size in terms of memory not elements)
Excellent feature, I just seem some (admittedly only high level) surface overlap in this language feature.
[0]: https://doc.rust-lang.org/rust-by-example/primitives/tuples....
[1]: https://blog.rust-lang.org/2021/02/11/Rust-1.50.0.html#const...
[2]: Which, while generally accepted to be a strongly typed language, is not near the level of Rust
In theory, arrays do not need to exist, and could in theory be expressed as tuples, but that would lead to rather unpleasant syntax. `(A, A, A, A, A, A, A, A, A, A)` is certainly not as nice as `[A; 10]`; — Rust could even have chosen to make the latter syntactic sugar for the former and make them interchangeable.
The crux to this, is that in Rust, unlike in many other languages, the length of an array is static and known at compile time. Many languages lack such a datatype, as it isn't strictly needed per the aforementioned reasons.
Product types: woo!
Addendum Explanation (feel free to skip readers):
When I say that, this is what I mean:
In TypeScript, an accepted thing about generic is that I can have multiple declared types either via a union e.g.
`TypeA | TypeB`
or intersection
`TypeA & TypeB`
In general practice, it is assumed you can do this, and not vice versa. If this is supposed to be avoided, you typically will use a constraining Type or specify a specific interface / type instead of making it outright generic.
This is not always the case with a language like Rust, it's much finer grained, so I must try and get out of the habit of thinking this way. It's the opposite, that generic typing is constrained by the structure you put them in, as I understand it.
This of course, is a super general overview of what I'm getting at, and there is a lot more nuance to TypeScript as well, but my everyday practice of using the language for the least 6 years or so has proven this to be the case often. I'm finding (and I do like this for what it's worth) Rust is not like this outwardly.
Here's a TypeScript analogy that might help clarify things: https://www.typescriptlang.org/play?#code/C4TwDgpgBASgrgZ2AG...
I'm not sure what you're getting at when mentioning unions & intersections. Are you comparing a type like `Foo<number | boolean>` with `Bar<number, boolean>`? Those are conceptually very different (even in TypeScript), but maybe I'm misunderstanding.
The big difference is structural vs nominal typing and the fact that Rust doesn't really have subtyping (besides lifetimes). TypeScript types are defined by their structure—an object type you define can be compatible with a different object type from a separate library that never heard of you, just by nature of sharing the same properties. However Rust types are delineated by their names/identities—two separate types are not directly interchangeable even if they're defined identically.
Consider if you write a generic function which takes as an argument any type that has an indexing operation. In the past that function couldn't take an array as an argument. Now it can.
OK, can see it now, too. Caches...
I found that not using the ISP DNS on your router helps with this problem on several sites, this being one of them, among other benefits
I recommend everyone use something like OpenDNS, Google's public DNS resolver (if you're comfortable with that) or CloudFlare via 1.1.1.1 (and associated addresses), or any other myriad of reputable third party DNS services
https://github.com/rust-lang/rust/blob/master/RELEASES.md#ve...
https://web.archive.org/web/20210211144406/https://blog.rust...
We have stabilized, anything added does not break. But we are still adding new things, though they are mostly minor at this point. This is one of two or three big features that people are still expecting us to add.
I wish I knew language design & compiler development better & had the time to propose it & see it through.
In the meantime it can be done by wrapping NonZero and storing !value, but that's a (tiny) runtime overhead on each get/set
However, new features are additive, so you won't learn stuff that's incorrect, you just may not be aware of later, new things.
1.50 is a nice round number and for that reason i can recommend it :)
> On Linux, the answer is pretty obviously no. Linux file descriptors are stored in an array of structs that has its capacity bounded to INT_MAX, so any negative int would either be considered nonsense (if treated as negative) or be higher than INT_MAX (if it was bit-reinterpreted as an unsigned value).
> The Single UNIX Specification explicitly says that open can't return a negative file descriptor, and says in the page for dup2 that you should get EBADF if you try to claim a negative file descriptor.
> Unfortunately, their description of file descriptor allocation never explicitly says that the negative range is out of bounds, but open references this algorithm while simultaneously claiming that it never gives a negative result.
> Also, Wikipedia says it can't be negative, but they don't give a source on that particular claim (urgh!).
-1 was picked as a conservative choice that's enough for the common case of Option.
pub fn clamp(self, min: f64, max: f64) -> f64
Rust has generics iirc, so why there's this?Therefore clamp is implemented on Ord (meaning there's always a value to return), and there's an efficient implementation on floats which can define its behaviour with respect to NaN.
If you want the actual details,
* https://internals.rust-lang.org/t/clamp-function-for-primiti...
Warning: can't set `control_brace_style = ClosingNextLine`, unstable features are only available in nightly channel.
It seems like a trivial and desirable feature but it's only been available in nightly for over a year now. The poor (and rigidly enforced) default on this same issue was why I ripped prettier out of every project I work on.Also, I don't know how hard it is to contribute to this kind of issue, but I'm glad to put in some time (with my still newbie level of Rust skills) if that can help resolve it.