summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2019-12-14 05:56:46 +0100
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2019-12-14 05:56:46 +0100
commitd6c3da21f7fe7a79bcfbc2a180dc111e42300a04 (patch)
treebbe8e02712824cd0fe22028bd3072fbfce104d62 /core
parent293314405f5b8d4003db5ef8f428e659ae36872d (diff)
downloadiced-d6c3da21f7fe7a79bcfbc2a180dc111e42300a04.tar.gz
iced-d6c3da21f7fe7a79bcfbc2a180dc111e42300a04.tar.bz2
iced-d6c3da21f7fe7a79bcfbc2a180dc111e42300a04.zip
Write docs for subscriptions and reorganize a bit
Diffstat (limited to 'core')
-rw-r--r--core/src/command.rs4
-rw-r--r--core/src/lib.rs2
-rw-r--r--core/src/subscription.rs65
3 files changed, 64 insertions, 7 deletions
diff --git a/core/src/command.rs b/core/src/command.rs
index 14b48b5b..e0e5ab5c 100644
--- a/core/src/command.rs
+++ b/core/src/command.rs
@@ -34,8 +34,8 @@ impl<T> Command<T> {
}
}
- /// Creates a [`Command`] that performs the actions of all the givens
- /// futures.
+ /// Creates a [`Command`] that performs the actions of all the given
+ /// commands.
///
/// Once this command is run, all the futures will be exectued at once.
///
diff --git a/core/src/lib.rs b/core/src/lib.rs
index 6f13c310..821b09c1 100644
--- a/core/src/lib.rs
+++ b/core/src/lib.rs
@@ -9,7 +9,7 @@
//! [Iced]: https://github.com/hecrj/iced
//! [`iced_native`]: https://github.com/hecrj/iced/tree/master/native
//! [`iced_web`]: https://github.com/hecrj/iced/tree/master/web
-//#![deny(missing_docs)]
+#![deny(missing_docs)]
#![deny(missing_debug_implementations)]
#![deny(unused_results)]
#![deny(unsafe_code)]
diff --git a/core/src/subscription.rs b/core/src/subscription.rs
index 21868c20..8de6cae8 100644
--- a/core/src/subscription.rs
+++ b/core/src/subscription.rs
@@ -1,6 +1,21 @@
-//! Generate events asynchronously for you application.
-
-/// An event subscription.
+//! Listen to external events in your application.
+
+/// 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<Hasher, Input, Output> {
recipes: Vec<Box<dyn Recipe<Hasher, Input, Output = Output>>>,
}
@@ -9,12 +24,19 @@ impl<H, I, O> Subscription<H, I, O>
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<H, I, Output = O> + 'static,
) -> Self {
@@ -23,6 +45,10 @@ where
}
}
+ /// Batches all the provided subscriptions and returns the resulting
+ /// [`Subscription`].
+ ///
+ /// [`Subscription`]: struct.Subscription.html
pub fn batch(
subscriptions: impl Iterator<Item = Subscription<H, I, O>>,
) -> Self {
@@ -33,10 +59,16 @@ where
}
}
+ /// Returns the different recipes of the [`Subscription`].
+ ///
+ /// [`Subscription`]: struct.Subscription.html
pub fn recipes(self) -> Vec<Box<dyn Recipe<H, I, Output = O>>> {
self.recipes
}
+ /// 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,
@@ -68,12 +100,37 @@ impl<I, O, H> std::fmt::Debug for Subscription<I, O, H> {
}
}
-/// The connection of an event subscription.
+/// 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<Hasher: std::hash::Hasher, Input> {
+ /// 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<Self>,
input: Input,