Guest User

Untitled

a guest
Aug 20th, 2019
66
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extern crate bytes; // 0.4.12
  2. use bytes::BufMut;
  3.  
  4. #[derive(Debug)]
  5. struct Message {
  6. id: u32,
  7. body: Body,
  8. }
  9.  
  10. #[derive(Debug)]
  11. enum Body {
  12. SendMessage { inner: SendMessage },
  13. ReceiveMessage { inner: ReceiveMessage },
  14. }
  15.  
  16. #[derive(Debug)]
  17. struct ReceiveMessage {
  18. from: String,
  19. message: String
  20. }
  21.  
  22. #[derive(Debug)]
  23. struct SendMessage {
  24. to: String,
  25. message: String
  26. }
  27.  
  28. fn main() {
  29. let m = Message {
  30. id: 1,
  31. body: Body::SendMessage { inner: SendMessage {
  32. to: "#rust".to_string(),
  33. message: "help".to_string(),
  34. }},
  35. };
  36.  
  37. println!("{:#?}", m);
  38.  
  39. // This seems super awkward, is there a way of not listing each attribute?
  40. match m.body {
  41. Body::SendMessage{inner} => println!("{} please {}", inner.to, inner.message),
  42. _ => ()
  43. };
  44.  
  45. // Is there some way I can get this behaviour? I don't really want to
  46. // implement IntoBuf for every variant if I can help it?
  47. let b = bytes::BytesMut::new();
  48. //b.put(m);
  49. }
RAW Paste Data