about summary refs log tree commit diff
path: root/src/libstd/sys/sgx
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2019-02-26 02:48:13 +0000
committerbors <bors@rust-lang.org>2019-02-26 02:48:13 +0000
commitfb162e69449b423c5aed0d9c39f6c046fa300c30 (patch)
tree5d4292df6a7db1b62137c4472f1e65317d3919e1 /src/libstd/sys/sgx
parent55c173c8ae8bda689fd609f391ee5e2e5b1b6d44 (diff)
parent4785c748f2190440fb3f90b5319f121f2d31e0e4 (diff)
downloadrust-fb162e69449b423c5aed0d9c39f6c046fa300c30.tar.gz
rust-fb162e69449b423c5aed0d9c39f6c046fa300c30.zip
Auto merge of #58357 - sfackler:vectored-io, r=alexcrichton
Add vectored read and write support

This functionality has lived for a while in the tokio ecosystem, where
it can improve performance by minimizing copies.

r? @alexcrichton
Diffstat (limited to 'src/libstd/sys/sgx')
-rw-r--r--src/libstd/sys/sgx/io.rs32
-rw-r--r--src/libstd/sys/sgx/mod.rs52
-rw-r--r--src/libstd/sys/sgx/net.rs18
3 files changed, 75 insertions, 27 deletions
diff --git a/src/libstd/sys/sgx/io.rs b/src/libstd/sys/sgx/io.rs
new file mode 100644
index 00000000000..8b02d3fd19d
--- /dev/null
+++ b/src/libstd/sys/sgx/io.rs
@@ -0,0 +1,32 @@
+pub struct IoVec<'a>(&'a [u8]);
+
+impl<'a> IoVec<'a> {
+    #[inline]
+    pub fn new(buf: &'a [u8]) -> IoVec<'a> {
+        IoVec(buf)
+    }
+
+    #[inline]
+    pub fn as_slice(&self) -> &[u8] {
+        self.0
+    }
+}
+
+pub struct IoVecMut<'a>(&'a mut [u8]);
+
+impl<'a> IoVecMut<'a> {
+    #[inline]
+    pub fn new(buf: &'a mut [u8]) -> IoVecMut<'a> {
+        IoVecMut(buf)
+    }
+
+    #[inline]
+    pub fn as_slice(&self) -> &[u8] {
+        self.0
+    }
+
+    #[inline]
+    pub fn as_mut_slice(&mut self) -> &mut [u8] {
+        self.0
+    }
+}
diff --git a/src/libstd/sys/sgx/mod.rs b/src/libstd/sys/sgx/mod.rs
index 4225ecbb206..403dd61187f 100644
--- a/src/libstd/sys/sgx/mod.rs
+++ b/src/libstd/sys/sgx/mod.rs
@@ -3,7 +3,6 @@
 //! This module contains the facade (aka platform-specific) implementations of
 //! OS level functionality for Fortanix SGX.
 
-use io;
 use os::raw::c_char;
 use sync::atomic::{AtomicBool, Ordering};
 
@@ -20,6 +19,7 @@ pub mod env;
 pub mod ext;
 pub mod fd;
 pub mod fs;
+pub mod io;
 pub mod memchr;
 pub mod mutex;
 pub mod net;
@@ -41,12 +41,12 @@ pub fn init() {
 
 /// This function is used to implement functionality that simply doesn't exist.
 /// Programs relying on this functionality will need to deal with the error.
-pub fn unsupported<T>() -> io::Result<T> {
+pub fn unsupported<T>() -> ::io::Result<T> {
     Err(unsupported_err())
 }
 
-pub fn unsupported_err() -> io::Error {
-    io::Error::new(io::ErrorKind::Other,
+pub fn unsupported_err() -> ::io::Error {
+    ::io::Error::new(::io::ErrorKind::Other,
                    "operation not supported on SGX yet")
 }
 
@@ -55,58 +55,58 @@ pub fn unsupported_err() -> io::Error {
 /// returned, the program might very well be able to function normally. This is
 /// what happens when `SGX_INEFFECTIVE_ERROR` is set to `true`. If it is
 /// `false`, the behavior is the same as `unsupported`.
-pub fn sgx_ineffective<T>(v: T) -> io::Result<T> {
+pub fn sgx_ineffective<T>(v: T) -> ::io::Result<T> {
     static SGX_INEFFECTIVE_ERROR: AtomicBool = AtomicBool::new(false);
     if SGX_INEFFECTIVE_ERROR.load(Ordering::Relaxed) {
-        Err(io::Error::new(io::ErrorKind::Other,
+        Err(::io::Error::new(::io::ErrorKind::Other,
                        "operation can't be trusted to have any effect on SGX"))
     } else {
         Ok(v)
     }
 }
 
-pub fn decode_error_kind(code: i32) -> io::ErrorKind {
+pub fn decode_error_kind(code: i32) -> ::io::ErrorKind {
     use fortanix_sgx_abi::Error;
 
     // FIXME: not sure how to make sure all variants of Error are covered
     if code == Error::NotFound as _ {
-        io::ErrorKind::NotFound
+        ::io::ErrorKind::NotFound
     } else if code == Error::PermissionDenied as _ {
-        io::ErrorKind::PermissionDenied
+        ::io::ErrorKind::PermissionDenied
     } else if code == Error::ConnectionRefused as _ {
-        io::ErrorKind::ConnectionRefused
+        ::io::ErrorKind::ConnectionRefused
     } else if code == Error::ConnectionReset as _ {
-        io::ErrorKind::ConnectionReset
+        ::io::ErrorKind::ConnectionReset
     } else if code == Error::ConnectionAborted as _ {
-        io::ErrorKind::ConnectionAborted
+        ::io::ErrorKind::ConnectionAborted
     } else if code == Error::NotConnected as _ {
-        io::ErrorKind::NotConnected
+        ::io::ErrorKind::NotConnected
     } else if code == Error::AddrInUse as _ {
-        io::ErrorKind::AddrInUse
+        ::io::ErrorKind::AddrInUse
     } else if code == Error::AddrNotAvailable as _ {
-        io::ErrorKind::AddrNotAvailable
+        ::io::ErrorKind::AddrNotAvailable
     } else if code == Error::BrokenPipe as _ {
-        io::ErrorKind::BrokenPipe
+        ::io::ErrorKind::BrokenPipe
     } else if code == Error::AlreadyExists as _ {
-        io::ErrorKind::AlreadyExists
+        ::io::ErrorKind::AlreadyExists
     } else if code == Error::WouldBlock as _ {
-        io::ErrorKind::WouldBlock
+        ::io::ErrorKind::WouldBlock
     } else if code == Error::InvalidInput as _ {
-        io::ErrorKind::InvalidInput
+        ::io::ErrorKind::InvalidInput
     } else if code == Error::InvalidData as _ {
-        io::ErrorKind::InvalidData
+        ::io::ErrorKind::InvalidData
     } else if code == Error::TimedOut as _ {
-        io::ErrorKind::TimedOut
+        ::io::ErrorKind::TimedOut
     } else if code == Error::WriteZero as _ {
-        io::ErrorKind::WriteZero
+        ::io::ErrorKind::WriteZero
     } else if code == Error::Interrupted as _ {
-        io::ErrorKind::Interrupted
+        ::io::ErrorKind::Interrupted
     } else if code == Error::Other as _ {
-        io::ErrorKind::Other
+        ::io::ErrorKind::Other
     } else if code == Error::UnexpectedEof as _ {
-        io::ErrorKind::UnexpectedEof
+        ::io::ErrorKind::UnexpectedEof
     } else {
-        io::ErrorKind::Other
+        ::io::ErrorKind::Other
     }
 }
 
diff --git a/src/libstd/sys/sgx/net.rs b/src/libstd/sys/sgx/net.rs
index 6e86b06b286..c4c2de43ff7 100644
--- a/src/libstd/sys/sgx/net.rs
+++ b/src/libstd/sys/sgx/net.rs
@@ -1,5 +1,5 @@
 use fmt;
-use io;
+use io::{self, IoVec, IoVecMut};
 use net::{SocketAddr, Shutdown, Ipv4Addr, Ipv6Addr, ToSocketAddrs};
 use time::Duration;
 use sys::{unsupported, Void, sgx_ineffective, AsInner, FromInner, IntoInner, TryIntoInner};
@@ -103,10 +103,26 @@ impl TcpStream {
         self.inner.inner.read(buf)
     }
 
+    pub fn read_vectored(&self, buf: &mut [IoVecMut<'_>]) -> io::Result<usize> {
+        let buf = match buf.get_mut(0) {
+            Some(buf) => buf,
+            None => return Ok(0),
+        };
+        self.read(buf)
+    }
+
     pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
         self.inner.inner.write(buf)
     }
 
+    pub fn write_vectored(&self, buf: &[IoVec<'_>]) -> io::Result<usize> {
+        let buf = match buf.get(0) {
+            Some(buf) => buf,
+            None => return Ok(0),
+        };
+        self.write(buf)
+    }
+
     pub fn peer_addr(&self) -> io::Result<SocketAddr> {
         addr_to_sockaddr(&self.peer_addr)
     }