Skip to content
Permalink
Browse files

impl Deref/DerefMut for IoVec types

Returning &'a mut [u8] was unsound, and we may as well just have them
directly deref to their slices to make it easier to work with them.
  • Loading branch information...
sfackler committed Feb 12, 2019
1 parent 31bcec6 commit 596f18201c7863d8b02fe6fa1872cf3ba2b6b381
@@ -236,7 +236,7 @@ impl<R: Read> Read for BufReader<R> {
}

fn read_vectored(&mut self, bufs: &mut [IoVecMut<'_>]) -> io::Result<usize> {
let total_len = bufs.iter().map(|b| b.as_slice().len()).sum::<usize>();
let total_len = bufs.iter().map(|b| b.len()).sum::<usize>();
if self.pos == self.cap && total_len >= self.buf.len() {
return self.inner.read_vectored(bufs);
}
@@ -595,7 +595,7 @@ impl<W: Write> Write for BufWriter<W> {
}

fn write_vectored(&mut self, bufs: &[IoVec<'_>]) -> io::Result<usize> {
let total_len = bufs.iter().map(|b| b.as_slice().len()).sum::<usize>();
let total_len = bufs.iter().map(|b| b.len()).sum::<usize>();
if self.buf.len() + total_len > self.buf.capacity() {
self.flush_buf()?;
}
@@ -224,7 +224,6 @@ impl<T> Read for Cursor<T> where T: AsRef<[u8]> {
fn read_vectored(&mut self, bufs: &mut [IoVecMut<'_>]) -> io::Result<usize> {
let mut nread = 0;
for buf in bufs {
let buf = buf.as_mut_slice();
let n = self.read(buf)?;
nread += n;
if n < buf.len() {
@@ -272,7 +271,6 @@ fn slice_write_vectored(
{
let mut nwritten = 0;
for buf in bufs {
let buf = buf.as_slice();
let n = slice_write(pos_mut, slice, buf)?;
nwritten += n;
if n < buf.len() {
@@ -317,7 +315,7 @@ fn vec_write_vectored(
{
let mut nwritten = 0;
for buf in bufs {
nwritten += vec_write(pos_mut, vec, buf.as_slice())?;
nwritten += vec_write(pos_mut, vec, buf)?;
}
Ok(nwritten)
}
@@ -196,7 +196,7 @@ impl<'a> Read for &'a [u8] {
fn read_vectored(&mut self, bufs: &mut [IoVecMut<'_>]) -> io::Result<usize> {
let mut nread = 0;
for buf in bufs {
nread += self.read(buf.as_mut_slice())?;
nread += self.read(buf)?;
if self.is_empty() {
break;
}
@@ -269,7 +269,7 @@ impl<'a> Write for &'a mut [u8] {
fn write_vectored(&mut self, bufs: &[IoVec<'_>]) -> io::Result<usize> {
let mut nwritten = 0;
for buf in bufs {
nwritten += self.write(buf.as_slice())?;
nwritten += self.write(buf)?;
if self.is_empty() {
break;
}
@@ -303,10 +303,10 @@ impl Write for Vec<u8> {

#[inline]
fn write_vectored(&mut self, bufs: &[IoVec<'_>]) -> io::Result<usize> {
let len = bufs.iter().map(|b| b.as_slice().len()).sum();
let len = bufs.iter().map(|b| b.len()).sum();
self.reserve(len);
for buf in bufs {
self.extend_from_slice(buf.as_slice());
self.extend_from_slice(buf);
}
Ok(len)
}
@@ -264,6 +264,7 @@ use fmt;
use slice;
use str;
use memchr;
use ops::{Deref, DerefMut};
use ptr;
use sys;

@@ -531,7 +532,7 @@ pub trait Read {
/// `read`.
#[unstable(feature = "iovec", issue = "0")]
fn read_vectored(&mut self, bufs: &mut [IoVecMut<'_>]) -> Result<usize> {
match bufs.iter_mut().map(|b| b.as_mut_slice()).find(|b| !b.is_empty()) {
match bufs.iter_mut().find(|b| !b.is_empty()) {
Some(buf) => self.read(buf),
None => Ok(0),
}
@@ -896,7 +897,7 @@ pub struct IoVecMut<'a>(sys::io::IoVecMut<'a>);
#[unstable(feature = "iovec", issue = "0")]
impl<'a> fmt::Debug for IoVecMut<'a> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(self.as_slice(), fmt)
fmt::Debug::fmt(self.0.as_slice(), fmt)
}
}

@@ -911,18 +912,22 @@ impl<'a> IoVecMut<'a> {
pub fn new(buf: &'a mut [u8]) -> IoVecMut<'a> {
IoVecMut(sys::io::IoVecMut::new(buf))
}
}

#[unstable(feature = "iovec", issue = "0")]
impl<'a> Deref for IoVecMut<'a> {
type Target = [u8];

/// Returns a shared reference to the inner slice.
#[unstable(feature = "iovec", issue = "0")]
#[inline]
pub fn as_slice(&self) -> &'a [u8] {
fn deref(&self) -> &[u8] {
self.0.as_slice()
}
}

/// Returns a mutable reference to the inner slice.
#[unstable(feature = "iovec", issue = "0")]
#[unstable(feature = "iovec", issue = "0")]
impl<'a> DerefMut for IoVecMut<'a> {
#[inline]
pub fn as_mut_slice(&mut self) -> &'a mut [u8] {
fn deref_mut(&mut self) -> &mut [u8] {
self.0.as_mut_slice()
}
}
@@ -939,7 +944,7 @@ pub struct IoVec<'a>(sys::io::IoVec<'a>);
#[unstable(feature = "iovec", issue = "0")]
impl<'a> fmt::Debug for IoVec<'a> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(self.as_slice(), fmt)
fmt::Debug::fmt(self.0.as_slice(), fmt)
}
}

@@ -954,11 +959,14 @@ impl<'a> IoVec<'a> {
pub fn new(buf: &'a [u8]) -> IoVec<'a> {
IoVec(sys::io::IoVec::new(buf))
}
}

#[unstable(feature = "iovec", issue = "0")]
impl<'a> Deref for IoVec<'a> {
type Target = [u8];

/// Returns a shared reference to the inner slice.
#[unstable(feature = "iovec", issue = "0")]
#[inline]
pub fn as_slice(&self) -> &'a [u8] {
fn deref(&self) -> &[u8] {
self.0.as_slice()
}
}
@@ -1103,7 +1111,7 @@ pub trait Write {
/// `write`.
#[unstable(feature = "iovec", issue = "0")]
fn write_vectored(&mut self, bufs: &[IoVec<'_>]) -> Result<usize> {
match bufs.iter().map(|b| b.as_slice()).find(|b| !b.is_empty()) {
match bufs.iter().find(|b| !b.is_empty()) {
Some(buf) => self.write(buf),
None => Ok(0),
}
@@ -1813,7 +1821,7 @@ impl<T: Read, U: Read> Read for Chain<T, U> {
fn read_vectored(&mut self, bufs: &mut [IoVecMut<'_>]) -> Result<usize> {
if !self.done_first {
match self.first.read_vectored(bufs)? {
0 if bufs.iter().any(|b| !b.as_slice().is_empty()) => self.done_first = true,
0 if bufs.iter().any(|b| !b.is_empty()) => self.done_first = true,
n => return Ok(n),
}
}
@@ -156,7 +156,7 @@ impl Read for Repeat {
fn read_vectored(&mut self, bufs: &mut [IoVecMut<'_>]) -> io::Result<usize> {
let mut nwritten = 0;
for buf in bufs {
nwritten += self.read(buf.as_mut_slice())?;
nwritten += self.read(buf)?;
}
Ok(nwritten)
}
@@ -207,7 +207,7 @@ impl Write for Sink {

#[inline]
fn write_vectored(&mut self, bufs: &[IoVec<'_>]) -> io::Result<usize> {
let total_len = bufs.iter().map(|b| b.as_slice().len()).sum();
let total_len = bufs.iter().map(|b| b.len()).sum();
Ok(total_len)
}

@@ -7,7 +7,7 @@ impl<'a> IoVec<'a> {
}

#[inline]
pub fn as_slice(&self) -> &'a [u8] {
pub fn as_slice(&self) -> &[u8] {
self.0
}
}
@@ -21,12 +21,12 @@ impl<'a> IoVecMut<'a> {
}

#[inline]
pub fn as_slice(&self) -> &'a [u8] {
pub fn as_slice(&self) -> &[u8] {
self.0
}

#[inline]
pub fn as_mut_slice(&mut self) -> &'a mut [u8] {
pub fn as_mut_slice(&mut self) -> &mut [u8] {
self.0
}
}
@@ -7,7 +7,7 @@ impl<'a> IoVec<'a> {
}

#[inline]
pub fn as_slice(&self) -> &'a [u8] {
pub fn as_slice(&self) -> &[u8] {
self.0
}
}
@@ -21,12 +21,12 @@ impl<'a> IoVecMut<'a> {
}

#[inline]
pub fn as_slice(&self) -> &'a [u8] {
pub fn as_slice(&self) -> &[u8] {
self.0
}

#[inline]
pub fn as_mut_slice(&mut self) -> &'a mut [u8] {
pub fn as_mut_slice(&mut self) -> &mut [u8] {
self.0
}
}
@@ -35,7 +35,7 @@ impl TcpStream {
}

pub fn read_vectored(&self, buf: &mut [IoVecMut<'_>]) -> io::Result<usize> {
match buf.iter_mut().map(|b| b.as_mut_slice()).find(|b| !b.is_empty()) {
match buf.iter_mut().find(|b| !b.is_empty()) {
Some(buf) => self.read(buf),
None => Ok(0),
}
@@ -46,7 +46,7 @@ impl TcpStream {
}

pub fn write_vectored(&self, bufs: &[IoVec<'_>]) -> io::Result<usize> {
match buf.iter().map(|b| b.as_slice()).find(|b| !b.is_empty()) {
match buf.iter().find(|b| !b.is_empty()) {
Some(buf) => self.write(buf),
None => Ok(0),
}
@@ -7,7 +7,7 @@ impl<'a> IoVec<'a> {
}

#[inline]
pub fn as_slice(&self) -> &'a [u8] {
pub fn as_slice(&self) -> &[u8] {
self.0
}
}
@@ -21,12 +21,12 @@ impl<'a> IoVecMut<'a> {
}

#[inline]
pub fn as_slice(&self) -> &'a [u8] {
pub fn as_slice(&self) -> &[u8] {
self.0
}

#[inline]
pub fn as_mut_slice(&mut self) -> &'a mut [u8] {
pub fn as_mut_slice(&mut self) -> &mut [u8] {
self.0
}
}
@@ -105,7 +105,7 @@ impl TcpStream {

pub fn read_vectored(&self, buf: &mut [IoVecMut<'_>]) -> io::Result<usize> {
let buf = match buf.get(0) {
Some(buf) => buf.as_mut_slice(),
Some(buf) => buf,
None => return Ok(0),
};
self.read(buf)
@@ -117,7 +117,7 @@ impl TcpStream {

pub fn write_vectored(&self, buf: &[IoVec<'_>]) -> io::Result<usize> {
let buf = match buf.get(0) {
Some(buf) => buf.as_slice(),
Some(buf) => buf,
None => return Ok(0),
};
self.read(buf)
@@ -21,7 +21,7 @@ impl<'a> IoVec<'a> {
}

#[inline]
pub fn as_slice(&self) -> &'a [u8] {
pub fn as_slice(&self) -> &[u8] {
unsafe {
slice::from_raw_parts(self.vec.iov_base as *mut u8, self.vec.iov_len)
}
@@ -46,14 +46,14 @@ impl<'a> IoVecMut<'a> {
}

#[inline]
pub fn as_slice(&self) -> &'a [u8] {
pub fn as_slice(&self) -> &[u8] {
unsafe {
slice::from_raw_parts(self.vec.iov_base as *mut u8, self.vec.iov_len)
}
}

#[inline]
pub fn as_mut_slice(&mut self) -> &'a mut [u8] {
pub fn as_mut_slice(&mut self) -> &mut [u8] {
unsafe {
slice::from_raw_parts_mut(self.vec.iov_base as *mut u8, self.vec.iov_len)
}
@@ -7,7 +7,7 @@ impl<'a> IoVec<'a> {
}

#[inline]
pub fn as_slice(&self) -> &'a [u8] {
pub fn as_slice(&self) -> &[u8] {
self.0
}
}
@@ -21,12 +21,12 @@ impl<'a> IoVecMut<'a> {
}

#[inline]
pub fn as_slice(&self) -> &'a [u8] {
pub fn as_slice(&self) -> &[u8] {
self.0
}

#[inline]
pub fn as_mut_slice(&mut self) -> &'a mut [u8] {
pub fn as_mut_slice(&mut self) -> &mut [u8] {
self.0
}
}
@@ -22,7 +22,7 @@ impl<'a> IoVec<'a> {
}

#[inline]
pub fn as_slice(&self) -> &'a [u8] {
pub fn as_slice(&self) -> &[u8] {
unsafe {
slice::from_raw_parts(self.vec.buf as *mut u8, self.vec.len as usize)
}
@@ -48,14 +48,14 @@ impl<'a> IoVecMut<'a> {
}

#[inline]
pub fn as_slice(&self) -> &'a [u8] {
pub fn as_slice(&self) -> &[u8] {
unsafe {
slice::from_raw_parts(self.vec.buf as *mut u8, self.vec.len as usize)
}
}

#[inline]
pub fn as_mut_slice(&mut self) -> &'a mut [u8] {
pub fn as_mut_slice(&mut self) -> &mut [u8] {
unsafe {
slice::from_raw_parts_mut(self.vec.buf as *mut u8, self.vec.len as usize)
}

0 comments on commit 596f182

Please sign in to comment.
You can’t perform that action at this time.