1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//! Macro to implement inherit
//!
//! Provide <code>class</code> to cover a struct itself, it's impl and trait implements that need to inherit.
//! To borrow as mut, see <code>def_as_mut</code>
//!
use ;
use parse_class;
use TokenStream;
use lazy_static;
use quote;
use ClassInfo;
use crateSerializable;
lazy_static!
///
/// need to wrap struct and implements.
///
/// using <code>#\[keep\]</code> for method make that method keep in the original impl.
///
/// methods without <code>#\[keep\]</code> will be put into trait <code>\_\_XXX\_\_</code>.
///
/// and macro will auto generate a <code>new</code> function which return <code>Pin<Box<Self>></code>.
///
/// expression in the method will be converted.
///
/// instead of use <code>self</code>, using <code>this</code>.
///
/// <code>self</code> will be convert to use <code>unsafe { self.\_\_real\_\_.as_ref().unwrap() }</code>.
///
/// <code>self_mut</code> will be convert to use <code>unsafe { self.\_\_real\_\_.as_mut().unwrap() }</code>.
///
/// <code>\_super</code> will be convert to use <code>self.\_\_prototype\_\_</code>.
///
/// <code>\_super_mut</code> will be convert to use <code>unsafe { self.\_\_prototype\_\_.as_mut().get_unchecked_mut() }</code>.
///
///
/// An example to use this macro:
/// ```rust
/// class! {
/// struct Example {
/// data: String
/// }
/// impl Example {
/// #[keep]
/// fn with() -> Pin<Box<Self>> where Self: Sized {
/// Self::new("example".to_string())
/// }
///
/// fn set_data(&mut self, data: String) {
/// this.data = data;
/// }
///
/// fn get_data(&self) -> String {
/// this.data.clone()
/// }
/// }
/// impl Something for Example {
/// //do something
/// }
/// }
/// class! {
/// extends Example;
/// struct Sub { }
/// impl Sub { }
/// }
/// ```
/// the struct will become:
/// ```rust
/// struct Example {
/// __real__: *mut dyn __Example__,
/// _pinned: ::std::marker::PhantomPinned,
/// data: String
/// }
/// struct Sub {
/// __prototype__: ::std::pin::Pin<Box<Example>>
/// __real__: *mut dyn __Sub__,
/// _pinned: ::std::marker::PhantomPinned,
/// }
/// ```
///
/// this macro will define macro <code>as_mut</code>
///
/// example:
/// ```rust
/// def_as_mut!();
///
/// fn main() {
/// let mut example = Sub::new("data".to_string());
/// as_mut!(example).set_data("modified".to_string());
/// assert_eq!(example.get_data(), "modified".to_string());
/// }
/// ```