use std::io;
use std::path::Path;
use tokio::net::unix::{OwnedReadHalf, OwnedWriteHalf};
use tokio::net::{UnixListener, UnixStream};
use crate::traits::transport::Transport;
pub struct UdsTransport;
impl UdsTransport {
pub async fn connect(
path: impl AsRef<Path>,
) -> io::Result<(OwnedReadHalf, OwnedWriteHalf)> {
Ok(UnixStream::connect(path).await?.into_split())
}
pub fn bind(path: impl AsRef<Path>) -> io::Result<UnixListener> {
UnixListener::bind(path)
}
pub async fn accept(listener: &UnixListener) -> io::Result<(OwnedReadHalf, OwnedWriteHalf)> {
let (stream, _) = listener.accept().await?;
Ok(stream.into_split())
}
}
impl Transport for UdsTransport {
type Endpoint = Path;
type Listener = UnixListener;
type Read = OwnedReadHalf;
type Write = OwnedWriteHalf;
async fn connect(endpoint: &Path) -> io::Result<(OwnedReadHalf, OwnedWriteHalf)> {
UdsTransport::connect(endpoint).await
}
async fn bind(endpoint: &Path) -> io::Result<UnixListener> {
UdsTransport::bind(endpoint)
}
async fn accept(listener: &UnixListener) -> io::Result<(OwnedReadHalf, OwnedWriteHalf)> {
UdsTransport::accept(listener).await
}
}