Crate scuffle_context

Source
Expand description

A crate designed to provide the ability to cancel futures using a context go-like approach, allowing for graceful shutdowns and cancellations.

See the changelog for a full release history.

§Feature flags

  • docs — Enables changelog and documentation of feature flags

§Why do we need this?

Its often useful to wait for all the futures to shutdown or to cancel them when we no longer care about the results. This crate provides an interface to cancel all futures associated with a context or wait for them to finish before shutting down. Allowing for graceful shutdowns and cancellations.

§Usage

Here is an example of how to use the Context to cancel a spawned task.

let (ctx, handler) = Context::new();

tokio::spawn(async {
    // Do some work
    tokio::time::sleep(std::time::Duration::from_secs(10)).await;
}.with_context(ctx));

// Will stop the spawned task and cancel all associated futures.
handler.cancel();

§License

This project is licensed under the MIT or Apache-2.0 license. You can choose between one of them if you use this work.

SPDX-License-Identifier: MIT OR Apache-2.0

Modules§

changelog
Changelogs generated by [scuffle_changelog]

Structs§

Context
A context for cancelling futures and waiting for shutdown.
Handler
A handler is used to manage contexts and to cancel them.
ContextRef
A reference to a context which implements Future and can be polled. Can either be owned or borrowed.
FutureWithContext
A future with a context attached to it.

Traits§

ContextFutExt
Extends a future with useful functions.

Structs§

StreamWithContext
A stream with a context attached to it.

Traits§

ContextStreamExt
Extends a stream with useful functions.