From b5b17ed4d800c03beb3ad535d1069a7784e8dc1d Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 19 Jan 2020 10:17:08 +0100 Subject: Create `iced_futures` and wire everything up --- core/src/subscription.rs | 188 ----------------------------------------------- 1 file changed, 188 deletions(-) delete mode 100644 core/src/subscription.rs (limited to 'core/src/subscription.rs') diff --git a/core/src/subscription.rs b/core/src/subscription.rs deleted file mode 100644 index 87e51e48..00000000 --- a/core/src/subscription.rs +++ /dev/null @@ -1,188 +0,0 @@ -//! Listen to external events in your application. -mod tracker; - -pub use tracker::Tracker; - -use futures::stream::BoxStream; - -/// A request to listen to external events. -/// -/// Besides performing async actions on demand with [`Command`], most -/// applications also need to listen to external events passively. -/// -/// A [`Subscription`] is normally provided to some runtime, like a [`Command`], -/// and it will generate events as long as the user keeps requesting it. -/// -/// For instance, you can use a [`Subscription`] to listen to a WebSocket -/// connection, keyboard presses, mouse events, time ticks, etc. -/// -/// This type is normally aliased by runtimes with a specific `Input` and/or -/// `Hasher`. -/// -/// [`Command`]: ../struct.Command.html -/// [`Subscription`]: struct.Subscription.html -pub struct Subscription { - recipes: Vec>>, -} - -impl Subscription -where - H: std::hash::Hasher, -{ - /// Returns an empty [`Subscription`] that will not produce any output. - /// - /// [`Subscription`]: struct.Subscription.html - pub fn none() -> Self { - Self { - recipes: Vec::new(), - } - } - - /// Creates a [`Subscription`] from a [`Recipe`] describing it. - /// - /// [`Subscription`]: struct.Subscription.html - /// [`Recipe`]: trait.Recipe.html - pub fn from_recipe( - recipe: impl Recipe + 'static, - ) -> Self { - Self { - recipes: vec![Box::new(recipe)], - } - } - - /// Batches all the provided subscriptions and returns the resulting - /// [`Subscription`]. - /// - /// [`Subscription`]: struct.Subscription.html - pub fn batch( - subscriptions: impl IntoIterator>, - ) -> Self { - Self { - recipes: subscriptions - .into_iter() - .flat_map(|subscription| subscription.recipes) - .collect(), - } - } - - /// Returns the different recipes of the [`Subscription`]. - /// - /// [`Subscription`]: struct.Subscription.html - pub fn recipes(self) -> Vec>> { - self.recipes - } - - /// Transforms the [`Subscription`] output with the given function. - /// - /// [`Subscription`]: struct.Subscription.html - pub fn map( - mut self, - f: impl Fn(O) -> A + Send + Sync + 'static, - ) -> Subscription - where - H: 'static, - I: '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())) - as Box> - }) - .collect(), - } - } -} - -impl std::fmt::Debug for Subscription { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Subscription").finish() - } -} - -/// The description of a [`Subscription`]. -/// -/// A [`Recipe`] is the internal definition of a [`Subscription`]. It is used -/// by runtimes to run and identify subscriptions. You can use it to create your -/// own! -/// -/// [`Subscription`]: struct.Subscription.html -/// [`Recipe`]: trait.Recipe.html -pub trait Recipe { - /// The events that will be produced by a [`Subscription`] with this - /// [`Recipe`]. - /// - /// [`Subscription`]: struct.Subscription.html - /// [`Recipe`]: trait.Recipe.html - type Output; - - /// Hashes the [`Recipe`]. - /// - /// This is used by runtimes to uniquely identify a [`Subscription`]. - /// - /// [`Subscription`]: struct.Subscription.html - /// [`Recipe`]: trait.Recipe.html - fn hash(&self, state: &mut Hasher); - - /// Executes the [`Recipe`] and produces the stream of events of its - /// [`Subscription`]. - /// - /// It receives some generic `Input`, which is normally defined by runtimes. - /// - /// [`Subscription`]: struct.Subscription.html - /// [`Recipe`]: trait.Recipe.html - fn stream( - self: Box, - input: BoxStream<'static, Input>, - ) -> BoxStream<'static, Self::Output>; -} - -struct Map { - recipe: Box>, - mapper: std::sync::Arc B + Send + Sync>, -} - -impl Map { - fn new( - recipe: Box>, - mapper: std::sync::Arc B + Send + Sync + 'static>, - ) -> Self { - Map { recipe, mapper } - } -} - -impl Recipe for Map -where - A: 'static, - B: 'static, - H: std::hash::Hasher, -{ - type Output = B; - - fn hash(&self, state: &mut H) { - use std::hash::Hash; - - std::any::TypeId::of::().hash(state); - self.recipe.hash(state); - } - - fn stream( - self: Box, - input: BoxStream<'static, I>, - ) -> futures::stream::BoxStream<'static, Self::Output> { - use futures::StreamExt; - - let mapper = self.mapper; - - self.recipe - .stream(input) - .map(move |element| mapper(element)) - .boxed() - } -} -- cgit