From fe512cb09073fda4fb72e435316c2daacac542b9 Mon Sep 17 00:00:00 2001 From: Al Liu Date: Fri, 8 Dec 2023 18:15:00 +0800 Subject: [PATCH] reconstruct files --- quinn/src/runtime.rs | 95 +---------------------- quinn/src/runtime/async_io.rs | 133 +++++++++++++++++++++++++++++++++ quinn/src/runtime/async_std.rs | 23 ------ quinn/src/runtime/smol.rs | 23 ------ 4 files changed, 136 insertions(+), 138 deletions(-) create mode 100644 quinn/src/runtime/async_io.rs delete mode 100644 quinn/src/runtime/async_std.rs delete mode 100644 quinn/src/runtime/smol.rs diff --git a/quinn/src/runtime.rs b/quinn/src/runtime.rs index 81a685170c..33efb722a9 100644 --- a/quinn/src/runtime.rs +++ b/quinn/src/runtime.rs @@ -98,96 +98,7 @@ mod tokio; #[cfg(feature = "runtime-tokio")] pub use self::tokio::TokioRuntime; -#[cfg(feature = "runtime-async-std")] -mod async_std; -#[cfg(feature = "runtime-async-std")] -pub use self::async_std::AsyncStdRuntime; - -#[cfg(feature = "runtime-smol")] -mod smol; -#[cfg(feature = "runtime-smol")] -pub use self::smol::SmolRuntime; - #[cfg(feature = "async-io")] -mod sealed { - use async_io::{Async, Timer}; - use std::{ - future::Future, - io, - pin::Pin, - task::{Context, Poll}, - time::Instant, - }; - - use super::{AsyncTimer, AsyncUdpSocket}; - - impl AsyncTimer for Timer { - fn reset(mut self: Pin<&mut Self>, t: Instant) { - self.set_at(t) - } - - fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<()> { - Future::poll(self, cx).map(|_| ()) - } - } - - #[derive(Debug)] - pub(super) struct AsyncIOUdpSocket { - pub(super) io: Async, - pub(super) inner: udp::UdpSocketState, - } - - impl AsyncIOUdpSocket { - pub(super) fn new(sock: std::net::UdpSocket) -> io::Result { - Ok(Self { - inner: udp::UdpSocketState::new((&sock).into())?, - io: Async::new(sock)?, - }) - } - } - - impl AsyncUdpSocket for AsyncIOUdpSocket { - fn poll_send( - &self, - cx: &mut Context, - transmits: &[udp::Transmit], - ) -> Poll> { - loop { - ready!(self.io.poll_writable(cx))?; - if let Ok(res) = self.inner.send((&self.io).into(), transmits) { - return Poll::Ready(Ok(res)); - } - } - } - - fn poll_recv( - &self, - cx: &mut Context, - bufs: &mut [io::IoSliceMut<'_>], - meta: &mut [udp::RecvMeta], - ) -> Poll> { - loop { - ready!(self.io.poll_readable(cx))?; - if let Ok(res) = self.inner.recv((&self.io).into(), bufs, meta) { - return Poll::Ready(Ok(res)); - } - } - } - - fn local_addr(&self) -> io::Result { - self.io.as_ref().local_addr() - } - - fn may_fragment(&self) -> bool { - self.inner.may_fragment() - } - - fn max_transmit_segments(&self) -> usize { - self.inner.max_gso_segments() - } - - fn max_receive_segments(&self) -> usize { - self.inner.gro_segments() - } - } -} +mod async_io; +#[cfg(feature = "async-io")] +pub use self::async_io::*; diff --git a/quinn/src/runtime/async_io.rs b/quinn/src/runtime/async_io.rs new file mode 100644 index 0000000000..6b7517efba --- /dev/null +++ b/quinn/src/runtime/async_io.rs @@ -0,0 +1,133 @@ +use async_io::{Async, Timer}; +use std::{ + future::Future, + io, + pin::Pin, + sync::Arc, + task::{Context, Poll}, + time::Instant, +}; + +use super::{AsyncTimer, AsyncUdpSocket, Runtime}; + +#[cfg(feature = "smol")] +pub use smol::SmolRuntime; + +#[cfg(feature = "smol")] +mod smol { + use super::*; + /// A Quinn runtime for smol + #[derive(Debug)] + pub struct SmolRuntime; + + impl Runtime for SmolRuntime { + fn new_timer(&self, t: Instant) -> Pin> { + Box::pin(Timer::at(t)) + } + + fn spawn(&self, future: Pin + Send>>) { + ::smol::spawn(future).detach(); + } + + fn wrap_udp_socket( + &self, + sock: std::net::UdpSocket, + ) -> io::Result> { + Ok(Arc::new(AsyncIOUdpSocket::new(sock)?)) + } + } +} + +#[cfg(feature = "async-std")] +pub use async_std::AsyncStdRuntime; + +#[cfg(feature = "async-std")] +mod async_std { + use super::*; + /// A Quinn runtime for async-std + #[derive(Debug)] + pub struct AsyncStdRuntime; + + impl Runtime for AsyncStdRuntime { + fn new_timer(&self, t: Instant) -> Pin> { + Box::pin(Timer::at(t)) + } + + fn spawn(&self, future: Pin + Send>>) { + ::async_std::task::spawn(future); + } + + fn wrap_udp_socket( + &self, + sock: std::net::UdpSocket, + ) -> io::Result> { + Ok(Arc::new(AsyncIOUdpSocket::new(sock)?)) + } + } +} + +impl AsyncTimer for Timer { + fn reset(mut self: Pin<&mut Self>, t: Instant) { + self.set_at(t) + } + + fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<()> { + Future::poll(self, cx).map(|_| ()) + } +} + +#[derive(Debug)] +struct AsyncIOUdpSocket { + io: Async, + inner: udp::UdpSocketState, +} + +impl AsyncIOUdpSocket { + fn new(sock: std::net::UdpSocket) -> io::Result { + Ok(Self { + inner: udp::UdpSocketState::new((&sock).into())?, + io: Async::new(sock)?, + }) + } +} + +impl AsyncUdpSocket for AsyncIOUdpSocket { + fn poll_send(&self, cx: &mut Context, transmits: &[udp::Transmit]) -> Poll> { + loop { + ready!(self.io.poll_writable(cx))?; + if let Ok(res) = self.inner.send((&self.io).into(), transmits) { + return Poll::Ready(Ok(res)); + } + } + } + + fn poll_recv( + &self, + cx: &mut Context, + bufs: &mut [io::IoSliceMut<'_>], + meta: &mut [udp::RecvMeta], + ) -> Poll> { + loop { + ready!(self.io.poll_readable(cx))?; + if let Ok(res) = self.inner.recv((&self.io).into(), bufs, meta) { + return Poll::Ready(Ok(res)); + } + } + } + + fn local_addr(&self) -> io::Result { + self.io.as_ref().local_addr() + } + + fn may_fragment(&self) -> bool { + self.inner.may_fragment() + } + + fn max_transmit_segments(&self) -> usize { + self.inner.max_gso_segments() + } + + fn max_receive_segments(&self) -> usize { + self.inner.gro_segments() + } +} diff --git a/quinn/src/runtime/async_std.rs b/quinn/src/runtime/async_std.rs deleted file mode 100644 index 4a5130a408..0000000000 --- a/quinn/src/runtime/async_std.rs +++ /dev/null @@ -1,23 +0,0 @@ -use std::{future::Future, io, pin::Pin, sync::Arc, time::Instant}; - -use async_io::Timer; - -use super::{sealed::AsyncIOUdpSocket, AsyncTimer, AsyncUdpSocket, Runtime}; - -/// A Quinn runtime for async-std -#[derive(Debug)] -pub struct AsyncStdRuntime; - -impl Runtime for AsyncStdRuntime { - fn new_timer(&self, t: Instant) -> Pin> { - Box::pin(Timer::at(t)) - } - - fn spawn(&self, future: Pin + Send>>) { - async_std::task::spawn(future); - } - - fn wrap_udp_socket(&self, sock: std::net::UdpSocket) -> io::Result> { - Ok(Arc::new(AsyncIOUdpSocket::new(sock)?)) - } -} diff --git a/quinn/src/runtime/smol.rs b/quinn/src/runtime/smol.rs deleted file mode 100644 index a9a537f5ec..0000000000 --- a/quinn/src/runtime/smol.rs +++ /dev/null @@ -1,23 +0,0 @@ -use std::{future::Future, io, pin::Pin, sync::Arc, time::Instant}; - -use async_io::Timer; - -use super::{sealed::AsyncIOUdpSocket, AsyncTimer, AsyncUdpSocket, Runtime}; - -/// A Quinn runtime for smol -#[derive(Debug)] -pub struct SmolRuntime; - -impl Runtime for SmolRuntime { - fn new_timer(&self, t: Instant) -> Pin> { - Box::pin(Timer::at(t)) - } - - fn spawn(&self, future: Pin + Send>>) { - smol::spawn(future).detach(); - } - - fn wrap_udp_socket(&self, sock: std::net::UdpSocket) -> io::Result> { - Ok(Arc::new(AsyncIOUdpSocket::new(sock)?)) - } -}