]> git.proxmox.com Git - rustc.git/blame - src/libstd/sys/unix/ext/io.rs
New upstream version 1.41.1+dfsg1
[rustc.git] / src / libstd / sys / unix / ext / io.rs
CommitLineData
d9579d0f
AL
1//! Unix-specific extensions to general I/O primitives
2
3#![stable(feature = "rust1", since = "1.0.0")]
4
532ac7d7 5use crate::fs;
60c5eb7d 6use crate::io;
532ac7d7
XL
7use crate::os::raw;
8use crate::sys;
532ac7d7 9use crate::sys_common::{AsInner, FromInner, IntoInner};
d9579d0f
AL
10
11/// Raw file descriptors.
12#[stable(feature = "rust1", since = "1.0.0")]
13pub type RawFd = raw::c_int;
14
15/// A trait to extract the raw unix file descriptor from an underlying
16/// object.
17///
18/// This is only available on unix platforms and must be imported in order
19/// to call the method. Windows platforms have a corresponding `AsRawHandle`
20/// and `AsRawSocket` set of traits.
21#[stable(feature = "rust1", since = "1.0.0")]
22pub trait AsRawFd {
23 /// Extracts the raw file descriptor.
24 ///
25 /// This method does **not** pass ownership of the raw file descriptor
9cc50fc6 26 /// to the caller. The descriptor is only guaranteed to be valid while
d9579d0f
AL
27 /// the original object has not yet been destroyed.
28 #[stable(feature = "rust1", since = "1.0.0")]
29 fn as_raw_fd(&self) -> RawFd;
30}
31
32/// A trait to express the ability to construct an object from a raw file
33/// descriptor.
34#[stable(feature = "from_raw_os", since = "1.1.0")]
35pub trait FromRawFd {
476ff2be 36 /// Constructs a new instance of `Self` from the given raw file
d9579d0f
AL
37 /// descriptor.
38 ///
39 /// This function **consumes ownership** of the specified file
40 /// descriptor. The returned object will take responsibility for closing
41 /// it when the object goes out of scope.
42 ///
43 /// This function is also unsafe as the primitives currently returned
44 /// have the contract that they are the sole owner of the file
45 /// descriptor they are wrapping. Usage of this function could
46 /// accidentally allow violating this contract which can cause memory
47 /// unsafety in code that relies on it being true.
48 #[stable(feature = "from_raw_os", since = "1.1.0")]
49 unsafe fn from_raw_fd(fd: RawFd) -> Self;
50}
51
c1a9b12d
SL
52/// A trait to express the ability to consume an object and acquire ownership of
53/// its raw file descriptor.
e9174d1e 54#[stable(feature = "into_raw_os", since = "1.4.0")]
c1a9b12d
SL
55pub trait IntoRawFd {
56 /// Consumes this object, returning the raw underlying file descriptor.
57 ///
58 /// This function **transfers ownership** of the underlying file descriptor
59 /// to the caller. Callers are then the unique owners of the file descriptor
60 /// and must close the descriptor once it's no longer needed.
e9174d1e 61 #[stable(feature = "into_raw_os", since = "1.4.0")]
c1a9b12d
SL
62 fn into_raw_fd(self) -> RawFd;
63}
64
d9579d0f
AL
65#[stable(feature = "rust1", since = "1.0.0")]
66impl AsRawFd for fs::File {
67 fn as_raw_fd(&self) -> RawFd {
68 self.as_inner().fd().raw()
69 }
70}
71#[stable(feature = "from_raw_os", since = "1.1.0")]
72impl FromRawFd for fs::File {
73 unsafe fn from_raw_fd(fd: RawFd) -> fs::File {
74 fs::File::from_inner(sys::fs::File::from_inner(fd))
75 }
76}
e9174d1e 77#[stable(feature = "into_raw_os", since = "1.4.0")]
c1a9b12d
SL
78impl IntoRawFd for fs::File {
79 fn into_raw_fd(self) -> RawFd {
80 self.into_inner().into_fd().into_raw()
81 }
82}
d9579d0f 83
3b2f2976
XL
84#[stable(feature = "asraw_stdio", since = "1.21.0")]
85impl AsRawFd for io::Stdin {
60c5eb7d
XL
86 fn as_raw_fd(&self) -> RawFd {
87 libc::STDIN_FILENO
88 }
d9579d0f
AL
89}
90
3b2f2976
XL
91#[stable(feature = "asraw_stdio", since = "1.21.0")]
92impl AsRawFd for io::Stdout {
60c5eb7d
XL
93 fn as_raw_fd(&self) -> RawFd {
94 libc::STDOUT_FILENO
95 }
d9579d0f 96}
c1a9b12d 97
3b2f2976
XL
98#[stable(feature = "asraw_stdio", since = "1.21.0")]
99impl AsRawFd for io::Stderr {
60c5eb7d
XL
100 fn as_raw_fd(&self) -> RawFd {
101 libc::STDERR_FILENO
102 }
c1a9b12d 103}
532ac7d7
XL
104
105#[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
106impl<'a> AsRawFd for io::StdinLock<'a> {
60c5eb7d
XL
107 fn as_raw_fd(&self) -> RawFd {
108 libc::STDIN_FILENO
109 }
532ac7d7
XL
110}
111
112#[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
113impl<'a> AsRawFd for io::StdoutLock<'a> {
60c5eb7d
XL
114 fn as_raw_fd(&self) -> RawFd {
115 libc::STDOUT_FILENO
116 }
532ac7d7
XL
117}
118
119#[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
120impl<'a> AsRawFd for io::StderrLock<'a> {
60c5eb7d
XL
121 fn as_raw_fd(&self) -> RawFd {
122 libc::STDERR_FILENO
123 }
532ac7d7 124}