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
97
98
99
100
101
#![deny(missing_docs)]
use serde::de::DeserializeOwned;
use serde_derive::{Deserialize, Serialize};
use std::{ffi::OsString, io::Error as IoError, panic::RefUnwindSafe, time::Duration};
use thiserror::Error;
mod alloc;
mod child;
mod error;
mod frame;
mod parent;
pub use alloc::Alloc;
pub use child::become_child;
pub use error::Error;
pub(crate) use error::ErrorResponse;
pub use parent::Sandbox;
#[derive(Serialize, Deserialize, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
#[non_exhaustive]
pub struct Response<Data> {
pub result: Data,
pub memory_used: usize,
pub time_taken: Duration,
pub stdout: String,
}
impl<Data> Response<Data> {
pub fn replace<New>(self, result: New) -> Response<New> {
let Response {
memory_used,
time_taken,
stdout,
..
} = self;
Response {
result,
memory_used,
time_taken,
stdout,
}
}
pub fn map<New>(self, func: impl FnOnce(Data) -> New) -> Response<New> {
let Response {
result,
memory_used,
time_taken,
stdout,
} = self;
Response {
result: func(result),
memory_used,
time_taken,
stdout,
}
}
}
pub trait Service: Sized + RefUnwindSafe + 'static {
type Req: serde::Serialize + DeserializeOwned;
type Res: serde::Serialize + DeserializeOwned;
type Config: serde::Serialize + DeserializeOwned + Clone + 'static;
fn args(config: &Self::Config) -> Vec<OsString>;
fn timeout(config: &Self::Config) -> Duration;
fn create(config: Self::Config) -> Result<Self, IoError>;
fn handle(&self, request: Self::Req) -> Self::Res;
}
pub(crate) type MessageRequest<S> = <S as Service>::Req;
pub(crate) type MessageResponse<S> = Response<Result<<S as Service>::Res, ErrorResponse>>;
pub(crate) type HandshakeRequest<S> = <S as Service>::Config;
pub(crate) type HandshakeResponse = Response<Result<(), String>>;