Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. #![deny(
  2. bare_trait_objects,
  3. elided_lifetimes_in_paths,
  4. )]
  5.  
  6. //////////////////////////////////////////
  7. #[derive(Default)]
  8. pub
  9. struct Rec<'a> {
  10. pub
  11. s: Option<&'a dyn Trait1>
  12. }
  13.  
  14. impl<'a> Rec<'a> {
  15. pub
  16. fn new () -> Rec<'a>
  17. {
  18. Rec::default()
  19. }
  20. }
  21.  
  22. pub
  23. trait Trait2<'a> {
  24. fn function_2 (self: &'_ Self, rec: &'_ mut Rec<'a>) -> bool;
  25. }
  26.  
  27. /////////////////////////////////////////
  28. pub
  29. struct Data<'a> {
  30. m: &'a dyn Trait1,
  31. }
  32.  
  33. impl<'a> Data<'a> {
  34. pub
  35. fn new (m: &'a dyn Trait1) -> Data<'a>
  36. {
  37. Data {
  38. m /*: m*/,
  39. }
  40. }
  41. }
  42.  
  43. impl<'a> Trait2<'a> for Data<'a> {
  44. fn function_2 (self: &'_ Self, r: &'_ mut Rec<'a>) -> bool
  45. {
  46. r.s = Some(self.m);
  47. true
  48. }
  49. }
  50.  
  51. /////////////////////////////////////////
  52. pub
  53. struct Struct {
  54. a: i32
  55. }
  56.  
  57. impl Struct {
  58. pub
  59. fn new (a: i32) -> Struct
  60. {
  61. Struct{a: a}
  62. }
  63. }
  64.  
  65. impl Trait1 for Struct {
  66. fn function_1 (self: &'_ Self, rec: &'_ Rec<'_>) -> bool
  67. {
  68. true
  69. }
  70. }
  71.  
  72. ///////////////////////////////////////////
  73. pub
  74. struct Struct1 {
  75.  
  76. }
  77.  
  78. impl Struct1 {
  79. pub
  80. fn get_struct1 (a: i32) -> Box<dyn Trait1>
  81. {
  82. let struc = Struct::new(a);
  83. Box::new(struc)
  84. }
  85. }
  86.  
  87. pub
  88. trait Trait1 {
  89. fn function_1 (self: &'_ Self, rec: &'_ Rec<'_>) -> bool;
  90. }
  91.  
  92. ///////////////////////////////////////////
  93. pub
  94. struct Exec {
  95. e: i32
  96. }
  97.  
  98. impl Exec {
  99. pub
  100. fn new (e: i32) -> Exec
  101. {
  102. Exec {e: e}
  103. }
  104.  
  105. pub
  106. fn function_exec (self: &'_ Self, w: &'_ Data<'_>) -> bool
  107. {
  108. let mut rec: Rec<'_> = Rec::new();
  109.  
  110. w.function_2(&mut rec)
  111. }
  112. }
  113.  
  114.  
  115. //////////////////////////////////////////
  116. fn main ()
  117. {
  118. let exec = Exec::new(1);
  119.  
  120. let s_1 = Struct1::get_struct1(2);
  121. let data: Data<'_> = Data::new(&*s_1); // get rid of one indirection
  122.  
  123. exec.function_exec(&data);
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement