Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. //////////////////////////////////////////
  2. pub struct Rec<'a> {
  3. pub s: Option<&'a Box<Trait1>>
  4. }
  5.  
  6. impl<'a> Rec<'a> {
  7. pub fn new() -> Rec<'a> {
  8. Rec{s: None}
  9. }
  10. }
  11.  
  12. pub trait Trait2<'a> {
  13. fn function_2(&self, rec: &'a mut Rec) -> bool;
  14. }
  15.  
  16. /////////////////////////////////////////
  17. pub struct Data<'b> {
  18. m: &'b Box<Trait1>
  19. }
  20.  
  21. impl<'b> Data<'b> {
  22. pub fn new(m: &'b Box<Trait1>) -> Data {
  23. Data { m: m }
  24. }
  25. }
  26.  
  27. impl<'c> Trait2<'c> for Data<'c> {
  28. fn function_2(&self, r: &'c mut Rec) -> bool {
  29. r.s = Some(self.m);
  30. true
  31. }
  32. }
  33.  
  34. /////////////////////////////////////////
  35. pub struct Struct {
  36. a: i32
  37. }
  38.  
  39. impl Struct {
  40. pub fn new(a: i32) -> Struct {
  41. Struct{a: a}
  42. }
  43. }
  44.  
  45. impl Trait1 for Struct {
  46. fn function_1(&self, rec: &Rec) -> bool {
  47. true
  48. }
  49. }
  50.  
  51. ///////////////////////////////////////////
  52. pub struct Struct1 {
  53.  
  54. }
  55.  
  56. impl Struct1 {
  57. pub fn get_struct1(a: i32) -> Box<Trait1> {
  58. let struc = Struct::new(a);
  59. Box::new(struc)
  60. }
  61. }
  62.  
  63. pub trait Trait1 {
  64. fn function_1(&self, rec: &Rec) -> bool;
  65. }
  66.  
  67. ///////////////////////////////////////////
  68. pub struct Exec {
  69. e: i32
  70. }
  71.  
  72. impl Exec {
  73. pub fn new(e: i32) -> Exec {
  74. Exec {e: e}
  75. }
  76.  
  77. pub fn function_exec(&self, w: &Data) -> bool {
  78. let mut rec: Rec = Rec::new();
  79.  
  80. w.function_2(&mut rec)
  81. }
  82. }
  83.  
  84.  
  85. //////////////////////////////////////////
  86. fn main() {
  87. let exec = Exec::new(1);
  88.  
  89. let s_1 = Struct1::get_struct1(2);
  90. let data : Data = Data::new(&s_1);
  91.  
  92. exec.function_exec(&data);
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement