diff options
author | 2020-10-29 10:26:33 +0100 | |
---|---|---|
committer | 2020-10-29 10:27:18 +0100 | |
commit | dd1b1cac0ae76655bd81d20e4b245f5afc1bde61 (patch) | |
tree | aca5875913028a66362bd65b29cfeb1f6556d0a1 /futures | |
parent | b40775fb7414b44307e343921ef015c65a916dab (diff) | |
download | iced-dd1b1cac0ae76655bd81d20e4b245f5afc1bde61.tar.gz iced-dd1b1cac0ae76655bd81d20e4b245f5afc1bde61.tar.bz2 iced-dd1b1cac0ae76655bd81d20e4b245f5afc1bde61.zip |
Accept a function pointer in `Subscription::map`
Instead of a closure, a function pointer can be hashed and used to
uniquely identify a particular `Subscription`.
This should fix a bug where two different instances of `Subscription`
producing the same output were not treated differently by the runtime,
causing one of them to be ignored.
Diffstat (limited to 'futures')
-rw-r--r-- | futures/src/subscription.rs | 19 |
1 files changed, 5 insertions, 14 deletions
diff --git a/futures/src/subscription.rs b/futures/src/subscription.rs index 7a75fc31..e97ff3ab 100644 --- a/futures/src/subscription.rs +++ b/futures/src/subscription.rs @@ -76,10 +76,6 @@ where /// /// 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 @@ -103,24 +99,19 @@ where /// Transforms the [`Subscription`] output with the given function. /// /// [`Subscription`]: struct.Subscription.html - pub fn map<A>( - mut self, - f: impl Fn(O) -> A + Send + Sync + 'static, - ) -> Subscription<H, E, A> + pub fn map<A>(mut self, f: fn(O) -> A) -> Subscription<H, E, A> where H: 'static, E: 'static, O: 'static, A: 'static, { - let function = std::sync::Arc::new(f); - Subscription { recipes: self .recipes .drain(..) .map(|recipe| { - Box::new(Map::new(recipe, function.clone())) + Box::new(Map::new(recipe, f)) as Box<dyn Recipe<H, E, Output = A>> }) .collect(), @@ -186,13 +177,13 @@ pub trait Recipe<Hasher: std::hash::Hasher, Event> { struct Map<Hasher, Event, A, B> { recipe: Box<dyn Recipe<Hasher, Event, Output = A>>, - mapper: std::sync::Arc<dyn Fn(A) -> B + Send + Sync>, + mapper: fn(A) -> B, } impl<H, E, A, B> Map<H, E, A, B> { fn new( recipe: Box<dyn Recipe<H, E, Output = A>>, - mapper: std::sync::Arc<dyn Fn(A) -> B + Send + Sync + 'static>, + mapper: fn(A) -> B, ) -> Self { Map { recipe, mapper } } @@ -209,8 +200,8 @@ where fn hash(&self, state: &mut H) { use std::hash::Hash; - std::any::TypeId::of::<B>().hash(state); self.recipe.hash(state); + self.mapper.hash(state); } fn stream(self: Box<Self>, input: BoxStream<E>) -> BoxStream<Self::Output> { |