1 //! Abstracts out the APIs necessary to `Runtime` for integrating the I/O
2 //! driver. When the `time` feature flag is **not** enabled. These APIs are
3 //! shells. This isolates the complexity of dealing with conditional
4 //! compilation.
5 
6 /// Re-exported for convenience.
7 pub(crate) use std::io::Result;
8 
9 pub(crate) use variant::*;
10 
11 #[cfg(feature = "io-driver")]
12 mod variant {
13     use crate::io::driver;
14     use crate::park::{Either, ParkThread};
15 
16     use std::io;
17 
18     /// The driver value the runtime passes to the `timer` layer.
19     ///
20     /// When the `io-driver` feature is enabled, this is the "real" I/O driver
21     /// backed by Mio. Without the `io-driver` feature, this is a thread parker
22     /// backed by a condition variable.
23     pub(crate) type Driver = Either<driver::Driver, ParkThread>;
24 
25     /// The handle the runtime stores for future use.
26     ///
27     /// When the `io-driver` feature is **not** enabled, this is `()`.
28     pub(crate) type Handle = Option<driver::Handle>;
29 
create_driver(enable: bool) -> io::Result<(Driver, Handle)>30     pub(crate) fn create_driver(enable: bool) -> io::Result<(Driver, Handle)> {
31         #[cfg(loom)]
32         assert!(!enable);
33 
34         if enable {
35             let driver = driver::Driver::new()?;
36             let handle = driver.handle();
37 
38             Ok((Either::A(driver), Some(handle)))
39         } else {
40             let driver = ParkThread::new();
41             Ok((Either::B(driver), None))
42         }
43     }
44 }
45 
46 #[cfg(not(feature = "io-driver"))]
47 mod variant {
48     use crate::park::ParkThread;
49 
50     use std::io;
51 
52     /// I/O is not enabled, use a condition variable based parker
53     pub(crate) type Driver = ParkThread;
54 
55     /// There is no handle
56     pub(crate) type Handle = ();
57 
create_driver(_enable: bool) -> io::Result<(Driver, Handle)>58     pub(crate) fn create_driver(_enable: bool) -> io::Result<(Driver, Handle)> {
59         let driver = ParkThread::new();
60 
61         Ok((driver, ()))
62     }
63 }
64