Debian Codegraph @

third_party/rust/rustix/src/backend/libc/conv.rs

raw
1 //! Libc call arguments and return values are often things like `c_int`,
2 //! `c_uint`, or libc-specific pointer types. This module provides functions
3 //! for converting between rustix's types and libc types.
4
5 use super::c;
6 #[cfg(all(feature = "alloc", not(any(windows, target_os = "espidf"))))]
7 use super::fd::IntoRawFd;
8 use super::fd::{AsRawFd, BorrowedFd, FromRawFd, LibcFd, OwnedFd, RawFd};
9 #[cfg(not(windows))]
10 use crate::ffi::CStr;
11 use crate::io;
12
13 #[cfg(not(windows))]
14 #[inline]
15 pub(super) fn c_str(c: &CStr) -> *const c::c_char {
16 c.as_ptr().cast()
17 }
18
19 #[cfg(not(any(windows, target_os = "espidf", target_os = "vita", target_os = "wasi")))]
20 #[inline]
21 pub(super) fn no_fd() -> LibcFd {
22 -1
23 }
24
25 #[inline]
26 pub(super) fn borrowed_fd(fd: BorrowedFd<'_>) -> LibcFd {
27 fd.as_raw_fd() as LibcFd
28 }
29
30 #[cfg(all(
31 feature = "alloc",
32 not(any(windows, target_os = "espidf", target_os = "redox"))
33 ))]
34 #[inline]
35 pub(super) fn owned_fd(fd: OwnedFd) -> LibcFd {
36 fd.into_raw_fd() as LibcFd
37 }
38
39 #[inline]
40 pub(super) fn ret(raw: c::c_int) -> io::Result<()> {
41 if raw == 0 {
42 Ok(())
43 } else {
44 Err(io::Errno::last_os_error())
45 }
46 }
47
48 #[cfg(apple)]
49 #[inline]
50 pub(super) fn nonnegative_ret(raw: c::c_int) -> io::Result<()> {
51 if raw >= 0 {
52 Ok(())
53 } else {
54 Err(io::Errno::last_os_error())
55 }
56 }
57
58 #[cfg(not(any(windows, target_os = "wasi")))]
59 #[inline]
60 pub(super) unsafe fn ret_infallible(raw: c::c_int) {
61 debug_assert_eq!(raw, 0, "unexpected error: {:?}", io::Errno::last_os_error());
62 }
63
64 #[inline]
65 pub(super) fn ret_c_int(raw: c::c_int) -> io::Result<c::c_int> {
66 if raw == -1 {
67 Err(io::Errno::last_os_error())
68 } else {
69 Ok(raw)
70 }
71 }
72
73 #[cfg(any(
74 linux_kernel,
75 all(solarish, feature = "event"),
76 all(target_os = "redox", feature = "event")
77 ))]
78 #[inline]
79 pub(super) fn ret_u32(raw: c::c_int) -> io::Result<u32> {
80 if raw == -1 {
81 Err(io::Errno::last_os_error())
82 } else {
83 Ok(raw as u32)
84 }
85 }
86
87 #[inline]
88 pub(super) fn ret_usize(raw: c::ssize_t) -> io::Result<usize> {
89 if raw == -1 {
90 Err(io::Errno::last_os_error())
91 } else {
92 debug_assert!(raw >= 0);
93 Ok(raw as usize)
94 }
95 }
96
97 #[cfg(not(windows))]
98 #[cfg(feature = "fs")]
99 #[inline]
100 pub(super) fn ret_off_t(raw: c::off_t) -> io::Result<c::off_t> {
101 if raw == -1 {
102 Err(io::Errno::last_os_error())
103 } else {
104 Ok(raw)
105 }
106 }
107
108 #[cfg(not(any(windows, target_os = "wasi")))]
109 #[inline]
110 pub(super) fn ret_pid_t(raw: c::pid_t) -> io::Result<c::pid_t> {
111 if raw == -1 {
112 Err(io::Errno::last_os_error())
113 } else {
114 Ok(raw)
115 }
116 }
117
118 /// Convert a `c_int` returned from a libc function to an `OwnedFd`, if valid.
119 ///
120 /// # Safety
121 ///
122 /// The caller must ensure that this is the return value of a libc function
123 /// which returns an owned file descriptor.
124 #[inline]
125 pub(super) unsafe fn ret_owned_fd(raw: LibcFd) -> io::Result<OwnedFd> {
126 if raw == !0 {
127 Err(io::Errno::last_os_error())
128 } else {
129 Ok(OwnedFd::from_raw_fd(raw as RawFd))
130 }
131 }
132
133 #[cfg(not(any(windows, target_os = "wasi")))]
134 #[inline]
135 pub(super) fn ret_discarded_fd(raw: LibcFd) -> io::Result<()> {
136 if raw == !0 {
137 Err(io::Errno::last_os_error())
138 } else {
139 Ok(())
140 }
141 }
142
143 #[cfg(all(feature = "alloc", not(any(windows, target_os = "wasi"))))]
144 #[inline]
145 pub(super) fn ret_discarded_char_ptr(raw: *mut c::c_char) -> io::Result<()> {
146 if raw.is_null() {
147 Err(io::Errno::last_os_error())
148 } else {
149 Ok(())
150 }
151 }
152
153 /// Convert the buffer-length argument value of a `send` or `recv` call.
154 #[cfg(not(any(windows, target_os = "redox", target_os = "wasi")))]
155 #[inline]
156 pub(super) fn send_recv_len(len: usize) -> usize {
157 len
158 }
159
160 /// Convert the buffer-length argument value of a `send` or `recv` call.
161 #[cfg(windows)]
162 #[inline]
163 pub(super) fn send_recv_len(len: usize) -> i32 {
164 // On Windows, the length argument has type `i32`; saturate the length,
165 // since `send` and `recv` are allowed to send and recv less data than
166 // requested.
167 len.try_into().unwrap_or(i32::MAX)
168 }
169
170 /// Convert the return value of a `send` or `recv` call.
171 #[cfg(not(any(windows, target_os = "redox", target_os = "wasi")))]
172 #[inline]
173 pub(super) fn ret_send_recv(len: isize) -> io::Result<usize> {
174 ret_usize(len)
175 }
176
177 /// Convert the return value of a `send` or `recv` call.
178 #[cfg(windows)]
179 #[inline]
180 pub(super) fn ret_send_recv(len: i32) -> io::Result<usize> {
181 ret_usize(len as isize)
182 }
183
184 /// Convert the value to the `msg_iovlen` field of a `msghdr` struct.
185 #[cfg(all(
186 not(any(windows, target_os = "espidf", target_os = "redox", target_os = "wasi")),
187 any(
188 target_os = "android",
189 all(
190 target_os = "linux",
191 not(target_env = "musl"),
192 not(all(target_env = "uclibc", any(target_arch = "arm", target_arch = "mips")))
193 )
194 )
195 ))]
196 #[inline]
197 pub(super) fn msg_iov_len(len: usize) -> c::size_t {
198 len
199 }
200
201 /// Convert the value to the `msg_iovlen` field of a `msghdr` struct.
202 #[cfg(all(
203 not(any(
204 windows,
205 target_os = "espidf",
206 target_os = "redox",
207 target_os = "vita",
208 target_os = "wasi"
209 )),
210 not(any(
211 target_os = "android",
212 all(
213 target_os = "linux",
214 not(target_env = "musl"),
215 not(all(target_env = "uclibc", any(target_arch = "arm", target_arch = "mips")))
216 )
217 ))
218 ))]
219 #[inline]
220 pub(crate) fn msg_iov_len(len: usize) -> c::c_int {
221 len.try_into().unwrap_or(c::c_int::MAX)
222 }
223
224 /// Convert the value to a `socklen_t`.
225 #[cfg(any(
226 bsd,
227 solarish,
228 target_env = "musl",
229 target_os = "aix",
230 target_os = "emscripten",
231 target_os = "fuchsia",
232 target_os = "haiku",
233 target_os = "hurd",
234 target_os = "nto",
235 ))]
236 #[inline]
237 pub(crate) fn msg_control_len(len: usize) -> c::socklen_t {
238 len.try_into().unwrap_or(c::socklen_t::MAX)
239 }
240
241 /// Convert the value to a `size_t`.
242 #[cfg(not(any(
243 bsd,
244 solarish,
245 windows,
246 target_env = "musl",
247 target_os = "aix",
248 target_os = "emscripten",
249 target_os = "espidf",
250 target_os = "fuchsia",
251 target_os = "haiku",
252 target_os = "hurd",
253 target_os = "nto",
254 target_os = "redox",
255 target_os = "vita",
256 target_os = "wasi",
257 )))]
258 #[inline]
259 pub(crate) fn msg_control_len(len: usize) -> c::size_t {
260 len
261 }