mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-09-03 00:00:05 +02:00
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:
committed by
Lucio Franco
parent
3b8ee2d991
commit
5fd5329497
@@ -26,10 +26,10 @@ use std::{cmp, fmt};
|
|||||||
#[pin_project]
|
#[pin_project]
|
||||||
pub struct BufReader<R> {
|
pub struct BufReader<R> {
|
||||||
#[pin]
|
#[pin]
|
||||||
inner: R,
|
pub(super) inner: R,
|
||||||
buf: Box<[u8]>,
|
pub(super) buf: Box<[u8]>,
|
||||||
pos: usize,
|
pub(super) pos: usize,
|
||||||
cap: usize,
|
pub(super) cap: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<R: AsyncRead> BufReader<R> {
|
impl<R: AsyncRead> BufReader<R> {
|
||||||
|
|||||||
@@ -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> {
|
impl<RW: AsyncRead + AsyncWrite> AsyncWrite for BufStream<RW> {
|
||||||
fn poll_write(
|
fn poll_write(
|
||||||
self: Pin<&mut Self>,
|
self: Pin<&mut Self>,
|
||||||
|
|||||||
@@ -31,9 +31,9 @@ use std::task::{Context, Poll};
|
|||||||
#[pin_project]
|
#[pin_project]
|
||||||
pub struct BufWriter<W> {
|
pub struct BufWriter<W> {
|
||||||
#[pin]
|
#[pin]
|
||||||
inner: W,
|
pub(super) inner: W,
|
||||||
buf: Vec<u8>,
|
pub(super) buf: Vec<u8>,
|
||||||
written: usize,
|
pub(super) written: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W: AsyncWrite> BufWriter<W> {
|
impl<W: AsyncWrite> BufWriter<W> {
|
||||||
|
|||||||
Reference in New Issue
Block a user