9 ms·
Rust 1.56.0 and Rust 2021
- nynx 5y agoWoot woot, transmute is const now
- orra 5y agoCurious what the applications are. Collections? Would you write a lot of "unsafe" const code, given the chance?
- luizfelberti 5y agoI will shamefully admit to using transmute in very sinful ways, such as having enums that are repr(u16) or something like that at multiple numeric ranges such as: #[repr(u16)] enum Foo { A = 0, B } #[repr(u16)] enum Bar { A = 3200, B } struct FooOrBar(u16); And then proceeding to violate all of the unwritten rules of Rust by wrangling casts across these types like a goddamn wizard I'm not proud of this...
- lilyball 5y agoThis sounds like a great example of where you might want a union (https://doc.rust-lang.org/1.56.0/reference/items/unions.html https://doc.rust-lang.org/1.56.0/reference/items/unions.html)
- luizfelberti 5y agoOoh I forgot the language had those! I still feel like this trick has it's place though, as an example take a look at where I stole this trick from, by matklad [0] and tell me what you think :) [0] https://github.com/rust-analyzer/rowan/blob/d2c7843858da9d9e378de790bd14487c848f6f2e/examples/s_expressions.rs#L51 https://github.com/rust-analyzer/rowan/blob/d2c7843858da9d9e...
- lilyball 5y agoI think that this code is acceptable as long as you're careful. And I wish Rust had a `#[derive(TryFrom)]` sort of thing for deriving conversions from the declared primitive type to the enum.
- brink 5y agoIt's useful for handling data that came from outside Rust. If you have a byte array that you've received over a network connection that represents an array of floats, or you need to convert a 32bit RGBA pixel buffer that you got from some clang ffi binding to a byte pixel buffer without having to split/copy to a new vector.
- nynx 5y agoI can't think of an example now, but it has come up before when working on collections and other things.
- steveklabnik 5y agoOne thing that's come up a couple times are... well, what transmute is, that is, two types that have the same representation, and you want to cast between them. For example, the "try Rust out in the Linux kernel" code has a function that turns a byte slice into a &Cstr via a transmute. Those kinds of conversions can be useful inside of const fns.
- ishanjain28 5y agoHi Steve, I came across this post which presents an example using rust's transmute and it's incorrect behavior when the alignment is different. https://andrewkelley.me/post/unsafe-zig-safer-than-unsafe-rust.html https://andrewkelley.me/post/unsafe-zig-safer-than-unsafe-ru... Is this issue still present? I tried to figure it out the other day using latest rust's nightly but the llvm ir output has so much going on that I didn't _really_ understand what was going on
- MaulingMonkey 5y agoPer transmute's docs @ https://doc.rust-lang.org/std/mem/fn.transmute.html https://doc.rust-lang.org/std/mem/fn.transmute.html: > Because transmute is a by-value operation, alignment of the transmuted values themselves is not a concern. As with any other function, the compiler already ensures both T and U are properly aligned. However, when transmuting values that point elsewhere (such as pointers, references, boxes…), the caller has to ensure proper alignment of the pointed-to values. The post you've linked transmutes a reference, and as the caller fails to explicitly ensure proper alignment, it explicitly risks invoking undefined behavior - I would consider the code buggy. The easiest way to prove it's broken would be to create an reference that's more likely to be unaligned (&array[1] instead of &array[0]?) and run the code on a less misalignment-tolerant platform (ARM?). Here are some 100% sound alternatives using bytemuck (no unsafe required!) and core::ptr::{read,write}_unaligned (unsafe required): https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=bc30cc223eb8a454ca78338cfbee0a7a https://play.rust-lang.org/?version=stable&mode=debug&editio...
- steveklabnik 5y agoI am unsure about the specific details about IR that's emitted (though at first glance it seems we do emit the alignment attribute now, I think?), but transmute is, as the documentation says, "incredibly" unsafe: > Because transmute is a by-value operation, alignment of the transmuted values themselves is not a concern. As with any other function, the compiler already ensures both T and U are properly aligned. However, when transmuting values that point elsewhere (such as pointers, references, boxes…), the caller has to ensure proper alignment of the pointed-to values. This code is doing the latter incorrectly, and therefore invokes UB, as far as I can tell. To be honest, I don't use transmute very often and so I don't have every single last corner case about it memorized. It's not so much an "issue" as it is "Here's an API that's extremely sharp in Rust, and a similar, but less sharp API in Zig."
- kzrdude 5y agoHappy that the const subset of the language grows
- eska 5y agoFinally a nice way to init hash maps and all other kinds of collections! The former was really unattractive to Rust newbies!
- mbStavola 5y agoI'm so excited to finally have disjoint captures in closures; many papercuts on that one when I worked heavily with Amethyst a while back as there were a few closure-based APIs. Thanks to all the contributors for getting us to the 2021 edition!
- k__ 5y agoThat's the stuff Rust needs more of!
- stusmall 5y agoI love the concept of editions. I haven't anything like it in other languages. Having the ability to have one project made up of crates of many different editions is brilliant. It makes breaking language changes that would otherwise be a nonstarter become minor and easy to manage. Love it
- xamolxix 5y ago> I haven't anything like it in other languages Is that different from compiling one lib with --std=c++11 and another with --std=c++17?
- EdSchouten 5y agoYes, in that in C++ it would break if one library used a modern language feature as part of its public header files.
- chc 5y agoSurely the difference there is that Rust doesn't have header files.
- masklinn 5y agoNot necessarily, you could imagine a system where the header file specifies its edition.
- cpeterso 5y agoA header file can check #if __STDC_VERSION__ or __cplusplus versions to make some code conditionally available for, say, C11 or C++11. #if __STDC_VERSION__ >= 201112L // C11 feature #endif #if __cplusplus >= 201103L // C++11 feature #endif
- dralley 5y agoBut don't header files generally get concatenated together due to the way "dumb" textual includes work?
- jjice 5y agoReally excited for the reserved prefixes. Lots of possibilities there. I'm really rooting for f-strings to stream line format! calls.
- dthul 5y agoLove the 2021 edition changes and also the fact that they are so minor!
- pjmlp 5y agoFinally here! Congratulations to everyone.
- yakubin 5y agoI'm eagerly awaiting the Cargo feature "named profiles"[1]. It's already merged, but not yet announced to be planned for any specific Cargo version. It will allow users to create custom profiles with different build parameters from the standard ones, so you can e.g. create a "profiling" profile which is based on the "release" profile, so that it has all the nice optimizations, but with debug information included, so that it works well with cargo-flamegraph: [profile.profiling] inherits = "release" debug = true [1]: <https://github.com/rust-lang/cargo/pull/9943 https://github.com/rust-lang/cargo/pull/9943> And then there's the less fireworky, but still appreciated, Iterator::map_while[2], which is going to be in Rust 1.57. [2]: <https://github.com/rust-lang/rust/pull/89086 https://github.com/rust-lang/rust/pull/89086>
- steveklabnik 5y agoUnless something is special about this feature, new features generally aren't "planned" for specific versions in general. Since this landed two weeks ago, that means that it should land in the beta version of Cargo once that branches for this release (I didn't check if it has yet or not), which would place it to be stable in the next release of Rust, 1.57.0.
- yakubin 5y agoThanks for the clarification. I just went by what's specified in the sidebar on GitHub in the "Milestone" section.
- steveklabnik 5y agoAny time. Cargo doesn't use the Milestone feature of GitHub at all, so that's why that's empty :)
- lfairy 5y agoBTW, this article explains how Rust releases work: https://doc.rust-lang.org/book/appendix-07-nightly-rust.html https://doc.rust-lang.org/book/appendix-07-nightly-rust.html It's a release train, similar to that used by Chromium and Firefox. Edition releases (e.g. Rust 2021) are reserved for breaking changes only, and to retain Rust's stability promise, are opt-in.
- nayuki 5y agoI'm confused about the bullet point "IntoIterator for arrays". I thought this was added already in Rust 1.53.0. Did something else happen? See https://blog.rust-lang.org/2021/06/17/Rust-1.53.0.html#whats-in-1530-stable https://blog.rust-lang.org/2021/06/17/Rust-1.53.0.html#whats...
- davisoneee 5y agoTo follow Steve's comment: > Until Rust 1.53, only references to arrays implement IntoIterator. This means you can iterate over &[1, 2, 3] and &mut [1, 2, 3], but not over [1, 2, 3] directly. ...now, you can also iterate over [1, 2, 3] etc. https://doc.rust-lang.org/edition-guide/rust-2021/IntoIterator-for-arrays.html https://doc.rust-lang.org/edition-guide/rust-2021/IntoIterat...
- steveklabnik 5y agoRight, but the point is that you can do that as of 1.53.0, you didn't have to wait until this release and the edition to do it.
- tialaramex 5y agoYou know all this, but while some obvious things work in 1.53.0 one important thing causes scary warnings because it is shadowed by the back compat hack. "for x in myArray" works fine, just like "for x in myVector" but whereas "myVector.into_iter().foo()" does what you expect, "myArray.into_iter().foo() is actually giving foo an iterator over the references just as it would have in 2017 and now produces a warning about this into the bargain. In Rust 2021 myArray.into_iter() does what a modern Rust programmer expects it to do, provide an iterator over myArray itself. The warning does explain how you can get that iterator in 1.53.0 of course, but you need to write some ugly syntax whereas in Rust 2021 the obvious syntax just does what you expect as if arrays had always been IntoIterator.
- steveklabnik 5y agoThere's an explanation at the bottom of the section; this landed as a hack in previous editions, but works via the normal mechanisms in Rust 2021. "Since this special case for .into_iter() is only required to avoid breaking existing code, it is removed in the new edition, Rust 2021, which will be released later this year." As a user, you're right that there's not really an external-facing change here.
- mjw1007 5y agoIn 2017 or so when Rust editions were invented, the notion was that editions were primarily a "rallying point" (a way to make Rust's continuous release process feel more like Java's or C++'s), and only secondarily an (opt-in) change to the language itself. See for example the summary at the top of https://rust-lang.github.io/rfcs/2052-epochs.html https://rust-lang.github.io/rfcs/2052-epochs.html It seems to to me that that aspect has now been dropped: TFA simply says "Editions are a mechanism for opt-in changes that may otherwise pose backwards compatibility risk." I'm not sure this change of direction has been officially announced anywhere, though. (I think this is a good change: last time I saw cases of people asking questions like "How do I do foo in Rust 2018", and getting a mix of answers like "Nothing in Rust 2018 affects foo" and "Since Rust 1.20 you've been able to use std::foo::bar to do that".)
- steveklabnik 5y agoIt was discussed as part of the 2021 Edition RFC.
- tialaramex 5y agoInteresting. It mentions stuff like the library and documentation. When IntoIterator for arrays first stabilized (with the hack hiding into_iter() for backwards compatibility) I considered making all the documentation changes to use natural arrays in standard library examples, which of course would now be the obvious way to write it whereas previously it was ugly. I didn't do it (and now I have a job keeping me too busy) and I haven't gone back to look at examples to see if all/ most / some were updated to use arrays in the now natural way.
- nicoburns 5y agoThe 2018 was widely considered to be overly pressured (contrary to the usual Rust approach of shipping it when it’s ready), which led to burnt out compiler devs, and a bunch of people with unmet expectations. I think the approach to decouple Big Bang features from editions has been made in response to that.
- mjw1007 5y ago
- Fiahil 5y agoIt's not written in the announcement, but I am under the impression that Rust 1.56 compiles a bit faster. Is that right ?
- benschulz 5y agoI believe this is mostly due to the switch to LLVM 13[1]. [1]: https://twitter.com/ryan_levick/status/1443202538099073027 https://twitter.com/ryan_levick/status/1443202538099073027
- eska 5y agoThat seems to have been a mixed bag. But they also enabled PGO (or was it LTO?) and that was mentioned to be a bigger improvement.
- woodruffw 5y agoPGO requires a runtime profile, so I doubt they've enabled that by default :-) Rust has had LTO for quite a while, and it's normally a source of longer compilation times rather than shorter ones (since LTO in LLVM-world involves mashing all of the bitcode together and (re-)running a lot of expensive analyses to further optimize across translation unit boundaries. OTOH they've been making continuous improvements to the incremental compilation mode since 1.51/2, so that's probably among the sources of improvements here.
- 5y ago
- Luker88 5y agoWonderful, but still disappointed that easy strict-type-wrapping is not a thing yet. There have been a few RFC trying to work on deriving a type from another, and while I agree it's much more complex than it sounds, I also find it's a huge missing point. If I have to reimplement a wrapper myself for all Traits, I most likely won't bother, leading to less typesafety, leading to more bugs :(