Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document raw pointer <-> usize casts. #970

Merged
merged 10 commits into from
May 17, 2021
8 changes: 8 additions & 0 deletions src/expressions/operator-expr.md
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,12 @@ Here `*T` means either `*const T` or `*mut T`.
* `char` casts to the value of the code point, then uses a numeric cast if needed.
* `u8` to `char` cast
* Casts to the `char` with the corresponding code point.
* Pointer to address cast
* Casting from a valid raw pointer to `usize` will produce the address that is pointed to. Note that the pointer's provenance is lost in this conversion.
ben0x539 marked this conversation as resolved.
Show resolved Hide resolved
* Address to pointer cast
* Casting from `usize` to a raw pointer will produce a raw pointer to the same location as the original pointer, if the `usize` was obtained through a pointer to address cast of a valid pointer.

Note that the two pointers are not equivalent. Dereferencing the pointer obtained from the address to pointer cast may be [undefined behavior] if aliasing rules are not followed.
ben0x539 marked this conversation as resolved.
Show resolved Hide resolved

\* if integer-to-float casts with this rounding mode and overflow behavior are
not supported natively by the hardware, these casts will likely be slower than
Expand All @@ -373,6 +379,8 @@ expected.
number, preferring the one with an even least significant digit if exactly
halfway between two floating point numbers.

[undefined behavior]: ../behavior-considered-undefined.md

## Assignment expressions

> **<sup>Syntax</sup>**\
Expand Down