use std::io;
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
#[derive(Debug)]
pub struct ReplayStream<S> {
replay: Vec<u8>,
read_pos: usize,
inner: S,
}
impl<S> ReplayStream<S> {
#[must_use]
pub fn new(replay: Vec<u8>, inner: S) -> Self {
Self {
replay,
read_pos: 0,
inner,
}
}
}
impl<S: AsyncRead + Unpin> AsyncRead for ReplayStream<S> {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
let this = self.get_mut();
if this.read_pos < this.replay.len() {
let remaining = &this.replay[this.read_pos..];
let take = remaining.len().min(buf.remaining());
buf.put_slice(&remaining[..take]);
this.read_pos += take;
return Poll::Ready(Ok(()));
}
Pin::new(&mut this.inner).poll_read(cx, buf)
}
}
impl<S: AsyncWrite + Unpin> AsyncWrite for ReplayStream<S> {
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
Pin::new(&mut self.get_mut().inner).poll_write(cx, buf)
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(&mut self.get_mut().inner).poll_flush(cx)
}
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(&mut self.get_mut().inner).poll_shutdown(cx)
}
}
#[cfg(test)]
mod tests {
use super::*;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
#[tokio::test]
async fn read_drains_replay_then_delegates() {
let (mut a, b) = tokio::io::duplex(1024);
let mut wrapped = ReplayStream::new(b"prefix-".to_vec(), b);
tokio::spawn(async move {
a.write_all(b"suffix").await.unwrap();
a.shutdown().await.unwrap();
});
let mut got = Vec::new();
wrapped.read_to_end(&mut got).await.unwrap();
assert_eq!(got, b"prefix-suffix");
}
#[tokio::test]
async fn writes_go_to_inner_directly() {
let (a, b) = tokio::io::duplex(1024);
let mut wrapped = ReplayStream::new(b"ignored".to_vec(), b);
wrapped.write_all(b"hello").await.unwrap();
wrapped.shutdown().await.unwrap();
drop(wrapped);
let mut got = Vec::new();
let mut a = a;
a.read_to_end(&mut got).await.unwrap();
assert_eq!(got, b"hello");
}
}