Rust 1.77.0 released
Posted Mar 22, 2024 10:09 UTC (Fri)
by josh (subscriber, #17465)
[Link] (6 responses)
Posted Mar 22, 2024 12:51 UTC (Fri)
by intelfx (subscriber, #130118)
[Link] (5 responses)
Got an example?
Posted Mar 22, 2024 21:23 UTC (Fri)
by josh (subscriber, #17465)
[Link] (1 responses)
Posted Mar 22, 2024 22:55 UTC (Fri)
by tialaramex (subscriber, #21167)
[Link]
It does report some problems (which the new definition presumably doesn't have) as motivation, but they're not soundness or correctness AFAICT.
Posted Mar 22, 2024 23:31 UTC (Fri)
by geofft (subscriber, #59789)
[Link] (2 responses)
As I understand it, the basic problem is that Rust's
The implementation of
The other approach is to start with an actual object (or just a reference to one), which gives you no trouble constructing a real reference to one of its members. But that requires the object to exist. You can't necessarily create an object of an arbitrary type; you may have to call its constructor, which might not be possible without an unwanted side effect (consider
(Also, it took me about an hour to come up with this comment and be at least somewhat confident it was correct - I had previously written out a totally different analysis based on misunderstanding the validity of raw pointers themselves - which is by itself another good reason that this should be encapsulated in some super obvious standard library facility instead of expecting people to get it correct on their own, even if technically they could.)
Posted Mar 23, 2024 13:10 UTC (Sat)
by intelfx (subscriber, #130118)
[Link] (1 responses)
Posted Mar 25, 2024 2:28 UTC (Mon)
by NYKevin (subscriber, #129325)
[Link]
Posted Mar 22, 2024 23:14 UTC (Fri)
by tialaramex (subscriber, #21167)
[Link] (4 responses)
I'm actually not sure what (if any) mechanism existed to write a constant (not just immutable) &CStr in stable before this landed, and obviously if you do a bunch of C interop that's pretty annoying. Constant &str was easy - that's what ordinary "string literals" are in Rust anyway.
Posted Mar 23, 2024 14:24 UTC (Sat)
by rodrigorc (guest, #89475)
[Link]
I used to use the `cstr` crate (https://docs.rs/cstr), that provides a `cstr!` macro and was able to build a const `&'static CStr`.
But this syntax is much nicer `c"hello"` vs `cstr!("hello")`.
Posted Mar 23, 2024 15:00 UTC (Sat)
by atnot (subscriber, #124910)
[Link] (2 responses)
It wasn't too hard, just a bit annoying. There's a const constructor for it from bytes:
https://doc.rust-lang.org/stable/std/ffi/struct.CStr.html...
It does require a null terminator, but in const context forgetting that is a compile error and it's fairly easy to wrap in a macro anyway, though not as pretty.
Posted Mar 23, 2024 22:28 UTC (Sat)
by intelfx (subscriber, #130118)
[Link] (1 responses)
I'm probably clueless, but how do you make CStr.from_bytes_with_nul() generate a compile error? It returns Result, and .unwrap() does not seem to be const.
Posted Mar 24, 2024 8:55 UTC (Sun)
by bluss (guest, #47454)
[Link]
Rust 1.77.0 released
Rust 1.77.0 released
Rust 1.77.0 released
Rust 1.77.0 released
Some explanation in the documentation for the standard library macro Rust 1.77.0 released
addr_of!
, as well as a 2019 issue in one of the third-party crates that was only resolved when addr_of!
got added (in Rust 1.51, March 2021).
&
operator gets you a reference, not a raw pointer. A raw pointer is basically like a C pointer; it doesn't really make any promises about pointing anywhere useful. A reference is in memory just a pointer too, but the semantics are different and basically the biggest thing that makes Rust special: it's a guarantee that the thing it points to is valid (has not yet been deallocated/freed) and that at any given time there is either at most one non-const (&mut
) reference or that all references are const. The obvious / conventional way to implement offset_of
is to make a null pointer of type pointer to the struct in question, index into the field in question, take its address, and return as an integer (see e.g. the most recent C implementation in Linux). But if you implement this in Rust, even if you start with a raw null pointer 0 as *const Foo
, indexing into a field and taking the address gets you a reference, not a null pointer - and it is a thoroughly invalid reference, and so it is undefined behavior.
addr_of!
uses the syntax &raw
(which is not yet available in stable Rust, but the standard library can use it), which immediately creates a raw pointer, not a reference, avoiding the undefined behavior.
std::fs::File
or std::process::Child
). There was a prior issue in that same third-party crate due to creating a reference to an object that was not properly constructed, though resolving this issue did not by itself remove all the UB in that crate.
Rust 1.77.0 released
Rust 1.77.0 released
Rust 1.77.0 released
Rust 1.77.0 released
Rust 1.77.0 released
Rust 1.77.0 released
>
> https://doc.rust-lang.org/stable/std/ffi/struct.CStr.html...
>
> It does require a null terminator, but in const context forgetting that is a compile error
Rust 1.77.0 released