1use crate::{
2 sys::AsSysFd,
3 traits::{Executor, Reactor, RuntimeKit, Task},
4};
5use futures_core::Stream;
6use futures_io::{AsyncRead, AsyncWrite};
7use std::{
8 fmt,
9 future::Future,
10 io::{self, Read, Write},
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 + fmt::Debug, R: Reactor + fmt::Debug> RuntimeKit for RuntimeParts<E, R> {}
30
31impl<E: Executor, R: Reactor> Executor for RuntimeParts<E, R> {
32 fn block_on<T, F: Future<Output = T>>(&self, f: F) -> T {
33 self.executor.block_on(f)
34 }
35
36 fn spawn<T: Send + 'static, F: Future<Output = T> + Send + 'static>(
37 &self,
38 f: F,
39 ) -> impl Task<T> + 'static {
40 self.executor.spawn(f)
41 }
42
43 fn spawn_blocking<T: Send + 'static, F: FnOnce() -> T + Send + 'static>(
44 &self,
45 f: F,
46 ) -> impl Task<T> + 'static {
47 self.executor.spawn_blocking(f)
48 }
49}
50
51impl<E: Executor, R: Reactor> Reactor for RuntimeParts<E, R> {
52 type TcpStream = R::TcpStream;
53
54 fn register<H: Read + Write + AsSysFd + Send + 'static>(
55 &self,
56 socket: H,
57 ) -> io::Result<impl AsyncRead + AsyncWrite + Send + Unpin + 'static> {
58 self.reactor.register(socket)
59 }
60
61 fn sleep(&self, dur: Duration) -> impl Future<Output = ()> + Send + 'static {
62 self.reactor.sleep(dur)
63 }
64
65 fn interval(&self, dur: Duration) -> impl Stream<Item = Instant> + Send + 'static {
66 self.reactor.interval(dur)
67 }
68
69 fn tcp_connect_addr(
70 &self,
71 addr: SocketAddr,
72 ) -> impl Future<Output = io::Result<Self::TcpStream>> + Send + 'static {
73 self.reactor.tcp_connect_addr(addr)
74 }
75}