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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
//! Late-bound structure definitions.
//!
//! This crate exposes the [`late_struct!`] macro, which defines a structure whose set of fields
//! can be extended by any crate within a compiled artifact using the [`late_field!`] macro.
//! Unlike regular structures, dependents on the crate which originally defined the structure are
//! allowed to extend it. Additionally, the structure we defined can be instantiated in any crate
//! using a [`LateInstance`], even if dependents of that crate are still extending it.
//!
//! # Basic Usage
//!
//! For example, let's say we had a crate hierarchy where "`dependent` depends on `dependency`."
//!
//! In `dependency`, we could define a new late-struct marker using the [`late_struct!`] macro...
//!
//! ```
//! use late_struct::late_struct;
//!
//! // Marker type for our application context.
//! // Any type could be used here.
//! pub struct AppContext;
//!
//! late_struct!(AppContext);
//! ```
//!
//! ...and then, in `dependent`, we could proceed to add a field to it using the [`late_field!`]
//! macro:
//!
//! ```
//! # mod dependency {
//! # use late_struct::late_struct;
//! # pub struct AppContext;
//! # late_struct!(AppContext);
//! # }
//! use late_struct::late_field;
//!
//! use dependency::AppContext;
//!
//! #[derive(Debug, Default)] // Type must implement `Debug`, `Default`, and live for `'static`.
//! pub struct MyField(Vec<u32>);
//!
//! late_field!(MyField[AppContext]);
//! ```
//!
//! ...just note that, by default, the field value must implement [`Debug`](std::fmt::Debug),
//! [`Default`], and live for `'static`.
//!
//! We can then refer to the structure we've created with a [`LateInstance`]. For example, back in
//! `dependency`, we can write...
//!
//! ```
//! # use late_struct::late_struct;
//! # pub struct AppContext;
//! # late_struct!(AppContext);
//! use late_struct::LateInstance;
//!
//! pub fn create_my_instance() -> LateInstance<AppContext> {
//! LateInstance::new()
//! }
//! ```
//!
//! ...even though downstream crates such as `dependent` are still adding fields to it. Finally, we
//! can access fields using the [`LateInstance::get`] and [`LateInstance::get_mut`] methods. For
//! example, in the `dependent` crate, we could write...
//!
//! ```
//! # mod dependency {
//! # use late_struct::{late_struct, LateInstance};
//! #
//! # pub struct AppContext;
//! #
//! # late_struct!(AppContext);
//! #
//! # pub fn create_my_instance() -> LateInstance<AppContext> {
//! # LateInstance::new()
//! # }
//! # }
//! use dependency::{AppContext, create_my_instance};
//! # use late_struct::late_field;
//! #
//! # #[derive(Debug, Default)] // Type must implement `Default` and live for `'static`.
//! # pub struct MyField(Vec<u32>);
//! #
//! # late_field!(MyField[AppContext]);
//!
//! pub fn example() {
//! let mut instance = create_my_instance();
//!
//! instance.get_mut::<MyField>().0.push(1);
//! instance.get_mut::<MyField>().0.push(2);
//! instance.get_mut::<MyField>().0.push(3);
//!
//! eprintln!("Our numbers are {:?}", instance.get::<MyField>());
//! }
//! ```
//!
//! See the documentation of [`LateInstance`] for more ways to access the instance.
//!
//! Note that the "key type" used to refer to a given field can be distinct from its value type. For
//! example, in the previous snippet, we could make `MyField` a zero-sized marker type and set it up
//! to refer to a value of type `Vec<u32>` instead. We do this by changing our `late_field!` macro
//! invocation like so...
//!
//! ```
//! # mod dependency {
//! # use late_struct::{late_struct, LateInstance};
//! #
//! # pub struct AppContext;
//! #
//! # late_struct!(AppContext);
//! #
//! # pub fn create_my_instance() -> LateInstance<AppContext> {
//! # LateInstance::new()
//! # }
//! # }
//! use dependency::{AppContext, create_my_instance};
//! # use late_struct::late_field;
//! #
//!
//! // The `#[non_exhaustive]` attribute helps ensure that other crates don't
//! // accidentally try to instantiate what should just be a marker type.
//! #[non_exhaustive]
//! pub struct MyField;
//!
//! late_field!(MyField[AppContext] => Vec<u32>);
//! // ^^^^^^^^^^^ this is how we specify the
//! // field's value type explicitly.
//!
//! pub fn example() {
//! let mut instance = create_my_instance();
//!
//! // Notice that we're now accessing the `&mut Vec<u32>` directly
//! // rather than the `MyField` wrapper.
//! instance.get_mut::<MyField>().push(1);
//! instance.get_mut::<MyField>().push(2);
//! instance.get_mut::<MyField>().push(3);
//!
//! eprintln!("Our numbers are {:?}", instance.get::<MyField>());
//! }
//! ```
//!
//! # Advanced Usage
//!
//! By default, all fields of a given struct are required to implement [`Debug`](std::fmt::Debug),
//! [`Default`], and `'static`. These requirements, however, can be changed on a per-struct basis.
//! For instance, we can remove the `Debug` requirement and instead require [`Send`], [`Sync`], and
//! a custom trait `Reflect` with the following [`late_struct!`] definition...
//!
//! ```
//! use late_struct::late_struct;
//!
//! trait Reflect {
//! fn say_hi(&self);
//! }
//!
//! struct MyStruct;
//!
//! late_struct!(MyStruct => dyn 'static + Reflect + Send + Sync);
//! // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//! // field value types must upcast to this type
//! ```
//!
//! The only mandatory trait bounds on a field are that it have a [`Default`] initializer, be
//! [`Sized`], and live for `'static`.
//!
//! We can then enumerate these fields at runtime using the [`LateInstance::fields`] method and
//! access those fields' erased values using the [`LateInstance::get_erased`], and
//! [`LateInstance::get_erased_mut`] methods like so...
//!
//! ```
//! # use late_struct::{late_field, late_struct, LateInstance};
//! # use std::sync::Arc;
//! #
//! # trait Reflect {
//! # fn say_hi(&self);
//! # }
//! #
//! # struct MyStruct;
//! #
//! # late_struct!(MyStruct => dyn 'static + Reflect + Send + Sync);
//! #
//! #[derive(Default)]
//! struct MyField;
//!
//! impl Reflect for MyField {
//! fn say_hi(&self) {
//! println!("Hello!");
//! }
//! }
//!
//! late_field!(MyField[MyStruct]);
//!
//! fn say_greetings_on_a_thread(instance: Arc<LateInstance<MyStruct>>) {
//! std::thread::spawn(move || {
//! for field in instance.fields() {
//! instance.get_erased(field).say_hi();
//! }
//! })
//! .join()
//! .unwrap()
//! }
//! #
//! # say_greetings_on_a_thread(Arc::new(LateInstance::new()));
//! ```
//!
//! Struct members can also be made to satisfy non-[`dyn` compatible] standard traits such
//! as [`Eq`], [`Hash`], and [`Clone`] by making the members implement the [`DynEq`], [`DynHash`],
//! and [`DynClone`] traits respectively. This lets us write, for instance...
//!
//! ```
//! use std::{fmt::Debug, collections::HashSet};
//! use late_struct::{late_field, late_struct, DynEq, DynHash, DynClone, LateInstance};
//!
//! trait MyStructMember: Debug + DynEq + DynHash + DynClone {}
//!
//! impl<T> MyStructMember for T
//! where
//! T: Debug + DynEq + DynHash + DynClone,
//! {
//! }
//!
//! struct MyStruct;
//!
//! late_struct!(MyStruct => dyn MyStructMember);
//!
//! #[derive(Debug, Clone, Hash, Eq, PartialEq, Default)]
//! struct MyField(u32);
//!
//! late_field!(MyField[MyStruct]);
//!
//! fn demo() {
//! // The struct implements `Default`...
//! let my_instance = LateInstance::<MyStruct>::default();
//!
//! // ...debug...
//! eprintln!("{my_instance:?}");
//!
//! // ...clone...
//! let my_instance_2 = my_instance.clone();
//!
//! // ...eq...
//! assert_eq!(my_instance, my_instance_2);
//!
//! // ...and hash!
//! let mut map = HashSet::new();
//!
//! assert!(map.insert(my_instance));
//! assert!(!map.insert(my_instance_2));
//! }
//! # demo();
//! ```
//!
//! # Internals
//!
//! Internally, each field we define with [`late_field!`] creates a `static` containing a
//! [`LateFieldDescriptor`] and uses [`linkme`] (or `inventory` on WebAssembly) to add it to a
//! global list of all fields in the crate. When our first [`LateInstance`] is instantiated, all
//! these `LateFieldDescriptor`s are collected and laid out into a structure at runtime, with each
//! fields' offset being written back into an `AtomicUsize` in the `LateFieldDescriptor`.
//!
//! From there, structure instantiation and field fetching work more-or-less like they would with a
//! regular structure. `LateInstance` creates one big heap allocation for the structure it
//! represents and initializes each field accordingly. To access a field, all we have to do is
//! offset the structure's base pointer by the dynamically-initialized offset stored in the field's
//! `LateFieldDescriptor`, making field accesses extremely cheap.
//!
//! Many of these internals are exposed to the end user. See [`LateStructDescriptor`] and
//! [`LateFieldDescriptor`] (which you can obtain from the [`LateStruct::descriptor`] and
//! [`LateField::descriptor`] methods respectively) to learn about various options for reflecting
//! upon the layout of a structure.
//!
//! [`dyn` compatible]: https://doc.rust-lang.org/1.87.0/reference/items/traits.html#r-items.traits.dyn-compatible
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;