dope_sys/io/raw.rs
1use crate::{io::socket::msg, kernel};
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 byte extent.
13pub unsafe trait Buffer: Sized {
14 /// # Safety
15 /// `len` must not exceed this buffer's admitted 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 admitted extent.
22 /// On failure, `S` reads this call's positive, unchanged errno.
23 unsafe fn try_native<S: kernel::raw::Source + ?Sized, E: From<crate::Errno>>(
24 self,
25 raw: libc::ssize_t,
26 ) -> Result<Self, E> {
27 if raw == -1 {
28 // SAFETY: the caller retains this failed call's positive, unchanged errno.
29 return Err(E::from(unsafe { kernel::raw::Code::last::<S>() }.into()));
30 }
31 // SAFETY: the caller retains this completed count's admitted extent proof.
32 Ok(unsafe { self.prefix(raw as usize) })
33 }
34}
35
36// SAFETY: slice length is its initialized byte extent.
37unsafe impl Buffer for &[u8] {
38 unsafe fn prefix(self, len: usize) -> Self {
39 unsafe { self.get_unchecked(..len) }
40 }
41}
42
43// SAFETY: the exclusive slice retains its initialized byte extent.
44unsafe impl Buffer for &mut [u8] {
45 unsafe fn prefix(self, len: usize) -> Self {
46 unsafe { self.get_unchecked_mut(..len) }
47 }
48}
49
50// SAFETY: the exclusive slice retains its writable byte extent.
51unsafe impl Buffer for &mut [std::mem::MaybeUninit<u8>] {
52 unsafe fn prefix(self, len: usize) -> Self {
53 unsafe { self.get_unchecked_mut(..len) }
54 }
55}