| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
use std::{ |
| 5 |
io, |
| 6 |
pin::Pin, |
| 7 |
sync::{Arc, Mutex}, |
| 8 |
task::{Context, Poll}, |
| 9 |
}; |
| 10 |
use tokio::{ |
| 11 |
io::{AsyncRead, AsyncWrite, ReadBuf}, |
| 12 |
net::TcpStream, |
| 13 |
}; |
| 14 |
|
| 15 |
type ReadFn = Box<dyn Fn(Pin<&mut TcpStream>, &mut Context, &mut ReadBuf) -> Poll<io::Result<()>>>; |
| 16 |
type WriteFn = Box<dyn Fn(Pin<&mut TcpStream>, &mut Context, &[u8]) -> Poll<io::Result<usize>>>; |
| 17 |
type ShutdownFn = Box<dyn Fn(Pin<&mut TcpStream>, &mut Context) -> Poll<io::Result<()>>>; |
| 18 |
|
| 19 |
#[derive(Default)] |
| 20 |
struct OverrideMethods { |
| 21 |
next_read: Option<ReadFn>, |
| 22 |
next_write: Option<WriteFn>, |
| 23 |
next_shutdown: Option<ShutdownFn>, |
| 24 |
} |
| 25 |
|
| 26 |
#[derive(Default)] |
| 27 |
pub struct Overrides(Mutex<OverrideMethods>); |
| 28 |
|
| 29 |
impl Overrides { |
| 30 |
pub fn next_read(&self, input: Option<ReadFn>) { |
| 31 |
if let Ok(mut overrides) = self.0.lock() { |
| 32 |
overrides.next_read = input; |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
pub fn next_write(&self, input: Option<WriteFn>) { |
| 37 |
if let Ok(mut overrides) = self.0.lock() { |
| 38 |
overrides.next_write = input; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
pub fn next_shutdown(&self, input: Option<ShutdownFn>) { |
| 43 |
if let Ok(mut overrides) = self.0.lock() { |
| 44 |
overrides.next_shutdown = input; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
pub fn is_consumed(&self) -> bool { |
| 49 |
if let Ok(overrides) = self.0.lock() { |
| 50 |
overrides.next_read.is_none() |
| 51 |
&& overrides.next_write.is_none() |
| 52 |
&& overrides.next_shutdown.is_none() |
| 53 |
} else { |
| 54 |
false |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
unsafe impl Send for Overrides {} |
| 60 |
unsafe impl Sync for Overrides {} |
| 61 |
|
| 62 |
pub struct TestStream { |
| 63 |
stream: TcpStream, |
| 64 |
overrides: Arc<Overrides>, |
| 65 |
} |
| 66 |
|
| 67 |
impl TestStream { |
| 68 |
pub fn new(stream: TcpStream) -> Self { |
| 69 |
let overrides = Arc::new(Overrides::default()); |
| 70 |
Self { stream, overrides } |
| 71 |
} |
| 72 |
|
| 73 |
pub fn overrides(&self) -> Arc<Overrides> { |
| 74 |
self.overrides.clone() |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
impl AsyncRead for TestStream { |
| 79 |
fn poll_read( |
| 80 |
self: Pin<&mut Self>, |
| 81 |
ctx: &mut Context<'_>, |
| 82 |
buf: &mut ReadBuf<'_>, |
| 83 |
) -> Poll<io::Result<()>> { |
| 84 |
let s = self.get_mut(); |
| 85 |
let stream = Pin::new(&mut s.stream); |
| 86 |
let action = match s.overrides.0.lock() { |
| 87 |
Ok(mut overrides) => overrides.next_read.take(), |
| 88 |
_ => None, |
| 89 |
}; |
| 90 |
if let Some(f) = action { |
| 91 |
(f)(stream, ctx, buf) |
| 92 |
} else { |
| 93 |
stream.poll_read(ctx, buf) |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
impl AsyncWrite for TestStream { |
| 99 |
fn poll_write( |
| 100 |
self: Pin<&mut Self>, |
| 101 |
ctx: &mut Context<'_>, |
| 102 |
buf: &[u8], |
| 103 |
) -> Poll<io::Result<usize>> { |
| 104 |
let s = self.get_mut(); |
| 105 |
let stream = Pin::new(&mut s.stream); |
| 106 |
let action = match s.overrides.0.lock() { |
| 107 |
Ok(mut overrides) => overrides.next_write.take(), |
| 108 |
_ => None, |
| 109 |
}; |
| 110 |
if let Some(f) = action { |
| 111 |
(f)(stream, ctx, buf) |
| 112 |
} else { |
| 113 |
stream.poll_write(ctx, buf) |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
fn poll_flush(mut self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<io::Result<()>> { |
| 118 |
Pin::new(&mut self.stream).poll_flush(ctx) |
| 119 |
} |
| 120 |
|
| 121 |
fn poll_shutdown(self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<io::Result<()>> { |
| 122 |
let s = self.get_mut(); |
| 123 |
let stream = Pin::new(&mut s.stream); |
| 124 |
let action = match s.overrides.0.lock() { |
| 125 |
Ok(mut overrides) => overrides.next_shutdown.take(), |
| 126 |
_ => None, |
| 127 |
}; |
| 128 |
if let Some(f) = action { |
| 129 |
(f)(stream, ctx) |
| 130 |
} else { |
| 131 |
stream.poll_shutdown(ctx) |
| 132 |
} |
| 133 |
} |
| 134 |
} |