]> git.proxmox.com Git - rustc.git/blame - vendor/io-lifetimes/src/traits.rs
New upstream version 1.66.0+dfsg1
[rustc.git] / vendor / io-lifetimes / src / traits.rs
CommitLineData
064997fb
FG
1#[cfg(not(io_lifetimes_use_std))]
2#[cfg(any(unix, target_os = "wasi"))]
3use crate::BorrowedFd;
4#[cfg(any(unix, target_os = "wasi"))]
5use crate::OwnedFd;
6#[cfg(not(io_lifetimes_use_std))]
7#[cfg(windows)]
8use crate::{BorrowedHandle, BorrowedSocket};
9#[cfg(windows)]
10use crate::{OwnedHandle, OwnedSocket};
11
12/// A trait to borrow the file descriptor from an underlying object.
13///
14/// This is only available on unix platforms and must be imported in order to
15/// call the method. Windows platforms have a corresponding `AsHandle` and
16/// `AsSocket` set of traits.
17#[cfg(not(io_lifetimes_use_std))]
18#[cfg(any(unix, target_os = "wasi"))]
19pub trait AsFd {
20 /// Borrows the file descriptor.
21 ///
22 /// # Example
23 ///
24 /// ```rust,no_run
25 /// # #![cfg_attr(io_lifetimes_use_std, feature(io_safety))]
26 /// use std::fs::File;
27 /// # use std::io;
28 /// use io_lifetimes::{AsFd, BorrowedFd};
29 ///
30 /// let mut f = File::open("foo.txt")?;
31 /// let borrowed_fd: BorrowedFd<'_> = f.as_fd();
32 /// # Ok::<(), io::Error>(())
33 /// ```
34 fn as_fd(&self) -> BorrowedFd<'_>;
35}
36
37/// A trait to borrow the handle from an underlying object.
38#[cfg(not(io_lifetimes_use_std))]
39#[cfg(windows)]
40pub trait AsHandle {
41 /// Borrows the handle.
42 ///
43 /// # Example
44 ///
45 /// ```rust,no_run
46 /// # #![cfg_attr(io_lifetimes_use_std, feature(io_safety))]
47 /// use std::fs::File;
48 /// # use std::io;
49 /// use io_lifetimes::{AsHandle, BorrowedHandle};
50 ///
51 /// let mut f = File::open("foo.txt")?;
52 /// let borrowed_handle: BorrowedHandle<'_> = f.as_handle();
53 /// # Ok::<(), io::Error>(())
54 /// ```
55 fn as_handle(&self) -> BorrowedHandle<'_>;
56}
57
58/// A trait to borrow the socket from an underlying object.
59#[cfg(not(io_lifetimes_use_std))]
60#[cfg(windows)]
61pub trait AsSocket {
62 /// Borrows the socket.
63 fn as_socket(&self) -> BorrowedSocket<'_>;
64}
65
66/// A trait to express the ability to consume an object and acquire ownership
67/// of its file descriptor.
68#[cfg(any(unix, target_os = "wasi"))]
69pub trait IntoFd {
70 /// Consumes this object, returning the underlying file descriptor.
71 ///
72 /// # Example
73 ///
74 /// ```rust,no_run
75 /// # #![cfg_attr(io_lifetimes_use_std, feature(io_safety))]
76 /// use std::fs::File;
77 /// # use std::io;
78 /// use io_lifetimes::{IntoFd, OwnedFd};
79 ///
80 /// let f = File::open("foo.txt")?;
81 /// let owned_fd: OwnedFd = f.into_fd();
82 /// # Ok::<(), io::Error>(())
83 /// ```
84 fn into_fd(self) -> OwnedFd;
85}
86
87/// A trait to express the ability to consume an object and acquire ownership
88/// of its handle.
89#[cfg(windows)]
90pub trait IntoHandle {
91 /// Consumes this object, returning the underlying handle.
92 ///
93 /// # Example
94 ///
95 /// ```rust,no_run
96 /// # #![cfg_attr(io_lifetimes_use_std, feature(io_safety))]
97 /// use std::fs::File;
98 /// # use std::io;
99 /// use io_lifetimes::{IntoHandle, OwnedHandle};
100 ///
101 /// let f = File::open("foo.txt")?;
102 /// let owned_handle: OwnedHandle = f.into_handle();
103 /// # Ok::<(), io::Error>(())
104 /// ```
105 fn into_handle(self) -> OwnedHandle;
106}
107
108/// A trait to express the ability to consume an object and acquire ownership
109/// of its socket.
110#[cfg(windows)]
111pub trait IntoSocket {
112 /// Consumes this object, returning the underlying socket.
113 fn into_socket(self) -> OwnedSocket;
114}
115
116/// A trait to express the ability to construct an object from a file
117/// descriptor.
118#[cfg(any(unix, target_os = "wasi"))]
119pub trait FromFd {
120 /// Constructs a new instance of `Self` from the given file descriptor.
121 ///
122 /// # Example
123 ///
124 /// ```rust,no_run
125 /// # #![cfg_attr(io_lifetimes_use_std, feature(io_safety))]
126 /// use std::fs::File;
127 /// # use std::io;
128 /// use io_lifetimes::{FromFd, IntoFd, OwnedFd};
129 ///
130 /// let f = File::open("foo.txt")?;
131 /// let owned_fd: OwnedFd = f.into_fd();
132 /// let f = File::from_fd(owned_fd);
133 /// # Ok::<(), io::Error>(())
134 /// ```
135 fn from_fd(owned: OwnedFd) -> Self;
136
137 /// Constructs a new instance of `Self` from the given file descriptor
138 /// converted from `into_owned`.
139 ///
140 /// # Example
141 ///
142 /// ```rust,no_run
143 /// # #![cfg_attr(io_lifetimes_use_std, feature(io_safety))]
144 /// use std::fs::File;
145 /// # use std::io;
146 /// use io_lifetimes::{FromFd, IntoFd};
147 ///
148 /// let f = File::open("foo.txt")?;
149 /// let f = File::from_into_fd(f);
150 /// # Ok::<(), io::Error>(())
151 /// ```
152 #[inline]
153 fn from_into_fd<Owned: IntoFd>(into_owned: Owned) -> Self
154 where
155 Self: Sized,
156 {
157 Self::from_fd(into_owned.into_fd())
158 }
159}
160
161/// A trait to express the ability to construct an object from a handle.
162#[cfg(windows)]
163pub trait FromHandle {
164 /// Constructs a new instance of `Self` from the given handle.
165 ///
166 /// # Example
167 ///
168 /// ```rust,no_run
169 /// # #![cfg_attr(io_lifetimes_use_std, feature(io_safety))]
170 /// use std::fs::File;
171 /// # use std::io;
172 /// use io_lifetimes::{FromHandle, IntoHandle, OwnedHandle};
173 ///
174 /// let f = File::open("foo.txt")?;
175 /// let owned_handle: OwnedHandle = f.into_handle();
176 /// let f = File::from_handle(owned_handle);
177 /// # Ok::<(), io::Error>(())
178 /// ```
179 fn from_handle(owned: OwnedHandle) -> Self;
180
181 /// Constructs a new instance of `Self` from the given handle converted
182 /// from `into_owned`.
183 ///
184 /// # Example
185 ///
186 /// ```rust,no_run
187 /// # #![cfg_attr(io_lifetimes_use_std, feature(io_safety))]
188 /// use std::fs::File;
189 /// # use std::io;
190 /// use io_lifetimes::{FromHandle, IntoHandle};
191 ///
192 /// let f = File::open("foo.txt")?;
193 /// let f = File::from_into_handle(f);
194 /// # Ok::<(), io::Error>(())
195 /// ```
196 #[inline]
197 fn from_into_handle<Owned: IntoHandle>(into_owned: Owned) -> Self
198 where
199 Self: Sized,
200 {
201 Self::from_handle(into_owned.into_handle())
202 }
203}
204
205/// A trait to express the ability to construct an object from a socket.
206#[cfg(windows)]
207pub trait FromSocket {
208 /// Constructs a new instance of `Self` from the given socket.
209 fn from_socket(owned: OwnedSocket) -> Self;
210
211 /// Constructs a new instance of `Self` from the given socket converted
212 /// from `into_owned`.
213 #[inline]
214 fn from_into_socket<Owned: IntoSocket>(into_owned: Owned) -> Self
215 where
216 Self: Sized,
217 {
218 Self::from_socket(into_owned.into_socket())
219 }
220}
221
222#[cfg(not(io_lifetimes_use_std))]
223#[cfg(any(unix, target_os = "wasi"))]
224impl<T: AsFd> AsFd for &T {
225 #[inline]
226 fn as_fd(&self) -> BorrowedFd<'_> {
227 T::as_fd(self)
228 }
229}
230
231#[cfg(not(io_lifetimes_use_std))]
232#[cfg(any(unix, target_os = "wasi"))]
233impl<T: AsFd> AsFd for &mut T {
234 #[inline]
235 fn as_fd(&self) -> BorrowedFd<'_> {
236 T::as_fd(self)
237 }
238}
239
240#[cfg(not(io_lifetimes_use_std))]
241#[cfg(windows)]
242impl<T: AsHandle> AsHandle for &T {
243 #[inline]
244 fn as_handle(&self) -> BorrowedHandle<'_> {
245 T::as_handle(self)
246 }
247}
248
249#[cfg(not(io_lifetimes_use_std))]
250#[cfg(windows)]
251impl<T: AsHandle> AsHandle for &mut T {
252 #[inline]
253 fn as_handle(&self) -> BorrowedHandle<'_> {
254 T::as_handle(self)
255 }
256}
257
258#[cfg(not(io_lifetimes_use_std))]
259#[cfg(windows)]
260impl<T: AsSocket> AsSocket for &T {
261 #[inline]
262 fn as_socket(&self) -> BorrowedSocket<'_> {
263 T::as_socket(self)
264 }
265}
266
267#[cfg(not(io_lifetimes_use_std))]
268#[cfg(windows)]
269impl<T: AsSocket> AsSocket for &mut T {
270 #[inline]
271 fn as_socket(&self) -> BorrowedSocket<'_> {
272 T::as_socket(self)
273 }
274}