diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/Cargo.toml | 2 | ||||
-rw-r--r-- | core/src/lib.rs | 8 | ||||
-rw-r--r-- | core/src/subscription.rs | 52 |
3 files changed, 61 insertions, 1 deletions
diff --git a/core/Cargo.toml b/core/Cargo.toml index c623ba78..0a8fd8ef 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -10,6 +10,8 @@ repository = "https://github.com/hecrj/iced" [features] # Exposes a future-based `Command` type command = ["futures"] +# Exposes a future-based `Subscription` type +subscription = ["futures"] [dependencies] futures = { version = "0.3", optional = true } diff --git a/core/src/lib.rs b/core/src/lib.rs index 65304e8b..6f13c310 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)] @@ -38,3 +38,9 @@ mod command; #[cfg(feature = "command")] pub use command::Command; + +#[cfg(feature = "subscription")] +pub mod subscription; + +#[cfg(feature = "subscription")] +pub use subscription::Subscription; diff --git a/core/src/subscription.rs b/core/src/subscription.rs new file mode 100644 index 00000000..796982c7 --- /dev/null +++ b/core/src/subscription.rs @@ -0,0 +1,52 @@ +//! Generate events asynchronously for you application. + +/// An event subscription. +pub struct Subscription<T> { + handles: Vec<Box<dyn Handle<Output = T>>>, +} + +impl<T> Subscription<T> { + pub fn none() -> Self { + Self { + handles: Vec::new(), + } + } + + pub fn batch(subscriptions: impl Iterator<Item = Subscription<T>>) -> Self { + Self { + handles: subscriptions + .flat_map(|subscription| subscription.handles) + .collect(), + } + } + + pub fn handles(self) -> Vec<Box<dyn Handle<Output = T>>> { + self.handles + } +} + +impl<T, A> From<A> for Subscription<T> +where + A: Handle<Output = T> + 'static, +{ + fn from(handle: A) -> Self { + Self { + handles: vec![Box::new(handle)], + } + } +} + +/// The handle of an event subscription. +pub trait Handle { + type Output; + + fn id(&self) -> u64; + + fn stream(&self) -> futures::stream::BoxStream<'static, Self::Output>; +} + +impl<T> std::fmt::Debug for Subscription<T> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Subscription").finish() + } +} |