Skip to content

Commit 4b720a3

Browse files
committed
Initial definition for Aliasing
Not precise, but something we can point beginners to for the moment while more is worked on.
1 parent f4683c8 commit 4b720a3

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

reference/src/glossary.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,42 @@
11
## Glossary
22

3+
#### Aliasing
4+
5+
(Please note: a full aliasing model for Rust has not yet been constructed, but
6+
at the moment we can give the following guidelines.)
7+
8+
*Aliasing* is any time two pointers and/or references point to the same "span"
9+
of memory. A span of memory is similar to how a slice works: there's a base byte
10+
address as well as a length in bytes.
11+
12+
Consider the following example:
13+
14+
```rust
15+
fn main() {
16+
let u: u64 = 7_u64;
17+
let r: &u64 = &u;
18+
let s: &[u8] = unsafe {
19+
core::slice::from_raw_parts(&u as *const u64 as *const u8, 8)
20+
};
21+
let (head, tail) = s.split_first().unwrap();
22+
}
23+
```
24+
25+
In this case, both `r` and `s` alias each other, since they both point to the
26+
memory of `u`.
27+
28+
However, `head` and `tail` do not alias each other: `head` points to the first
29+
byte of `u` and `tail` points to the other seven bytes of `u` after it.
30+
31+
* The span length of `&T`, `&mut T`, `*const T`, or `*mut T` when `T` is
32+
[`Sized`](https://doc.rust-lang.org/core/marker/trait.Sized.html) is
33+
`size_of<T>()`.
34+
* When `T` is not `Sized` the span length is `size_of_val(t)`.
35+
36+
One interesting side effect of these rules is that references and pointers to
37+
Zero Sized Types _never_ alias each other, because their span length is always 0
38+
bytes.
39+
340
#### Interior mutability
441

542
*Interior Mutation* means mutating memory where there also exists a live shared reference pointing to the same memory; or mutating memory through a pointer derived from a shared reference.

0 commit comments

Comments
 (0)