Skip to main content

tokio_xmpp/client/
receiver.rs

1// Copyright (c) 2025 xmpp-rs contributors.
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 crate::Client;
8use crate::Event;
9use core::{pin::Pin, task::Context};
10use futures::StreamExt;
11use futures::{task::Poll, Stream};
12use std::sync::Arc;
13use tokio::sync::Mutex;
14use xmpp_parsers::{jid::Jid, stream_features::StreamFeatures};
15
16/// Read half of a [`Client`](crate::Client).
17#[derive(Debug)]
18pub struct ClientReceiver(pub(super) Arc<Mutex<Client>>);
19
20impl ClientReceiver {
21    /// Return the bound JID.
22    ///
23    /// See the documentation of [`Client::bound_jid`](crate::Client::bound_jid) for more
24    /// information.
25    pub async fn bound_jid(&self) -> Option<Jid> {
26        self.0.lock().await.bound_jid.clone()
27    }
28
29    /// Return the received stream features.
30    ///
31    /// See the documentation of [`Client::get_stream_features`](crate::Client::get_stream_features)
32    /// for more information.
33    pub async fn get_stream_features(&self) -> Option<StreamFeatures> {
34        self.0.lock().await.features.clone()
35    }
36}
37
38impl Stream for ClientReceiver {
39    type Item = Event;
40
41    fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
42        let Ok(mut client) = self.0.try_lock() else {
43            return Poll::Pending;
44        };
45
46        client.poll_next_unpin(cx)
47    }
48}