pub struct Baton<'c> { /* private fields */ }
Expand description

Obligation to do work, or notify someone else to do it

Task cancellation in async Rust

In async Rust, futures can be cancelled at any await point, and simply discarded, if whatever was waiting for them loses interest. From the point of view of a reader of the async block, this is as if the code simply stopped running at some await point, for reasons outside of its own control, and discarded all of its state.

When notify_one is being used, there is therefore a risk that the waiting task that notify_one chooses to wake up gets cancelled before it is able to do the work that the notifier intended.

(This risk only arises if the process of responding to the notification might await. In that case you will also want to be using an async mutex, since it is generally forbidden to await with a sync mutex held.)

Baton helps with this risk. Option<Baton> is returned by wait_baton, and should be kept until the work is completed, and then disposed.

If the Baton is simply dropped (for example, due to task cancellation), the condvar will be re-notified.

How to handle a Baton

Use wait_baton rather than plain wait. When wait_baton completes, keep the baton while you do whatever work there is to be done.

After having done the necessary work, as the caller of notify_one was expecting, call Baton::dispose.

Infinite loop, or even livelock, risk

It is important to dispose of the baton even if your processing suffers a (possibly persistent) error. If you accidentally drop the baton (eg on an error path), another task will be woken up and perhaps perform the same failing actions, leading to the program looping uselessly, eating cpu.

Depending on your runtime’s scheduler, that might even be a livelock.

Implementations

Declare that responsibility has been discharged

The baton will be consumed, without generating any notifications.

Pass on the baton to someone else, if anyone else is waiting

This is equivalent to mem::drop.

Trait Implementations

Formats the value using the given formatter. Read more

Executes the destructor for this type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait. Read more

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait. Read more

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s. Read more

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s. Read more

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait. Read more

Use this to cast from one trait object type to another. Read more

Use this to upcast a trait to one of its supertraits. Read more

Use this to cast from one trait object type to another. This method is more customizable than the dyn_cast method. Here you can also specify the “source” trait from which the cast is defined. This can for example allow using casts from a supertrait of the current trait object. Read more

Use this to cast from one trait object type to another. With this method the type parameter is a config type that uniquely specifies which cast should be preformed. Read more

Returns the argument unchanged.

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

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

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

Should always be Self

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

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

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