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: GuardImplementations§
§impl<W> Obligation<W>where
W: Work,
impl<W> Obligation<W>where
W: Work,
pub fn new(work: W) -> Obligation<W>
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 async fn complete<C>(self, context: C) -> <W as Execute<C>>::Outputwhere
W: Execute<C>,
pub async fn complete<C>(self, context: C) -> <W as Execute<C>>::Outputwhere
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>,
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,
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>,
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>,
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>
impl<W> Debug for Obligation<W>
§impl<W> Default for Obligation<W>
impl<W> Default for Obligation<W>
§fn default() -> Obligation<W>
fn default() -> Obligation<W>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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