diff options
| author | Steven Fackler <sfackler@gmail.com> | 2019-02-11 19:31:37 -0800 |
|---|---|---|
| committer | Steven Fackler <sfackler@gmail.com> | 2019-02-13 19:40:17 -0800 |
| commit | 596f18201c7863d8b02fe6fa1872cf3ba2b6b381 (patch) | |
| tree | 1b152d89591c590eb92bf1b18591836e88ccf347 /src/libstd/io/mod.rs | |
| parent | 31bcec648aa57391115f877a2ca022d7ff6415aa (diff) | |
| download | rust-596f18201c7863d8b02fe6fa1872cf3ba2b6b381.tar.gz rust-596f18201c7863d8b02fe6fa1872cf3ba2b6b381.zip | |
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.
Diffstat (limited to 'src/libstd/io/mod.rs')
| -rw-r--r-- | src/libstd/io/mod.rs | 36 |
1 files changed, 22 insertions, 14 deletions
diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index b9765605f8e..7f9c5a0316c 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -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), } } |
