Skip to content

Trait Objects memory diagram uses different order for different cells #68

@tv42

Description

@tv42

https://google.github.io/comprehensive-rust/generics/trait-objects.html has this diagram:

```bob
Stack Heap
.- - - - - - - - - - - - - -. .- - - - - - - - - - - - - - - - - - - - - - - -.
: : : :
: xs : : :
: +-----------+-------+ : : +-----+-----+ :
: | ptr | o---+---+-----+-->| o o | o o | :
: | len | 2 | : : +-|-|-+-|-|-+ :
: | capacity | 2 | : : | | | | +----+----+----+----+----+ :
: +-----------+-------+ : : | | | '-->| H | e | l | l | o | :
: : : | | | +----+----+----+----+----+ :
`- - - - - - - - - - - - - -' : | | | :
: | | | +-------------------------+ :
: | | '---->| "<str as Display>::fmt" | :
: | | +-------------------------+ :
: | | :
: | | +-------------------------+ :
: | '-->| "<i32 as Display>::fmt" | :
: | +-------------------------+ :
: | :
: | +----+----+----+----+ :
: '---->| 7b | 00 | 00 | 00 | :
: +----+----+----+----+ :
: :
: :
'- - - - - - - - - - - - - - - - - - - - - - - -'
```

Quoting https://doc.rust-lang.org/reference/types/trait-object.html

Each instance of a pointer to a trait object includes:

  • a pointer to an instance of a type T that implements SomeTrait
  • a virtual method table, often just called a vtable, which contains, for each method of SomeTrait and its supertraits that T implements, a pointer to T's implementation (i.e. a function pointer).

It seems the diagram here draws one object as (instance, vtable) and the second as (vtable, instance). Consistency would help, and based on https://doc.rust-lang.org/reference/types/trait-object.html it seems (instance, vtable) is the safer assumption.

Metadata

Metadata

Assignees

Labels

waiting for authorWaiting on the issue author to reply

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions