// Registration simply stores the callback.
pub fn register(&mut self, callback: Box<FnMut(i32)>) {
- self.callbacks.push(callback); /*@*/
+ self.callbacks.push(callback);
}
// We can also write a generic version of `register`, such that it will be instantiated with some concrete closure type `F`
//@ that doesn't work out this time. Remember the `'static` bound above? Borrowing `count` in the environment would
//@ violate that bound, as the borrow is only valid for this block. If the callbacks are triggered later, we'd be in trouble.
//@ We have to explicitly tell Rust to `move` ownership of the variable into the closure. Its environment will then contain a
- //@ `usize` rather than a `&mut uszie`, and the closure has no effect on this local variable anymore.
+ //@ `usize` rather than a `&mut usize`, and the closure has no effect on this local variable anymore.
let mut count: usize = 0;
c.register_generic(move |val| {
count = count+1;
//@ a *pointer* to such a type (be it a `Box` or a borrow) will need to complete this information. We say that pointers to
//@ trait objects are *fat*. They store not only the address of the object, but (in the case of trait objects) also a *vtable*: A
//@ table of function pointers, determining the code that's run when a trait method is called. There are some restrictions for traits to be usable
-//@ as trait objects. This is called *object safety* and described in [the documentation](http://doc.rust-lang.org/stable/book/trait-objects.html) and [the reference](http://doc.rust-lang.org/reference.html#trait-objects).
+//@ as trait objects. This is called *object safety* and described in [the documentation](https://doc.rust-lang.org/stable/book/trait-objects.html) and [the reference](https://doc.rust-lang.org/reference.html#trait-objects).
//@ In case of the `FnMut` trait, there's only a single action to be performed: Calling the closure. You can thus think of a pointer to `FnMut` as
//@ a pointer to the code, and a pointer to the environment. This is how Rust recovers the typical encoding of closures as a special case of a more
//@ general concept.
// to work with an arbitrary type `T` that's passed to the callbacks. Since you need to call multiple callbacks with the
// same `t: T`, you will either have to restrict `T` to `Copy` types, or pass a borrow.
-//@ [index](main.html) | [previous](part10.html) | [next](part12.html)
+//@ [index](main.html) | [previous](part10.html) | [raw source](https://www.ralfj.de/git/rust-101.git/blob_plain/HEAD:/workspace/src/part11.rs) | [next](part12.html)