#[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
impl BranchSet
Sourcepub 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,
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.
Sourcepub fn add_with_serdes<T, S, F, Fut>(
&mut self,
name: impl Into<String>,
serdes: S,
factory: F,
) -> BranchHandle<T>
pub fn add_with_serdes<T, S, F, Fut>( &mut self, name: impl Into<String>, serdes: S, factory: F, ) -> BranchHandle<T>
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§
Auto Trait Implementations§
impl !RefUnwindSafe for BranchSet
impl !Sync for BranchSet
impl !UnwindSafe for BranchSet
impl Forget for BranchSet
impl Freeze for BranchSet
impl Move for BranchSet
impl Send for BranchSet
impl Unpin for BranchSet
impl UnsafeUnpin for BranchSet
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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§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