1 //! SGX-specific extensions to general I/O primitives
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")]
8 pub use crate::sys
::abi
::usercalls
::raw
::Fd
as RawFd
;
9 use crate::sys
::{self, AsInner, FromInner, IntoInner, TryIntoInner}
;
11 /// A trait to extract the raw SGX file descriptor from an underlying
13 #[unstable(feature = "sgx_platform", issue = "56975")]
15 /// Extracts the raw file descriptor.
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
;
24 /// A trait to express the ability to construct an object from a raw file
26 #[unstable(feature = "sgx_platform", issue = "56975")]
28 /// Constructs a new instance of `Self` from the given raw file
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.
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;
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.
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.
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>;
62 impl AsRawFd
for net
::TcpStream
{
63 fn as_raw_fd(&self) -> RawFd
{
64 *self.as_inner().as_inner().as_inner().as_inner()
68 impl AsRawFd
for net
::TcpListener
{
69 fn as_raw_fd(&self) -> RawFd
{
70 *self.as_inner().as_inner().as_inner().as_inner()
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
)))
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
))
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()),
96 let sys
= sys
::net
::TcpStream
::from_inner((socket
, peer_addr
));
97 Err(net
::TcpStream
::from_inner(sys
))
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()),
108 let sys
= sys
::net
::TcpListener
::from_inner(socket
);
109 Err(net
::TcpListener
::from_inner(sys
))