pub struct ApplyContext<F>(/* private fields */);Expand description
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
logwise::declare_logging_domain!();
use logwise::context::{Context, ApplyContext};
async fn process_data() {
logwise::info_sync!("Processing data");
}
// 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:
- Saving the current thread-local context
- Setting its wrapped context as current
- Polling the inner future
- Restoring the original context
This ensures the wrapped future always sees the correct context, regardless of which thread or executor polls it.
Implementations§
Source§impl<F> ApplyContext<F>
impl<F> ApplyContext<F>
Sourcepub fn new(context: Context, f: F) -> Self
pub fn new(context: Context, f: F) -> Self
Creates a new ApplyContext wrapper.
§Arguments
context- The context to apply during pollingf- The future to wrap
§Examples
logwise::declare_logging_domain!();
use logwise::context::{Context, ApplyContext};
use std::future::Future;
async fn my_task() -> i32 {
logwise::info_sync!("Running task");
42
}
let ctx = Context::new_task(None, "wrapped_task".to_string(), logwise::Level::Info, true);
let wrapped = ApplyContext::new(ctx, my_task());
let result = wrapped.await;
assert_eq!(result, 42);Trait Implementations§
Source§impl<F> Future for ApplyContext<F>where
F: Future,
impl<F> Future for ApplyContext<F>where
F: Future,
Auto Trait Implementations§
impl<F> Freeze for ApplyContext<F>where
F: Freeze,
impl<F> RefUnwindSafe for ApplyContext<F>where
F: RefUnwindSafe,
impl<F> Send for ApplyContext<F>where
F: Send,
impl<F> Sync for ApplyContext<F>where
F: Sync,
impl<F> Unpin for ApplyContext<F>where
F: Unpin,
impl<F> UnwindSafe for ApplyContext<F>where
F: UnwindSafe,
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
§impl<F> IntoFuture for Fwhere
F: Future,
impl<F> IntoFuture for Fwhere
F: Future,
§type IntoFuture = F
type IntoFuture = F
Which kind of future are we turning this into?
§fn into_future(self) -> <F as IntoFuture>::IntoFuture
fn into_future(self) -> <F as IntoFuture>::IntoFuture
Creates a future from a value. Read more