Skip to main content

atomr_streams/
runnable.rs

1//! RunnableGraph — a `Source` + terminal `Sink` waiting to be materialized.
2//! akka.net: `Dsl/RunnableGraph.cs`.
3
4use std::future::Future;
5
6use crate::source::Source;
7
8pub struct RunnableGraph<M> {
9    runner: Box<dyn FnOnce() -> futures::future::BoxFuture<'static, M> + Send + 'static>,
10}
11
12impl<M: Send + 'static> RunnableGraph<M> {
13    pub fn new<F, Fut>(f: F) -> Self
14    where
15        F: FnOnce() -> Fut + Send + 'static,
16        Fut: Future<Output = M> + Send + 'static,
17    {
18        use futures::FutureExt;
19        RunnableGraph { runner: Box::new(move || f().boxed()) }
20    }
21
22    pub async fn run(self) -> M {
23        (self.runner)().await
24    }
25}
26
27impl<T: Send + 'static> RunnableGraph<Vec<T>> {
28    pub fn to_seq(source: Source<T>) -> Self {
29        Self::new(move || crate::sink::Sink::collect(source))
30    }
31}