1 #[cfg(linux_kernel)]
2 use core::marker::PhantomData;
3 #[cfg(not(any(apple, target_os = "wasi")))]
4 use {crate::backend::c, bitflags::bitflags};
5
6 #[cfg(not(any(apple, target_os = "wasi")))]
7 bitflags! {
8 /// `O_*` constants for use with [`pipe_with`].
9 ///
10 /// [`pipe_with`]: crate::pipe::pipe_with
11 #[repr(transparent)]
12 #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
13 pub struct PipeFlags: u32 {
14 /// `O_CLOEXEC`
15 const CLOEXEC = bitcast!(c::O_CLOEXEC);
16 /// `O_DIRECT`
17 #[cfg(not(any(
18 solarish,
19 target_os = "espidf",
20 target_os = "haiku",
21 target_os = "nto",
22 target_os = "openbsd",
23 target_os = "redox",
24 target_os = "vita",
25 )))]
26 const DIRECT = bitcast!(c::O_DIRECT);
27 /// `O_NONBLOCK`
28 const NONBLOCK = bitcast!(c::O_NONBLOCK);
29
30 /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
31 const _ = !0;
32 }
33 }
34
35 #[cfg(linux_kernel)]
36 bitflags! {
37 /// `SPLICE_F_*` constants for use with [`splice`], [`vmsplice`], and
38 /// [`tee`].
39 #[repr(transparent)]
40 #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
41 pub struct SpliceFlags: c::c_uint {
42 /// `SPLICE_F_MOVE`
43 const MOVE = c::SPLICE_F_MOVE;
44 /// `SPLICE_F_NONBLOCK`
45 const NONBLOCK = c::SPLICE_F_NONBLOCK;
46 /// `SPLICE_F_MORE`
47 const MORE = c::SPLICE_F_MORE;
48 /// `SPLICE_F_GIFT`
49 const GIFT = c::SPLICE_F_GIFT;
50
51 /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
52 const _ = !0;
53 }
54 }
55
56 /// A buffer type for use with [`vmsplice`].
57 ///
58 /// It is guaranteed to be ABI compatible with the iovec type on Unix platforms
59 /// and `WSABUF` on Windows. Unlike `IoSlice` and `IoSliceMut` it is
60 /// semantically like a raw pointer, and therefore can be shared or mutated as
61 /// needed.
62 ///
63 /// [`vmsplice`]: crate::pipe::vmsplice
64 #[cfg(linux_kernel)]
65 #[repr(transparent)]
66 pub struct IoSliceRaw<'a> {
67 _buf: c::iovec,
68 _lifetime: PhantomData<&'a ()>,
69 }
70
71 #[cfg(linux_kernel)]
72 impl<'a> IoSliceRaw<'a> {
73 /// Creates a new `IoSlice` wrapping a byte slice.
from_slice(buf: &'a [u8]) -> Self74 pub fn from_slice(buf: &'a [u8]) -> Self {
75 IoSliceRaw {
76 _buf: c::iovec {
77 iov_base: buf.as_ptr() as *mut u8 as *mut c::c_void,
78 iov_len: buf.len() as _,
79 },
80 _lifetime: PhantomData,
81 }
82 }
83
84 /// Creates a new `IoSlice` wrapping a mutable byte slice.
from_slice_mut(buf: &'a mut [u8]) -> Self85 pub fn from_slice_mut(buf: &'a mut [u8]) -> Self {
86 IoSliceRaw {
87 _buf: c::iovec {
88 iov_base: buf.as_mut_ptr() as *mut c::c_void,
89 iov_len: buf.len() as _,
90 },
91 _lifetime: PhantomData,
92 }
93 }
94 }
95
96 #[cfg(not(any(apple, target_os = "wasi")))]
97 #[test]
test_types()98 fn test_types() {
99 assert_eq_size!(PipeFlags, c::c_int);
100
101 #[cfg(linux_kernel)]
102 assert_eq_size!(SpliceFlags, c::c_int);
103 }
104