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
// SPDX-License-Identifier: MIT OR Apache-2.0
//! Async context preservation.
use Future;
use Pin;
use Poll;
use Context;
/// A [`Future`] wrapper that preserves context across async executor boundaries.
///
/// Many async executors don't preserve thread-local state between poll calls,
/// which can cause context loss in async code. `ApplyContext` solves this by
/// saving and restoring the context around each poll.
///
/// # Use Cases
///
/// - Working with executors that use thread pools
/// - Spawning tasks that need to maintain parent context
/// - Ensuring consistent logging context in async code
///
/// # Examples
///
/// ```rust
/// logwise::declare_logging_domain!();
/// # fn main() {
/// use logwise::context::{Context, ApplyContext};
///
/// async fn process_data() {
/// logwise::info_sync!("Processing data");
/// }
///
/// # async fn example() {
/// // Create a context for this operation
/// let ctx = Context::new_task(None, "data_processor".to_string(), logwise::Level::Info, true);
///
/// // Wrap the future to preserve context
/// let future = ApplyContext::new(ctx, process_data());
///
/// // The context will be active during all poll calls
/// future.await;
/// # }
/// # }
/// ```
///
/// # Implementation Details
///
/// `ApplyContext` implements [`Future`] by:
/// 1. Saving the current thread-local context
/// 2. Setting its wrapped context as current
/// 3. Polling the inner future
/// 4. Restoring the original context
///
/// This ensures the wrapped future always sees the correct context, regardless
/// of which thread or executor polls it.
;