1use crate::ActorError;
2
3#[derive(Debug)]
4pub struct Message {
5 inner: Vec<u8>,
6 result_tx: Option<tokio::sync::oneshot::Sender<Vec<u8>>>,
7}
8impl Clone for Message {
9 fn clone(&self) -> Self {
10 Self {
11 inner: self.inner.clone(),
12 result_tx: None,
13 }
14 }
15}
16
17impl Message {
18 pub fn new(inner: Vec<u8>, result_tx: Option<tokio::sync::oneshot::Sender<Vec<u8>>>) -> Self {
19 Self { inner, result_tx }
20 }
21
22 pub fn from<T>(inner: T, result_tx: Option<tokio::sync::oneshot::Sender<Vec<u8>>>) -> Self
23 where
24 T: serde::Serialize,
25 {
26 let inner = bincode::serialize(&inner).unwrap();
27 Self { inner, result_tx }
28 }
29
30 pub fn inner(&self) -> &Vec<u8> {
31 &self.inner
32 }
33
34 pub fn result_tx(&mut self) -> Option<tokio::sync::oneshot::Sender<Vec<u8>>> {
35 let tx = self.result_tx.take();
36 self.result_tx = None;
37 tx
38 }
39
40 pub fn serialize<T>(&self) -> Result<Vec<u8>, ActorError>
41 where
42 T: serde::Serialize,
43 {
44 Ok(bincode::serialize(&self.inner)?)
45 }
46
47 pub fn deserialize<R>(&self) -> Result<R, ActorError>
48 where
49 R: serde::de::DeserializeOwned,
50 {
51 Ok(bincode::deserialize::<R>(&self.inner)?)
52 }
53}
54
55pub struct JobSpec {
56 max_iter: Option<usize>,
57 interval: Option<std::time::Duration>,
58 start_at: std::time::SystemTime,
59}
60
61impl JobSpec {
62 pub fn new(
63 max_iter: Option<usize>,
64 interval: Option<std::time::Duration>,
65 start_at: std::time::SystemTime,
66 ) -> Self {
67 if let None = interval {
68 Self {
69 max_iter: Some(1),
70 interval,
71 start_at,
72 }
73 } else {
74 Self {
75 max_iter,
76 interval,
77 start_at,
78 }
79 }
80 }
81
82 pub fn max_iter(&self) -> Option<usize> {
83 self.max_iter
84 }
85
86 pub fn start_at(&self) -> std::time::SystemTime {
87 self.start_at
88 }
89
90 pub fn interval(&self) -> Option<std::time::Duration> {
91 self.interval
92 }
93}
94
95impl Default for JobSpec {
96 fn default() -> Self {
97 Self {
98 max_iter: Some(1),
99 interval: None,
100 start_at: std::time::SystemTime::now(),
101 }
102 }
103}