Skip to content

Commit

Permalink
Add send_stream function and implement Sink for UiInboxSender
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmerlin committed Jan 27, 2024
1 parent a155cf2 commit a857930
Showing 1 changed file with 45 additions and 3 deletions.
48 changes: 45 additions & 3 deletions crates/egui_inbox/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,13 +309,14 @@ impl<T> UiInbox<T> {

#[cfg(feature = "async")]
mod async_impl {
use std::pin::pin;
use std::pin::{pin, Pin};
use std::task::{Context, Poll};

use futures::{select, FutureExt};
use futures::{select, FutureExt, Sink, SinkExt, StreamExt};

use hello_egui_utils::spawn;

use crate::{UiInbox, UiInboxSender};
use crate::{SendError, UiInbox, UiInboxSender};

impl<T> UiInbox<T> {
/// Spawns a future that will automatically be cancelled when the inbox is dropped.
Expand Down Expand Up @@ -352,6 +353,47 @@ mod async_impl {
spawn(future);
}
}

impl<T> UiInboxSender<T> {
/// Send each item of a stream to the inbox, as they come in.
pub async fn send_stream(
&mut self,
stream: impl futures::Stream<Item = T> + Send + 'static,
) -> Result<(), SendError<T>> {
let stream = stream.map(|i| Ok(i));
let mut stream = pin!(stream);
self.send_all(&mut stream).await
}
}

impl<T> Sink<T> for UiInboxSender<T> {
type Error = SendError<T>;

fn poll_ready(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}

fn start_send(self: Pin<&mut Self>, item: T) -> Result<(), Self::Error> {
UiInboxSender::send(&self, item)
}

fn poll_flush(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}

fn poll_close(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
}
}

impl<T> UiInboxSender<T> {
Expand Down

0 comments on commit a857930

Please sign in to comment.