Skip to main content

Obligation

Struct Obligation 

pub struct Obligation<W>
where W: Work,
{ work: W, guard: Guard, }
Expand description

Work that must be completed, with a guard that reports an unfinished drop.

The work and its guard move together. complete runs the work’s asynchronous operation. try_complete attempts a synchronous state change and returns the obligation if it is rejected. transfer converts the work, for example from a reservation to cleanup, without disarming the guard. Callers cannot mark work complete separately or replace it through a mutable reference.

Types using this wrapper should keep it in a private field and implement the operations their callers need. The traits are open to new implementations. Batches that report no pending work can be dropped without completing them.

§Panics

Dropping unfinished work emits an error event and panics in tests, under the test-support feature or when debug assertions are enabled, unless the thread is already unwinding. Other release builds log without panicking. Dropping the guard never runs cleanup.

§Examples

A buffered writer can require a flush before it is dropped. Here a flush attempt completes the obligation and returns any I/O error to the caller:

use std::io;
use tokio::io::{AsyncWrite, AsyncWriteExt};
use o_sfu_transport::obligation::{Execute, Obligation, Work};

/// A writer with a pending flush.
struct Flush<W>(W);

impl<W> Work for Flush<W> {
    fn label(&self) -> &'static str {
        "flush buffered output"
    }
}

impl<W: AsyncWrite + Unpin + Send> Execute<()> for Flush<W> {
    type Output = io::Result<()>;

    async fn execute(mut self, (): ()) -> Self::Output {
        self.0.flush().await
    }
}

async fn flush_output<W: AsyncWrite + Unpin + Send>(writer: W) -> io::Result<()> {
    let pending = Obligation::new(Flush(writer));
    pending.complete(()).await
}

A work type can define a next step through Transfer. Conversion preserves any existing diagnostic label. The returned work still needs to run:

use o_sfu_transport::obligation::{Obligation, Transfer};

fn next_step<W: Transfer>(pending: Obligation<W>) -> Obligation<W::Target> {
    pending.transfer()
}

Fields§

§work: W§guard: Guard

Implementations§

§

impl<W> Obligation<W>
where W: Work,

pub fn new(work: W) -> Obligation<W>

Guards work without starting it.

The diagnostic label is a static string and needs no allocation. Work that reports nothing pending starts with an unarmed guard.

pub const fn work(&self) -> &W

Borrows the work without allowing it to be removed or replaced.

pub async fn complete<C>(self, context: C) -> <W as Execute<C>>::Output
where W: Execute<C>,

Runs the work and disarms the guard after execution returns.

Execute::execute must attempt any required fallback before returning, even if that fallback also fails. The guard moves into the future when it is created and stays armed while execution is pending. Dropping the future reports unfinished work, including before its first poll.

pub fn try_complete<C>( self, context: C, ) -> Result<<W as TryComplete<C>>::Output, Obligation<W>>
where W: TryComplete<C>,

Attempts a synchronous state change and keeps rejected work guarded.

§Errors

Returns Err(Self) when TryComplete::try_complete rejects the change. The returned obligation is still armed with the same label. The caller can retry or transfer the work to cleanup.

pub fn transfer(self) -> Obligation<<W as Transfer>::Target>
where W: Transfer,

Converts work through Transfer and preserves an armed guard’s label.

A pending target arms an unarmed guard with its own label. Transferring work never completes an existing obligation.

pub fn extend<T>(&mut self, items: impl IntoIterator<Item = T>)
where W: Extend<T>,

Adds work using the batch’s Extend implementation.

Adding work to an empty batch arms its guard. Extending an armed batch leaves it armed, even when the iterator is empty.

pub fn merge(&mut self, other: Obligation<W>)
where W: Extend<W>,

Combines both batches under one guard without running either batch.

Extend<Self> must move the donor’s work before its guard is disarmed. If both batches are armed, the combined batch keeps the destination’s label.

Trait Implementations§

§

impl<W> Debug for Obligation<W>
where W: Debug + Work,

§

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

Formats the value using the given formatter. Read more
§

impl<W> Default for Obligation<W>
where W: Work + Default,

§

fn default() -> Obligation<W>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<W> Freeze for Obligation<W>
where W: Freeze,

§

impl<W> RefUnwindSafe for Obligation<W>
where W: RefUnwindSafe,

§

impl<W> Send for Obligation<W>
where W: Send,

§

impl<W> Sync for Obligation<W>
where W: Sync,

§

impl<W> Unpin for Obligation<W>
where W: Unpin,

§

impl<W> UnsafeUnpin for Obligation<W>
where W: UnsafeUnpin,

§

impl<W> UnwindSafe for Obligation<W>
where W: UnwindSafe,

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FutureExt for T

§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

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

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<T> CryptoSafe for T
where T: Send + Sync + Debug,

§

impl<T> CryptoSafe for T

§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,