use std::pin::Pin;
use std::task::{Context, Poll};
use zmq::{Error, SocketType};
use crate::socket::{MessageBuf, Sender, SocketBuilder, ZmqSocket};
use crate::Sink;
pub fn push(endpoint: &str) -> Result<SocketBuilder<'_, Push>, zmq::Error> {
let socket = zmq::Context::new().socket(SocketType::PUSH)?;
Ok(SocketBuilder::new(socket, endpoint))
}
pub struct Push(Sender);
impl Push {
pub fn as_raw_socket(&self) -> &zmq::Socket {
&self.0.socket.get_ref().0
}
}
impl<T: Into<MessageBuf>> Sink<T> for Push {
type Error = Error;
fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Sink::<T>::poll_ready(Pin::new(&mut self.get_mut().0), cx)
}
fn start_send(self: Pin<&mut Self>, item: T) -> Result<(), Self::Error> {
Pin::new(&mut self.get_mut().0).start_send(item)
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Error>> {
Sink::<T>::poll_flush(Pin::new(&mut self.get_mut().0), cx)
}
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Error>> {
Sink::<T>::poll_close(Pin::new(&mut self.get_mut().0), cx)
}
}
impl From<zmq::Socket> for Push {
fn from(socket: zmq::Socket) -> Self {
Self(Sender {
socket: ZmqSocket::from(socket),
buffer: MessageBuf::default(),
})
}
}