Buffered writer seems to work.

Fortune for rsh's current commit: Curse − 凶
sock-buffering
Avril 3 years ago
parent 19d1db35d6
commit 818659b83c
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -26,6 +26,12 @@ pub fn vec_uninit<T>(sz: usize) -> Vec<MaybeUninit<T>>
}
}
pub trait IsTrue{}
#[derive(Debug)]
pub struct Assert<const VALUE: bool>;
impl IsTrue for Assert<true>{}
/// Create a blanket-implementing trait that is a subtrait of any number of traits.
///
/// # Usage

@ -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(())
}
}

Loading…
Cancel
Save