xmlstream: make sink implementation generic
This allows to use any serialisable type. The advantage is that moves and clones are avoided (which would otherwise be needed to construct e.g. a XmppStreamElement from a Stanza or Message).
This commit is contained in:
parent
519718d9b5
commit
c6f928d5c8
6 changed files with 62 additions and 34 deletions
|
|
@ -5,7 +5,7 @@ use crate::{
|
|||
client::{login::client_login, stream::ClientState},
|
||||
connect::ServerConnector,
|
||||
error::Error,
|
||||
xmlstream::Timeouts,
|
||||
xmlstream::{Timeouts, XmppStream, XmppStreamElement},
|
||||
Stanza,
|
||||
};
|
||||
|
||||
|
|
@ -75,7 +75,9 @@ impl<C: ServerConnector> Client<C> {
|
|||
/// Make sure to disable reconnect.
|
||||
pub async fn send_end(&mut self) -> Result<(), Error> {
|
||||
match self.state {
|
||||
ClientState::Connected { ref mut stream, .. } => Ok(stream.close().await?),
|
||||
ClientState::Connected { ref mut stream, .. } => {
|
||||
Ok(<XmppStream<C::Stream> as SinkExt<&XmppStreamElement>>::close(stream).await?)
|
||||
}
|
||||
ClientState::Connecting { .. } => {
|
||||
self.state = ClientState::Disconnected;
|
||||
Ok(())
|
||||
|
|
|
|||
|
|
@ -97,7 +97,10 @@ impl<C: ServerConnector> Stream for Client<C> {
|
|||
bound_jid,
|
||||
} => {
|
||||
// Poll sink
|
||||
match Pin::new(&mut stream).poll_ready(cx) {
|
||||
match <XmppStream<C::Stream> as Sink<&XmppStreamElement>>::poll_ready(
|
||||
Pin::new(&mut stream),
|
||||
cx,
|
||||
) {
|
||||
Poll::Pending => (),
|
||||
Poll::Ready(Ok(())) => (),
|
||||
Poll::Ready(Err(e)) => {
|
||||
|
|
@ -195,36 +198,45 @@ impl<C: ServerConnector> Sink<Stanza> for Client<C> {
|
|||
|
||||
fn start_send(mut self: Pin<&mut Self>, item: Stanza) -> Result<(), Self::Error> {
|
||||
match self.state {
|
||||
ClientState::Connected { ref mut stream, .. } => Pin::new(stream)
|
||||
.start_send(&item.into())
|
||||
.map_err(|e| e.into()),
|
||||
ClientState::Connected { ref mut stream, .. } => {
|
||||
Pin::new(stream).start_send(&item).map_err(|e| e.into())
|
||||
}
|
||||
_ => Err(Error::InvalidState),
|
||||
}
|
||||
}
|
||||
|
||||
fn poll_ready(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
|
||||
match self.state {
|
||||
ClientState::Connected { ref mut stream, .. } => {
|
||||
Pin::new(stream).poll_ready(cx).map_err(|e| e.into())
|
||||
}
|
||||
ClientState::Connected { ref mut stream, .. } => <XmppStream<C::Stream> as Sink<
|
||||
&XmppStreamElement,
|
||||
>>::poll_ready(
|
||||
Pin::new(stream), cx
|
||||
)
|
||||
.map_err(|e| e.into()),
|
||||
_ => Poll::Pending,
|
||||
}
|
||||
}
|
||||
|
||||
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
|
||||
match self.state {
|
||||
ClientState::Connected { ref mut stream, .. } => {
|
||||
Pin::new(stream).poll_flush(cx).map_err(|e| e.into())
|
||||
}
|
||||
ClientState::Connected { ref mut stream, .. } => <XmppStream<C::Stream> as Sink<
|
||||
&XmppStreamElement,
|
||||
>>::poll_flush(
|
||||
Pin::new(stream), cx
|
||||
)
|
||||
.map_err(|e| e.into()),
|
||||
_ => Poll::Pending,
|
||||
}
|
||||
}
|
||||
|
||||
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
|
||||
match self.state {
|
||||
ClientState::Connected { ref mut stream, .. } => {
|
||||
Pin::new(stream).poll_close(cx).map_err(|e| e.into())
|
||||
}
|
||||
ClientState::Connected { ref mut stream, .. } => <XmppStream<C::Stream> as Sink<
|
||||
&XmppStreamElement,
|
||||
>>::poll_close(
|
||||
Pin::new(stream), cx
|
||||
)
|
||||
.map_err(|e| e.into()),
|
||||
_ => Poll::Pending,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue