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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
impl<A> ActorRef<A>
where
A: Actor,
{
/// Sends a message to the actor and waits for a reply.
///
/// The `ask` pattern is used when you expect a response from the actor. This method returns
/// an `AskRequest`, which can be awaited asynchronously, or sent in a blocking manner using one of the [`request`](crate::request) traits.
///
/// # Example
///
/// ```
/// use piying::actor::{Actor, ActorRef, Spawn};
///
/// # #[derive(piying::Actor)]
/// # struct MyActor;
/// #
/// # struct Msg;
/// #
/// # impl piying::message::Message<Msg> for MyActor {
/// # type Reply = ();
/// # async fn handle(&mut self, msg: Msg, ctx: &mut piying::message::Context<Self, Self::Reply>) -> Self::Reply { }
/// # }
/// #
/// # tokio_test::block_on(async {
/// let actor_ref = MyActor::spawn(MyActor);
/// # let msg = Msg;
/// let reply = actor_ref.ask(msg).await?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// # });
/// ```
#[inline]
#[track_caller]
#[doc(alias = "send")]
pub fn ask<M>(
&self,
msg: M,
) -> AskRequest<'_, A, M, WithoutRequestTimeout, WithoutRequestTimeout>
where
A: Message<M>,
M: Send + 'static,
{
AskRequest::new(
self,
msg,
#[cfg(all(debug_assertions, feature = "tracing"))]
std::panic::Location::caller(),
)
}
/// Sends a message to the actor without waiting for a reply.
///
/// The `tell` pattern is used for one-way communication, where no response is expected from the actor. This method
/// returns a `TellRequest`, which can be awaited asynchronously, or configured using one of the [`request`](crate::request) traits.
///
/// # Example
///
/// ```
/// use piying::actor::{Actor, ActorRef, Spawn};
///
/// # #[derive(piying::Actor)]
/// # struct MyActor;
/// #
/// # struct Msg;
/// #
/// # impl piying::message::Message<Msg> for MyActor {
/// # type Reply = ();
/// # async fn handle(&mut self, msg: Msg, ctx: &mut piying::message::Context<Self, Self::Reply>) -> Self::Reply { }
/// # }
/// #
/// # tokio_test::block_on(async {
/// let actor_ref = MyActor::spawn(MyActor);
/// # let msg = Msg;
/// actor_ref.tell(msg).await?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// # });
/// ```
#[inline]
#[track_caller]
#[doc(alias = "send_async")]
pub fn tell<M>(&self, msg: M) -> TellRequest<'_, A, M, WithoutRequestTimeout>
where
A: Message<M>,
M: Send + 'static,
{
TellRequest::new(
self,
msg,
#[cfg(all(debug_assertions, feature = "tracing"))]
std::panic::Location::caller(),
)
}
}