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
289
290
291
292
// Copyright (c) Microsoft Corporation.
// Copyright (c) Folo authors.
use Box as StdBox;
use ;
/// A linked object that acts like a `std::boxed::Box<dyn MyTrait>`.
///
/// Intended to represent linked instances of `T` where `T: MyTrait`. This is for use with types
/// that are always exposed to user code as trait objects via `linked::Box<dyn MyTrait>`.
///
/// The `Box` itself implements the linked object API from [`#[linked::object]`][3]. The type
/// `T` is not decorated with the [`#[linked::object]`][3] attribute when using `Box` and simply
/// uses linked object patterns in its constructor.
///
/// # Usage
///
/// Use it like a regular `std::boxed::Box<T>` that also happens to support the linked object
/// patterns. The box can be used via all the standard mechanisms such as:
///
/// * [`linked::instances!`][1]
/// * [`linked::thread_local_rc!`][2]
/// * [`linked::thread_local_arc!`][4] (if `T: Sync`)
/// * [`linked::InstancePerThread<T>`][5]
/// * [`linked::InstancePerThreadSync<T>`][6] (if `T: Sync`)
/// * [`linked::Family<T>`][7]
///
/// # Implementation
///
/// Instead of a typical constructor, create one that returns `linked::Box<dyn MyTrait>`. Inside
/// this constructor, create a `linked::Box` instance using the [`linked::new_box!` macro][8].
/// The first macro parameter is the type of the trait object inside the box, and the second is a
/// `Self` struct-expression to create one linked instance of the implementation type.
///
/// ```
/// # trait ConfigSource {}
/// # impl ConfigSource for XmlConfig {}
/// // If using linked::Box, do not put `#[linked::object]` on the struct.
/// // The linked::Box itself is the linked object and our struct is only its contents.
/// struct XmlConfig {
/// config: String,
/// }
///
/// impl XmlConfig {
/// pub fn new_as_config_source() -> linked::Box<dyn ConfigSource> {
/// // Constructing instances works logically the same as for regular linked objects.
/// //
/// // The only differences are:
/// // 1. We use `linked::new_box!` instead of `linked::new!`
/// // 2. There is an additional parameter to the macro to name the trait object type
/// linked::new_box!(
/// dyn ConfigSource,
/// Self {
/// config: "xml".to_string(),
/// }
/// )
/// }
/// }
/// ```
///
/// Any connections between the instances should be established via the captured state of this
/// closure (e.g. sharing an `Arc` or setting up messaging channels).
///
/// # Example
///
/// Using the linked objects as `linked::Box<dyn ConfigSource>`, without the user code knowing the
/// exact type of the object in the box:
///
/// ```
/// trait ConfigSource {
/// fn config(&self) -> String;
/// }
///
/// struct XmlConfig {}
/// struct IniConfig {}
///
/// impl ConfigSource for XmlConfig {
/// fn config(&self) -> String {
/// "xml".to_string()
/// }
/// }
///
/// impl ConfigSource for IniConfig {
/// fn config(&self) -> String {
/// "ini".to_string()
/// }
/// }
///
/// impl XmlConfig {
/// pub fn new_as_config_source() -> linked::Box<dyn ConfigSource> {
/// linked::new_box!(dyn ConfigSource, XmlConfig {})
/// }
/// }
///
/// impl IniConfig {
/// pub fn new_as_config_source() -> linked::Box<dyn ConfigSource> {
/// linked::new_box!(dyn ConfigSource, IniConfig {})
/// }
/// }
///
/// let xml_config = XmlConfig::new_as_config_source();
/// let ini_config = IniConfig::new_as_config_source();
///
/// let configs: [linked::Box<dyn ConfigSource>; 2] = [xml_config, ini_config];
///
/// assert_eq!(configs[0].config(), "xml".to_string());
/// assert_eq!(configs[1].config(), "ini".to_string());
/// ```
///
/// [1]: crate::instances
/// [2]: crate::thread_local_rc
/// [3]: crate::object
/// [4]: crate::thread_local_arc
/// [5]: crate::InstancePerThread
/// [6]: crate::InstancePerThreadSync
/// [7]: crate::Family
/// [8]: crate::new_box
/// Defines the template used to create every instance in a `linked::Box<T>` object family.
///
/// This macro is meant to be used in the context of creating a new linked instance of
/// `T` that is meant to be always expressed via an abstraction (`dyn SomeTrait`).
///
/// # Arguments
///
/// * `$dyn_trait` - The trait object that the linked object is to be used as (e.g. `dyn SomeTrait`).
/// * `$ctor` - The Self-expression that serves as the template for constructing new linked
/// instances on demand. This will move-capture any referenced state. All captured
/// values must be thread-safe (`Send` + `Sync` + `'static`).
///
/// # Example
///
/// ```rust
/// # trait ConfigSource {}
/// # impl ConfigSource for XmlConfig {}
/// // If using linked::Box, do not put `#[linked::object]` on the struct.
/// // The linked::Box itself is the linked object and our struct is only its contents.
/// struct XmlConfig {
/// config: String,
/// }
///
/// impl XmlConfig {
/// pub fn new_as_config_source() -> linked::Box<dyn ConfigSource> {
/// linked::new_box!(
/// dyn ConfigSource,
/// Self {
/// config: "xml".to_string(),
/// }
/// )
/// }
/// }
/// ```
///
/// See `examples/linked_box.rs` for a complete example.