Advertisement
Guest User

Untitled

a guest
May 30th, 2015
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. #![feature(collections)]
  2. extern crate eventual;
  3.  
  4. use eventual::*;
  5.  
  6. #[derive(Debug)]
  7. struct Foo;
  8.  
  9. struct A<'a> {
  10. on_foo_handlers: Vec<Sender<(&'a mut A<'a>, &'a Foo), &'static str>>
  11. }
  12.  
  13. impl<'a> A<'a> {
  14. fn on_foo(&mut self) -> Stream<(&'a mut A<'a>, &'a Foo), &'static str> {
  15. let (sender, receiver) = Stream::pair();
  16. self.on_foo_handlers.push(sender);
  17. receiver
  18. }
  19.  
  20. fn bar(&mut self) {
  21. // something
  22. }
  23.  
  24. fn listen(mut self) {
  25. loop {
  26. // be busy
  27. self.on_foo_handlers = self.on_foo_handlers.map_in_place(|s| {
  28. s.send((&mut self, &Foo)).await().unwrap()
  29. });
  30. }
  31. }
  32. }
  33.  
  34. fn main() {
  35. let mut a = A { on_foo_handlers: Vec::new() };
  36. a.on_foo().each(|(s, foo)| println!("{:?}, {:?}", s.bar(), foo)).fire();
  37. a.listen();
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement