Skip to content
0:00 / 0:00

#dynamicdispatch#staticdispatch#vtables

Crust of Rust: Dispatch and Fat Pointers

Jon Gjengset

111K subscribers • 38 videos on Hallway

YouTube
  • Recommended by 2 people: 2 in engineering communities.
  • Presented by Jon Gjengset

Intermediate. Assumes working knowledge.

107K views5 years ago2 recommendations

Programming languagesSystems programmingRustPerformance and optimizationMemory management

Jon Gjengset explains static and dynamic dispatch in Rust, diving deep into generics, monomorphization, trait objects, and vtables.

In this episode of Crust of Rust, we go over static and dynamic dispatch in Rust by diving deep into generics, monomorphization, and trait objects. As part of that, we also discuss what exactly the Sized trait is, what it's for, and how it interacts with Dynamically Sized traits. Introduction Monomorphization (Static) Dispatch Trait Objects The Sized Trait Sizing Unsized Types Can I Recover The Concrete Type? Dynamic Dispatch Vtables Limitation: Multiple Traits Limitation: Associated Types Limitation: Static Trait Methods Disallowing Trait Objects Limitation: Generic Methods Limitation: No Non-Receiver Self Partial Object Safety Dropping Trait Objects Dynamically Sized Types Manual Vtables in std Q&A: Making Your Own DST Box([u8]) vs. Vec(u8) dyn Fn() vs. fn() vs. impl Fn() No Coherence This Stream Runtime Trait Detection Double-Dereferencing dyn Fn() Unsafe Vtable Comparions Slice of Trait Objects Codegen Units and Vtables The Any Trait You can read more about Rust monomorphization in the Rust book at doc.rust-lang.org/book/ch10-01-syntax.html?highlight=perform…. It also has some good discussion about Trait Objects (doc.rust-lang.org/book/ch17-02-trait-objects.html) and Dynamically Sized Types (doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamical…). For more details, the Rust reference also has more details on Trait Objects (doc.rust-lang.org/nightly/reference/types/trait-object.html) and Object Safety (doc.rust-lang.org/nightly/reference/items/traits.html#object…), and so does the Rustonomicon (doc.rust-lang.org/nomicon/exotic-sizes.html). Live version with chat: youtu.be/TOwiCW8zXzU

Why it's here

2 people recommend this

What people say. Recommended as an excellent, detailed explanation of dynamic dispatch, trait objects, and object safety in Rust.

Moments people linked to

u/Merzbroin r/rust • Recommends

On “How to get a trait object for one auto-trait and one trait”

If I'm not mistaken the Copy trait is not object-safe which means you won't be able to use it with dynamic dispatch. jonhoo just put out an excellent video which goes through all this in considerable detail, and will do a much better job of explaining it than I am capable of. EDIT: Here is a timestamp of when he shows combining multiple traits link to this video and here is a timestamp of when he shows why Clone (and also Copy as in your requirement) are not object-safe traits link to this video

u/charlesdartin r/rust • Recommends

On “Hey Rustaceans! Got an easy question? Ask here (20/2021)!”

If you want the longer explanation here's an excellent two hour video (would not recommend if you're just starting out with rust) link to this video

u/vzwGreyin r/rust • Mentions

On “How to get a trait object for one auto-trait and one trait”

In general when you want to combine traits in a trait object you'll want to make a new trait that depends on the trait you want, like so: trait Trait1And2: Trait1 + Trait2 {} fn foo(v: Box<dyn Trait1And2>) { /* ... */ } For auto traits you can just use the `+` operator. For example for `Send`: fn foo(v: Box<dyn Trait1 + Send>) { /* ... */ } However, `Clone` is not actually Object Safe because the `Clone::clone` method returns `Self` and thus cannot be used in trait objects. If you made a new trait `Trait3` that depended on `Clone` `Trait3` would end up being not Object Safe as well. Jon Gjengset recently covered this in his livestream about trait objects as well. Theoreti…
15View on Reddit