Skip to content

Commit ba9323f

Browse files
authored
Rollup merge of rust-lang#59009 - sfackler:fix-sgx-vectors, r=alexcrichton
Fix SGX implementations of read/write_vectored.
2 parents cf6d881 + ab8e1d2 commit ba9323f

File tree

3 files changed

+34
-32
lines changed

3 files changed

+34
-32
lines changed

src/libstd/io/mod.rs

+28-12
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,28 @@ fn read_to_end_with_reservation<R: Read + ?Sized>(r: &mut R,
390390
ret
391391
}
392392

393+
pub(crate) fn default_read_vectored<F>(read: F, bufs: &mut [IoVecMut<'_>]) -> Result<usize>
394+
where
395+
F: FnOnce(&mut [u8]) -> Result<usize>
396+
{
397+
let buf = bufs
398+
.iter_mut()
399+
.find(|b| !b.is_empty())
400+
.map_or(&mut [][..], |b| &mut **b);
401+
read(buf)
402+
}
403+
404+
pub(crate) fn default_write_vectored<F>(write: F, bufs: &[IoVec<'_>]) -> Result<usize>
405+
where
406+
F: FnOnce(&[u8]) -> Result<usize>
407+
{
408+
let buf = bufs
409+
.iter()
410+
.find(|b| !b.is_empty())
411+
.map_or(&[][..], |b| &**b);
412+
write(buf)
413+
}
414+
393415
/// The `Read` trait allows for reading bytes from a source.
394416
///
395417
/// Implementors of the `Read` trait are called 'readers'.
@@ -528,14 +550,11 @@ pub trait Read {
528550
/// written to possibly being only partially filled. This method must behave
529551
/// as a single call to `read` with the buffers concatenated would.
530552
///
531-
/// The default implementation simply passes the first nonempty buffer to
532-
/// `read`.
553+
/// The default implementation calls `read` with either the first nonempty
554+
/// buffer provided, or an empty one if none exists.
533555
#[unstable(feature = "iovec", issue = "58452")]
534556
fn read_vectored(&mut self, bufs: &mut [IoVecMut<'_>]) -> Result<usize> {
535-
match bufs.iter_mut().find(|b| !b.is_empty()) {
536-
Some(buf) => self.read(buf),
537-
None => Ok(0),
538-
}
557+
default_read_vectored(|b| self.read(b), bufs)
539558
}
540559

541560
/// Determines if this `Read`er can work with buffers of uninitialized
@@ -1107,14 +1126,11 @@ pub trait Write {
11071126
/// read from possibly being only partially consumed. This method must
11081127
/// behave as a call to `write` with the buffers concatenated would.
11091128
///
1110-
/// The default implementation simply passes the first nonempty buffer to
1111-
/// `write`.
1129+
/// The default implementation calls `write` with either the first nonempty
1130+
/// buffer provided, or an empty one if none exists.
11121131
#[unstable(feature = "iovec", issue = "58452")]
11131132
fn write_vectored(&mut self, bufs: &[IoVec<'_>]) -> Result<usize> {
1114-
match bufs.iter().find(|b| !b.is_empty()) {
1115-
Some(buf) => self.write(buf),
1116-
None => Ok(0),
1117-
}
1133+
default_write_vectored(|b| self.write(b), bufs)
11181134
}
11191135

11201136
/// Flush this output stream, ensuring that all intermediately buffered

src/libstd/sys/redox/net/tcp.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,15 @@ impl TcpStream {
3535
}
3636

3737
pub fn read_vectored(&self, bufs: &mut [IoVecMut<'_>]) -> io::Result<usize> {
38-
match bufs.iter_mut().find(|b| !b.is_empty()) {
39-
Some(buf) => self.read(buf),
40-
None => Ok(0),
41-
}
38+
io::default_read_vectored(|b| self.read(b), bufs)
4239
}
4340

4441
pub fn write(&self, buf: &[u8]) -> Result<usize> {
4542
self.0.write(buf)
4643
}
4744

4845
pub fn write_vectored(&self, bufs: &[IoVec<'_>]) -> io::Result<usize> {
49-
match bufs.iter().find(|b| !b.is_empty()) {
50-
Some(buf) => self.write(buf),
51-
None => Ok(0),
52-
}
46+
io::default_write_vectored(|b| self.write(b), bufs)
5347
}
5448

5549
pub fn take_error(&self) -> Result<Option<Error>> {

src/libstd/sys/sgx/net.rs

+4-12
Original file line numberDiff line numberDiff line change
@@ -103,24 +103,16 @@ impl TcpStream {
103103
self.inner.inner.read(buf)
104104
}
105105

106-
pub fn read_vectored(&self, buf: &mut [IoVecMut<'_>]) -> io::Result<usize> {
107-
let buf = match buf.get_mut(0) {
108-
Some(buf) => buf,
109-
None => return Ok(0),
110-
};
111-
self.read(buf)
106+
pub fn read_vectored(&self, bufs: &mut [IoVecMut<'_>]) -> io::Result<usize> {
107+
io::default_read_vectored(|b| self.read(b), bufs)
112108
}
113109

114110
pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
115111
self.inner.inner.write(buf)
116112
}
117113

118-
pub fn write_vectored(&self, buf: &[IoVec<'_>]) -> io::Result<usize> {
119-
let buf = match buf.get(0) {
120-
Some(buf) => buf,
121-
None => return Ok(0),
122-
};
123-
self.write(buf)
114+
pub fn write_vectored(&self, bufs: &[IoVec<'_>]) -> io::Result<usize> {
115+
io::default_write_vectored(|b| self.write(b), bufs)
124116
}
125117

126118
pub fn peer_addr(&self) -> io::Result<SocketAddr> {

0 commit comments

Comments
 (0)