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