Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. impl<U: Send + 'static> CountLogger<U> {
  2. pub fn count(&mut self) {
  3. self.count += 1;
  4. println!("[COUNT] - {}", self.count);
  5. }
  6. }
  7. pub enum CountLoggerMessage {
  8. count {},
  9. }
  10. impl<U: Send + 'static> CountLogger<U> {
  11. pub fn route_message(&mut self, msg: CountLoggerMessage) {
  12. match msg {
  13. CountLoggerMessage::count {} => self.count(),
  14. };
  15. }
  16. }
  17. #[derive(Clone)]
  18. pub struct CountLoggerActor {
  19. sender: Sender<CountLoggerMessage>,
  20. }
  21. impl CountLoggerActor {
  22. pub fn new<U: Send + 'static>(actor_impl: CountLogger<U>) -> Self {
  23. let (sender, receiver) = channel(0);
  24. let id = "random string".to_owned();
  25. tokio::spawn(CountLoggerRouter {
  26. receiver,
  27. id,
  28. actor_impl,
  29. });
  30. Self { sender }
  31. }
  32. pub fn count(&self) {
  33. let msg = CountLoggerMessage::count {};
  34. tokio::spawn(self.sender.clone().send(msg).map(|_| ()).map_err(|_| ()));
  35. }
  36. }
  37. pub struct CountLoggerRouter<U: Send + 'static> {
  38. receiver: Receiver<CountLoggerMessage>,
  39. id: String,
  40. actor_impl: CountLogger<U>,
  41. }
  42. impl<U: Send + 'static> Future for CountLoggerRouter<U> {
  43. type Item = ();
  44. type Error = ();
  45. fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
  46. match self.receiver.poll() {
  47. Ok(Async::Ready(Some(msg))) => {
  48. task::current().notify();
  49. self.actor_impl.route_message(msg);
  50. Ok(Async::NotReady)
  51. }
  52. Ok(Async::Ready(None)) => {
  53. self.receiver.close();
  54. Ok(Async::Ready(()))
  55. }
  56. _ => Ok(Async::NotReady),
  57. }
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement