|
1 | 1 | use core::future::Future; |
2 | 2 | use core::ops::{Deref, DerefMut}; |
3 | 3 |
|
| 4 | +use futures::{Stream, StreamExt as _}; |
4 | 5 | use tokio::io::{AsyncRead, AsyncWrite}; |
| 6 | +use tokio::sync::mpsc; |
5 | 7 |
|
6 | 8 | /// Accepts connections on a transport |
7 | 9 | pub trait Accept { |
@@ -80,3 +82,134 @@ where |
80 | 82 | Ok(((self.f)(cx), tx, rx)) |
81 | 83 | } |
82 | 84 | } |
| 85 | + |
| 86 | +/// A wrapper around a [Stream] of connections |
| 87 | +pub struct AcceptStream<T>(tokio::sync::Mutex<T>); |
| 88 | + |
| 89 | +impl<T> From<T> for AcceptStream<T> { |
| 90 | + fn from(stream: T) -> Self { |
| 91 | + Self(tokio::sync::Mutex::new(stream)) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +impl<T, C, O, I> Accept for AcceptStream<T> |
| 96 | +where |
| 97 | + T: Stream<Item = (C, O, I)> + Unpin, |
| 98 | + C: Send + Sync + 'static, |
| 99 | + O: AsyncWrite + Send + Sync + Unpin + 'static, |
| 100 | + I: AsyncRead + Send + Sync + Unpin + 'static, |
| 101 | +{ |
| 102 | + type Context = C; |
| 103 | + type Outgoing = O; |
| 104 | + type Incoming = I; |
| 105 | + |
| 106 | + async fn accept(&self) -> std::io::Result<(Self::Context, Self::Outgoing, Self::Incoming)> { |
| 107 | + (&self).accept().await |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +impl<T, C, O, I> Accept for &AcceptStream<T> |
| 112 | +where |
| 113 | + T: Stream<Item = (C, O, I)> + Unpin, |
| 114 | + C: Send + Sync + 'static, |
| 115 | + O: AsyncWrite + Send + Sync + Unpin + 'static, |
| 116 | + I: AsyncRead + Send + Sync + Unpin + 'static, |
| 117 | +{ |
| 118 | + type Context = C; |
| 119 | + type Outgoing = O; |
| 120 | + type Incoming = I; |
| 121 | + |
| 122 | + async fn accept(&self) -> std::io::Result<(Self::Context, Self::Outgoing, Self::Incoming)> { |
| 123 | + let mut stream = self.0.lock().await; |
| 124 | + let Some((cx, tx, rx)) = stream.next().await else { |
| 125 | + return Err(std::io::ErrorKind::UnexpectedEof.into()); |
| 126 | + }; |
| 127 | + Ok((cx, tx, rx)) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +/// A wrapper around an [mpsc::Receiver] of connections |
| 132 | +pub struct AcceptReceiver<C, O, I>(tokio::sync::Mutex<mpsc::Receiver<(C, O, I)>>); |
| 133 | + |
| 134 | +impl<C, O, I> From<mpsc::Receiver<(C, O, I)>> for AcceptReceiver<C, O, I> { |
| 135 | + fn from(stream: mpsc::Receiver<(C, O, I)>) -> Self { |
| 136 | + Self(tokio::sync::Mutex::new(stream)) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +impl<C, O, I> Accept for AcceptReceiver<C, O, I> |
| 141 | +where |
| 142 | + C: Send + Sync + 'static, |
| 143 | + O: AsyncWrite + Send + Sync + Unpin + 'static, |
| 144 | + I: AsyncRead + Send + Sync + Unpin + 'static, |
| 145 | +{ |
| 146 | + type Context = C; |
| 147 | + type Outgoing = O; |
| 148 | + type Incoming = I; |
| 149 | + |
| 150 | + async fn accept(&self) -> std::io::Result<(Self::Context, Self::Outgoing, Self::Incoming)> { |
| 151 | + (&self).accept().await |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +impl<C, O, I> Accept for &AcceptReceiver<C, O, I> |
| 156 | +where |
| 157 | + C: Send + Sync + 'static, |
| 158 | + O: AsyncWrite + Send + Sync + Unpin + 'static, |
| 159 | + I: AsyncRead + Send + Sync + Unpin + 'static, |
| 160 | +{ |
| 161 | + type Context = C; |
| 162 | + type Outgoing = O; |
| 163 | + type Incoming = I; |
| 164 | + |
| 165 | + async fn accept(&self) -> std::io::Result<(Self::Context, Self::Outgoing, Self::Incoming)> { |
| 166 | + let mut stream = self.0.lock().await; |
| 167 | + let Some((cx, tx, rx)) = stream.recv().await else { |
| 168 | + return Err(std::io::ErrorKind::UnexpectedEof.into()); |
| 169 | + }; |
| 170 | + Ok((cx, tx, rx)) |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +/// A wrapper around an [mpsc::UnboundedReceiver] of connections |
| 175 | +pub struct AcceptUnboundedReceiver<C, O, I>(tokio::sync::Mutex<mpsc::UnboundedReceiver<(C, O, I)>>); |
| 176 | + |
| 177 | +impl<C, O, I> From<mpsc::UnboundedReceiver<(C, O, I)>> for AcceptUnboundedReceiver<C, O, I> { |
| 178 | + fn from(stream: mpsc::UnboundedReceiver<(C, O, I)>) -> Self { |
| 179 | + Self(tokio::sync::Mutex::new(stream)) |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +impl<C, O, I> Accept for AcceptUnboundedReceiver<C, O, I> |
| 184 | +where |
| 185 | + C: Send + Sync + 'static, |
| 186 | + O: AsyncWrite + Send + Sync + Unpin + 'static, |
| 187 | + I: AsyncRead + Send + Sync + Unpin + 'static, |
| 188 | +{ |
| 189 | + type Context = C; |
| 190 | + type Outgoing = O; |
| 191 | + type Incoming = I; |
| 192 | + |
| 193 | + async fn accept(&self) -> std::io::Result<(Self::Context, Self::Outgoing, Self::Incoming)> { |
| 194 | + (&self).accept().await |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +impl<C, O, I> Accept for &AcceptUnboundedReceiver<C, O, I> |
| 199 | +where |
| 200 | + C: Send + Sync + 'static, |
| 201 | + O: AsyncWrite + Send + Sync + Unpin + 'static, |
| 202 | + I: AsyncRead + Send + Sync + Unpin + 'static, |
| 203 | +{ |
| 204 | + type Context = C; |
| 205 | + type Outgoing = O; |
| 206 | + type Incoming = I; |
| 207 | + |
| 208 | + async fn accept(&self) -> std::io::Result<(Self::Context, Self::Outgoing, Self::Incoming)> { |
| 209 | + let mut stream = self.0.lock().await; |
| 210 | + let Some((cx, tx, rx)) = stream.recv().await else { |
| 211 | + return Err(std::io::ErrorKind::UnexpectedEof.into()); |
| 212 | + }; |
| 213 | + Ok((cx, tx, rx)) |
| 214 | + } |
| 215 | +} |
0 commit comments