1 // Rust-101, Part 11: Trait Objects, Box, Lifetime bounds
2 // ======================================================
4 //@ We will play around with closures a bit more. Let us implement some kind of generic "callback"
5 //@ mechanism, providing two functions: Registering a new callback, and calling all registered
8 //@ First of all, we need to find a way to store the callbacks. Clearly, there will be a `Vec`
9 //@ involved, so that we can always grow the number of registered callbacks. A callback will be a
10 //@ closure, i.e., something implementing `FnMut(i32)` (we want to call this multiple times, so
11 //@ clearly `FnOnce` would be no good). So our first attempt may be the following.
12 // For now, we just decide that the callbacks have an argument of type `i32`.
13 struct CallbacksV1<F: FnMut(i32)> {
16 //@ However, this will not work. Remember how the "type" of a closure is specific to the
17 //@ environment of captured variables. Different closures all implementing `FnMut(i32)` may have
18 //@ different types. However, a `Vec<F>` is a *uniformly typed* vector.
20 //@ We will thus need a way to store things of *different* types in the same vector. We know all
21 //@ these types implement `FnMut(i32)`. For this scenario, Rust provides *trait objects*: The truth
22 //@ is, `FnMut(i32)` is not just a trait. It is also a type, that can be given to anything
23 //@ implementing this trait. So, we may write the following.
24 /* struct CallbacksV2 {
25 callbacks: Vec<FnMut(i32)>,
27 //@ But, Rust complains about this definition. It says something about "Sized". What's the trouble?
28 //@ See, for many things we want to do, it is crucial that Rust knows the precise, fixed size of
29 //@ the type - that is, how large this type will be when represented in memory. For example, for a
30 //@ `Vec`, the elements are stored one right after the other. How should that be possible, without
31 //@ a fixed size? The point is, `FnMut(i32)` could be of any size. We don't know how large that
32 //@ "type that implements `FnMut(i32)`" is. Rust calls this an *unsized* type.
33 //@ Whenever we introduce a type variable, Rust will implicitly add a bound to that variable,
34 //@ demanding that it is sized. That's why we did not have to worry about this so far. <br/> You
35 //@ can opt-out of this implicit bound by saying `T: ?Sized`. Then `T` may or may not be sized.
37 //@ So, what can we do, if we can't store the callbacks in a vector? We can put them in a box.
38 //@ Semantically, `Box<T>` is a lot like `T`: You fully own the data stored there. On the machine,
39 //@ however, `Box<T>` is a *pointer* to a heap-allocated `T`. It is a lot like `std::unique_ptr` in
40 //@ C++. In our current example, the important bit is that since it's a pointer, `T` can be
41 //@ unsized, but `Box<T>` itself will always be sized. So we can put it in a `Vec`.
42 pub struct Callbacks {
43 callbacks: Vec<Box<FnMut(i32)>>,
47 // Now we can provide some functions. The constructor should be straight-forward.
48 pub fn new() -> Self {
49 Callbacks { callbacks: Vec::new() } /*@*/
52 // Registration simply stores the callback.
53 pub fn register(&mut self, callback: Box<FnMut(i32)>) {
54 self.callbacks.push(callback);
57 // We can also write a generic version of `register`, such that it will be instantiated with
58 // some concrete closure type `F` and do the creation of the `Box` and the conversion from `F`
59 // to `FnMut(i32)` itself.
61 //@ For this to work, we need to demand that the type `F` does not contain any short-lived
62 //@ references. After all, we will store it in our list of callbacks indefinitely. If the
63 //@ closure contained a pointer to our caller's stackframe, that pointer could be invalid by
64 //@ the time the closure is called. We can mitigate this by bounding `F` by a *lifetime*: `F:
65 //@ 'a` says that all data of type `F` will *outlive* (i.e., will be valid for at least as long
66 //@ as) lifetime `'a`.
67 //@ Here, we use the special lifetime `'static`, which is the lifetime of the entire program.
68 //@ The same bound has been implicitly added in the version of `register` above, and in the
69 //@ definition of `Callbacks`.
70 pub fn register_generic<F: FnMut(i32)+'static>(&mut self, callback: F) {
71 self.callbacks.push(Box::new(callback)); /*@*/
74 // And here we call all the stored callbacks.
75 pub fn call(&mut self, val: i32) {
76 // Since they are of type `FnMut`, we need to mutably iterate.
77 for callback in self.callbacks.iter_mut() {
78 //@ Here, `callback` has type `&mut Box<FnMut(i32)>`. We can make use of the fact that
79 //@ `Box` is a *smart pointer*: In particular, we can use it as if it were a normal
80 //@ reference, and use `*` to get to its contents. Then we obtain a mutable reference
81 //@ to these contents, because we call a `FnMut`.
82 (&mut *callback)(val); /*@*/
83 //@ Just like it is the case with normal references, this typically happens implicitly
84 //@ with smart pointers, so we can also directly call the function.
85 //@ Try removing the `&mut *`.
87 //@ The difference to a reference is that `Box` implies full ownership: Once you drop
88 //@ the box (i.e., when the entire `Callbacks` instance is dropped), the content it
89 //@ points to on the heap will be deleted.
95 // Now we are ready for the demo. Remember to edit `main.rs` to run it.
97 let mut c = Callbacks::new();
98 c.register(Box::new(|val| println!("Callback 1: {}", val)));
102 //@ We can even register callbacks that modify their environment. Per default, Rust will
103 //@ attempt to capture a reference to `count`, to borrow it. However, that doesn't work out
104 //@ this time. Remember the `'static` bound above? Borrowing `count` in the environment
105 //@ would violate that bound, as the reference is only valid for this block. If the
106 //@ callbacks are triggered later, we'd be in trouble.
107 //@ We have to explicitly tell Rust to `move` ownership of the variable into the closure.
108 //@ Its environment will then contain a `usize` rather than a `&mut usize`, and the closure
109 //@ has no effect on this local variable anymore.
110 let mut count: usize = 0;
111 c.register_generic(move |val| {
113 println!("Callback 2: {} ({}. time)", val, count);
116 c.call(1); c.call(2);
119 //@ ## Run-time behavior
120 //@ When you run the program above, how does Rust know what to do with the callbacks? Since an
121 //@ unsized type lacks some information, a *pointer* to such a type (be it a `Box` or a reference)
122 //@ will need to complete this information. We say that pointers to trait objects are *fat*. They
123 //@ store not only the address of the object, but (in the case of trait objects) also a *vtable*: A
124 //@ table of function pointers, determining the code that's run when a trait method is called.
125 //@ There are some restrictions for traits to be usable as trait objects. This is called *object
126 //@ safety* and described in [the documentation](https://doc.rust-lang.org/stable/book/trait-
127 //@ objects.html) and [the reference](https://doc.rust-lang.org/reference.html#trait-objects).
128 //@ In case of the `FnMut` trait, there's only a single action to be performed: Calling the
129 //@ closure. You can thus think of a pointer to `FnMut` as a pointer to the code, and a pointer to
130 //@ the environment. This is how Rust recovers the typical encoding of closures as a special case
131 //@ of a more general concept.
133 //@ Whenever you write a generic function, you have a choice: You can make it generic, like
134 //@ `register_generic`. Or you can use trait objects, like `register`. The latter will result in
135 //@ only a single compiled version (rather than one version per type it is instantiated with). This
136 //@ makes for smaller code, but you pay the overhead of the virtual function calls. (Of course, in
137 //@ the case of `register` above, there's no function called on the trait object.)
138 //@ Isn't it beautiful how traits can nicely handle this tradeoff (and much more, as we saw, like
139 //@ closures and operator overloading)?
141 // **Exercise 11.1**: We made the arbitrary choice of using `i32` for the arguments. Generalize the
142 // data structures above to work with an arbitrary type `T` that's passed to the callbacks. Since
143 // you need to call multiple callbacks with the same `val: T` (in our `call` function), you will
144 // either have to restrict `T` to `Copy` types, or pass a reference.
146 //@ [index](main.html) | [previous](part10.html) | [raw source](workspace/src/part11.rs) |
147 //@ [next](part12.html)