Skip to content

Commit

Permalink
Clarify casting of pointers-to-unsized
Browse files Browse the repository at this point in the history
  • Loading branch information
CAD97 committed Aug 2, 2022
1 parent b52460f commit 2f36835
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/expressions/operator-expr.md
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,27 @@ Casts an enum to its discriminant, then uses a numeric cast if needed.

Casts to the `char` with the corresponding code point.

#### Pointer to pointer cast

Casting from a pointer to unsized type to pointer to sized type discards the pointer metadata, resulting in just a pointer to the referenced memory.
Casting between pointers to unsized type preserves the pointer metadata unchanged (e.g. the slice element length remains the same).

To illustrate:

```rust
#![feature(ptr_metadata)]
use std::ptr;

let u8_slice_ptr = ptr::slice_from_raw_parts::<u8>(ptr::null(), 4);
assert_eq!((ptr::null(), 4), u8_slice_ptr.to_raw_parts());

let u8_ptr = u8_slice_ptr as *const u8;
assert_eq!((ptr::null(), ()), u8_ptr.to_raw_parts());

let u16_slice_ptr = u8_slice_ptr as *const [u16];
assert_eq!((ptr::null(), 4), u16_slice_ptr.to_raw_parts());
```

#### Pointer to address cast

Casting from a raw pointer to an integer produces the machine address of the referenced memory.
Expand Down

0 comments on commit 2f36835

Please sign in to comment.