rustix/backend/linux_raw/pipe/
types.rs

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