use core::future::Future;
#[cfg(feature = "std")]
use std::os::fd::{AsFd, OwnedFd};
#[cfg(feature = "std")]
pub type ReadResult = (usize, alloc::vec::Vec<OwnedFd>);
#[cfg(not(feature = "std"))]
pub type ReadResult = usize;
pub trait Socket: core::fmt::Debug {
type ReadHalf: ReadHalf;
type WriteHalf: WriteHalf;
const CAN_TRANSFER_FDS: bool = false;
fn split(self) -> (Self::ReadHalf, Self::WriteHalf);
}
pub trait ReadHalf: core::fmt::Debug {
fn read(&mut self, buf: &mut [u8]) -> impl Future<Output = crate::Result<ReadResult>>;
}
pub trait WriteHalf: core::fmt::Debug {
fn write(
&mut self,
buf: &[u8],
#[cfg(feature = "std")] fds: &[impl AsFd],
) -> impl Future<Output = crate::Result<()>>;
}
#[cfg(feature = "std")]
pub trait FetchPeerCredentials {
fn fetch_peer_credentials(&self) -> impl Future<Output = std::io::Result<super::Credentials>>;
}
#[cfg(feature = "std")]
pub trait UnixSocket: AsFd {}
#[cfg(feature = "std")]
impl<T> FetchPeerCredentials for T
where
T: UnixSocket,
{
async fn fetch_peer_credentials(&self) -> std::io::Result<super::Credentials> {
crate::unix_utils::get_peer_credentials(self)
}
}
#[doc(hidden)]
pub mod impl_for_doc {
#[derive(Debug)]
pub struct Socket;
impl super::Socket for Socket {
type ReadHalf = ReadHalf;
type WriteHalf = WriteHalf;
fn split(self) -> (Self::ReadHalf, Self::WriteHalf) {
(ReadHalf, WriteHalf)
}
}
#[derive(Debug)]
pub struct ReadHalf;
impl super::ReadHalf for ReadHalf {
async fn read(&mut self, _buf: &mut [u8]) -> crate::Result<super::ReadResult> {
unreachable!("This is only for doc tests")
}
}
#[derive(Debug)]
pub struct WriteHalf;
impl super::WriteHalf for WriteHalf {
async fn write(
&mut self,
_buf: &[u8],
#[cfg(feature = "std")] _fds: &[impl super::AsFd],
) -> crate::Result<()> {
unreachable!("This is only for doc tests")
}
}
}