projects
/
rust-101.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
add some structure to the list of parts
[rust-101.git]
/
workspace
/
src
/
part11.rs
diff --git
a/workspace/src/part11.rs
b/workspace/src/part11.rs
index f868a3c05394548317b4f36e3bb71e863539efa0..cc2a2524dcc0233c77cbb1ac7e0cd6899223d807 100644
(file)
--- a/
workspace/src/part11.rs
+++ b/
workspace/src/part11.rs
@@
-1,9
+1,8
@@
-// Rust-101, Part 11: Trait Objects, Box (WIP)
-// ===========================================
-
+// Rust-101, Part 11: Trait Objects, Box, Rc, Lifetime bounds
+// ==========================================================
mod callbacks {
mod callbacks {
- // For now, we just decide that the callba
kc
s have an argument of type `i32`.
+ // For now, we just decide that the callba
ck
s have an argument of type `i32`.
struct CallbacksV1<F: FnMut(i32)> {
callbacks: Vec<F>,
}
struct CallbacksV1<F: FnMut(i32)> {
callbacks: Vec<F>,
}
@@
-12,23
+11,23
@@
mod callbacks {
callbacks: Vec<FnMut(i32)>,
} */
callbacks: Vec<FnMut(i32)>,
} */
- struct Callbacks {
+
pub
struct Callbacks {
callbacks: Vec<Box<FnMut(i32)>>,
}
impl Callbacks {
// Now we can provide some functions. The constructor should be straight-forward.
callbacks: Vec<Box<FnMut(i32)>>,
}
impl Callbacks {
// Now we can provide some functions. The constructor should be straight-forward.
- fn new() -> Self {
+
pub
fn new() -> Self {
unimplemented!()
}
// Registration simply stores the callback.
unimplemented!()
}
// Registration simply stores the callback.
- fn register(&mut self, callback: Box<FnMut(i32)>) {
+
pub
fn register(&mut self, callback: Box<FnMut(i32)>) {
unimplemented!()
}
// And here we call all the stored callbacks.
unimplemented!()
}
// And here we call all the stored callbacks.
- fn call(&mut self, val: i32) {
+
pub
fn call(&mut self, val: i32) {
// Since they are of type `FnMut`, we need to mutably iterate. Notice that boxes dereference implicitly.
for callback in self.callbacks.iter_mut() {
unimplemented!()
// Since they are of type `FnMut`, we need to mutably iterate. Notice that boxes dereference implicitly.
for callback in self.callbacks.iter_mut() {
unimplemented!()
@@
-36,47
+35,48
@@
mod callbacks {
}
}
}
}
- // Now we are read for the demo.
- pub fn demo() {
- let mut c = Callbacks::new();
+ // Now we are ready for the demo.
+ pub fn demo(c: &mut Callbacks) {
c.register(Box::new(|val| println!("Callback 1: {}", val)));
c.register(Box::new(|val| println!("Callback 1: {}", val)));
-
c.call(0);
let mut count: usize = 0;
c.call(0);
let mut count: usize = 0;
- c.register(Box::new(move |val| { count = count+1; println!("Callback 2, {}. time: {}", count, val); } ));
- c.call(1);
- c.call(2);
+ c.register(Box::new(move |val| {
+ count = count+1;
+ println!("Callback 2, {}. time: {}", count, val);
+ } ));
+ c.call(1); c.call(2);
}
}
-
}
// Remember to edit `main.rs` to run the demo.
pub fn main() {
}
// Remember to edit `main.rs` to run the demo.
pub fn main() {
- callbacks::demo();
+ let mut c = callbacks::Callbacks::new();
+ callbacks::demo(&mut c);
}
mod callbacks_clone {
}
mod callbacks_clone {
- use std::rc;
+ use std::rc
::Rc
;
#[derive(Clone)]
#[derive(Clone)]
- struct Callbacks {
- callbacks: Vec<
rc::
Rc<Fn(i32)>>,
+
pub
struct Callbacks {
+ callbacks: Vec<Rc<Fn(i32)>>,
}
}
- // The methods on these clonable callbacks are just like the ones above.
impl Callbacks {
impl Callbacks {
- fn new() -> Self {
+
pub
fn new() -> Self {
unimplemented!()
}
unimplemented!()
}
- fn register(&mut self, callback: rc::Rc<Fn(i32)>) {
+ // For the `register` function, we don't actually have to use trait objects in the argument.
+
+ pub fn register<F: Fn(i32)+'static>(&mut self, callback: F) {
unimplemented!()
}
unimplemented!()
}
- fn call(&mut self, val: i32) {
- // We only need a shared iterator here. `Rc` also implicitly dereferences, so we can
just
call the callback.
+
pub
fn call(&mut self, val: i32) {
+ // We only need a shared iterator here. `Rc` also implicitly dereferences, so we can
simply
call the callback.
for callback in self.callbacks.iter() {
unimplemented!()
}
for callback in self.callbacks.iter() {
unimplemented!()
}
@@
-84,12
+84,9
@@
mod callbacks_clone {
}
// The demo works just as above. Our counting callback doesn't work anymore though, because we are using `Fn` now.
}
// The demo works just as above. Our counting callback doesn't work anymore though, because we are using `Fn` now.
- fn demo() {
- let mut c = Callbacks::new();
- c.register(rc::Rc::new(|val| println!("Callback 1: {}", val)));
-
- c.call(0);
- c.call(1);
+ fn demo(c: &mut Callbacks) {
+ c.register(|val| println!("Callback 1: {}", val));
+ c.call(0); c.call(1);
}
}
}
}
@@
-97,3
+94,4
@@
mod callbacks_clone {
// 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.
// 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.
+