1use crate::{
2 sys::IO,
3 traits::{AsyncIOHandle, Executor, Reactor, RuntimeKit, Task},
4 util::IOHandle,
5};
6use futures_core::Stream;
7use std::{
8 fmt,
9 future::Future,
10 io,
11 net::SocketAddr,
12 time::{Duration, Instant},
13};
14
15#[derive(Debug)]
17pub struct RuntimeParts<E: Executor, R: Reactor> {
18 executor: E,
19 reactor: R,
20}
21
22impl<E: Executor, R: Reactor> RuntimeParts<E, R> {
23 pub fn new(executor: E, reactor: R) -> Self {
25 Self { executor, reactor }
26 }
27}
28
29impl<E: Executor + Sync + fmt::Debug, R: Reactor + Sync + fmt::Debug> RuntimeKit
30 for RuntimeParts<E, R>
31{
32}
33
34impl<E: Executor, R: Reactor> Executor for RuntimeParts<E, R> {
35 fn block_on<T, F: Future<Output = T>>(&self, f: F) -> T {
36 self.executor.block_on(f)
37 }
38
39 fn spawn<T: Send + 'static>(
40 &self,
41 f: impl Future<Output = T> + Send + 'static,
42 ) -> impl Task<T> {
43 self.executor.spawn(f)
44 }
45
46 fn spawn_blocking<F: FnOnce() -> T + Send + 'static, T: Send + 'static>(
47 &self,
48 f: F,
49 ) -> impl Task<T> {
50 self.executor.spawn_blocking(f)
51 }
52}
53
54impl<E: Executor + Sync, R: Reactor + Sync> Reactor for RuntimeParts<E, R> {
55 fn register<H: IO + Send + 'static>(
56 &self,
57 socket: IOHandle<H>,
58 ) -> io::Result<impl AsyncIOHandle + Send> {
59 self.reactor.register(socket)
60 }
61
62 fn sleep(&self, dur: Duration) -> impl Future<Output = ()> {
63 self.reactor.sleep(dur)
64 }
65
66 fn interval(&self, dur: Duration) -> impl Stream<Item = Instant> {
67 self.reactor.interval(dur)
68 }
69
70 fn tcp_connect(
71 &self,
72 addr: SocketAddr,
73 ) -> impl Future<Output = io::Result<impl AsyncIOHandle + Send>> + Send {
74 self.reactor.tcp_connect(addr)
75 }
76}