-
Notifications
You must be signed in to change notification settings - Fork 246
feat(io): implement Read
, ReadReady
, BufRead
, Write
, and WriteReady
for VecDeque<u8>
#697
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,5 @@ mod slice_ref; | |
mod boxx; | ||
#[cfg(feature = "alloc")] | ||
mod vec; | ||
#[cfg(feature = "alloc")] | ||
mod vec_deque; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
//! Adapted from std. | ||
|
||
use alloc::collections::vec_deque::VecDeque; | ||
|
||
use crate::{BufRead, Read, ReadExactError, Write}; | ||
|
||
/// Read is implemented for `VecDeque<u8>` by consuming bytes from the front of the `VecDeque`. | ||
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] | ||
mkroening marked this conversation as resolved.
Show resolved
Hide resolved
|
||
impl Read for VecDeque<u8> { | ||
/// Fill `buf` with the contents of the "front" slice as returned by | ||
/// [`as_slices`][`VecDeque::as_slices`]. If the contained byte slices of the `VecDeque` are | ||
/// discontiguous, multiple calls to `read` will be needed to read the entire content. | ||
#[inline] | ||
async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> { | ||
let (ref mut front, _) = self.as_slices(); | ||
let n = Read::read(front, buf).await?; | ||
self.drain(..n); | ||
Ok(n) | ||
} | ||
|
||
#[inline] | ||
async fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), ReadExactError<Self::Error>> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks like it's more code than the read above combined with the provided read_exactly – is there really a point to spelling this out over letting the compiler assemble the provided one? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Apparently, yes. I did not reevaluate this code coming from |
||
let (front, back) = self.as_slices(); | ||
|
||
// Use only the front buffer if it is big enough to fill `buf`, else use | ||
// the back buffer too. | ||
match buf.split_at_mut_checked(front.len()) { | ||
None => buf.copy_from_slice(&front[..buf.len()]), | ||
Some((buf_front, buf_back)) => match back.split_at_checked(buf_back.len()) { | ||
Some((back, _)) => { | ||
buf_front.copy_from_slice(front); | ||
buf_back.copy_from_slice(back); | ||
} | ||
None => { | ||
self.clear(); | ||
return Err(ReadExactError::UnexpectedEof); | ||
} | ||
}, | ||
} | ||
|
||
self.drain(..buf.len()); | ||
Ok(()) | ||
} | ||
} | ||
|
||
/// BufRead is implemented for `VecDeque<u8>` by reading bytes from the front of the `VecDeque`. | ||
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] | ||
impl BufRead for VecDeque<u8> { | ||
/// Returns the contents of the "front" slice as returned by | ||
/// [`as_slices`][`VecDeque::as_slices`]. If the contained byte slices of the `VecDeque` are | ||
/// discontiguous, multiple calls to `fill_buf` will be needed to read the entire content. | ||
#[inline] | ||
async fn fill_buf(&mut self) -> Result<&[u8], Self::Error> { | ||
let (front, _) = self.as_slices(); | ||
Ok(front) | ||
} | ||
|
||
#[inline] | ||
fn consume(&mut self, amt: usize) { | ||
self.drain(..amt); | ||
} | ||
} | ||
|
||
/// Write is implemented for `VecDeque<u8>` by appending to the `VecDeque`, growing it as needed. | ||
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] | ||
impl Write for VecDeque<u8> { | ||
#[inline] | ||
async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> { | ||
self.extend(buf); | ||
Ok(buf.len()) | ||
} | ||
|
||
#[inline] | ||
async fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> { | ||
self.extend(buf); | ||
Ok(()) | ||
} | ||
|
||
#[inline] | ||
async fn flush(&mut self) -> Result<(), Self::Error> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is redundant with the provided method. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
Ok(()) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,5 @@ mod slice_ref; | |
mod boxx; | ||
#[cfg(feature = "alloc")] | ||
mod vec; | ||
#[cfg(feature = "alloc")] | ||
mod vec_deque; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
//! Adapted from std. | ||
|
||
use core::convert::Infallible; | ||
|
||
use alloc::collections::vec_deque::VecDeque; | ||
|
||
use crate::{BufRead, ErrorType, Read, ReadExactError, ReadReady, Write, WriteReady}; | ||
|
||
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] | ||
impl ErrorType for VecDeque<u8> { | ||
type Error = Infallible; | ||
} | ||
|
||
/// Read is implemented for `VecDeque<u8>` by consuming bytes from the front of the `VecDeque`. | ||
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] | ||
impl Read for VecDeque<u8> { | ||
/// Fill `buf` with the contents of the "front" slice as returned by | ||
/// [`as_slices`][`VecDeque::as_slices`]. If the contained byte slices of the `VecDeque` are | ||
/// discontiguous, multiple calls to `read` will be needed to read the entire content. | ||
#[inline] | ||
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> { | ||
let (ref mut front, _) = self.as_slices(); | ||
let n = Read::read(front, buf)?; | ||
self.drain(..n); | ||
Ok(n) | ||
} | ||
|
||
#[inline] | ||
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), ReadExactError<Self::Error>> { | ||
mkroening marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let (front, back) = self.as_slices(); | ||
|
||
// Use only the front buffer if it is big enough to fill `buf`, else use | ||
// the back buffer too. | ||
match buf.split_at_mut_checked(front.len()) { | ||
None => buf.copy_from_slice(&front[..buf.len()]), | ||
Some((buf_front, buf_back)) => match back.split_at_checked(buf_back.len()) { | ||
Some((back, _)) => { | ||
buf_front.copy_from_slice(front); | ||
buf_back.copy_from_slice(back); | ||
} | ||
None => { | ||
self.clear(); | ||
return Err(ReadExactError::UnexpectedEof); | ||
} | ||
}, | ||
} | ||
|
||
self.drain(..buf.len()); | ||
Ok(()) | ||
} | ||
} | ||
|
||
/// BufRead is implemented for `VecDeque<u8>` by reading bytes from the front of the `VecDeque`. | ||
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] | ||
impl BufRead for VecDeque<u8> { | ||
/// Returns the contents of the "front" slice as returned by | ||
/// [`as_slices`][`VecDeque::as_slices`]. If the contained byte slices of the `VecDeque` are | ||
/// discontiguous, multiple calls to `fill_buf` will be needed to read the entire content. | ||
#[inline] | ||
fn fill_buf(&mut self) -> Result<&[u8], Self::Error> { | ||
let (front, _) = self.as_slices(); | ||
Ok(front) | ||
} | ||
|
||
#[inline] | ||
fn consume(&mut self, amt: usize) { | ||
self.drain(..amt); | ||
} | ||
} | ||
|
||
/// Write is implemented for `VecDeque<u8>` by appending to the `VecDeque`, growing it as needed. | ||
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] | ||
impl Write for VecDeque<u8> { | ||
#[inline] | ||
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> { | ||
self.extend(buf); | ||
Ok(buf.len()) | ||
} | ||
|
||
#[inline] | ||
fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> { | ||
self.extend(buf); | ||
Ok(()) | ||
} | ||
|
||
#[inline] | ||
fn flush(&mut self) -> Result<(), Self::Error> { | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] | ||
impl ReadReady for VecDeque<u8> { | ||
#[inline] | ||
fn read_ready(&mut self) -> Result<bool, Self::Error> { | ||
Ok(true) | ||
} | ||
} | ||
|
||
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] | ||
impl WriteReady for VecDeque<u8> { | ||
#[inline] | ||
fn write_ready(&mut self) -> Result<bool, Self::Error> { | ||
Ok(true) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's pretty verbatim in places. I think that's fine given that both are licensed under "MIT OR Apache-2.0", and the original files do not have any copyright notes that point out concrete contributors (so the embedded-hal copyright notice should already cover that).