3 When code involves polymorphism, there needs to be a mechanism to determine
4 which specific version is actually run. This is called ‘dispatch’. There are
5 two major forms of dispatch: static dispatch and dynamic dispatch. While Rust
6 favors static dispatch, it also supports dynamic dispatch through a mechanism
7 called ‘trait objects’.
11 For the rest of this chapter, we’ll need a trait and some implementations.
12 Let’s make a simple one, `Foo`. It has one method that is expected to return a
17 fn method(&self) -> String;
21 We’ll also implement this trait for `u8` and `String`:
24 # trait Foo { fn method(&self) -> String; }
26 fn method(&self) -> String { format!("u8: {}", *self) }
30 fn method(&self) -> String { format!("string: {}", *self) }
37 We can use this trait to perform static dispatch with trait bounds:
40 # trait Foo { fn method(&self) -> String; }
41 # impl Foo for u8 { fn method(&self) -> String { format!("u8: {}", *self) } }
42 # impl Foo for String { fn method(&self) -> String { format!("string: {}", *self) } }
43 fn do_something<T: Foo>(x: T) {
49 let y = "Hello".to_string();
56 Rust uses ‘monomorphization’ to perform static dispatch here. This means that
57 Rust will create a special version of `do_something()` for both `u8` and
58 `String`, and then replace the call sites with calls to these specialized
59 functions. In other words, Rust generates something like this:
62 # trait Foo { fn method(&self) -> String; }
63 # impl Foo for u8 { fn method(&self) -> String { format!("u8: {}", *self) } }
64 # impl Foo for String { fn method(&self) -> String { format!("string: {}", *self) } }
65 fn do_something_u8(x: u8) {
69 fn do_something_string(x: String) {
75 let y = "Hello".to_string();
78 do_something_string(y);
82 This has a great upside: static dispatch allows function calls to be
83 inlined because the callee is known at compile time, and inlining is
84 the key to good optimization. Static dispatch is fast, but it comes at
85 a tradeoff: ‘code bloat’, due to many copies of the same function
86 existing in the binary, one for each type.
88 Furthermore, compilers aren’t perfect and may “optimize” code to become slower.
89 For example, functions inlined too eagerly will bloat the instruction cache
90 (cache rules everything around us). This is part of the reason that `#[inline]`
91 and `#[inline(always)]` should be used carefully, and one reason why using a
92 dynamic dispatch is sometimes more efficient.
94 However, the common case is that it is more efficient to use static dispatch,
95 and one can always have a thin statically-dispatched wrapper function that does
96 a dynamic dispatch, but not vice versa, meaning static calls are more flexible.
97 The standard library tries to be statically dispatched where possible for this
102 Rust provides dynamic dispatch through a feature called ‘trait objects’. Trait
103 objects, like `&Foo` or `Box<Foo>`, are normal values that store a value of
104 *any* type that implements the given trait, where the precise type can only be
107 A trait object can be obtained from a pointer to a concrete type that
108 implements the trait by *casting* it (e.g. `&x as &Foo`) or *coercing* it
109 (e.g. using `&x` as an argument to a function that takes `&Foo`).
111 These trait object coercions and casts also work for pointers like `&mut T` to
112 `&mut Foo` and `Box<T>` to `Box<Foo>`, but that’s all at the moment. Coercions
113 and casts are identical.
115 This operation can be seen as ‘erasing’ the compiler’s knowledge about the
116 specific type of the pointer, and hence trait objects are sometimes referred to
119 Coming back to the example above, we can use the same trait to perform dynamic
120 dispatch with trait objects by casting:
123 # trait Foo { fn method(&self) -> String; }
124 # impl Foo for u8 { fn method(&self) -> String { format!("u8: {}", *self) } }
125 # impl Foo for String { fn method(&self) -> String { format!("string: {}", *self) } }
126 fn do_something(x: &Foo) {
132 do_something(&x as &Foo);
139 # trait Foo { fn method(&self) -> String; }
140 # impl Foo for u8 { fn method(&self) -> String { format!("u8: {}", *self) } }
141 # impl Foo for String { fn method(&self) -> String { format!("string: {}", *self) } }
142 fn do_something(x: &Foo) {
147 let x = "Hello".to_string();
152 A function that takes a trait object is not specialized to each of the types
153 that implements `Foo`: only one copy is generated, often (but not always)
154 resulting in less code bloat. However, this comes at the cost of requiring
155 slower virtual function calls, and effectively inhibiting any chance of
156 inlining and related optimizations from occurring.
160 Rust does not put things behind a pointer by default, unlike many managed
161 languages, so types can have different sizes. Knowing the size of the value at
162 compile time is important for things like passing it as an argument to a
163 function, moving it about on the stack and allocating (and deallocating) space
164 on the heap to store it.
166 For `Foo`, we would need to have a value that could be at least either a
167 `String` (24 bytes) or a `u8` (1 byte), as well as any other type for which
168 dependent crates may implement `Foo` (any number of bytes at all). There’s no
169 way to guarantee that this last point can work if the values are stored without
170 a pointer, because those other types can be arbitrarily large.
172 Putting the value behind a pointer means the size of the value is not relevant
173 when we are tossing a trait object around, only the size of the pointer itself.
177 The methods of the trait can be called on a trait object via a special record
178 of function pointers traditionally called a ‘vtable’ (created and managed by
181 Trait objects are both simple and complicated: their core representation and
182 layout is quite straight-forward, but there are some curly error messages and
183 surprising behaviors to discover.
185 Let’s start simple, with the runtime representation of a trait object. The
186 `std::raw` module contains structs with layouts that are the same as the
187 complicated built-in types, [including trait objects][stdraw]:
191 pub struct TraitObject {
198 [stdraw]: ../std/raw/struct.TraitObject.html
200 That is, a trait object like `&Foo` consists of a ‘data’ pointer and a ‘vtable’
203 The data pointer addresses the data (of some unknown type `T`) that the trait
204 object is storing, and the vtable pointer points to the vtable (‘virtual method
205 table’) corresponding to the implementation of `Foo` for `T`.
208 A vtable is essentially a struct of function pointers, pointing to the concrete
209 piece of machine code for each method in the implementation. A method call like
210 `trait_object.method()` will retrieve the correct pointer out of the vtable and
211 then do a dynamic call of it. For example:
215 destructor: fn(*mut ()),
218 method: fn(*const ()) -> String,
223 fn call_method_on_u8(x: *const ()) -> String {
224 // the compiler guarantees that this function is only called
225 // with `x` pointing to a u8
226 let byte: &u8 = unsafe { &*(x as *const u8) };
231 static Foo_for_u8_vtable: FooVtable = FooVtable {
232 destructor: /* compiler magic */,
236 // cast to a function pointer
237 method: call_method_on_u8 as fn(*const ()) -> String,
243 fn call_method_on_String(x: *const ()) -> String {
244 // the compiler guarantees that this function is only called
245 // with `x` pointing to a String
246 let string: &String = unsafe { &*(x as *const String) };
251 static Foo_for_String_vtable: FooVtable = FooVtable {
252 destructor: /* compiler magic */,
253 // values for a 64-bit computer, halve them for 32-bit ones
257 method: call_method_on_String as fn(*const ()) -> String,
261 The `destructor` field in each vtable points to a function that will clean up
262 any resources of the vtable’s type: for `u8` it is trivial, but for `String` it
263 will free the memory. This is necessary for owning trait objects like
264 `Box<Foo>`, which need to clean-up both the `Box` allocation as well as the
265 internal type when they go out of scope. The `size` and `align` fields store
266 the size of the erased type, and its alignment requirements; these are
267 essentially unused at the moment since the information is embedded in the
268 destructor, but will be used in the future, as trait objects are progressively
271 Suppose we’ve got some values that implement `Foo`. The explicit form of
272 construction and use of `Foo` trait objects might look a bit like (ignoring the
273 type mismatches: they’re all pointers anyway):
276 let a: String = "foo".to_string();
280 let b = TraitObject {
284 vtable: &Foo_for_String_vtable
288 let y = TraitObject {
292 vtable: &Foo_for_u8_vtable
296 (b.vtable.method)(b.data);
299 (y.vtable.method)(y.data);
304 Not every trait can be used to make a trait object. For example, vectors implement
305 `Clone`, but if we try to make a trait object:
308 let v = vec![1, 2, 3];
309 let o = &v as &Clone;
315 error: cannot convert to a trait object because trait `core::clone::Clone` is not object-safe [E0038]
316 let o = &v as &Clone;
318 note: the trait cannot require that `Self : Sized`
319 let o = &v as &Clone;
323 The error says that `Clone` is not ‘object-safe’. Only traits that are
324 object-safe can be made into trait objects. A trait is object-safe if both of
327 * the trait does not require that `Self: Sized`
328 * all of its methods are object-safe
330 So what makes a method object-safe? Each method must require that `Self: Sized`
331 or all of the following:
333 * must not have any type parameters
334 * must not use `Self`
336 Whew! As we can see, almost all of these rules talk about `Self`. A good intuition
337 is “except in special circumstances, if your trait’s method uses `Self`, it is not