17 ms·
Announcing Rust 1.19
- JoshTriplett 9y agoIncredibly excited to see unions available in stable Rust now! The release notes mention my RFC, but a huge thanks also to Vadim Petrochenkov for the implementation, and all the myriad RFC contributors.
- GolDDranks 9y agoNow that unions have stabilised, is there any significant un-expressable things in C APIs left? Bitfields?
- DDSDev 9y agoI have run into issues with bitfields, and flexible array members.
- GolDDranks 9y agoI'm not familiar with flexible array members, what are they?
- jcranmer 9y agoHaving an empty array at the end of the struct which is really in-line data: struct foo { int num_elements; char data[0]; } struct foo *d = malloc(sizeof(foo) + num); d->num_elements = num; /* Now d->data is effectively an array char[num_elements] */
- masklinn 9y agoIIRC that should work in Rust, Foo would be a DST.
- Manishearth 9y agoSo this is tricky. The way custom DSTs work in Rust is super annoying at the moment, but slightly less annoying for generics. You are free to define a custom DST that is like: struct Foo { header: u32, flexible: [SomeType] } However, there is no way to construct this type. The most you can do is calculate field offsets and write a ton of unsafe code. If the type was instead struct Foo<T: ?Sized> { header: u32, flexible: T } you would be able to construct a `&Foo<[SomeType]>` from a `&Foo<[SomeType; N]>` via DST coercions. This only works if you know the size of the array at compile time. For a runtime sized thing you have to implement a bespoke vector-like thing. You can find an example of that code in https://github.com/servo/servo/blob/e19fefcb474ea6593a684a1ca4ce616e61188ff0/components/servo_arc/lib.rs#L515-L594 https://github.com/servo/servo/blob/e19fefcb474ea6593a684a1c... where we have a generic "HeaderWithSlice<H, [T]>" type which can be heap allocated as a header followed by the flexible DST [T]. This could be improved. The HeaderWithSlice thing could probably be a useful crate for implementing completely-heap-allocated vectors (where the len/cap are on the heap) or shared reference counted types with flexible members. We haven't really split it out as a crate because we don't actually ever mutate it so the amount of code we need is significantly less (but it's not as useful as it could be). So yeah, flexible array members can be implemented in Rust, but it's a lot of hacky work. It's an equivalent amount of work and unsafety to write a custom Index impl on a repr(C) type with a zero-length array at the end. The DST doesn't actually get you much here.
- jancsika 9y agoI've never quite understood this one. For example-- what happens if I create an array of foos and then malloc data of each element to some arbitrary size?
- repsilat 9y agoYou don't do that. Each one is usually allocated on its own, and you keep a pointer to each. Though you could store them contiguously if each contains a length member or they're delimited in some other way.
- DDSDev 9y agoThis page[1] gives a explanation of flexible array members. They were introduced in C99. [1] https://en.wikipedia.org/wiki/Flexible_array_member https://en.wikipedia.org/wiki/Flexible_array_member
- JoshTriplett 9y agoYes, to the best of my knowledge, bitfields are the last thing in C APIs you can't express conveniently in Rust; you can handle them with careful macros, but that's annoying, just as it was for unions. In terms of things you can express now but that could still use some work: anonymous structs and unions. You currently still have to name all the intermediate aggregates, even when the C API doesn't. See https://internals.rust-lang.org/t/pre-rfc-anonymous-struct-and-union-types/3894 https://internals.rust-lang.org/t/pre-rfc-anonymous-struct-a... .
- deleted 9y ago[deleted]
- digikata 9y agoThe tough thing about bitfields in C are their poor portability as they can get laid out differently in memory by platform and compiler. They're often avoided even in C when you're looking for a good degree of interoperation (e.g. structs used in networking or on-disk-formats, etc.).
- JoshTriplett 9y agoRight. And we can't just say "we need bitfields", we need 100% C-compatible bitfields, for FFI.
- adrianratnapala 9y agoBut not very important for FFI, since bitfields rarely seem to show up in C APIs (presumably for exactly the reasons mentioned above). A kind of similar issue appears even with enums, because it is hard to guess the size of the integer that a given enum is implemented with. This is why it some not to use C enums in public interfaces to libraries.
- JoshTriplett 9y ago> A kind of similar issue appears even with enums, because it is hard to guess the size of the integer that a given enum is implemented with. True; fortunately, Rust has a means of declaring the size of an enum, with #[repr(u8)] and similar. But you do have to figure out the size of the C enum.
- jmcomets 9y agoI was pretty surprised, how the heck are unions different from enums? I read that they're essentially the same as C unions (~untagged enums), but I have no clue how `match` works in that case.
- Rusky 9y agoA pattern on a union just interprets it as the specified field. It doesn't try to determine which one was actually written to last.
- Cieplak 9y agoGreat running into you at Shizen @steveklabnik! Just started using Rust in a serious capacity this month to secure some C++ functions that are called by our Erlang apps, with great assistance from Rustler [1]. Several people have complained to me about the decision to remove async IO from Rust, but I'm really grateful that it happened, because it lets Rust focus on being the best at what it is. Erlang's concurrency primitives and Rust's performance & security are a match made in heaven. [1] https://github.com/hansihe/rustler https://github.com/hansihe/rustler
- steveklabnik 9y agoHey hey! You too. Sorry I had to skip town before we could grab a drink, it was a hectic trip! > the decision to remove async IO from Rust I'd be interested in hearing more about this, did they maybe mean green threads? https://tokio.rs/ https://tokio.rs/ is the big async-io project, it's certainly not removed in any sense!
- Cieplak 9y agoI should clarify the "remove async IO" as the decision to not include an event-loop primitive in the core language or stdlib, which is tantamount to complaining about GC not being part of Rust core :) Metal IO is an awesome library as well: https://github.com/carllerche/mio https://github.com/carllerche/mio
- kod 9y agoTokio depends on mio.
- steveklabnik 9y agoAh yes, I forgot that we had libuv at some point, duh! Thanks :)
- jmull 9y agoHm. I'm not sure about the addition of unions. Why add something that is unsafe to read or write? You need an additional mechanism to let you know which type it is OK to access. They mention the case where the type can be distinguished by the lest significant bit, but wouldn't it be better to handle that case as an enum? That is, the least significant bits define the enum tag, while the remaining bits define the associated value. (By the way, I really mean this as a straight question, not a criticism in the form of a rhetorical question. I really don't know enough about it to be criticizing it.)
- vvanders 9y agoThey're largely meant as an easier interop with the C ABI and C APIs that use unions. You wouldn't be using them in your higher level Rust library.
- steveklabnik 9y agohttps://github.com/rust-lang/rfcs/blob/master/text/1444-union.md#motivation https://github.com/rust-lang/rfcs/blob/master/text/1444-unio... TL;DR: FFI with C is much harder without unions. There are smaller reasons as well.
- kbsletten 9y agoI believe that they've framed it in this article as mostly for compatibility with C/C++. A simple example is JS objects are often represented as a single f64 with the NaN space punned for 32-bit pointers and 53-bit integers. If you want to put some code written in Rust in that environment, if you could define a union that could expose a safer API by using bit twiddling you could. Presumably if you have the memory to spare or are unconstrained in design you won't use this feature which is part of why it took so long to get into the language.
- GolDDranks 9y agoUnions are used in C APIs so having them is nice for FFI. Besides, enums aren't always optimal for some things. Let's say that you have an array of 32-bit values whose prime-numbered indexes contain either a signed or unsigned 32-bit integer, depending on a single global boolean tag. You can't encode that invariant into a (non-dependent) type system. If you want to do it safely, using enums, you are going to have a tag for every value, and that's going to cost you. (A bit crazy example, but bear with me.) Unions are a way to circumvent the type system a bit. They allow you to keep track of the stuff inside memory slots using the way you see the best. But there's a reason they're unsafe - the responsibility is on you!
- ofek 9y agoWow, a break yielding a value from within a loop is awesome! Do any other langs have that?
- kibwen 9y agoI feel like this has to have precedent from somewhere, most likely from another expression-oriented language, but I can't seem to find one right now. I've checked the RFC expecting to see a discussion of precedence from other languages, but no luck so far.
- joshmarlow 9y agoIf you're using the loop macro, you can do it from Common Lisp (see [0]). (block outer (loop for i from 0 return 100) ; 100 returned from LOOP (print "This will print") 200) ==> 200 (block outer (loop for i from 0 do (return-from outer 100)) ; 100 returned from BLOCK (print "This won't print") 200) ==> 100 [0] - http://www.gigamonkeys.com/book/loop-for-black-belts.html http://www.gigamonkeys.com/book/loop-for-black-belts.html EDIT: I always do formatting wrong on here.
- msbarnett 9y agoSure, Ruby: x = (1..100).each do |i| if i > 9 break i end end Evaluating that gives > x => 10 You can also supply a value to next within a loop, which is occasionally useful
- static_noise 9y agoOne thing that recently surprised me is that Rust lacks default values and named arguments for functions. Some consider it a counterpattern or bad memory to be able to not fill out all parameters or reference them by name. However some interfaces easily require like 50 different function parameters that cannot be removed in a simple way and they all make sense in different configurations. Without default values and named parameters you're lost there. I don't get this design decision on Rust side at all.
- shepmaster 9y agoIf you do have the struct implementing `Default`, as suggested from sibling comments, and it's fully public, you can also use the "functional update syntax" (where the `..` is): #[derive(Default)] struct Config { interesting_thing: bool, a: i32, b: i32, c: i32, } fn main() { Config { interesting_thing: true, ..Config::default() }; } In addition, the builder pattern is especially useful for larger configuration objects like this.
- fnord123 9y agoI think the derive_builder crate is worth a mention here: https://docs.rs/derive_builder/0.5.0/derive_builder/ https://docs.rs/derive_builder/0.5.0/derive_builder/
- saghm 9y agoThe main reason I haven't used derive_builder much is that I'd much rather have a compile-time guarantee that all the field that I want are filled out than have to check for an error at runtime (or worse, just `unwrap/expect` everywhere). I'm not sure there's currently a way around this on stable, but I'd personally prefer writing boilerplate for my types than having to handle errors in places that shouldn't need them.
- vvanders 9y agoCan't you just do that with passing a struct + Default[1]? [1] https://doc.rust-lang.org/std/default/trait.Default.html https://doc.rust-lang.org/std/default/trait.Default.html