Skip to content
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

feat: impl std::io::Write for SliceDeque<u8> #84

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ default-features = false
[features]
default = [ "use_std" ]

unstable = []

# Enables features that require the standard library
use_std = [ "libc/use_std" ]
# Enables support for the bytes_buf trait. That is,
Expand Down
34 changes: 33 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ extern crate bytes;
mod mirrored;
pub use mirrored::{AllocError, Buffer};

#[cfg(all(feature = "bytes_buf", feature = "use_std"))]
#[cfg(all(feature = "use_std"))]
use std::io;

use core::{cmp, convert, fmt, hash, iter, mem, ops, ptr, slice, str};
Expand Down Expand Up @@ -267,6 +267,38 @@ pub struct SliceDeque<T> {
buf: Buffer<T>,
}

#[cfg(feature = "use_std")]
impl io::Write for SliceDeque<u8> {
#[inline]
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.extend_from_slice(buf);
Ok(buf.len())
}

#[inline]
fn write_vectored(
&mut self, bufs: &[io::IoSlice<'_>],
) -> io::Result<usize> {
let len = bufs.iter().map(|b| b.len()).sum();
self.reserve(len);
for buf in bufs {
self.extend_from_slice(buf);
}
Ok(len)
}

#[inline]
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
self.extend_from_slice(buf);
Ok(())
}

#[inline]
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}

// Safe because it is possible to free this from a different thread
unsafe impl<T> Send for SliceDeque<T> where T: Send {}
// Safe because this doesn't use any kind of interior mutability.
Expand Down