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
// Copyright (c) Microsoft Corporation.
// Copyright (c) Folo authors.
/// Defines the template used to create every instance in a linked object family.
///
/// You are expected to use this in the constructor of a [linked object][crate],
/// except when you want to always express the linked object via trait objects (`dyn Xyz`),
/// in which case you should use [`linked::new_box`][crate::new_box].
///
/// The macro body must be a struct-expression of the `Self` type. Any variables the macro body
/// captures must be thread-safe (`Send` + `Sync` + `'static`). The returned object itself does
/// not need to be thread-safe.
///
/// # Example
///
/// ```
/// use std::sync::{Arc, Mutex};
///
/// #[linked::object]
/// struct TokenCache {
/// tokens_created: usize,
/// name: String,
/// master_key: Arc<Mutex<String>>,
/// is_multidimensional: bool,
/// }
///
/// impl TokenCache {
/// fn new(name: String, is_multidimensional: bool) -> Self {
/// // Any shared data referenced by the macro body must be thread-safe.
/// let master_key = Arc::new(Mutex::new(String::new()));
///
/// linked::new!(Self {
/// tokens_created: 0,
/// name: name.clone(),
/// master_key: Arc::clone(&master_key),
/// is_multidimensional,
/// })
/// }
/// }
/// ```
///
/// Complex expressions are supported within the `Self` struct-expression:
///
/// ```
/// #[linked::object]
/// struct TokenCache {
/// token_sources: Vec<linked::Box<dyn TokenSource>>,
/// }
/// # trait TokenSource {}
///
/// impl TokenCache {
/// fn new(source_families: Vec<linked::Family<linked::Box<dyn TokenSource>>>) -> Self {
/// linked::new!(Self {
/// token_sources: source_families
/// .iter()
/// .cloned()
/// .map(linked::Family::into)
/// .collect()
/// })
/// }
/// }
/// ```
///
/// For a complete example, see `examples/linked_basic.rs`.
) => ;
=> ;
=> ;
}