]> git.proxmox.com Git - rustc.git/blob - vendor/rustix/src/io/read_write.rs
New upstream version 1.67.1+dfsg1
[rustc.git] / vendor / rustix / src / io / read_write.rs
1 //! `read` and `write`, optionally positioned, optionally vectored
2
3 use crate::{backend, io};
4 use backend::fd::AsFd;
5
6 // Declare `IoSlice` and `IoSliceMut`.
7 #[cfg(not(windows))]
8 #[cfg(not(feature = "std"))]
9 pub use backend::io::io_slice::{IoSlice, IoSliceMut};
10 #[cfg(not(windows))]
11 #[cfg(feature = "std")]
12 pub use std::io::{IoSlice, IoSliceMut};
13
14 /// `RWF_*` constants for use with [`preadv2`] and [`pwritev2`].
15 #[cfg(any(target_os = "android", target_os = "linux"))]
16 pub use backend::io::types::ReadWriteFlags;
17
18 /// `read(fd, buf)`—Reads from a stream.
19 ///
20 /// # References
21 /// - [POSIX]
22 /// - [Linux]
23 /// - [Apple]
24 ///
25 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/read.html
26 /// [Linux]: https://man7.org/linux/man-pages/man2/read.2.html
27 /// [Apple]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/read.2.html
28 #[inline]
29 pub fn read<Fd: AsFd>(fd: Fd, buf: &mut [u8]) -> io::Result<usize> {
30 backend::io::syscalls::read(fd.as_fd(), buf)
31 }
32
33 /// `write(fd, buf)`—Writes to a stream.
34 ///
35 /// # References
36 /// - [POSIX]
37 /// - [Linux]
38 /// - [Apple]
39 ///
40 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/write.html
41 /// [Linux]: https://man7.org/linux/man-pages/man2/write.2.html
42 /// [Apple]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/write.2.html
43 #[inline]
44 pub fn write<Fd: AsFd>(fd: Fd, buf: &[u8]) -> io::Result<usize> {
45 backend::io::syscalls::write(fd.as_fd(), buf)
46 }
47
48 /// `pread(fd, buf, offset)`—Reads from a file at a given position.
49 ///
50 /// # References
51 /// - [POSIX]
52 /// - [Linux]
53 /// - [Apple]
54 ///
55 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/pread.html
56 /// [Linux]: https://man7.org/linux/man-pages/man2/pread.2.html
57 /// [Apple]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/pread.2.html
58 #[inline]
59 pub fn pread<Fd: AsFd>(fd: Fd, buf: &mut [u8], offset: u64) -> io::Result<usize> {
60 backend::io::syscalls::pread(fd.as_fd(), buf, offset)
61 }
62
63 /// `pwrite(fd, bufs)`—Writes to a file at a given position.
64 ///
65 /// # References
66 /// - [POSIX]
67 /// - [Linux]
68 /// - [Apple]
69 ///
70 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/pwrite.html
71 /// [Linux]: https://man7.org/linux/man-pages/man2/pwrite.2.html
72 /// [Apple]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/pwrite.2.html
73 #[inline]
74 pub fn pwrite<Fd: AsFd>(fd: Fd, buf: &[u8], offset: u64) -> io::Result<usize> {
75 backend::io::syscalls::pwrite(fd.as_fd(), buf, offset)
76 }
77
78 /// `readv(fd, bufs)`—Reads from a stream into multiple buffers.
79 ///
80 /// # References
81 /// - [POSIX]
82 /// - [Linux]
83 /// - [Apple]
84 ///
85 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/readv.html
86 /// [Linux]: https://man7.org/linux/man-pages/man2/readv.2.html
87 /// [Apple]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/readv.2.html
88 #[inline]
89 pub fn readv<Fd: AsFd>(fd: Fd, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
90 backend::io::syscalls::readv(fd.as_fd(), bufs)
91 }
92
93 /// `writev(fd, bufs)`—Writes to a stream from multiple buffers.
94 ///
95 /// # References
96 /// - [POSIX]
97 /// - [Linux]
98 /// - [Apple]
99 ///
100 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/writev.html
101 /// [Linux]: https://man7.org/linux/man-pages/man2/writev.2.html
102 /// [Apple]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/writev.2.html
103 #[inline]
104 pub fn writev<Fd: AsFd>(fd: Fd, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
105 backend::io::syscalls::writev(fd.as_fd(), bufs)
106 }
107
108 /// `preadv(fd, bufs, offset)`—Reads from a file at a given position into
109 /// multiple buffers.
110 ///
111 /// # References
112 /// - [Linux]
113 ///
114 /// [Linux]: https://man7.org/linux/man-pages/man2/preadv.2.html
115 #[cfg(not(any(target_os = "haiku", target_os = "redox", target_os = "solaris")))]
116 #[inline]
117 pub fn preadv<Fd: AsFd>(fd: Fd, bufs: &mut [IoSliceMut<'_>], offset: u64) -> io::Result<usize> {
118 backend::io::syscalls::preadv(fd.as_fd(), bufs, offset)
119 }
120
121 /// `pwritev(fd, bufs, offset)`—Writes to a file at a given position from
122 /// multiple buffers.
123 ///
124 /// # References
125 /// - [Linux]
126 ///
127 /// [Linux]: https://man7.org/linux/man-pages/man2/pwritev.2.html
128 #[cfg(not(any(target_os = "haiku", target_os = "redox", target_os = "solaris")))]
129 #[inline]
130 pub fn pwritev<Fd: AsFd>(fd: Fd, bufs: &[IoSlice<'_>], offset: u64) -> io::Result<usize> {
131 backend::io::syscalls::pwritev(fd.as_fd(), bufs, offset)
132 }
133
134 /// `preadv2(fd, bufs, offset, flags)`—Reads data, with several options.
135 ///
136 /// An `offset` of `u64::MAX` means to use and update the current file offset.
137 ///
138 /// # References
139 /// - [Linux]
140 ///
141 /// [Linux]: https://man7.org/linux/man-pages/man2/preadv2.2.html
142 #[cfg(any(target_os = "android", target_os = "linux"))]
143 #[inline]
144 pub fn preadv2<Fd: AsFd>(
145 fd: Fd,
146 bufs: &mut [IoSliceMut<'_>],
147 offset: u64,
148 flags: ReadWriteFlags,
149 ) -> io::Result<usize> {
150 backend::io::syscalls::preadv2(fd.as_fd(), bufs, offset, flags)
151 }
152
153 /// `pwritev2(fd, bufs, offset, flags)`—Writes data, with several options.
154 ///
155 /// An `offset` of `u64::MAX` means to use and update the current file offset.
156 ///
157 /// # References
158 /// - [Linux]
159 ///
160 /// [Linux]: https://man7.org/linux/man-pages/man2/pwritev2.2.html
161 #[cfg(any(target_os = "android", target_os = "linux"))]
162 #[inline]
163 pub fn pwritev2<Fd: AsFd>(
164 fd: Fd,
165 bufs: &[IoSlice<'_>],
166 offset: u64,
167 flags: ReadWriteFlags,
168 ) -> io::Result<usize> {
169 backend::io::syscalls::pwritev2(fd.as_fd(), bufs, offset, flags)
170 }