use std::pin::Pin;
use std::task::{Context, Poll};
use zmq::SocketType;
use crate::{
reactor::{AsRawSocket, ZmqSocket},
socket::{Multipart, Receiver, SocketBuilder},
RecvError, SocketError, Stream,
};
pub fn pull(endpoint: &str) -> Result<SocketBuilder<'_, Pull>, SocketError> {
Ok(SocketBuilder::new(SocketType::PULL, endpoint))
}
pub struct Pull(Receiver);
impl Pull {
pub fn as_raw_socket(&self) -> &zmq::Socket {
self.0.socket.as_socket()
}
}
impl From<zmq::Socket> for Pull {
fn from(socket: zmq::Socket) -> Self {
Self(Receiver {
socket: ZmqSocket::from(socket),
})
}
}
impl Stream for Pull {
type Item = Result<Multipart, RecvError>;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
Pin::new(&mut self.get_mut().0)
.poll_next(cx)
.map(|poll| poll.map(|result| result.map_err(Into::into)))
}
}