Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![deny(
- bare_trait_objects,
- elided_lifetimes_in_paths,
- )]
- //////////////////////////////////////////
- #[derive(Default)]
- pub
- struct Rec<'a> {
- pub
- s: Option<&'a dyn Trait1>
- }
- impl<'a> Rec<'a> {
- pub
- fn new () -> Rec<'a>
- {
- Rec::default()
- }
- }
- pub
- trait Trait2<'a> {
- fn function_2 (self: &'_ Self, rec: &'_ mut Rec<'a>) -> bool;
- }
- /////////////////////////////////////////
- pub
- struct Data<'a> {
- m: &'a dyn Trait1,
- }
- impl<'a> Data<'a> {
- pub
- fn new (m: &'a dyn Trait1) -> Data<'a>
- {
- Data {
- m /*: m*/,
- }
- }
- }
- impl<'a> Trait2<'a> for Data<'a> {
- fn function_2 (self: &'_ Self, r: &'_ mut Rec<'a>) -> bool
- {
- r.s = Some(self.m);
- true
- }
- }
- /////////////////////////////////////////
- pub
- struct Struct {
- a: i32
- }
- impl Struct {
- pub
- fn new (a: i32) -> Struct
- {
- Struct{a: a}
- }
- }
- impl Trait1 for Struct {
- fn function_1 (self: &'_ Self, rec: &'_ Rec<'_>) -> bool
- {
- true
- }
- }
- ///////////////////////////////////////////
- pub
- struct Struct1 {
- }
- impl Struct1 {
- pub
- fn get_struct1 (a: i32) -> Box<dyn Trait1>
- {
- let struc = Struct::new(a);
- Box::new(struc)
- }
- }
- pub
- trait Trait1 {
- fn function_1 (self: &'_ Self, rec: &'_ Rec<'_>) -> bool;
- }
- ///////////////////////////////////////////
- pub
- struct Exec {
- e: i32
- }
- impl Exec {
- pub
- fn new (e: i32) -> Exec
- {
- Exec {e: e}
- }
- pub
- fn function_exec (self: &'_ Self, w: &'_ Data<'_>) -> bool
- {
- let mut rec: Rec<'_> = Rec::new();
- w.function_2(&mut rec)
- }
- }
- //////////////////////////////////////////
- fn main ()
- {
- let exec = Exec::new(1);
- let s_1 = Struct1::get_struct1(2);
- let data: Data<'_> = Data::new(&*s_1); // get rid of one indirection
- exec.function_exec(&data);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement