Skip to main content

BranchSet

Struct BranchSet 

Source
#[non_exhaustive]
pub struct BranchSet { /* private fields */ }
Expand description

A set of parallel branches whose results have different types, for DurableContext::parallel_mixed.

Each add or add_with_serdes call adds one branch and returns a BranchHandle typed with that branch’s result. Branches start, and are numbered, in the order they were added. The number of branches is unlimited.

§Examples

use aws_durable_execution_sdk as durable;
use durable::BranchSet;

async fn handler(
    _event: serde_json::Value,
    ctx: durable::DurableContext,
) -> Result<String, durable::BoxError> {
    let mut branches = BranchSet::new();
    let count = branches.add("count", |_ctx| async { Ok(7_u32) });
    let label = branches.add("label", |_ctx| async { Ok("ready".to_owned()) });
    ctx.parallel_mixed(branches).name("fan-out").await?;
    Ok(format!("{}: {}", count.into_result()?, label.into_result()?))
}

Implementations§

Source§

impl BranchSet

Source

pub fn new() -> Self

Creates an empty set.

Source

pub fn add<T, F, Fut>( &mut self, name: impl Into<String>, factory: F, ) -> BranchHandle<T>
where T: Serialize + DeserializeOwned + Send + 'static, F: FnOnce(DurableContext) -> Fut + Send + 'static, Fut: Future<Output = Result<T, BoxError>> + Send + 'static,

Adds a branch whose result is encoded and decoded as JSON.

This is add_with_serdes with JsonSerdes. The batch record holds the branch’s value as JSON, as DurableContext::parallel does with its default serdes.

Source

pub fn add_with_serdes<T, S, F, Fut>( &mut self, name: impl Into<String>, serdes: S, factory: F, ) -> BranchHandle<T>
where T: Send + 'static, S: Serdes<T> + 'static, F: FnOnce(DurableContext) -> Fut + Send + 'static, Fut: Future<Output = Result<T, BoxError>> + Send + 'static,

Adds a branch whose result is encoded and decoded by serdes.

name and factory have the same types as in Branch::new. History records name, or parallel-branch-{index} when it is empty, and a completion predicate sees Some(name). serdes must implement Serdes<T>, so a serdes written for another type fails to compile. It is called wherever the item serdes of DurableContext::parallel is called for this branch, with the same SerdesContext. To share one serdes across branches, pass a clone of one Arc to each call.

§Examples
use std::sync::Arc;

use aws_durable_execution_sdk as durable;
use durable::serdes::{FileSystemSerdes, SerdesContext};
use durable::{BoxError, BranchSet, Serdes};

struct Order {
    id: u64,
}

// A serdes for `Order` only: attaching it to a branch of another
// type fails to compile.
struct OrderSerdes;

impl Serdes<Order> for OrderSerdes {
    async fn serialize(&self, value: Order, _ctx: SerdesContext) -> Result<String, BoxError> {
        Ok(value.id.to_string())
    }
    async fn deserialize(&self, wire: String, _ctx: SerdesContext) -> Result<Order, BoxError> {
        Ok(Order { id: wire.parse()? })
    }
}

async fn handler(
    _event: serde_json::Value,
    ctx: durable::DurableContext,
) -> Result<u64, durable::BoxError> {
    let files = Arc::new(FileSystemSerdes::new("/mnt/efs"));
    let mut branches = BranchSet::new();
    let order = branches.add_with_serdes("order", OrderSerdes, |_ctx| async {
        Ok(Order { id: 42 })
    });
    let report = branches.add_with_serdes("report", Arc::clone(&files), |_ctx| async {
        Ok("a large report".to_owned())
    });
    ctx.parallel_mixed(branches).name("process-order").await?;
    let order = order.into_result()?;
    let _report: String = report.into_result()?;
    Ok(order.id)
}

Trait Implementations§

Source§

impl Debug for BranchSet

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for BranchSet

Source§

fn default() -> Self

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

Auto Trait Implementations§

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.

Source§

impl<T> Instrument for T

Source§

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

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

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
Source§

impl<Unshared, Shared> IntoShared<Shared> for Unshared
where Shared: FromUnshared<Unshared>,

Source§

fn into_shared(self) -> Shared

Creates a shared type from an unshared type.
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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.
Source§

impl<T> WithSubscriber for T

Source§

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
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

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