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
use std::convert::Infallible;
use std::future::Future;
use crate::context::Context;
use crate::exit::Exit;
use crate::{ArcError, Never};
pub trait Actor<'a, A, M>: Send + Sync + 'static {
type Out: IntoExitReason;
type Fut: Future<Output = Self::Out> + Send + Sync + 'a;
fn run(self, context: &'a mut Context<M>, args: A) -> Self::Fut;
}
pub trait IntoExitReason {
fn into_exit_reason(self) -> Exit;
}
impl IntoExitReason for Exit {
fn into_exit_reason(self) -> Exit {
self
}
}
impl IntoExitReason for () {
fn into_exit_reason(self) -> Exit {
Default::default()
}
}
impl IntoExitReason for Infallible {
fn into_exit_reason(self) -> Exit {
unreachable!("Whoa! An instance of {}: {:?}", std::any::type_name::<Self>(), self)
}
}
impl<E> IntoExitReason for Result<(), E>
where
E: Into<ArcError>,
{
fn into_exit_reason(self) -> Exit {
if let Err(reason) = self {
Exit::custom(reason.into())
} else {
Exit::normal()
}
}
}
impl<E> IntoExitReason for Result<Never, E>
where
E: Into<ArcError>,
{
fn into_exit_reason(self) -> Exit {
if let Err(reason) = self {
Exit::custom(reason.into())
} else {
unreachable!()
}
}
}
impl IntoExitReason for Result<(), Exit> {
fn into_exit_reason(self) -> Exit {
if let Err(reason) = self {
reason
} else {
Exit::normal()
}
}
}
impl IntoExitReason for Result<Never, Exit> {
fn into_exit_reason(self) -> Exit {
if let Err(reason) = self {
reason
} else {
unreachable!()
}
}
}
impl<'a, A, M, F, Fut, Out> Actor<'a, A, M> for F
where
M: 'a,
F: FnOnce(&'a mut Context<M>, A) -> Fut,
Fut: Future<Output = Out> + 'a,
Fut: Send + Sync,
Out: IntoExitReason,
F: Send + Sync + 'static,
{
type Out = Out;
type Fut = Fut;
fn run(self, context: &'a mut Context<M>, args: A) -> Self::Fut {
self(context, args)
}
}