mod_trait_exerci/
lib.rs

1pub mod mod_bare;
2pub mod mod_where_fn;
3pub mod mod_static_fn;
4pub mod mod_box_static_fn;
5pub mod mod_dynamic_fn;
6pub mod mod_box_dynamic_fn;
7
8pub mod mod_trait {
9    #[derive(Debug, Default, PartialEq, Copy, Clone)]
10    pub struct StructType {
11        data: (u32),
12    }
13
14    #[derive(Debug, Default, PartialEq, Copy, Clone)]
15    pub struct TupleType(pub u32);
16
17    pub trait TraitCanal {
18        fn get_object(&self) -> Self where Self: Sized;  // For keyword `dyn`
19        //fn get_object(&self) -> Self;                  // E0038 For keyword `dyn`; OK for static functions
20        fn get_tuple(&self) -> (u32);
21    }
22
23    impl TraitCanal for StructType {
24        fn get_object(&self) -> Self {
25            StructType{data: self.data}
26        }
27
28        fn get_tuple(&self) -> (u32) {
29            println!("impl TraitCanal for StructType");
30            (self.data)
31        }
32    }
33
34    impl TraitCanal for TupleType {
35        fn get_object(&self) -> Self {
36            TupleType(self.0)
37        }
38
39        fn get_tuple(&self) -> (u32) {
40            println!("impl TraitCanal for TupleType");
41            (self.0)
42        }
43    }
44
45    impl StructType {
46        pub fn new(_data: u32) -> Self {
47            StructType{data: _data}
48        }
49    }
50
51    impl TupleType {
52        pub fn new(_data: u32) -> Self {
53            TupleType(_data)
54        }
55    }
56}