dope-core 0.77.0

Owned native I/O and completion driver for dope
Documentation
use std::os::fd::{self, AsFd as _};

pub use addr::{
    Addr,
    inet::{Inet, Listener},
    prepared::{Prepared, View},
    unix::Unix,
};
pub use backlog::Backlog;
use dope_sys::io::socket::addr::{Error, Local};
use dope_sys::{
    Errno,
    io::socket::{self, Socket as _, option::Source as _},
};

use crate::{Backend, io::fd::handles::Descriptor};
use o3::mem::slot;

type Completion<'slot, 'd, P, V> =
    Result<(slot::Occupied<'slot, Descriptor<'d>>, V), slot::Occupied<'slot, P>>;

mod addr;
mod backlog;

pub mod install;
pub mod msg;
pub mod option;
pub mod peer;
pub mod pending;

#[derive(Clone, Copy, PartialEq, Eq)]
#[repr(transparent)]
pub struct StreamSpec(socket::Domain);

impl StreamSpec {
    pub(crate) const fn domain(self) -> socket::Domain {
        self.0
    }
}

#[derive(Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum Reuse {
    Address,
    Port,
}

#[derive(Clone, Copy)]
pub struct Config {
    pub reuse: Option<Reuse>,
    pub(crate) fast_open: Option<Backlog>,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            reuse: Some(Reuse::Address),
            fast_open: None,
        }
    }
}

impl Config {
    pub const fn for_datagram(reuse: Option<Reuse>) -> Self {
        Self {
            reuse,
            fast_open: None,
        }
    }

    /// Builds the reuse and fast-open policy for a TCP listener.
    /// `Some(backlog)` requires Fast Open before binding and fails creation
    /// when the platform does not support it.
    pub const fn for_tcp(reuse: Option<Reuse>, fast: Option<Backlog>) -> Self {
        Self {
            reuse,
            fast_open: fast,
        }
    }

    pub(crate) const fn fast_open(&self) -> Option<Backlog> {
        self.fast_open
    }

    pub(crate) const fn has_addr(&self) -> bool {
        matches!(self.reuse, Some(Reuse::Address | Reuse::Port))
    }

    pub(crate) const fn has_port(&self) -> bool {
        matches!(self.reuse, Some(Reuse::Port))
    }
}

const _: () = assert!(std::mem::size_of::<Option<Reuse>>() == std::mem::size_of::<bool>());

#[repr(transparent)]
pub(crate) struct Handle {
    fd: <Backend as socket::Prepare>::Handle,
}

impl Handle {
    pub(crate) const fn new(fd: <Backend as socket::Prepare>::Handle) -> Self {
        Self { fd }
    }

    pub(crate) const fn owned(&self) -> &<Backend as socket::Prepare>::Handle {
        &self.fd
    }

    pub(crate) fn into_inner(self) -> <Backend as socket::Prepare>::Handle {
        self.fd
    }

    pub(crate) fn bind(&self, addr: &impl Prepared) -> Result<(), Errno> {
        addr.view()
            .native()
            .try_bind::<Backend, Errno>(self.fd.as_fd())
    }

    pub(crate) fn listen(&self, backlog: i32) -> Result<(), Errno> {
        Backend::listen(self.fd.as_fd(), backlog)
    }

    pub(crate) fn try_reuse(&self, config: &Config) -> Result<(), (Reuse, Errno)> {
        if config.has_addr() {
            Backend::set_reuse::<Errno>(self.fd.as_fd(), socket::Reuse::Address)
                .map_err(|error| (Reuse::Address, error))?;
        }
        if !config.has_port() {
            return Ok(());
        }
        Backend::set_reuse::<Errno>(self.fd.as_fd(), socket::Reuse::Port)
            .map_err(|error| (Reuse::Port, error))
    }
}

const _: () = {
    assert!(size_of::<Handle>() == size_of::<fd::OwnedFd>());
    assert!(align_of::<Handle>() == align_of::<fd::OwnedFd>());
    assert!(size_of::<Result<(), Errno>>() == size_of::<i32>());
    assert!(size_of::<Result<(), (Reuse, Errno)>>() == 2 * size_of::<i32>());
};

#[derive(Clone, Copy, Debug)]
pub enum BindError {
    Socket(<Backend as socket::Socket>::Error),
    Bind(crate::Errno),
    Listen(crate::Errno),
    LocalAddress(Local<Errno>),
    Address(Error),
}