Skip to main content

atomr_core/pattern/
pipe_to.rs

1//! `PipeTo` — send the eventual output of a future to an actor.
2
3use std::future::Future;
4
5use crate::actor::ActorRef;
6
7pub fn pipe_to<M, F>(fut: F, target: ActorRef<M>)
8where
9    M: Send + 'static,
10    F: Future<Output = M> + Send + 'static,
11{
12    tokio::spawn(async move {
13        let m = fut.await;
14        target.tell(m);
15    });
16}
17
18#[cfg(test)]
19mod tests {
20    use super::*;
21    use crate::actor::Inbox;
22    use std::time::Duration;
23
24    #[tokio::test]
25    async fn pipes_future_to_actor() {
26        let mut inbox = Inbox::<u32>::new("pipe");
27        pipe_to(async { 42u32 }, inbox.actor_ref().clone());
28        let m = inbox.receive(Duration::from_millis(100)).await.unwrap();
29        assert_eq!(m, 42);
30    }
31}