summaryrefslogtreecommitdiffstats
path: root/futures/src
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón <hector0193@gmail.com>2020-03-20 12:10:52 +0100
committerLibravatar GitHub <noreply@github.com>2020-03-20 12:10:52 +0100
commitf7ec679fec1b69c6dc5bc12d60627629f086bf22 (patch)
tree58ec4dd11e50f11ef85b972fee211930e7a0e7c0 /futures/src
parent93f5640a2dc06d8f1bf2b0d033d45c62f8985380 (diff)
parentfb744a338c1b7566a3db9a3d24c03729b4858217 (diff)
downloadiced-f7ec679fec1b69c6dc5bc12d60627629f086bf22.tar.gz
iced-f7ec679fec1b69c6dc5bc12d60627629f086bf22.tar.bz2
iced-f7ec679fec1b69c6dc5bc12d60627629f086bf22.zip
Merge pull request #224 from hecrj/feature/panes-widget
Pane grid widget
Diffstat (limited to '')
-rw-r--r--futures/src/subscription.rs70
1 files changed, 70 insertions, 0 deletions
diff --git a/futures/src/subscription.rs b/futures/src/subscription.rs
index b68444cd..8eccb7be 100644
--- a/futures/src/subscription.rs
+++ b/futures/src/subscription.rs
@@ -72,6 +72,34 @@ where
self.recipes
}
+ /// Adds a value to the [`Subscription`] context.
+ ///
+ /// The value will be part of the identity of a [`Subscription`].
+ ///
+ /// This is necessary if you want to use multiple instances of the same
+ /// [`Subscription`] to produce different kinds of messages based on some
+ /// external data.
+ ///
+ /// [`Subscription`]: struct.Subscription.html
+ pub fn with<T>(mut self, value: T) -> Subscription<H, E, (T, O)>
+ where
+ H: 'static,
+ E: 'static,
+ O: 'static,
+ T: std::hash::Hash + Clone + Send + Sync + 'static,
+ {
+ Subscription {
+ recipes: self
+ .recipes
+ .drain(..)
+ .map(|recipe| {
+ Box::new(With::new(recipe, value.clone()))
+ as Box<dyn Recipe<H, E, Output = (T, O)>>
+ })
+ .collect(),
+ }
+ }
+
/// Transforms the [`Subscription`] output with the given function.
///
/// [`Subscription`]: struct.Subscription.html
@@ -187,3 +215,45 @@ where
.boxed()
}
}
+
+struct With<Hasher, Event, A, B> {
+ recipe: Box<dyn Recipe<Hasher, Event, Output = A>>,
+ value: B,
+}
+
+impl<H, E, A, B> With<H, E, A, B> {
+ fn new(recipe: Box<dyn Recipe<H, E, Output = A>>, value: B) -> Self {
+ With { recipe, value }
+ }
+}
+
+impl<H, E, A, B> Recipe<H, E> for With<H, E, A, B>
+where
+ A: 'static,
+ B: 'static + std::hash::Hash + Clone + Send + Sync,
+ H: std::hash::Hasher,
+{
+ type Output = (B, A);
+
+ fn hash(&self, state: &mut H) {
+ use std::hash::Hash;
+
+ std::any::TypeId::of::<B>().hash(state);
+ self.value.hash(state);
+ self.recipe.hash(state);
+ }
+
+ fn stream(
+ self: Box<Self>,
+ input: BoxStream<'static, E>,
+ ) -> futures::stream::BoxStream<'static, Self::Output> {
+ use futures::StreamExt;
+
+ let value = self.value;
+
+ self.recipe
+ .stream(input)
+ .map(move |element| (value.clone(), element))
+ .boxed()
+ }
+}