1use std::pin::Pin;
29use std::task::{Context, Poll};
30
31use zmq::{Message, SocketType};
32
33use crate::{
34 reactor::{AsRawSocket, ZmqSocket},
35 socket::{Broker, Multipart, MultipartIter, SocketBuilder},
36 RecvError, SendError, Sink, SocketError, Stream,
37};
38
39pub fn pair<I: Iterator<Item = T> + Unpin, T: Into<Message>>(
41 endpoint: &str,
42) -> Result<SocketBuilder<'_, Pair<I, T>>, SocketError> {
43 Ok(SocketBuilder::new(SocketType::PAIR, endpoint))
44}
45
46pub struct Pair<I: Iterator<Item = T> + Unpin, T: Into<Message>>(Broker<I, T>);
48
49impl<I: Iterator<Item = T> + Unpin, T: Into<Message>> Pair<I, T> {
50 pub fn as_raw_socket(&self) -> &zmq::Socket {
52 self.0.socket.as_socket()
53 }
54}
55
56impl<I: Iterator<Item = T> + Unpin, T: Into<Message>> Sink<MultipartIter<I, T>> for Pair<I, T> {
57 type Error = SendError;
58
59 fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
60 Sink::poll_ready(Pin::new(&mut self.get_mut().0), cx)
61 .map(|result| result.map_err(Into::into))
62 }
63
64 fn start_send(self: Pin<&mut Self>, item: MultipartIter<I, T>) -> Result<(), Self::Error> {
65 Pin::new(&mut self.get_mut().0)
66 .start_send(item)
67 .map_err(Into::into)
68 }
69
70 fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
71 Sink::poll_flush(Pin::new(&mut self.get_mut().0), cx)
72 .map(|result| result.map_err(Into::into))
73 }
74
75 fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
76 Sink::poll_close(Pin::new(&mut self.get_mut().0), cx)
77 .map(|result| result.map_err(Into::into))
78 }
79}
80
81impl<I: Iterator<Item = T> + Unpin, T: Into<Message>> Stream for Pair<I, T> {
82 type Item = Result<Multipart, RecvError>;
83
84 fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
85 Pin::new(&mut self.get_mut().0)
86 .poll_next(cx)
87 .map(|poll| poll.map(|result| result.map_err(Into::into)))
88 }
89}
90
91impl<I: Iterator<Item = T> + Unpin, T: Into<Message>> From<zmq::Socket> for Pair<I, T> {
92 fn from(socket: zmq::Socket) -> Self {
93 Self(Broker {
94 socket: ZmqSocket::from(socket),
95 buffer: None,
96 })
97 }
98}