crb_agent/performers/
loopback.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use crate::address::Envelope;
use crate::agent::Agent;
use crate::message::loopback::{InContext, LoopbackEvent};
use crate::performers::{Next, StatePerformer, Transition, TransitionCommand};
use async_trait::async_trait;

impl<T> Next<T>
where
    T: Agent,
{
    pub fn in_context<E>(&self, event: E) -> Self
    where
        T: InContext<E>,
        E: Send + 'static,
    {
        let event = LoopbackEvent::new(event);
        Self::new(Loopback {
            envelope: Some(Box::new(event)),
        })
    }
}

pub struct Loopback<T> {
    envelope: Option<Envelope<T>>,
}

#[async_trait]
impl<T> StatePerformer<T> for Loopback<T>
where
    T: Agent,
{
    async fn perform(&mut self, agent: T, _session: &mut T::Context) -> Transition<T> {
        let envelope = self
            .envelope
            .take()
            .expect("Loopback performer must be called once");
        let command = TransitionCommand::InContext(envelope);
        Transition::Continue { agent, command }
    }
}