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