Skip to main content

dope_core/io/socket/msg/
mod.rs

1use std::mem;
2
3use o3::buffer::bytes::bounded;
4
5pub use builder::Builder;
6use dope_sys::io::{
7    self, socket,
8    socket::msg::{Header, Iovec},
9};
10
11use crate::io::transfer;
12
13mod builder;
14pub mod raw;
15
16/// Maximum number of scatter/gather entries accepted by one socket message.
17pub const MAX_IOVECS: usize = <crate::Backend as socket::Socket>::MAX_IOVECS;
18
19/// A send message retaining at most `N` iovecs and their exact live byte sum.
20pub struct Vectored<'a, const N: usize = MAX_IOVECS> {
21    header: &'a mut mem::MaybeUninit<Header>,
22    iovs: &'a [Iovec],
23    bytes: transfer::Len,
24}
25
26impl<'a, const N: usize> Vectored<'a, N> {
27    #[must_use]
28    pub fn into_message(self) -> Message<'a> {
29        let header = self.header.write(Header::new());
30        header.bind(self.iovs);
31        Message { header }
32    }
33
34    #[must_use]
35    pub const fn bytes(&self) -> transfer::Len {
36        self.bytes
37    }
38
39    #[must_use]
40    pub fn is_empty(&self) -> bool {
41        self.bytes == transfer::Len::ZERO
42    }
43}
44
45impl<'a, const N: usize> Vectored<'a, N> {
46    fn from_parts(
47        header: &'a mut mem::MaybeUninit<Header>,
48        iovs: &'a [Iovec],
49        bytes: transfer::Len,
50    ) -> Self {
51        Self {
52            header,
53            iovs,
54            bytes,
55        }
56    }
57}
58
59/// A complete immutable send pointer graph.
60#[derive(Clone, Copy)]
61#[repr(transparent)]
62pub struct Message<'a> {
63    header: &'a Header,
64}
65
66// SAFETY: construction bounds the iovec count and byte sum, and Message retains
67// the frozen header and its complete input graph through 'a.
68unsafe impl<'a> io::raw::Message<'a> for Message<'a> {
69    fn header(self) -> &'a Header {
70        self.header
71    }
72}
73
74const _: () = {
75    assert!(mem::size_of::<Message<'static>>() == mem::size_of::<&Header>());
76    assert!(mem::align_of::<Message<'static>>() == mem::align_of::<&Header>());
77    assert!(mem::size_of::<Option<Message<'static>>>() == mem::size_of::<Message<'static>>());
78};
79
80impl<'a, const N: usize> Vectored<'a, N> {
81    /// Projects a sole descriptor with its admitted byte bound.
82    pub fn contiguous(&self) -> Option<transfer::View<'a>> {
83        let [iovec] = self.iovs else {
84            return None;
85        };
86        // SAFETY: the graph retains the sole descriptor's admitted bytes for 'a.
87        let bytes = unsafe { bounded::raw::Input::new(socket::raw::Iovec::as_slice(iovec)) };
88        Some(transfer::View::from_prefix(bytes.into_prefix()))
89    }
90}