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
// Use README.md file as module documentation
// This makes easy to have a proper home for the github project as well as
// ensuring that the content is always up to date and tested
//!
pub use crate AsyncWrapContext;
pub use crate SyncWrapContext;
/// Context about the caller propagated into the context.
/// Procedural macro that will decorate the incoming async function with the provided context.
///
/// The context is expected to be a type that implements the `AsyncWrapContext` trait.
///
/// Usage example:
/// ```
/// # use context_manager_macro::async_wrap;
/// struct AsyncPrintDuration;
/// impl<T> context_manager::AsyncWrapContext<T> for AsyncPrintDuration {
/// async fn new() -> Self { Self }
/// }
///
/// #[async_wrap(AsyncPrintDuration)]
/// async fn foo<'a, T>(int_value: usize, str_ref: &'a str, generic: T) -> usize {
/// let type_name = std::any::type_name::<T>();
/// println!("Async call with int_value={int_value}, str_ref={str_ref}, type_of(T)={type_name}");
/// 10
/// }
/// ```
///
/// The decorator does not induce limits on the shape of the incoming function, in terms
/// of generics, sync/async, lifetime, etc.
///
/// The decorator will expand the incoming function by adding the context handling
/// rendering something similar to
/// ```
/// # use context_manager::{AsyncWrapContext, CallerContext};
/// # struct AsyncPrintDuration;
/// # impl<T> AsyncWrapContext<T> for AsyncPrintDuration {
/// # async fn new() -> Self { Self }
/// # }
/// async fn foo<'a, T>(int_value: usize, str_ref: &'a str, generic: T) -> usize {
/// AsyncPrintDuration::run(CallerContext { fn_name: "foo" }, async {
/// let type_name = std::any::type_name::<T>();
/// println!("Async call with int_value={int_value}, str_ref={str_ref}, type_of(T)={type_name}");
/// 10
/// }).await
/// }
/// ```
///
/// The structuring of the generated code is though to avoid any clone/copy of data,
/// as well as reducing the number of jumps needed to execute the original code.
///
/// # Possible compile errors
/// ## Passing a type that does not implement `AsyncWrapContext` trait will lead to compile errors.
/// ```compile_fail
/// # use context_manager_macro::async_wrap;
/// struct PrintDuration;
/// impl<T> context_manager::SyncWrapContext<T> for PrintDuration {
/// fn new() -> Self { Self }
/// }
///
/// #[async_wrap(PrintDuration)]
/// async fn foo() {}
/// ```
/// would lead to the following compile error
/// ```text
/// error[E0277]: the trait bound `PrintDuration: AsyncWrapContext<_>` is not satisfied
/// --> src/lib.rs:206:1
/// |
/// 11 | #[async_wrap(PrintDuration)]
/// | ^^^^^^^^^^^^^ the trait `AsyncWrapContext<_>` is not implemented for `PrintDuration`
/// |
/// = help: the trait `AsyncWrapContext<T>` is implemented for `AsyncTraceDuration`
/// ```
///
/// ## Decorating a synchronous function
/// This is intentional in order to avoid possibly stalling the async-runtime in use.
/// Please consider wrapping yourself the synchronous code in an async block, or using `#[decorate]` whether possible.
/// ```compile_fail
/// # use context_manager_macro::async_wrap;
/// struct AsyncPrintDuration;
/// impl<T> context_manager::AsyncWrapContext<T> for AsyncPrintDuration {
/// async fn new() -> Self { Self }
/// }
///
/// #[async_wrap(PrintDuration)]
/// fn foo() {}
/// ```
/// would lead to the following error
/// ```text
/// error: #[async_wrap] cannot operate on sync functions. Please consider using a #[decorate] macro or converting/wrapping the function to be async.
/// --> src/lib.rs:228:1
/// |
/// 11 | #[async_wrap(PrintDuration)]
/// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/// |
/// ```
pub use async_wrap;
/// Procedural macro that will decorate the incoming function with the provided context.
///
/// The context is expected to be a type that implements the `SyncWrapContext` trait.
///
/// Usage example:
/// ```
/// # use context_manager_macro::wrap;
/// struct PrintDuration;
/// impl<T> context_manager::SyncWrapContext<T> for PrintDuration {
/// fn new() -> Self { Self }
/// }
///
/// #[wrap(PrintDuration)]
/// fn sync_foo<'a, T>(int_value: usize, str_ref: &'a str, generic: T) -> usize {
/// let type_name = std::any::type_name::<T>();
/// println!("Sync call with int_value={int_value}, str_ref={str_ref}, type_of(T)={type_name}");
/// 10
/// }
///
/// #[wrap(PrintDuration)]
/// async fn async_foo<'a, T>(int_value: usize, str_ref: &'a str, generic: T) -> usize {
/// let type_name = std::any::type_name::<T>();
/// println!("Async call with int_value={int_value}, str_ref={str_ref}, type_of(T)={type_name}");
/// 10
/// }
/// ```
///
/// The decorator does not induce limits on the shape of the incoming function, in terms
/// of generics, sync/async, lifetime, etc.
///
/// The decorator will expand the incoming function by adding the context handling
/// rendering something similar to
/// ```
/// # use context_manager::{CallerContext, SyncWrapContext};
/// # struct PrintDuration;
/// # impl<T> SyncWrapContext<T> for PrintDuration {
/// # fn new() -> Self { Self }
/// # }
/// fn sync_foo<'a, T>(int_value: usize, str_ref: &'a str, generic: T) -> usize {
/// PrintDuration::run_sync(CallerContext { fn_name: "sync_foo" }, move || {
/// let type_name = std::any::type_name::<T>();
/// println!("Sync call with int_value={int_value}, str_ref={str_ref}, type_of(T)={type_name}");
/// 10
/// })
/// }
///
/// async fn async_foo<'a, T>(int_value: usize, str_ref: &'a str, generic: T) -> usize {
/// PrintDuration::run_async(CallerContext { fn_name: "async_foo" }, async {
/// let type_name = std::any::type_name::<T>();
/// println!("Async call with int_value={int_value}, str_ref={str_ref}, type_of(T)={type_name}");
/// 10
/// }).await
/// }
/// ```
///
/// The structuring of the generated code is though to avoid any clone/copy of data,
/// as well as reducing the number of jumps needed to execute the original code.
///
/// # Possible compile errors
/// ## Passing a type that does not implement `SyncWrapContext` trait will lead to compile errors.
/// ```compile_fail
/// # use context_manager_macro::wrap;
/// struct AsyncPrintDuration;
/// impl<T> context_manager::AsyncWrapContext<T> for AsyncPrintDuration {
/// async fn new() -> Self { Self }
/// }
///
/// #[wrap(AsyncPrintDuration)]
/// fn foo() {}
/// ```
/// would lead to the following compile error
/// ```text
/// ---- src/lib.rs - decorate (line 98) stdout ----
/// error[E0277]: the trait bound `AsyncPrintDuration: SyncWrapContext<_>` is not satisfied
/// --> src/lib.rs:106:12
/// |
/// 11 | #[wrap(AsyncPrintDuration)]
/// | ^^^^^^^^^^^^^^^^^^ the trait `SyncWrapContext<_>` is not implemented for `AsyncPrintDuration`
/// |
/// = help: the trait `SyncWrapContext<T>` is implemented for `TraceDuration`
/// ```
///
/// ## Decorating a constant function
/// Const functions are not supported for decoration.
/// This is a side-effect of embedding code that is not const compatible (like async blocks and closures).
///
/// ```compile_fail
/// # use context_manager_macro::wrap;
/// struct PrintDuration;
/// impl<T> context_manager::SyncWrapContext<T> for PrintDuration {
/// fn new() -> Self { Self }
/// }
///
/// #[wrap(PrintDuration)]
/// const fn foo() {}
/// ```
/// would lead to the following error
/// ```text
/// error: #[wrap] cannot operate on const functions
/// --> context_manager_macro/src/lib.rs:131:1
/// |
/// 11 | #[wrap(PrintDuration)]
/// | ^^^^^^^^^^^^^^^^^^^^^^
/// |
/// ```
pub use wrap;