summaryrefslogtreecommitdiffstats
path: root/futures
diff options
context:
space:
mode:
Diffstat (limited to 'futures')
-rw-r--r--futures/Cargo.toml2
-rw-r--r--futures/src/command.rs2
-rw-r--r--futures/src/lib.rs18
-rw-r--r--futures/src/runtime.rs8
-rw-r--r--futures/src/subscription.rs12
-rw-r--r--futures/src/subscription/tracker.rs45
6 files changed, 57 insertions, 30 deletions
diff --git a/futures/Cargo.toml b/futures/Cargo.toml
index 78e673e0..61ee00a5 100644
--- a/futures/Cargo.toml
+++ b/futures/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "iced_futures"
-version = "0.3.0"
+version = "0.4.1"
authors = ["Héctor Ramón Jiménez <hector0193@gmail.com>"]
edition = "2021"
description = "Commands, subscriptions, and runtimes for Iced"
diff --git a/futures/src/command.rs b/futures/src/command.rs
index d8adfe49..05c3a1d0 100644
--- a/futures/src/command.rs
+++ b/futures/src/command.rs
@@ -17,7 +17,7 @@ impl<T> Command<T> {
Self(Internal::None)
}
- /// Creates a [`Command`] that performs a single [`Action`].
+ /// Creates a [`Command`] that performs a single action.
pub const fn single(action: T) -> Self {
Self(Internal::Single(action))
}
diff --git a/futures/src/lib.rs b/futures/src/lib.rs
index b0b2f6ce..c0982db7 100644
--- a/futures/src/lib.rs
+++ b/futures/src/lib.rs
@@ -4,13 +4,19 @@
#![doc(
html_logo_url = "https://raw.githubusercontent.com/iced-rs/iced/9ab6923e943f784985e9ef9ca28b10278297225d/docs/logo.svg"
)]
-#![deny(missing_docs)]
-#![deny(missing_debug_implementations)]
-#![deny(unused_results)]
-#![forbid(unsafe_code)]
-#![forbid(rust_2018_idioms)]
+#![deny(
+ missing_debug_implementations,
+ missing_docs,
+ unused_results,
+ clippy::extra_unused_lifetimes,
+ clippy::from_over_into,
+ clippy::needless_borrow,
+ clippy::new_without_default,
+ clippy::useless_conversion
+)]
+#![forbid(unsafe_code, rust_2018_idioms)]
+#![allow(clippy::inherent_to_string, clippy::type_complexity)]
#![cfg_attr(docsrs, feature(doc_cfg))]
-
pub use futures;
mod command;
diff --git a/futures/src/runtime.rs b/futures/src/runtime.rs
index 2034ed6c..24f9f241 100644
--- a/futures/src/runtime.rs
+++ b/futures/src/runtime.rs
@@ -9,6 +9,8 @@ use std::marker::PhantomData;
///
/// If you have an [`Executor`], a [`Runtime`] can be leveraged to run any
/// [`Command`] or [`Subscription`] and get notified of the results!
+///
+/// [`Command`]: crate::Command
#[derive(Debug)]
pub struct Runtime<Hasher, Event, Executor, Sender, Message> {
executor: Executor,
@@ -51,10 +53,12 @@ where
self.executor.enter(f)
}
- /// Spawns a [`Command`] in the [`Runtime`].
+ /// Spawns a [`Future`] in the [`Runtime`].
///
/// The resulting `Message` will be forwarded to the `Sender` of the
/// [`Runtime`].
+ ///
+ /// [`Future`]: BoxFuture
pub fn spawn(&mut self, future: BoxFuture<Message>) {
use futures::{FutureExt, SinkExt};
@@ -62,8 +66,6 @@ where
let future = future.then(|message| async move {
let _ = sender.send(message).await;
-
- ()
});
self.executor.spawn(future);
diff --git a/futures/src/subscription.rs b/futures/src/subscription.rs
index 6f261827..0479c63c 100644
--- a/futures/src/subscription.rs
+++ b/futures/src/subscription.rs
@@ -125,9 +125,9 @@ impl<I, O, H> std::fmt::Debug for Subscription<I, O, H> {
/// - [`stopwatch`], a watch with start/stop and reset buttons showcasing how
/// to listen to time.
///
-/// [examples]: https://github.com/iced-rs/iced/tree/0.3/examples
-/// [`download_progress`]: https://github.com/iced-rs/iced/tree/0.3/examples/download_progress
-/// [`stopwatch`]: https://github.com/iced-rs/iced/tree/0.3/examples/stopwatch
+/// [examples]: https://github.com/iced-rs/iced/tree/0.4/examples
+/// [`download_progress`]: https://github.com/iced-rs/iced/tree/0.4/examples/download_progress
+/// [`stopwatch`]: https://github.com/iced-rs/iced/tree/0.4/examples/stopwatch
pub trait Recipe<Hasher: std::hash::Hasher, Event> {
/// The events that will be produced by a [`Subscription`] with this
/// [`Recipe`].
@@ -183,11 +183,7 @@ where
let mapper = self.mapper;
- Box::pin(
- self.recipe
- .stream(input)
- .map(move |element| mapper(element)),
- )
+ Box::pin(self.recipe.stream(input).map(mapper))
}
}
diff --git a/futures/src/subscription/tracker.rs b/futures/src/subscription/tracker.rs
index 421fb917..9fe110b0 100644
--- a/futures/src/subscription/tracker.rs
+++ b/futures/src/subscription/tracker.rs
@@ -1,6 +1,9 @@
use crate::{BoxFuture, MaybeSend, Subscription};
-use futures::{channel::mpsc, sink::Sink};
+use futures::{
+ channel::mpsc,
+ sink::{Sink, SinkExt},
+};
use std::{collections::HashMap, marker::PhantomData};
/// A registry of subscription streams.
@@ -64,7 +67,7 @@ where
+ MaybeSend
+ Clone,
{
- use futures::{future::FutureExt, stream::StreamExt};
+ use futures::stream::StreamExt;
let mut futures: Vec<BoxFuture<()>> = Vec::new();
@@ -85,19 +88,29 @@ where
continue;
}
- let (cancel, cancelled) = futures::channel::oneshot::channel();
+ let (cancel, mut canceled) = futures::channel::oneshot::channel();
// TODO: Use bus if/when it supports async
let (event_sender, event_receiver) =
futures::channel::mpsc::channel(100);
- let stream = recipe.stream(event_receiver.boxed());
-
- let future = futures::future::select(
- cancelled,
- stream.map(Ok).forward(receiver.clone()),
- )
- .map(|_| ());
+ let mut receiver = receiver.clone();
+ let mut stream = recipe.stream(event_receiver.boxed());
+
+ let future = async move {
+ loop {
+ let select =
+ futures::future::select(&mut canceled, stream.next());
+
+ match select.await {
+ futures::future::Either::Left(_)
+ | futures::future::Either::Right((None, _)) => break,
+ futures::future::Either::Right((Some(message), _)) => {
+ let _ = receiver.send(message).await;
+ }
+ }
+ }
+ };
let _ = self.subscriptions.insert(
id,
@@ -114,7 +127,7 @@ where
futures.push(Box::pin(future));
}
- self.subscriptions.retain(|id, _| alive.contains(&id));
+ self.subscriptions.retain(|id, _| alive.contains(id));
futures
}
@@ -143,3 +156,13 @@ where
});
}
}
+
+impl<Hasher, Event> Default for Tracker<Hasher, Event>
+where
+ Hasher: std::hash::Hasher + Default,
+ Event: 'static + Send + Clone,
+{
+ fn default() -> Self {
+ Self::new()
+ }
+}