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