Guest User

Untitled

a guest
Jul 21st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. use std::fmt::Debug;
  2.  
  3. pub trait THeader: Debug {
  4. fn m(&self) {
  5. println!("hello");
  6. }
  7.  
  8. fn get_comparator(&self) -> &str;
  9. }
  10. #[derive(Clone, Copy, Debug, PartialEq, Eq)]
  11. struct THeader1;
  12. #[derive(Clone, Copy, Debug, PartialEq, Eq)]
  13. struct THeader2;
  14.  
  15. impl THeader for THeader1
  16. {
  17. fn get_comparator(&self) -> &str {
  18. "header1"
  19. }
  20. }
  21. impl THeader for THeader2 {
  22. fn get_comparator(&self) -> &str {
  23. "header2"
  24. }
  25. }
  26.  
  27.  
  28. impl PartialEq<Box<THeader>> for Box<THeader> {
  29. fn eq(&self, other: &Box<THeader>) -> bool {
  30. self.get_comparator() == other.get_comparator()
  31. }
  32. }
  33.  
  34. #[derive(Debug)]
  35. pub enum Header {
  36. H(Box<THeader>),
  37. }
  38.  
  39.  
  40. impl PartialEq<Header> for Header {
  41. fn eq(&self, other: &Header) -> bool {
  42. match self {
  43. Header::H(a) => match other {
  44. Header::H(b) => a.get_comparator() == b.get_comparator(),
  45. _ => false
  46. },
  47. _ => false
  48. }
  49. }
  50. }
  51.  
  52. fn main()
  53. {
  54. let h1 = Header::H(Box::new(THeader1{}));
  55. let h2 = Header::H(Box::new(THeader2{}));
  56. if h1 == h2 {
  57. println!("eq");
  58. }
  59. }
Add Comment
Please, Sign In to add comment