Create BufStream from a BufReader + BufWriter (#1609)

This is handy if developers want to construct the inner buffers with a
particular capacity, and still end up with a `BufStream` at the end.
This commit is contained in:
Jon Gjengset
2019-09-30 14:22:59 -04:00
committed by Lucio Franco
parent 3b8ee2d991
commit 5fd5329497
3 changed files with 41 additions and 7 deletions
+4 -4
View File
@@ -26,10 +26,10 @@ use std::{cmp, fmt};
#[pin_project]
pub struct BufReader<R> {
#[pin]
inner: R,
buf: Box<[u8]>,
pos: usize,
cap: usize,
pub(super) inner: R,
pub(super) buf: Box<[u8]>,
pub(super) pos: usize,
pub(super) cap: usize,
}
impl<R: AsyncRead> BufReader<R> {
+34
View File
@@ -55,6 +55,40 @@ impl<RW: AsyncRead + AsyncWrite> BufStream<RW> {
}
}
impl<RW> From<BufReader<BufWriter<RW>>> for BufStream<RW> {
fn from(b: BufReader<BufWriter<RW>>) -> Self {
BufStream(b)
}
}
impl<RW> From<BufWriter<BufReader<RW>>> for BufStream<RW> {
fn from(b: BufWriter<BufReader<RW>>) -> Self {
// we need to "invert" the reader and writer
let BufWriter {
inner:
BufReader {
inner,
buf: rbuf,
pos,
cap,
},
buf: wbuf,
written,
} = b;
BufStream(BufReader {
inner: BufWriter {
inner,
buf: wbuf,
written,
},
buf: rbuf,
pos,
cap,
})
}
}
impl<RW: AsyncRead + AsyncWrite> AsyncWrite for BufStream<RW> {
fn poll_write(
self: Pin<&mut Self>,
+3 -3
View File
@@ -31,9 +31,9 @@ use std::task::{Context, Poll};
#[pin_project]
pub struct BufWriter<W> {
#[pin]
inner: W,
buf: Vec<u8>,
written: usize,
pub(super) inner: W,
pub(super) buf: Vec<u8>,
pub(super) written: usize,
}
impl<W: AsyncWrite> BufWriter<W> {