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