]> git.proxmox.com Git - rustc.git/blob - src/libstd/sys/sgx/ext/io.rs
New upstream version 1.41.1+dfsg1
[rustc.git] / src / libstd / sys / sgx / ext / io.rs
1 //! SGX-specific extensions to general I/O primitives
2 //!
3 //! SGX file descriptors behave differently from Unix file descriptors. See the
4 //! description of [`TryIntoRawFd`](trait.TryIntoRawFd.html) for more details.
5 #![unstable(feature = "sgx_platform", issue = "56975")]
6
7 use crate::net;
8 pub use crate::sys::abi::usercalls::raw::Fd as RawFd;
9 use crate::sys::{self, AsInner, FromInner, IntoInner, TryIntoInner};
10
11 /// A trait to extract the raw SGX file descriptor from an underlying
12 /// object.
13 #[unstable(feature = "sgx_platform", issue = "56975")]
14 pub trait AsRawFd {
15 /// Extracts the raw file descriptor.
16 ///
17 /// This method does **not** pass ownership of the raw file descriptor
18 /// to the caller. The descriptor is only guaranteed to be valid while
19 /// the original object has not yet been destroyed.
20 #[unstable(feature = "sgx_platform", issue = "56975")]
21 fn as_raw_fd(&self) -> RawFd;
22 }
23
24 /// A trait to express the ability to construct an object from a raw file
25 /// descriptor.
26 #[unstable(feature = "sgx_platform", issue = "56975")]
27 pub trait FromRawFd {
28 /// Constructs a new instance of `Self` from the given raw file
29 /// descriptor.
30 ///
31 /// This function **consumes ownership** of the specified file
32 /// descriptor. The returned object will take responsibility for closing
33 /// it when the object goes out of scope.
34 ///
35 /// This function is also unsafe as the primitives currently returned
36 /// have the contract that they are the sole owner of the file
37 /// descriptor they are wrapping. Usage of this function could
38 /// accidentally allow violating this contract which can cause memory
39 /// unsafety in code that relies on it being true.
40 #[unstable(feature = "sgx_platform", issue = "56975")]
41 unsafe fn from_raw_fd(fd: RawFd) -> Self;
42 }
43
44 /// A trait to express the ability to consume an object and acquire ownership of
45 /// its raw file descriptor.
46 #[unstable(feature = "sgx_platform", issue = "56975")]
47 pub trait TryIntoRawFd: Sized {
48 /// Consumes this object, returning the raw underlying file descriptor, if
49 /// this object is not cloned.
50 ///
51 /// This function **transfers ownership** of the underlying file descriptor
52 /// to the caller. Callers are then the unique owners of the file descriptor
53 /// and must close the descriptor once it's no longer needed.
54 ///
55 /// Unlike other platforms, on SGX, the file descriptor is shared between
56 /// all clones of an object. To avoid race conditions, this function will
57 /// only return `Ok` when called on the final clone.
58 #[unstable(feature = "sgx_platform", issue = "56975")]
59 fn try_into_raw_fd(self) -> Result<RawFd, Self>;
60 }
61
62 impl AsRawFd for net::TcpStream {
63 fn as_raw_fd(&self) -> RawFd {
64 *self.as_inner().as_inner().as_inner().as_inner()
65 }
66 }
67
68 impl AsRawFd for net::TcpListener {
69 fn as_raw_fd(&self) -> RawFd {
70 *self.as_inner().as_inner().as_inner().as_inner()
71 }
72 }
73
74 impl FromRawFd for net::TcpStream {
75 unsafe fn from_raw_fd(fd: RawFd) -> net::TcpStream {
76 let fd = sys::fd::FileDesc::from_inner(fd);
77 let socket = sys::net::Socket::from_inner(fd);
78 net::TcpStream::from_inner(sys::net::TcpStream::from_inner((socket, None)))
79 }
80 }
81
82 impl FromRawFd for net::TcpListener {
83 unsafe fn from_raw_fd(fd: RawFd) -> net::TcpListener {
84 let fd = sys::fd::FileDesc::from_inner(fd);
85 let socket = sys::net::Socket::from_inner(fd);
86 net::TcpListener::from_inner(sys::net::TcpListener::from_inner(socket))
87 }
88 }
89
90 impl TryIntoRawFd for net::TcpStream {
91 fn try_into_raw_fd(self) -> Result<RawFd, Self> {
92 let (socket, peer_addr) = self.into_inner().into_inner();
93 match socket.try_into_inner() {
94 Ok(fd) => Ok(fd.into_inner()),
95 Err(socket) => {
96 let sys = sys::net::TcpStream::from_inner((socket, peer_addr));
97 Err(net::TcpStream::from_inner(sys))
98 }
99 }
100 }
101 }
102
103 impl TryIntoRawFd for net::TcpListener {
104 fn try_into_raw_fd(self) -> Result<RawFd, Self> {
105 match self.into_inner().into_inner().try_into_inner() {
106 Ok(fd) => Ok(fd.into_inner()),
107 Err(socket) => {
108 let sys = sys::net::TcpListener::from_inner(socket);
109 Err(net::TcpListener::from_inner(sys))
110 }
111 }
112 }
113 }