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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/// Access a field of an anonymous struct object.
///
/// ```
/// use structz::*;
///
/// let mut person = stru! {
/// name: "John Doe",
/// age: 26,
/// tags: vec!["developer", "rustacean"],
/// };
///
/// // immutable borrow
/// assert_eq!(field!(&person.name), &"John Doe");
///
/// // mutable borrow
/// *field!(&mut person.age) += 1;
/// assert_eq!(field!(&person.age), &27);
///
/// // consume the struct and get the field value
/// let tags = field!(person.tags);
/// assert_eq!(tags, vec!["developer", "rustacean"]);
///
/// // `person` cannot be used anymore.
/// ```
///
/// Unlike the built-in member access operator `.`, structz uses some type deduction magic
/// to achieve access to the specified field. Therefore, it is not smart enough:
/// it cannot move a value of certain field out while keeping other fields remain valid,
/// and it also cannot avoid consume the entire struct object while moving a value that
/// implements `Copy` out.
///
/// However, if all fields implement `Copy`, then the anonymous struct will also implement `Copy`:
///
/// ```
/// use structz::*;
///
/// let pos = stru! {
/// x: 300,
/// y: 480,
/// marker: "Block",
/// };
/// assert_eq!(field!(pos.x), 300);
/// assert_eq!(field!(pos.y), 480);
/// assert_eq!(field!(pos.marker), "Block");
/// ```
/// Obtain a new struct, each field is an immutable reference to the corresponding field of the input struct.
///
/// ```
/// use structz::*;
///
/// let person = stru! {
/// name: "John",
/// age: 30,
/// tags: vec!["smart", "handsome"],
/// };
/// let ref_person = as_ref!(person);
/// assert_eq!(field!(ref_person.age), &30);
/// ```
/// Obtain a new struct, each field is a mutable reference to the corresponding field of the input struct.
///
/// ```
/// use structz::*;
///
/// let mut person = stru! {
/// name: "John",
/// age: 30,
/// tags: vec!["smart", "handsome"],
/// };
/// let mut_person = as_mut!(person);
/// field!(mut_person.tags).pop();
/// assert_eq!(field!(&person.tags), &vec!["smart"]);
/// ```
/// Create anonymous struct object.
///
/// Just like how an object of named struct is created, you declare a field name,
/// followed by a colon, and then its value. Finally, you separate each field with commas.
///
/// ```
/// use structz::*;
///
/// let son_age = 12;
/// let father = stru! {
/// name: "John",
/// age: son_age + 25,
/// };
/// assert_eq!(field!(father.age), 37);
/// ```
///
/// When field's value is omitted, it captures the value of the variable with the same name
/// in the context.
///
/// ```
/// use structz::*;
///
/// let name = "Smith";
/// let person = stru! {
/// name,
/// age: 30,
/// };
/// assert_eq!(field!(person.name), "Smith");
/// ```
/// Generate anonymous struct type.
///
/// Sometimes you may need to know the exact type of an anonymous struct object.
///
/// Just like how a named struct type is declared, you declare a field name,
/// followed by a colon, and then its type. Finally, you separate each field with commas.
///
/// ```
/// use structz::*;
///
/// type Person = stru_t! {
/// name: &'static str,
/// age: u8,
/// };
///
/// let person: Person = stru! {
/// age: 15,
/// name: "Alice",
/// };
/// ```
///
/// For cases where the anonymous structs are used as function arguments, it is recommended
/// that you use the [`macro@named_args`] instead.