channel.rs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. macro_rules! implement_sender {
  2. (name => $name:ident,
  3. wrap => $wrap_type:ident,
  4. with => $with_type:ident,
  5. variant => $variant:ident) => {
  6. pub struct $name {
  7. wrapped_sender: ::std::sync::mpsc::Sender<$with_type>,
  8. }
  9. impl $name {
  10. pub fn create(sender: ::std::sync::mpsc::Sender<$with_type>) -> $name {
  11. $name {
  12. wrapped_sender: sender
  13. }
  14. }
  15. pub fn send(&self, t: $wrap_type) -> Result<(), ::std::sync::mpsc::SendError<$wrap_type>> {
  16. let wrapped = self.wrap(t);
  17. let result = self.wrapped_sender.send(wrapped);
  18. result.map_err(|senderror| {
  19. let ::std::sync::mpsc::SendError(z) = senderror;
  20. ::std::sync::mpsc::SendError(self.unwrap(z))
  21. })
  22. }
  23. fn wrap(&self, d: $wrap_type) -> $with_type {
  24. $with_type::$variant(d)
  25. }
  26. fn unwrap(&self, msg: $with_type) -> $wrap_type {
  27. let d = match msg {
  28. $with_type::$variant(d) => d,
  29. _ => unreachable!()
  30. };
  31. d
  32. }
  33. }
  34. impl Clone for $name {
  35. fn clone(&self) -> $name {
  36. $name {
  37. wrapped_sender: self.wrapped_sender.clone()
  38. }
  39. }
  40. }
  41. }
  42. }