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 stream(endpoint: &str) -> Result<SocketBuilder<'_, ZmqStream>, SocketError> {
Ok(SocketBuilder::new(SocketType::STREAM, endpoint))
}
pub struct ZmqStream(Receiver);
impl From<zmq::Socket> for ZmqStream {
fn from(socket: zmq::Socket) -> Self {
Self(Receiver {
socket: ZmqSocket::from(socket),
})
}
}
impl Stream for ZmqStream {
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)))
}
}
impl ZmqStream {
pub fn as_raw_socket(&self) -> &zmq::Socket {
self.0.socket.as_socket()
}
}