ApplyContext

Struct ApplyContext 

Source
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:

  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.

Implementations§

Source§

impl<F> ApplyContext<F>

Source

pub fn new(context: Context, f: F) -> Self

Creates a new ApplyContext wrapper.

§Arguments
  • context - The context to apply during polling
  • f - 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,

Source§

type Output = <F as Future>::Output

The type of value produced on completion.
Source§

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>

Attempts to resolve the future to a final value, registering the current task for wakeup if the value is not yet available. Read more

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> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<F> IntoFuture for F
where F: Future,

§

type Output = <F as Future>::Output

The output that the future will produce on completion.
§

type IntoFuture = F

Which kind of future are we turning this into?
§

fn into_future(self) -> <F as IntoFuture>::IntoFuture

Creates a future from a value. Read more
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.