clipboard_stream/
stream.rs

1use std::{
2    pin::Pin,
3    task::{Context, Poll},
4};
5
6use futures::{Stream, channel::mpsc::Receiver};
7
8use crate::{
9    Msg,
10    body::{Body, BodySendersDropHandle, Kind},
11};
12
13/// Asynchronous stream for fetching clipboard item.
14///
15/// When the clipboard is updated, the [`ClipboardStream`] polls for the yields the new data.
16///
17/// # Example
18/// ```
19/// # use clipboard_stream::{ClipboardStream};
20/// # use futures::stream::StreamExt;
21/// # async fn stream(mut stream: ClipboardStream) {
22/// // stream: ClipboardStream
23/// while let Some(body) = stream.next().await {
24///     if let Ok(v) = body {
25///         println!("{:?}", v);
26///     }
27/// }
28/// # }
29/// ```
30#[derive(Debug)]
31pub struct ClipboardStream {
32    pub(crate) body_rx: Pin<Box<Receiver<Msg>>>,
33    pub(crate) kind: Kind,
34    pub(crate) drop_handle: BodySendersDropHandle,
35}
36
37impl Stream for ClipboardStream {
38    type Item = Result<Body, crate::error::Error>;
39
40    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
41        self.body_rx.as_mut().poll_next(cx)
42    }
43}
44
45impl Drop for ClipboardStream {
46    fn drop(&mut self) {
47        self.drop_handle.drop(&self.kind);
48    }
49}