Crate collections_futures

Crate collections_futures 

Source
Expand description

A way to manage multiple futures and their execution processing & results in a tree like structure.

Below is a example of how to use the TreeFuture struct to manage multiple futures and their results.

    use futures::stream::StreamExt;
    use collections_futures::TreeFuture;

    let mut tree_future = TreeFuture::new();
    let some_async_block = async move {
        // insert some futures which will return value upon completion
        tree_future.insert_future(1, async { 10 });
        tree_future.insert_future(2, async { 20 });

        let mut sum = 0;
        while let Some((task_id, result)) = tree_future.next().await {
            println!("Task ID: {}, Result: {:?}", task_id, result);
            sum += result.unwrap();
        }
        sum
    };
    assert_eq!(futures::executor::block_on(some_async_block), 30);

Or if you wish to utilize abort handle for a specific future within this collection which you already supplied you may do so like this, during time of initialization

    use futures::stream::StreamExt;
    use collections_futures::TreeFuture;

    let mut tree_future = TreeFuture::new();

    let async_block = async move {
        let abort_handle = tree_future.insert_abortable_future(1, async { 30 });

        abort_handle.abort(); // abort the future with task_id 1

        // returns (i32, Err(futures::stream::Aborted)) tuple
        let (task_id, future_err) = tree_future.next().await.unwrap();

        future_err
    };

    assert!(futures::executor::block_on(async_block).is_err());

Structs§

TreeFuture
Composed type which can hold multiple futures and poll them in order of insertion under a BTreeMap
TreeFutureLocal
Non-send flavour of TreeFuture which allows Creating a composed type that can hold multiple futures and poll them in order of insertion using a BTreeMap

Traits§

TreeFutureError
Trait for generating objects as errors
TreeFutureOutput
A marker trait coupling the output of the future with the tree future

Type Aliases§

BoxedResultFuture
Boxed future type which can hold any future and maps them to returns a Result<T, Box<dyn TreeFutureError>>
BoxedResultFutureLocal