You've already forked comprehensive-rust
mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-06-26 18:51:00 +02:00
Clarify the Box layout diagrams. (#374)
This commit is contained in:
@ -18,14 +18,15 @@ allows the compiler to optimize the memory layout:
|
|||||||
|
|
||||||
```bob
|
```bob
|
||||||
Stack Heap
|
Stack Heap
|
||||||
.- - - - - - - - - - - - -. .- - - - - - - - - - - - - - - - - - - - - - - -.
|
.- - - - - - - - - - - -. .- - - - - - - - - - - - - - - - - - - - - - -.
|
||||||
: : : :
|
: : : :
|
||||||
: list : : :
|
: list : : :
|
||||||
: +--------+-------+ : : +--------+--------+ +--------+------+ :
|
: +----+----------+ : : +----+----------+ +----+----------+ :
|
||||||
: | 0 | 1 | : : .->| 0 | 2 | .->| ////// | //// | :
|
: | .0 | ".1/Tag" | : : | .0 | ".1/Tag" | | .0 | ".1/Tag" | :
|
||||||
: | "1/Tag"| o-----+----+-----+-' | "1/Tag"| o-----+-' | "1/Tag"| null | :
|
: +----+----------+ : : +----+----------+ +----+----------+ :
|
||||||
: +--------+-------+ : : +--------+--------+ +--------+------+ :
|
: | 1 | o-----+---+-----+--->| 1 | o-----+--->| 2 | null | :
|
||||||
: : : :
|
: +----+----------+ : : +----+----------+ +----+----------+ :
|
||||||
: : : :
|
: : : :
|
||||||
`- - - - - - - - - - - - -' '- - - - - - - - - - - - - - - - - - - - - - - -'
|
: : : :
|
||||||
|
`- - - - - - - - - - - -' '- - - - - - - - - - - - - - - - - - - - - - -'
|
||||||
```
|
```
|
||||||
|
@ -20,24 +20,24 @@ fn main() {
|
|||||||
.- - - - - - - - - - - - -. .- - - - - - - - - - - - - - - - - - - - - - - -.
|
.- - - - - - - - - - - - -. .- - - - - - - - - - - - - - - - - - - - - - - -.
|
||||||
: : : :
|
: : : :
|
||||||
: list : : :
|
: list : : :
|
||||||
: +--------+-------+ : : +--------+--------+ +--------+------+ :
|
: +------+----+----+ : : +------+----+----+ +------+----+----+ :
|
||||||
: | Tag | Cons | : : .->| Tag | Cons | .->| Tag | Nil | :
|
: | Tag | .0 | .1 | : : | Tag | .0 | .1 | | Tag | .0 | .1 | :
|
||||||
: | 0 | 1 | : : | | 0 | 2 | | | ////// | //// | :
|
: +------+----+----+ : : +------+----+----+ +------+----+----+ :
|
||||||
: | 1 | o-----+----+-----+-' | 1 | o------+-' | ////// | //// | :
|
: | Cons | 1 | o--+----+-----+--->| Cons | 2 | o--+--->| Nil | // | // | :
|
||||||
: +--------+-------+ : : +--------+--------+ +--------+------+ :
|
: +------+----+----+ : : +------+----+----+ +------+----+----+ :
|
||||||
: : : :
|
: : : :
|
||||||
: : : :
|
: : : :
|
||||||
`- - - - - - - - - - - - -' '- - - - - - - - - - - - - - - - - - - - - - - -'
|
'- - - - - - - - - - - - -' '- - - - - - - - - - - - - - - - - - - - - - - -'
|
||||||
```
|
```
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
|
|
||||||
* If the `Box` was not used here and we attempted to embed a `List` directly into the `List`,
|
* If the `Box` was not used here and we attempted to embed a `List` directly into the `List`,
|
||||||
the compiler would not compute a fixed size of the struct in memory, it would look infinite.
|
the compiler would not compute a fixed size of the struct in memory, it would look infinite.
|
||||||
|
|
||||||
* `Box` solves this problem as it has the same size as a regular pointer and just points at the next
|
|
||||||
element of the `List` in the heap.
|
|
||||||
|
|
||||||
* Remove the `Box` in the List definition and show the compiler error. `Recursive with indirection` is a hint you might want to use a Box or reference of some kind, instead of storing a value directly.
|
* `Box` solves this problem as it has the same size as a regular pointer and just points at the next
|
||||||
|
element of the `List` in the heap.
|
||||||
|
|
||||||
|
* Remove the `Box` in the List definition and show the compiler error. `Recursive with indirection` is a hint you might want to use a Box or reference of some kind, instead of storing a value directly.
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
Reference in New Issue
Block a user