Skip to main content

tokio_xmpp/client/
stream.rs

1// Copyright (c) 2019 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7use core::{pin::Pin, task::Context};
8use futures::{ready, task::Poll, Stream};
9
10use crate::{client::Client, Event};
11
12/// Incoming XMPP events
13///
14/// In an `async fn` you may want to use this with `use
15/// futures::stream::StreamExt;`
16impl Stream for Client {
17    type Item = Event;
18
19    /// Low-level read on the XMPP stream, allowing the underlying
20    /// machinery to:
21    ///
22    /// * connect,
23    /// * starttls,
24    /// * authenticate,
25    /// * bind a session, and finally
26    /// * receive stanzas
27    ///
28    /// ...for your client
29    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
30        Poll::Ready(match ready!(self.stanza_rx.poll_recv(cx)) {
31            None => None,
32            Some(event) => {
33                if let Event::Online {
34                    ref bound_jid,
35                    ref features,
36                    ..
37                } = event
38                {
39                    self.bound_jid = Some(bound_jid.clone());
40                    self.features = Some(features.clone());
41                }
42
43                Some(event)
44            }
45        })
46    }
47}