Skip to main content

core/io/io_slice/
repr_iovec.rs

1use crate::ffi::c_void;
2use crate::marker::PhantomData;
3use crate::slice;
4
5#[derive(Copy, Clone)]
6#[repr(C)]
7struct iovec {
8    iov_base: *mut c_void,
9    iov_len: usize,
10}
11
12#[derive(Copy, Clone)]
13#[repr(transparent)]
14pub(crate) struct IoSlice<'a> {
15    vec: iovec,
16    _p: PhantomData<&'a [u8]>,
17}
18
19impl<'a> IoSlice<'a> {
20    #[inline]
21    pub(crate) const fn new(buf: &'a [u8]) -> IoSlice<'a> {
22        IoSlice {
23            vec: iovec { iov_base: buf.as_ptr() as *mut u8 as *mut c_void, iov_len: buf.len() },
24            _p: PhantomData,
25        }
26    }
27
28    #[inline]
29    pub(crate) const fn advance(&mut self, n: usize) {
30        assert!(n <= self.vec.iov_len, "advancing IoSlice beyond its length");
31
32        // SAFETY:
33        //  * `n <= iov_len` as asserted above.
34        //  * The allocation pointed to by `iov_base` is valid up to `iov_base + iov_len`.
35        unsafe {
36            self.vec.iov_len -= n;
37            self.vec.iov_base = self.vec.iov_base.add(n);
38        }
39    }
40
41    #[inline]
42    pub(crate) const fn as_slice(&self) -> &'a [u8] {
43        // SAFETY:
44        //  * `iov_base` and `iov_len` come from a prior decomposition of a valid slice.
45        unsafe { slice::from_raw_parts(self.vec.iov_base as *mut u8, self.vec.iov_len) }
46    }
47}
48
49#[repr(transparent)]
50pub(crate) struct IoSliceMut<'a> {
51    vec: iovec,
52    _p: PhantomData<&'a mut [u8]>,
53}
54
55impl<'a> IoSliceMut<'a> {
56    #[inline]
57    pub(crate) const fn new(buf: &'a mut [u8]) -> IoSliceMut<'a> {
58        IoSliceMut {
59            vec: iovec { iov_base: buf.as_mut_ptr() as *mut c_void, iov_len: buf.len() },
60            _p: PhantomData,
61        }
62    }
63
64    #[inline]
65    pub(crate) const fn advance(&mut self, n: usize) {
66        assert!(n <= self.vec.iov_len, "advancing IoSliceMut beyond its length");
67
68        // SAFETY:
69        //  * `n <= iov_len` as asserted above.
70        //  * The allocation pointed to by `iov_base` is valid up to `iov_base + iov_len`.
71        unsafe {
72            self.vec.iov_len -= n;
73            self.vec.iov_base = self.vec.iov_base.add(n);
74        }
75    }
76
77    #[inline]
78    pub(crate) const fn as_slice(&self) -> &[u8] {
79        // SAFETY:
80        //  * `iov_base` and `iov_len` come from a prior decomposition of a valid slice.
81        unsafe { slice::from_raw_parts(self.vec.iov_base as *mut u8, self.vec.iov_len) }
82    }
83
84    #[inline]
85    pub(crate) const fn into_slice(self) -> &'a mut [u8] {
86        // SAFETY:
87        //  * `iov_base` and `iov_len` come from a prior decomposition of a valid slice.
88        unsafe { slice::from_raw_parts_mut(self.vec.iov_base as *mut u8, self.vec.iov_len) }
89    }
90
91    #[inline]
92    pub(crate) const fn as_mut_slice(&mut self) -> &mut [u8] {
93        // SAFETY:
94        //  * `iov_base` and `iov_len` come from a prior decomposition of a valid slice.
95        unsafe { slice::from_raw_parts_mut(self.vec.iov_base as *mut u8, self.vec.iov_len) }
96    }
97}