socket9 0.1.0-alpha.1

Extended untilities for the networking/unix sockets and raw network sockets
Documentation

use std::fmt;

use bitflags::bitflags;

/// A socket `type`.
#[repr(transparent)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct So9SockType(pub libc::c_int);

impl So9SockType
{
    /// [libc::SOCK_STREAM] type
    pub const STREAM: Self = Self(libc::SOCK_STREAM);

    /// [libc::SOCK_DGRAM] type  
    pub const DGRAM: Self = Self(libc::SOCK_DGRAM);

    /// [libc::SOCK_SEQPACKET] type  
    pub const SEQPACKET: Self = Self(libc::SOCK_SEQPACKET);

    /// [libc::SOCK_RAW] type  
    pub const RAW: Self = Self(libc::SOCK_RAW);

    /// [libc::SOCK_RDM]
    pub const RDM: Self = Self(libc::SOCK_RDM);
}

impl fmt::Display for So9SockType
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result 
    {
        if self == &Self::STREAM
        {
            write!(f, "SOCK_STREAM")
        }
        else if self == &Self::DGRAM
        {
            write!(f, "SOCK_DGRAM")
        }
        else if self == &Self::SEQPACKET
        {
            write!(f, "SOCK_SEQPACKET")
        }
        else if self == &Self::RAW
        {
            write!(f, "SOCK_RAW")
        }
        else if self == &Self::RDM
        {
            write!(f, "SOCK_RDM")
        }
        else
        {
            write!(f, "UNKNOWN {}", self.0)
        }
    }
}

impl From<i32> for So9SockType
{
    fn from(value: i32) -> Self 
    {
        Self(value)
    }
}

impl So9SockType
{
    pub 
    fn join(&self, dwflags: So9SockDwFlags) -> Self
    {
        So9SockType(self.0 | dwflags.bits())
    }
}

bitflags! {
    /// A flags which are an addition to the argument `type` for func [libc::socket]
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub struct So9SockDwFlags: i32 
    {
        /// [libc::SOCK_NONBLOCK]
        const NONBLOCK = libc::SOCK_NONBLOCK;

        /// [libc::SOCK_CLOEXEC]
        const CLOEXEC = libc::SOCK_CLOEXEC;
    }
}