33//!
44//! Please note that this API is not type-safe. For example, if you pass
55//! `Sandbox` instance to another backend, it will panic.
6+ use std:: { any:: Any , sync:: Arc } ;
67
78/// Type-erased `Sandbox`
89pub trait Sandbox : std:: fmt:: Debug {
9- fn id ( & self ) -> String ;
10+ fn id ( & self ) -> & str ;
1011 fn check_cpu_tle ( & self ) -> anyhow:: Result < bool > ;
1112 fn check_real_tle ( & self ) -> anyhow:: Result < bool > ;
1213 fn kill ( & self ) -> anyhow:: Result < ( ) > ;
1314 fn resource_usage ( & self ) -> anyhow:: Result < crate :: ResourceUsageData > ;
14- #[ doc( hidden) ]
15- fn clone_to_box ( & self ) -> Box < dyn Sandbox > ;
16- #[ doc( hidden) ]
17- fn clone_into_box_any ( & self ) -> Box < dyn std:: any:: Any > ;
18- }
19-
20- impl Clone for Box < dyn Sandbox > {
21- fn clone ( & self ) -> Self {
22- self . clone_to_box ( )
23- }
15+ fn into_arc_any ( self : Arc < Self > ) -> Arc < dyn Any + Send + Sync + ' static > ;
2416}
2517
2618impl < S : crate :: Sandbox > Sandbox for S {
27- fn id ( & self ) -> String {
19+ fn id ( & self ) -> & str {
2820 self . id ( )
2921 }
3022 fn check_cpu_tle ( & self ) -> anyhow:: Result < bool > {
@@ -39,12 +31,8 @@ impl<S: crate::Sandbox> Sandbox for S {
3931 fn resource_usage ( & self ) -> anyhow:: Result < crate :: ResourceUsageData > {
4032 self . resource_usage ( ) . map_err ( Into :: into)
4133 }
42- fn clone_to_box ( & self ) -> Box < dyn Sandbox > {
43- Box :: new ( self . clone ( ) )
44- }
45-
46- fn clone_into_box_any ( & self ) -> Box < dyn std:: any:: Any > {
47- Box :: new ( self . clone ( ) )
34+ fn into_arc_any ( self : Arc < Self > ) -> Arc < dyn Any + Send + Sync + ' static > {
35+ self
4836 }
4937}
5038
@@ -58,8 +46,7 @@ pub trait ChildProcess {
5846 & self ,
5947 timeout : Option < std:: time:: Duration > ,
6048 ) -> anyhow:: Result < crate :: WaitOutcome > ;
61- fn poll ( & self ) -> anyhow:: Result < ( ) > ;
62- fn is_finished ( & self ) -> anyhow:: Result < bool > ;
49+ fn is_finished ( & self ) -> bool ;
6350}
6451
6552impl < C : crate :: ChildProcess > ChildProcess for C {
@@ -90,50 +77,48 @@ impl<C: crate::ChildProcess> ChildProcess for C {
9077 ) -> anyhow:: Result < crate :: WaitOutcome > {
9178 self . wait_for_exit ( timeout) . map_err ( Into :: into)
9279 }
93- fn poll ( & self ) -> anyhow:: Result < ( ) > {
94- self . poll ( ) . map_err ( Into :: into)
95- }
96- fn is_finished ( & self ) -> anyhow:: Result < bool > {
97- self . is_finished ( ) . map_err ( Into :: into)
80+ fn is_finished ( & self ) -> bool {
81+ self . is_finished ( )
9882 }
9983}
10084
10185/// Type-erased `Backend`
10286pub trait Backend {
103- fn new_sandbox ( & self , options : crate :: SandboxOptions ) -> anyhow:: Result < Box < dyn Sandbox > > ;
87+ fn new_sandbox ( & self , options : crate :: SandboxOptions ) -> anyhow:: Result < Arc < dyn Sandbox > > ;
10488 fn spawn ( & self , options : ChildProcessOptions ) -> anyhow:: Result < Box < dyn ChildProcess > > ;
10589}
10690
10791impl < B : crate :: Backend > Backend for B {
108- fn new_sandbox ( & self , options : crate :: SandboxOptions ) -> anyhow:: Result < Box < dyn Sandbox > > {
92+ fn new_sandbox ( & self , options : crate :: SandboxOptions ) -> anyhow:: Result < Arc < dyn Sandbox > > {
10993 let sb = <Self as crate :: Backend >:: new_sandbox ( & self , options) ?;
110- Ok ( Box :: new ( sb) )
94+ Ok ( Arc :: new ( sb) )
11195 }
11296
11397 fn spawn ( & self , options : ChildProcessOptions ) -> anyhow:: Result < Box < dyn ChildProcess > > {
114- let down_sandbox = options
115- . sandbox
116- . clone_into_box_any ( )
117- . downcast ( )
118- . expect ( "sandbox type mismatch" ) ;
98+ let any_sandbox = options. sandbox . clone ( ) . into_arc_any ( ) ;
99+ let down_sandbox = any_sandbox. downcast ( ) . expect ( "sandbox type mismatch" ) ;
119100 let down_options = crate :: ChildProcessOptions {
120101 arguments : options. arguments ,
121102 environment : options. environment ,
122103 path : options. path ,
123104 pwd : options. pwd ,
124105 stdio : options. stdio ,
125- sandbox : * down_sandbox,
106+ sandbox : down_sandbox,
126107 } ;
127108 let cp = <Self as crate :: Backend >:: spawn ( & self , down_options) ?;
128109 Ok ( Box :: new ( cp) )
129110 }
130111}
131112
132- pub type ChildProcessOptions = crate :: ChildProcessOptions < Box < dyn Sandbox > > ;
113+ pub type ChildProcessOptions = crate :: ChildProcessOptions < dyn Sandbox > ;
133114
134115/// Returns backend instance
135116pub fn setup ( ) -> anyhow:: Result < Box < dyn Backend > > {
136- Ok ( Box :: new ( crate :: linux:: LinuxBackend :: new (
117+ #[ cfg( target_os = "linux" ) ]
118+ return Ok ( Box :: new ( crate :: linux:: LinuxBackend :: new (
137119 crate :: linux:: Settings :: new ( ) ,
138- ) ?) )
120+ ) ?) ) ;
121+
122+ #[ cfg( target_os = "windows" ) ]
123+ return Ok ( Box :: new ( crate :: windows:: WindowsBackend :: new ( ) ) ) ;
139124}
0 commit comments