1 //! WASI-specific extensions to general I/O primitives
3 #![unstable(feature = "wasi_ext", issue = "0")]
9 use crate::sys_common
::{AsInner, FromInner, IntoInner}
;
11 /// Raw file descriptors.
14 /// A trait to extract the raw WASI file descriptor from an underlying
17 /// Extracts the raw file descriptor.
19 /// This method does **not** pass ownership of the raw file descriptor
20 /// to the caller. The descriptor is only guaranteed to be valid while
21 /// the original object has not yet been destroyed.
22 fn as_raw_fd(&self) -> RawFd
;
25 /// A trait to express the ability to construct an object from a raw file
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 unsafe fn from_raw_fd(fd
: RawFd
) -> Self;
43 /// A trait to express the ability to consume an object and acquire ownership of
44 /// its raw file descriptor.
46 /// Consumes this object, returning the raw underlying file descriptor.
48 /// This function **transfers ownership** of the underlying file descriptor
49 /// to the caller. Callers are then the unique owners of the file descriptor
50 /// and must close the descriptor once it's no longer needed.
51 fn into_raw_fd(self) -> RawFd
;
54 impl AsRawFd
for net
::TcpStream
{
55 fn as_raw_fd(&self) -> RawFd
{
56 self.as_inner().fd().as_raw()
60 impl FromRawFd
for net
::TcpStream
{
61 unsafe fn from_raw_fd(fd
: RawFd
) -> net
::TcpStream
{
62 net
::TcpStream
::from_inner(sys
::net
::TcpStream
::from_inner(fd
))
66 impl IntoRawFd
for net
::TcpStream
{
67 fn into_raw_fd(self) -> RawFd
{
68 self.into_inner().into_fd().into_raw()
72 impl AsRawFd
for net
::TcpListener
{
73 fn as_raw_fd(&self) -> RawFd
{
74 self.as_inner().fd().as_raw()
78 impl FromRawFd
for net
::TcpListener
{
79 unsafe fn from_raw_fd(fd
: RawFd
) -> net
::TcpListener
{
80 net
::TcpListener
::from_inner(sys
::net
::TcpListener
::from_inner(fd
))
84 impl IntoRawFd
for net
::TcpListener
{
85 fn into_raw_fd(self) -> RawFd
{
86 self.into_inner().into_fd().into_raw()
90 impl AsRawFd
for net
::UdpSocket
{
91 fn as_raw_fd(&self) -> RawFd
{
92 self.as_inner().fd().as_raw()
96 impl FromRawFd
for net
::UdpSocket
{
97 unsafe fn from_raw_fd(fd
: RawFd
) -> net
::UdpSocket
{
98 net
::UdpSocket
::from_inner(sys
::net
::UdpSocket
::from_inner(fd
))
102 impl IntoRawFd
for net
::UdpSocket
{
103 fn into_raw_fd(self) -> RawFd
{
104 self.into_inner().into_fd().into_raw()
108 impl AsRawFd
for fs
::File
{
109 fn as_raw_fd(&self) -> RawFd
{
110 self.as_inner().fd().as_raw()
114 impl FromRawFd
for fs
::File
{
115 unsafe fn from_raw_fd(fd
: RawFd
) -> fs
::File
{
116 fs
::File
::from_inner(sys
::fs
::File
::from_inner(fd
))
120 impl IntoRawFd
for fs
::File
{
121 fn into_raw_fd(self) -> RawFd
{
122 self.into_inner().into_fd().into_raw()
126 impl AsRawFd
for io
::Stdin
{
127 fn as_raw_fd(&self) -> RawFd
{
128 sys
::stdio
::Stdin
.as_raw_fd()
132 impl AsRawFd
for io
::Stdout
{
133 fn as_raw_fd(&self) -> RawFd
{
134 sys
::stdio
::Stdout
.as_raw_fd()
138 impl AsRawFd
for io
::Stderr
{
139 fn as_raw_fd(&self) -> RawFd
{
140 sys
::stdio
::Stderr
.as_raw_fd()