|
|
|
@ -28,6 +28,7 @@ impl<T, const SIZE: usize> Buffered<T, SIZE>
|
|
|
|
|
/// Create a new staticly sized buffer wrapper around this stream
|
|
|
|
|
pub fn new(stream: T) -> Self
|
|
|
|
|
{
|
|
|
|
|
assert!(SIZE > 0, "Size of buffer cannot be 0");
|
|
|
|
|
Self {
|
|
|
|
|
buffer: SmallVec::new(),
|
|
|
|
|
stream,
|
|
|
|
@ -100,7 +101,7 @@ where T: AsyncWrite
|
|
|
|
|
if let Some(err) = err {
|
|
|
|
|
Err(err)
|
|
|
|
|
} else {
|
|
|
|
|
Ok(written)
|
|
|
|
|
Ok(buf.len())
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
tokio::pin!(fut);
|
|
|
|
@ -146,4 +147,39 @@ where T: AsyncRead
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//TODO: Write tests for writer
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests
|
|
|
|
|
{
|
|
|
|
|
use super::*;
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
async fn writer() -> eyre::Result<()>
|
|
|
|
|
{
|
|
|
|
|
use tokio::prelude::*;
|
|
|
|
|
|
|
|
|
|
let (tx, mut rx) = tokio::io::duplex(11);
|
|
|
|
|
let mut ttx = Buffered::<_, 16>::new(tx);
|
|
|
|
|
|
|
|
|
|
let back = tokio::spawn(async move {
|
|
|
|
|
|
|
|
|
|
println!("Writing bytes");
|
|
|
|
|
ttx.write_all(b"Hello world").await?;
|
|
|
|
|
println!("Waiting 1 second...");
|
|
|
|
|
tokio::time::delay_for(tokio::time::Duration::from_secs(1)).await;
|
|
|
|
|
println!("Flushing stream");
|
|
|
|
|
ttx.flush().await?;
|
|
|
|
|
Result::<_, std::io::Error>::Ok(())
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let mut output = Vec::new();
|
|
|
|
|
println!("Reading full stream...");
|
|
|
|
|
tokio::io::copy(&mut rx, &mut output).await?;
|
|
|
|
|
println!("Waiting for background...");
|
|
|
|
|
back.await.expect("Back panick")?;
|
|
|
|
|
|
|
|
|
|
println!("Expected {:?}, got {:?}", b"Hello world", &output);
|
|
|
|
|
assert_eq!(&output[..], b"Hello world");
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|