cc2a2524dcc0233c77cbb1ac7e0cd6899223d807
[rust-101.git] / workspace / src / part11.rs
1 // Rust-101, Part 11: Trait Objects, Box, Rc, Lifetime bounds
2 // ==========================================================
3
4 mod callbacks {
5     // For now, we just decide that the callbacks have an argument of type `i32`.
6     struct CallbacksV1<F: FnMut(i32)> {
7         callbacks: Vec<F>,
8     }
9
10     /* struct CallbacksV2 {
11         callbacks: Vec<FnMut(i32)>,
12     } */
13
14     pub struct Callbacks {
15         callbacks: Vec<Box<FnMut(i32)>>,
16     }
17
18     impl Callbacks {
19         // Now we can provide some functions. The constructor should be straight-forward.
20         pub fn new() -> Self {
21             unimplemented!()
22         }
23
24         // Registration simply stores the callback.
25         pub fn register(&mut self, callback: Box<FnMut(i32)>) {
26             unimplemented!()
27         }
28
29         // And here we call all the stored callbacks.
30         pub fn call(&mut self, val: i32) {
31             // Since they are of type `FnMut`, we need to mutably iterate. Notice that boxes dereference implicitly.
32             for callback in self.callbacks.iter_mut() {
33                 unimplemented!()
34             }
35         }
36     }
37
38     // Now we are ready for the demo.
39     pub fn demo(c: &mut Callbacks) {
40         c.register(Box::new(|val| println!("Callback 1: {}", val)));
41         c.call(0);
42
43         let mut count: usize = 0;
44         c.register(Box::new(move |val| {
45             count = count+1;
46             println!("Callback 2, {}. time: {}", count, val);
47         } ));
48         c.call(1); c.call(2);
49     }
50 }
51
52 // Remember to edit `main.rs` to run the demo.
53 pub fn main() {
54     let mut c = callbacks::Callbacks::new();
55     callbacks::demo(&mut c);
56 }
57
58 mod callbacks_clone {
59
60     use std::rc::Rc;
61
62     #[derive(Clone)]
63     pub struct Callbacks {
64         callbacks: Vec<Rc<Fn(i32)>>,
65     }
66
67     impl Callbacks {
68         pub fn new() -> Self {
69             unimplemented!()
70         }
71
72         // For the `register` function, we don't actually have to use trait objects in the argument.
73         
74         pub fn register<F: Fn(i32)+'static>(&mut self, callback: F) {
75             unimplemented!()
76         }
77
78         pub fn call(&mut self, val: i32) {
79             // We only need a shared iterator here. `Rc` also implicitly dereferences, so we can simply call the callback.
80             for callback in self.callbacks.iter() {
81                 unimplemented!()
82             }
83         }
84     }
85
86     // The demo works just as above. Our counting callback doesn't work anymore though, because we are using `Fn` now.
87     fn demo(c: &mut Callbacks) {
88         c.register(|val| println!("Callback 1: {}", val));
89         c.call(0); c.call(1);
90     }
91 }
92
93 // **Exercise 11.1**: We made the arbitrary choice of using `i32` for the arguments. Generalize the data-structures above
94 // to work with an arbitrary type `T` that's passed to the callbacks. Since you need to call multiple callbacks with the
95 // same `t: T`, you will either have to restrict `T` to `Copy` types, or pass a borrow.
96
97