dope_sys/io/raw.rs
1use crate::io::socket::msg;
2/// # Safety
3/// The header and all pointees stay initialized, immutable, and live through `'a`.
4/// Its iovec count fits the target and its byte sum is at most `i32::MAX`.
5pub unsafe trait Message<'a> {
6 fn header(self) -> &'a msg::Header;
7}
8
9/// A byte range that retains its owner through unchecked prefix projection.
10///
11/// # Safety
12/// `prefix` must preserve the original owner and its admitted initialized bytes.
13pub unsafe trait Buffer: Sized {
14 /// # Safety
15 /// `len` must not exceed this buffer's initialized extent.
16 unsafe fn prefix(self, len: usize) -> Self;
17
18 /// Decodes an admitted completion, reading `errno` only for minus one.
19 ///
20 /// # Safety
21 /// `raw` is -1 or a nonnegative count within this owner's initialized extent.
22 unsafe fn try_native<E: From<crate::Errno>>(
23 self,
24 raw: libc::ssize_t,
25 errno: impl FnOnce() -> crate::Errno,
26 ) -> Result<Self, E> {
27 if raw == -1 {
28 return Err(E::from(errno()));
29 }
30 // SAFETY: the caller retains this completed count's admitted extent proof.
31 Ok(unsafe { self.prefix(raw as usize) })
32 }
33}
34
35// SAFETY: slice length is its initialized byte extent.
36unsafe impl Buffer for &[u8] {
37 unsafe fn prefix(self, len: usize) -> Self {
38 unsafe { self.get_unchecked(..len) }
39 }
40}
41
42// SAFETY: the exclusive slice retains its initialized byte extent.
43unsafe impl Buffer for &mut [u8] {
44 unsafe fn prefix(self, len: usize) -> Self {
45 unsafe { self.get_unchecked_mut(..len) }
46 }
47}