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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
//! **Dynamic struct field indexing library**
//!
//! name-index implements functionality to allow users to dynamically access
//! the fields of realitively-homogenous structs by name at runtime.
//!
//! This functionality may prove useful for problems where fixed key-value
//! pairs are mapped as fields of a struct (for example to aid performance and
//! allow compile-time type checking) but which need to be accessed sparingly
//! at runtime by their respective name.
//!
//! The lookup functionality is not optimized for performance. In cases where
//! this lookup is frequently used, it is likely better to use alternatives
//! like [std::collections::HashMap].
//!
//! ## Example
//! ```rust
//! use name_index::NameIndex;
//!
//! #[derive(Default, NameIndex)]
//! struct MyStruct {
//! // The #[index] attributes tells the derive macro to
//! // start indexing all u32 fields from here on
//! #[index]
//! first: u32,
//! second: u32,
//! third: u32,
//! }
//!
//! fn main() {
//! let mut my_struct = MyStruct::default();
//!
//! // The name of the field we want to index
//! // Might come from user input for example
//! let name = "second";
//!
//! // Use NameIndex::get_ref_mut to get a mutable
//! // reference to our field
//! let field_ref: &mut u32 =
//! NameIndex::get_ref_mut(&mut my_struct, name)
//! .expect("second is a field of MyStruct");
//!
//! *field_ref = 5;
//!
//! assert_eq!(my_struct.second, 5);
//! }
//! ```
//!
//! For the previous example the [NameIndex] derive creates the following
//! (simplified[^simpl]) code:
//!
//! ```rust
//! # use name_index::{NameIndex, Field, FieldMut};
//! # struct MyStruct {
//! # first: u32,
//! # second: u32,
//! # third: u32,
//! # }
//! impl NameIndex<u32> for MyStruct {
//! fn get_ref_mut(&mut self, name: &str) -> Option<&mut u32> {
//! match name {
//! "first" => Some(&mut self.first),
//! "second" => Some(&mut self.second),
//! "third" => Some(&mut self.third),
//! _ => None,
//! }
//! }
//! // -- snip --
//! # fn get_ref(&self, name: &str) -> Option<&u32> {unimplemented!()}
//! # fn fields(&self) -> Vec<Field<u32>> {unimplemented!()}
//! # fn fields_mut(&mut self) -> Vec<FieldMut<u32>> {unimplemented!()}
//! # fn field_aliases(&self, name: &str) -> Vec<&'static str>
//! # {unimplemented!()}
//! # fn resolve_alias(&self, name: &str) -> Option<&'static str>
//! # {unimplemented!()}
//! }
//! ```
//!
//! ## Second Example
//! This example is supposed to illustrate how [NameIndexCopy] can be used
//! to save some redundant typing when the generic type of [NameIndex]
//! implements [std::marker::Copy].
//!
//! ```rust
//! use name_index::{NameIndex, NameIndexCopy};
//!
//! #[derive(NameIndex)]
//! struct MyStruct {
//! // When no `#[index]` attribute is given, the first field is
//! // automatically chosen as the reference.
//! first: u32,
//! second: u32,
//! third: u32,
//! }
//!
//! fn main() {
//! let mut my_struct = MyStruct {
//! first: 1,
//! second: 2,
//! third: 3,
//! };
//!
//! // We can use NameIndexCopy<u32> here because NameIndexCopy<T>
//! // is automatically implemented for NameIndex<T> when T: Copy
//!
//! // Get the value of my_struct.third
//! let val: u32 = NameIndexCopy::get(&my_struct, "third")
//! .expect("'third' is a field of MyStruct");
//!
//! assert_eq!(val, my_struct.third);
//!
//! // Set the value of my_struct.first to 7
//! NameIndexCopy::set(&mut my_struct, "first", 7)
//! .expect("'first' is a field of MyStruct");
//!
//! assert_eq!(my_struct.first, 7);
//! }
//! ```
//!
//! [^simpl]: Only the [NameIndex::get_ref_mut] function is implemented here
//! for clarity.
/// Macro to automatically derive NameIndex
///
/// This derive can only be used for regular structs (i.e. not on tuple
/// or unit structs).
///
/// The `#[index]` attribute can only be placed on one of the fields.
/// If it isn't present, the first field is chosen as if it had the attribute.
/// All fields with the same type, starting at that field, will be indexed
/// by the macro and can then be found through [NameIndex].
///
/// The `#[alias(...)]` attribute can be used to define aliases for the field.
/// Aliases behave exactly like the actual name of the field when using
/// [NameIndex::get_ref] and [NameIndex::get_ref_mut].
///
/// ## Example
/// ```rust
/// use name_index::NameIndex;
///
/// #[derive(Default, NameIndex)]
/// struct SomeStruct {
/// one: u16, // not indexed
/// random: String, // not indexed
/// #[index]
/// two: u16, // FOUND
/// three: u16, // FOUND
/// other: Vec<u8>, // not indexed
/// #[alias(f)]
/// four: u16, // FOUND
/// }
///
/// // SomeStruct now implements NameIndex<u16>
///
/// let s: SomeStruct = SomeStruct::default();
/// assert_eq!(s.get_ref("one"), None);
/// assert_eq!(s.get_ref("other"), None);
///
/// let two_ref: &u16 = &s.two;
/// let found_ref: Option<&u16> = s.get_ref("two");
/// assert!(found_ref.is_some());
/// assert!(std::ptr::eq(found_ref.unwrap(), two_ref));
///
/// // The "four" field of SomeStruct has the alias "f" and get_ref treats them
/// // the exact same way.
/// let four_ref: &u16 = s.get_ref("four").unwrap();
/// let f_ref: &u16 = s.get_ref("f").unwrap();
/// assert!(std::ptr::eq(four_ref, f_ref));
/// ```
pub use NameIndex;
/// Named reference to a struct field
///
/// The first entry of the tuple represents the name of the field and the
/// second holds a reference to the field.
///
/// Returned by [NameIndex::fields].
pub type Field<'a, T> = ;
/// Named mutable reference to a struct field
///
/// The first entry of the tuple represents the name of the field and the
/// second holds a mutable reference to the field.
///
/// Returned by [NameIndex::fields_mut].
pub type FieldMut<'a, T> = ;
/// Struct field access trait
///
/// Designed to be derived through [NameIndex][name_index_derive::NameIndex].
/// Struct primitive field access trait