@@ -50,17 +50,23 @@ impl<B> Coordinator<B>
50
50
where
51
51
B : Backend ,
52
52
{
53
- pub fn new ( backend : B ) -> Result < Self , Error > {
53
+ pub async fn new ( backend : B ) -> Result < Self , Error > {
54
54
let token = CancellationToken :: new ( ) ;
55
55
56
56
let [ stable, beta, nightly] =
57
57
Channel :: ALL . map ( |channel| Container :: new ( channel, token. clone ( ) , & backend) ) ;
58
58
59
+ let ( stable, beta, nightly) = join ! ( stable, beta, nightly) ;
60
+
61
+ let stable = stable?;
62
+ let beta = beta?;
63
+ let nightly = nightly?;
64
+
59
65
Ok ( Self {
60
66
backend,
61
- stable : stable? ,
62
- beta : beta? ,
63
- nightly : nightly? ,
67
+ stable,
68
+ beta,
69
+ nightly,
64
70
token,
65
71
} )
66
72
}
@@ -110,19 +116,24 @@ where
110
116
}
111
117
112
118
impl Coordinator < DockerBackend > {
113
- pub fn new_docker ( ) -> Result < Self , Error > {
114
- Self :: new ( DockerBackend ( ( ) ) )
119
+ pub async fn new_docker ( ) -> Result < Self , Error > {
120
+ Self :: new ( DockerBackend ( ( ) ) ) . await
115
121
}
116
122
}
117
123
118
124
#[ derive( Debug ) ]
119
125
pub struct Container {
120
126
task : JoinHandle < Result < ( ) > > ,
127
+ modify_cargo_toml : ModifyCargoToml ,
121
128
commander : Commander ,
122
129
}
123
130
124
131
impl Container {
125
- fn new ( channel : Channel , token : CancellationToken , backend : & impl Backend ) -> Result < Self > {
132
+ async fn new (
133
+ channel : Channel ,
134
+ token : CancellationToken ,
135
+ backend : & impl Backend ,
136
+ ) -> Result < Self > {
126
137
let ( mut child, stdin, stdout) = backend. run_worker_in_background ( channel) ?;
127
138
let IoQueue {
128
139
mut tasks,
@@ -151,7 +162,15 @@ impl Container {
151
162
id : Default :: default ( ) ,
152
163
} ;
153
164
154
- Ok ( Container { task, commander } )
165
+ let modify_cargo_toml = ModifyCargoToml :: new ( commander. clone ( ) )
166
+ . await
167
+ . context ( CouldNotLoadCargoTomlSnafu ) ?;
168
+
169
+ Ok ( Container {
170
+ task,
171
+ modify_cargo_toml,
172
+ commander,
173
+ } )
155
174
}
156
175
157
176
pub async fn compile (
@@ -195,7 +214,7 @@ impl Container {
195
214
} ;
196
215
197
216
let write_main = self . commander . one ( write_main) ;
198
- let modify_cargo_toml = modify_cargo_toml ( & self . commander , & request) ;
217
+ let modify_cargo_toml = self . modify_cargo_toml . modify_for ( & request) ;
199
218
200
219
let ( write_main, modify_cargo_toml) = join ! ( write_main, modify_cargo_toml) ;
201
220
@@ -257,8 +276,13 @@ impl Container {
257
276
}
258
277
259
278
pub async fn shutdown ( self ) -> Result < ( ) > {
260
- let Self { task, commander } = self ;
279
+ let Self {
280
+ task,
281
+ modify_cargo_toml,
282
+ commander,
283
+ } = self ;
261
284
drop ( commander) ;
285
+ drop ( modify_cargo_toml) ;
262
286
task. await . context ( ContainerTaskPanickedSnafu ) ?
263
287
}
264
288
}
@@ -302,41 +326,61 @@ struct Commander {
302
326
id : Arc < AtomicU64 > ,
303
327
}
304
328
305
- // Consider making this into a top-level message to the worker to avoid the roundtrip.
306
- async fn modify_cargo_toml (
307
- commander : & Commander ,
308
- request : & CompileRequest ,
309
- ) -> Result < ( ) , ModifyCargoTomlError > {
310
- use modify_cargo_toml_error:: * ;
329
+ #[ derive( Debug ) ]
330
+ struct ModifyCargoToml {
331
+ commander : Commander ,
332
+ cargo_toml : toml:: Value ,
333
+ }
311
334
335
+ impl ModifyCargoToml {
312
336
const PATH : & str = "Cargo.toml" ;
313
337
314
- let read_request = ReadFileRequest {
315
- path : PATH . to_owned ( ) ,
316
- } ;
317
- let cargo_toml = commander
318
- . one ( read_request )
319
- . await
320
- . context ( CouldNotReadSnafu ) ? ;
338
+ async fn new ( commander : Commander ) -> Result < Self , ModifyCargoTomlError > {
339
+ let cargo_toml = Self :: read ( & commander ) . await ? ;
340
+ Ok ( Self {
341
+ commander,
342
+ cargo_toml ,
343
+ } )
344
+ }
321
345
322
- let cargo_toml = String :: from_utf8 ( cargo_toml. 0 ) ?;
323
- let cargo_toml = toml:: from_str ( & cargo_toml) ?;
346
+ async fn modify_for ( & self , request : & CompileRequest ) -> Result < ( ) , ModifyCargoTomlError > {
347
+ let cargo_toml = self . cargo_toml . clone ( ) ;
348
+ let cargo_toml = request. modify_cargo_toml ( cargo_toml) ;
349
+ Self :: write ( & self . commander , cargo_toml) . await
350
+ }
351
+
352
+ async fn read ( commander : & Commander ) -> Result < toml:: Value , ModifyCargoTomlError > {
353
+ use modify_cargo_toml_error:: * ;
324
354
325
- let cargo_toml = request. modify_cargo_toml ( cargo_toml) ;
355
+ let path = Self :: PATH . to_owned ( ) ;
356
+ let cargo_toml = commander
357
+ . one ( ReadFileRequest { path } )
358
+ . await
359
+ . context ( CouldNotReadSnafu ) ?;
326
360
327
- let cargo_toml = toml :: to_string ( & cargo_toml) ?;
328
- let cargo_toml = cargo_toml . into_bytes ( ) ;
361
+ let cargo_toml = String :: from_utf8 ( cargo_toml. 0 ) ?;
362
+ let cargo_toml = toml :: from_str ( & cargo_toml ) ? ;
329
363
330
- let write_request = WriteFileRequest {
331
- path : PATH . to_owned ( ) ,
332
- content : cargo_toml,
333
- } ;
334
- commander
335
- . one ( write_request)
336
- . await
337
- . context ( CouldNotWriteSnafu ) ?;
364
+ Ok ( cargo_toml)
365
+ }
366
+
367
+ async fn write (
368
+ commander : & Commander ,
369
+ cargo_toml : toml:: Value ,
370
+ ) -> Result < ( ) , ModifyCargoTomlError > {
371
+ use modify_cargo_toml_error:: * ;
372
+
373
+ let cargo_toml = toml:: to_string ( & cargo_toml) ?;
374
+ let content = cargo_toml. into_bytes ( ) ;
375
+
376
+ let path = Self :: PATH . to_owned ( ) ;
377
+ commander
378
+ . one ( WriteFileRequest { path, content } )
379
+ . await
380
+ . context ( CouldNotWriteSnafu ) ?;
338
381
339
- Ok ( ( ) )
382
+ Ok ( ( ) )
383
+ }
340
384
}
341
385
342
386
#[ derive( Debug , Snafu ) ]
@@ -646,6 +690,9 @@ pub enum Error {
646
690
647
691
#[ snafu( display( "Failed to send worker message through channel" ) ) ]
648
692
UnableToSendWorkerMessage { source : mpsc:: error:: SendError < ( ) > } ,
693
+
694
+ #[ snafu( display( "Unable to load original Cargo.toml" ) ) ]
695
+ CouldNotLoadCargoToml { source : ModifyCargoTomlError } ,
649
696
}
650
697
651
698
struct IoQueue {
@@ -779,9 +826,9 @@ mod tests {
779
826
}
780
827
}
781
828
782
- fn new_coordinator ( ) -> Result < Coordinator < impl Backend > > {
783
- Coordinator :: new ( TestBackend :: new ( ) )
784
- //Coordinator::new_docker()
829
+ async fn new_coordinator ( ) -> Result < Coordinator < impl Backend > > {
830
+ Coordinator :: new ( TestBackend :: new ( ) ) . await
831
+ //Coordinator::new_docker().await
785
832
}
786
833
787
834
fn new_compile_request ( ) -> CompileRequest {
@@ -864,7 +911,7 @@ mod tests {
864
911
#[ tokio:: test]
865
912
#[ snafu:: report]
866
913
async fn test_compile_response ( ) -> Result < ( ) > {
867
- let coordinator = new_coordinator ( ) ?;
914
+ let coordinator = new_coordinator ( ) . await ?;
868
915
869
916
let response = coordinator
870
917
. compile ( new_compile_request ( ) )
@@ -884,7 +931,7 @@ mod tests {
884
931
#[ tokio:: test]
885
932
#[ snafu:: report]
886
933
async fn test_compile_streaming ( ) -> Result < ( ) > {
887
- let coordinator = new_coordinator ( ) ?;
934
+ let coordinator = new_coordinator ( ) . await ?;
888
935
889
936
let ActiveCompilation {
890
937
task,
@@ -919,7 +966,7 @@ mod tests {
919
966
#[ snafu:: report]
920
967
async fn test_compile_edition ( ) -> Result < ( ) > {
921
968
for edition in Edition :: ALL {
922
- let coordinator = new_coordinator ( ) ?;
969
+ let coordinator = new_coordinator ( ) . await ?;
923
970
924
971
let response = coordinator
925
972
. compile ( new_compile_hir_request_for ( edition) )
@@ -941,7 +988,7 @@ mod tests {
941
988
#[ tokio:: test]
942
989
#[ snafu:: report]
943
990
async fn test_compile_assembly ( ) -> Result < ( ) > {
944
- let coordinator = new_coordinator ( ) ?;
991
+ let coordinator = new_coordinator ( ) . await ?;
945
992
946
993
let response = coordinator
947
994
. compile ( new_compile_assembly_request ( ) )
@@ -966,7 +1013,7 @@ mod tests {
966
1013
#[ tokio:: test]
967
1014
#[ snafu:: report]
968
1015
async fn test_compile_hir ( ) -> Result < ( ) > {
969
- let coordinator = new_coordinator ( ) ?;
1016
+ let coordinator = new_coordinator ( ) . await ?;
970
1017
971
1018
let response = coordinator
972
1019
. compile ( new_compile_hir_request ( ) )
@@ -985,7 +1032,7 @@ mod tests {
985
1032
#[ tokio:: test]
986
1033
#[ snafu:: report]
987
1034
async fn test_compile_llvm_ir ( ) -> Result < ( ) > {
988
- let coordinator = new_coordinator ( ) ?;
1035
+ let coordinator = new_coordinator ( ) . await ?;
989
1036
990
1037
let response = coordinator
991
1038
. compile ( new_compile_llvm_ir_request ( ) )
@@ -1005,7 +1052,7 @@ mod tests {
1005
1052
#[ snafu:: report]
1006
1053
async fn test_compile_wasm ( ) -> Result < ( ) > {
1007
1054
// cargo-wasm only exists inside the container
1008
- let coordinator = Coordinator :: new_docker ( ) ?;
1055
+ let coordinator = Coordinator :: new_docker ( ) . await ?;
1009
1056
1010
1057
let response = coordinator
1011
1058
. compile ( new_compile_wasm_request ( ) )
0 commit comments