Skip to main content

Cx

Struct Cx 

Source
pub struct Cx { /* private fields */ }
Expand description

Capability context handle.

Cheaply cloneable. Check is_cancelled() at natural yield points (loop iterations, before I/O, before expensive computation).

Implementations§

Source§

impl Cx

Source

pub fn background() -> (Self, CxController)

Create a root context with no deadline.

Source

pub fn with_deadline(deadline: Duration) -> (Self, CxController)

Create a root context with a deadline.

Source

pub fn lab(clock: &LabClock) -> (Self, CxController)

Create a root context using a LabClock for deterministic testing.

Source

pub fn lab_with_deadline( clock: &LabClock, deadline: Duration, ) -> (Self, CxController)

Create a lab context with a deadline.

Source

pub fn child(&self, deadline: Duration) -> (Self, CxController)

Derive a child context with a tighter deadline.

The effective deadline is min(parent.deadline(), child_deadline). Cancelling the parent also cancels the child (checked via chain walk).

Source

pub fn child_inherit(&self) -> (Self, CxController)

Derive a child context that inherits the parent deadline.

Source

pub fn id(&self) -> u64

Unique identifier for this context (for tracing/logging).

Source

pub fn is_cancelled(&self) -> bool

Check if this context (or any ancestor) has been cancelled.

Source

pub fn is_expired(&self) -> bool

Check if the deadline has passed.

Source

pub fn is_done(&self) -> bool

Check if the context is done (cancelled or expired).

Source

pub fn deadline(&self) -> Option<Duration>

Return the effective deadline as a Duration from context creation, considering the full parent chain. Returns None if no deadline is set.

Source

pub fn remaining(&self) -> Option<Duration>

Remaining time until deadline (saturates to zero, never negative). Returns None if no deadline is set.

Source

pub fn remaining_us(&self) -> Option<u64>

Remaining time in microseconds, or None if no deadline.

Source

pub fn now(&self) -> Instant

Current time according to this context’s time source.

Source

pub fn is_lab(&self) -> bool

Whether this context uses a lab clock.

Source

pub fn sleep(&self, duration: Duration) -> bool

Sleep for the given duration, respecting cancellation and deadline.

Returns true if the full duration elapsed, false if cancelled or deadline expired early.

Source§

impl Cx

Source

pub fn check(&self) -> Result<(), CxError>

Check if the context is still live; return Err if cancelled or expired.

Intended for use at yield points:

cx.check()?;
// ... continue work ...

Trait Implementations§

Source§

impl Clone for Cx

Source§

fn clone(&self) -> Cx

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Cx

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Cx

§

impl RefUnwindSafe for Cx

§

impl Send for Cx

§

impl Sync for Cx

§

impl Unpin for Cx

§

impl UnsafeUnpin for Cx

§

impl UnwindSafe for Cx

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

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

Performs the conversion.
Source§

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

Source§

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

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

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

Performs the conversion.