Rollup merge of #59009 - sfackler:fix-sgx-vectors, r=alexcrichton

Fix SGX implementations of read/write_vectored.
This commit is contained in:
kennytm 2019-03-16 14:56:28 +08:00
commit d3f30c30ea
No known key found for this signature in database
GPG key ID: FEF6C8051D0E013C
3 changed files with 34 additions and 32 deletions

View file

@ -390,6 +390,28 @@ fn read_to_end_with_reservation<R: Read + ?Sized>(r: &mut R,
ret ret
} }
pub(crate) fn default_read_vectored<F>(read: F, bufs: &mut [IoVecMut<'_>]) -> Result<usize>
where
F: FnOnce(&mut [u8]) -> Result<usize>
{
let buf = bufs
.iter_mut()
.find(|b| !b.is_empty())
.map_or(&mut [][..], |b| &mut **b);
read(buf)
}
pub(crate) fn default_write_vectored<F>(write: F, bufs: &[IoVec<'_>]) -> Result<usize>
where
F: FnOnce(&[u8]) -> Result<usize>
{
let buf = bufs
.iter()
.find(|b| !b.is_empty())
.map_or(&[][..], |b| &**b);
write(buf)
}
/// The `Read` trait allows for reading bytes from a source. /// The `Read` trait allows for reading bytes from a source.
/// ///
/// Implementors of the `Read` trait are called 'readers'. /// Implementors of the `Read` trait are called 'readers'.
@ -528,14 +550,11 @@ pub trait Read {
/// written to possibly being only partially filled. This method must behave /// written to possibly being only partially filled. This method must behave
/// as a single call to `read` with the buffers concatenated would. /// as a single call to `read` with the buffers concatenated would.
/// ///
/// The default implementation simply passes the first nonempty buffer to /// The default implementation calls `read` with either the first nonempty
/// `read`. /// buffer provided, or an empty one if none exists.
#[unstable(feature = "iovec", issue = "58452")] #[unstable(feature = "iovec", issue = "58452")]
fn read_vectored(&mut self, bufs: &mut [IoVecMut<'_>]) -> Result<usize> { fn read_vectored(&mut self, bufs: &mut [IoVecMut<'_>]) -> Result<usize> {
match bufs.iter_mut().find(|b| !b.is_empty()) { default_read_vectored(|b| self.read(b), bufs)
Some(buf) => self.read(buf),
None => Ok(0),
}
} }
/// Determines if this `Read`er can work with buffers of uninitialized /// Determines if this `Read`er can work with buffers of uninitialized
@ -1107,14 +1126,11 @@ pub trait Write {
/// read from possibly being only partially consumed. This method must /// read from possibly being only partially consumed. This method must
/// behave as a call to `write` with the buffers concatenated would. /// behave as a call to `write` with the buffers concatenated would.
/// ///
/// The default implementation simply passes the first nonempty buffer to /// The default implementation calls `write` with either the first nonempty
/// `write`. /// buffer provided, or an empty one if none exists.
#[unstable(feature = "iovec", issue = "58452")] #[unstable(feature = "iovec", issue = "58452")]
fn write_vectored(&mut self, bufs: &[IoVec<'_>]) -> Result<usize> { fn write_vectored(&mut self, bufs: &[IoVec<'_>]) -> Result<usize> {
match bufs.iter().find(|b| !b.is_empty()) { default_write_vectored(|b| self.write(b), bufs)
Some(buf) => self.write(buf),
None => Ok(0),
}
} }
/// Flush this output stream, ensuring that all intermediately buffered /// Flush this output stream, ensuring that all intermediately buffered

View file

@ -35,10 +35,7 @@ impl TcpStream {
} }
pub fn read_vectored(&self, bufs: &mut [IoVecMut<'_>]) -> io::Result<usize> { pub fn read_vectored(&self, bufs: &mut [IoVecMut<'_>]) -> io::Result<usize> {
match bufs.iter_mut().find(|b| !b.is_empty()) { io::default_read_vectored(|b| self.read(b), bufs)
Some(buf) => self.read(buf),
None => Ok(0),
}
} }
pub fn write(&self, buf: &[u8]) -> Result<usize> { pub fn write(&self, buf: &[u8]) -> Result<usize> {
@ -46,10 +43,7 @@ impl TcpStream {
} }
pub fn write_vectored(&self, bufs: &[IoVec<'_>]) -> io::Result<usize> { pub fn write_vectored(&self, bufs: &[IoVec<'_>]) -> io::Result<usize> {
match bufs.iter().find(|b| !b.is_empty()) { io::default_write_vectored(|b| self.write(b), bufs)
Some(buf) => self.write(buf),
None => Ok(0),
}
} }
pub fn take_error(&self) -> Result<Option<Error>> { pub fn take_error(&self) -> Result<Option<Error>> {

View file

@ -103,24 +103,16 @@ impl TcpStream {
self.inner.inner.read(buf) self.inner.inner.read(buf)
} }
pub fn read_vectored(&self, buf: &mut [IoVecMut<'_>]) -> io::Result<usize> { pub fn read_vectored(&self, bufs: &mut [IoVecMut<'_>]) -> io::Result<usize> {
let buf = match buf.get_mut(0) { io::default_read_vectored(|b| self.read(b), bufs)
Some(buf) => buf,
None => return Ok(0),
};
self.read(buf)
} }
pub fn write(&self, buf: &[u8]) -> io::Result<usize> { pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
self.inner.inner.write(buf) self.inner.inner.write(buf)
} }
pub fn write_vectored(&self, buf: &[IoVec<'_>]) -> io::Result<usize> { pub fn write_vectored(&self, bufs: &[IoVec<'_>]) -> io::Result<usize> {
let buf = match buf.get(0) { io::default_write_vectored(|b| self.write(b), bufs)
Some(buf) => buf,
None => return Ok(0),
};
self.write(buf)
} }
pub fn peer_addr(&self) -> io::Result<SocketAddr> { pub fn peer_addr(&self) -> io::Result<SocketAddr> {