use bytes::Bytes;
use std::io;
use crate::SocketType;
#[allow(async_fn_in_trait)]
pub trait Socket {
async fn send(&mut self, msg: Vec<Bytes>) -> io::Result<()>;
async fn recv(&mut self) -> io::Result<Option<Vec<Bytes>>>;
fn socket_type(&self) -> SocketType;
fn has_more(&self) -> bool {
false
}
}
#[macro_export]
macro_rules! impl_socket_trait {
($socket_type:ty, $zmq_type:expr) => {
impl<S> $crate::Socket for $socket_type
where
S: compio_io::AsyncRead + compio_io::AsyncWrite + Unpin + 'static,
{
async fn send(&mut self, msg: Vec<bytes::Bytes>) -> std::io::Result<()> {
self.send(msg).await
}
async fn recv(&mut self) -> std::io::Result<Option<Vec<bytes::Bytes>>> {
self.recv().await
}
fn socket_type(&self) -> $crate::SocketType {
$zmq_type
}
}
};
}
#[macro_export]
macro_rules! impl_socket_trait_recv_only {
($socket_type:ty, $zmq_type:expr) => {
impl<S> $crate::Socket for $socket_type
where
S: compio_io::AsyncRead + compio_io::AsyncWrite + Unpin + 'static,
{
async fn send(&mut self, _msg: Vec<bytes::Bytes>) -> std::io::Result<()> {
Err(std::io::Error::new(
std::io::ErrorKind::Unsupported,
"this socket type does not support send",
))
}
async fn recv(&mut self) -> std::io::Result<Option<Vec<bytes::Bytes>>> {
self.recv().await
}
fn socket_type(&self) -> $crate::SocketType {
$zmq_type
}
}
};
}
#[macro_export]
macro_rules! impl_socket_trait_send_only {
($socket_type:ty, $zmq_type:expr) => {
impl<S> $crate::Socket for $socket_type
where
S: compio_io::AsyncRead + compio_io::AsyncWrite + Unpin + 'static,
{
async fn send(&mut self, msg: Vec<bytes::Bytes>) -> std::io::Result<()> {
self.send(msg).await
}
async fn recv(&mut self) -> std::io::Result<Option<Vec<bytes::Bytes>>> {
Err(std::io::Error::new(
std::io::ErrorKind::Unsupported,
"this socket type does not support recv",
))
}
fn socket_type(&self) -> $crate::SocketType {
$zmq_type
}
}
};
}