use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
pub type PipeFn<T> =
Box<dyn Fn(T, Next<T>) -> Pin<Box<dyn Future<Output = T> + Send>> + Send + Sync>;
#[allow(clippy::type_complexity)]
pub struct Next<T> {
next: Box<dyn FnOnce(T) -> Pin<Box<dyn Future<Output = T> + Send>> + Send>,
}
impl<T: Send + 'static> Next<T> {
pub async fn call(self, value: T) -> T {
(self.next)(value).await
}
}
#[async_trait::async_trait]
pub trait Pipe<T: Send + 'static>: Send + Sync {
async fn handle(&self, value: T, next: Next<T>) -> T;
}
pub fn into_pipe_fn<T, P>(pipe: P) -> PipeFn<T>
where
T: Send + 'static,
P: Pipe<T> + 'static,
{
let pipe = Arc::new(pipe);
Box::new(move |value, next| {
let pipe = pipe.clone();
Box::pin(async move { pipe.handle(value, next).await })
})
}
pub fn pipe_fn<T, F, Fut>(f: F) -> PipeFn<T>
where
T: Send + 'static,
F: Fn(T, Next<T>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = T> + Send + 'static,
{
Box::new(move |value, next| Box::pin(f(value, next)))
}
pub struct Pipeline<T: Send + 'static> {
value: Option<T>,
pipes: Vec<PipeFn<T>>,
}
impl<T: Send + 'static> Pipeline<T> {
pub fn send(value: T) -> Self {
Self {
value: Some(value),
pipes: vec![],
}
}
pub fn through(mut self, pipes: Vec<PipeFn<T>>) -> Self {
self.pipes = pipes;
self
}
#[allow(clippy::type_complexity)]
pub async fn then<F, Fut>(mut self, destination: F) -> T
where
F: FnOnce(T) -> Fut + Send + 'static,
Fut: Future<Output = T> + Send + 'static,
{
let value = self.value.take().expect("Pipeline already executed");
let mut next: Box<dyn FnOnce(T) -> Pin<Box<dyn Future<Output = T> + Send>> + Send> =
Box::new(move |val| Box::pin(destination(val)));
for pipe in self.pipes.into_iter().rev() {
let prev_next = next;
next = Box::new(move |val| {
let next = Next { next: prev_next };
pipe(val, next)
});
}
next(value).await
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_empty_pipeline() {
let result = Pipeline::send(42).then(|val| async move { val }).await;
assert_eq!(result, 42);
}
#[tokio::test]
async fn test_single_pipe() {
let result = Pipeline::send(1)
.through(vec![pipe_fn(|value: i32, next: Next<i32>| async move {
next.call(value + 1).await
})])
.then(|val| async move { val })
.await;
assert_eq!(result, 2);
}
#[tokio::test]
async fn test_multiple_pipes() {
let result = Pipeline::send(1)
.through(vec![
pipe_fn(|value: i32, next: Next<i32>| async move { next.call(value + 1).await }),
pipe_fn(|value: i32, next: Next<i32>| async move { next.call(value * 2).await }),
pipe_fn(|value: i32, next: Next<i32>| async move { next.call(value - 3).await }),
])
.then(|val| async move { val })
.await;
assert_eq!(result, 1);
}
#[tokio::test]
async fn test_pipes_execute_in_order() {
let log = std::sync::Arc::new(std::sync::Mutex::new(vec![]));
let log1 = log.clone();
let log2 = log.clone();
let log_dest = log.clone();
let result = Pipeline::send(0)
.through(vec![
pipe_fn(move |value: i32, next: Next<i32>| {
let log = log1.clone();
async move {
log.lock().unwrap().push("pipe1");
next.call(value).await
}
}),
pipe_fn(move |value: i32, next: Next<i32>| {
let log = log2.clone();
async move {
log.lock().unwrap().push("pipe2");
next.call(value).await
}
}),
])
.then(move |val| {
let log = log_dest.clone();
async move {
log.lock().unwrap().push("dest");
val
}
})
.await;
assert_eq!(result, 0);
let log = log.lock().unwrap();
assert_eq!(*log, vec!["pipe1", "pipe2", "dest"]);
}
#[tokio::test]
async fn test_struct_pipe() {
use async_trait::async_trait;
struct AddPipe(i32);
#[async_trait]
impl Pipe<i32> for AddPipe {
async fn handle(&self, value: i32, next: Next<i32>) -> i32 {
next.call(value + self.0).await
}
}
let result = Pipeline::send(10)
.through(vec![into_pipe_fn(AddPipe(5))])
.then(|val| async move { val })
.await;
assert_eq!(result, 15);
}
#[tokio::test]
async fn test_pipe_does_not_need_to_call_next() {
let result = Pipeline::send("hello")
.through(vec![pipe_fn(
|_value: &'static str, _next: Next<&'static str>| async move { "intercepted" },
)])
.then(|val| async move { val })
.await;
assert_eq!(result, "intercepted");
}
#[tokio::test]
async fn test_pipeline_with_strings() {
let result = Pipeline::send("hello")
.through(vec![pipe_fn(
|_value: &'static str, next: Next<&'static str>| async move {
next.call("modified").await
},
)])
.then(|val| async move { val })
.await;
assert_eq!(result, "modified");
}
}