11use std:: sync:: Arc ;
22
3- #[ cfg( feature = "tokio" ) ]
4- use tokio:: runtime:: Handle ;
5-
63use crate :: exec:: inline:: InlineDriver ;
74#[ cfg( feature = "tokio" ) ]
85use crate :: exec:: tokio:: TokioDriver ;
@@ -19,29 +16,35 @@ pub enum ExecutionMode {
1916 RayonThreadPool ( Arc < rayon:: ThreadPool > ) ,
2017 /// Spawns the tasks onto a provided Tokio runtime.
2118 #[ cfg( feature = "tokio" ) ]
22- TokioRuntime ( Handle ) ,
19+ TokioRuntime ( tokio:: runtime:: Handle ) ,
20+ }
21+
22+ #[ allow( clippy:: derivable_impls) ]
23+ impl Default for ExecutionMode {
24+ fn default ( ) -> Self {
25+ // Default to tokio-specific behavior if its enabled and there's a runtime running.
26+ #[ cfg( feature = "tokio" ) ]
27+ if let Ok ( h) = tokio:: runtime:: Handle :: try_current ( ) {
28+ return ExecutionMode :: TokioRuntime ( h) ;
29+ }
30+
31+ ExecutionMode :: Inline
32+ }
2333}
2434
2535impl ExecutionMode {
26- pub fn into_driver ( self ) -> Arc < dyn ExecDriver > {
36+ pub fn into_driver ( self , concurrency : usize ) -> Arc < dyn ExecDriver > {
2737 match self {
28- ExecutionMode :: Inline => {
29- // Default to tokio-specific behavior if its enabled and there's a runtime running.
30- #[ cfg( feature = "tokio" ) ]
31- match Handle :: try_current ( ) {
32- Ok ( h) => Arc :: new ( TokioDriver ( h) ) ,
33- Err ( _) => Arc :: new ( InlineDriver ) ,
34- }
35-
36- #[ cfg( not( feature = "tokio" ) ) ]
37- Arc :: new ( InlineDriver )
38- }
38+ ExecutionMode :: Inline => Arc :: new ( InlineDriver :: with_concurrency ( concurrency) ) ,
3939 #[ cfg( feature = "rayon" ) ]
4040 ExecutionMode :: RayonThreadPool ( _) => {
4141 todo ! ( )
4242 }
4343 #[ cfg( feature = "tokio" ) ]
44- ExecutionMode :: TokioRuntime ( handle) => Arc :: new ( TokioDriver ( handle) ) ,
44+ ExecutionMode :: TokioRuntime ( handle) => Arc :: new ( TokioDriver {
45+ handle,
46+ concurrency,
47+ } ) ,
4548 }
4649 }
4750}
0 commit comments