Skip to main content

atomr_core/pattern/
pipe_to.rs

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