Implement `time::every` in `iced_futures`

This commit is contained in:
Héctor Ramón Jiménez 2020-04-30 05:37:44 +02:00
parent bb9ccc4f62
commit e2076612cb
14 changed files with 110 additions and 169 deletions

View File

@ -6,7 +6,5 @@ edition = "2018"
publish = false publish = false
[dependencies] [dependencies]
iced = { path = "../..", features = ["canvas", "async-std", "debug"] } iced = { path = "../..", features = ["canvas", "tokio", "debug"] }
iced_native = { path = "../../native" }
chrono = "0.4" chrono = "0.4"
async-std = { version = "1.0", features = ["unstable"] }

View File

@ -1,7 +1,7 @@
use iced::{ use iced::{
canvas::{self, Cache, Canvas, Cursor, Geometry, LineCap, Path, Stroke}, canvas::{self, Cache, Canvas, Cursor, Geometry, LineCap, Path, Stroke},
executor, Application, Color, Command, Container, Element, Length, Point, executor, time, Application, Color, Command, Container, Element, Length,
Rectangle, Settings, Subscription, Vector, Point, Rectangle, Settings, Subscription, Vector,
}; };
pub fn main() { pub fn main() {
@ -43,7 +43,7 @@ impl Application for Clock {
fn update(&mut self, message: Message) -> Command<Message> { fn update(&mut self, message: Message) -> Command<Message> {
match message { match message {
Message::Tick(local_time) => { Message::Tick(local_time) => {
let now = local_time.into(); let now = local_time;
if now != self.now { if now != self.now {
self.now = now; self.now = now;
@ -56,7 +56,8 @@ impl Application for Clock {
} }
fn subscription(&self) -> Subscription<Message> { fn subscription(&self) -> Subscription<Message> {
time::every(std::time::Duration::from_millis(500)).map(Message::Tick) time::every(std::time::Duration::from_millis(500))
.map(|_| Message::Tick(chrono::Local::now()))
} }
fn view(&mut self) -> Element<Message> { fn view(&mut self) -> Element<Message> {
@ -130,40 +131,3 @@ fn hand_rotation(n: u32, total: u32) -> f32 {
2.0 * std::f32::consts::PI * turns 2.0 * std::f32::consts::PI * turns
} }
mod time {
use iced::futures;
pub fn every(
duration: std::time::Duration,
) -> iced::Subscription<chrono::DateTime<chrono::Local>> {
iced::Subscription::from_recipe(Every(duration))
}
struct Every(std::time::Duration);
impl<H, I> iced_native::subscription::Recipe<H, I> for Every
where
H: std::hash::Hasher,
{
type Output = chrono::DateTime<chrono::Local>;
fn hash(&self, state: &mut H) {
use std::hash::Hash;
std::any::TypeId::of::<Self>().hash(state);
self.0.hash(state);
}
fn stream(
self: Box<Self>,
_input: futures::stream::BoxStream<'static, I>,
) -> futures::stream::BoxStream<'static, Self::Output> {
use futures::stream::StreamExt;
async_std::stream::interval(self.0)
.map(|_| chrono::Local::now())
.boxed()
}
}
}

View File

@ -6,7 +6,5 @@ edition = "2018"
publish = false publish = false
[dependencies] [dependencies]
iced = { path = "../..", features = ["async-std", "canvas", "debug"] } iced = { path = "../..", features = ["canvas", "tokio"] }
iced_native = { path = "../../native" }
async-std = { version = "1.0", features = ["unstable"] }
itertools = "0.9" itertools = "0.9"

View File

@ -1,14 +1,13 @@
//! This example showcases an interactive version of the Game of Life, invented //! This example showcases an interactive version of the Game of Life, invented
//! by John Conway. It leverages a `Canvas` together with other widgets. //! by John Conway. It leverages a `Canvas` together with other widgets.
mod style; mod style;
mod time;
use grid::Grid; use grid::Grid;
use iced::{ use iced::{
button::{self, Button}, button::{self, Button},
executor, executor,
slider::{self, Slider}, slider::{self, Slider},
Align, Application, Column, Command, Container, Element, Length, Row, time, Align, Application, Column, Command, Container, Element, Length, Row,
Settings, Subscription, Text, Settings, Subscription, Text,
}; };
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};

View File

@ -1,34 +0,0 @@
use iced::futures;
pub fn every(
duration: std::time::Duration,
) -> iced::Subscription<std::time::Instant> {
iced::Subscription::from_recipe(Every(duration))
}
struct Every(std::time::Duration);
impl<H, I> iced_native::subscription::Recipe<H, I> for Every
where
H: std::hash::Hasher,
{
type Output = std::time::Instant;
fn hash(&self, state: &mut H) {
use std::hash::Hash;
std::any::TypeId::of::<Self>().hash(state);
self.0.hash(state);
}
fn stream(
self: Box<Self>,
_input: futures::stream::BoxStream<'static, I>,
) -> futures::stream::BoxStream<'static, Self::Output> {
use futures::stream::StreamExt;
async_std::stream::interval(self.0)
.map(|_| std::time::Instant::now())
.boxed()
}
}

View File

@ -6,7 +6,5 @@ edition = "2018"
publish = false publish = false
[dependencies] [dependencies]
iced = { path = "../..", features = ["canvas", "async-std", "debug"] } iced = { path = "../..", features = ["canvas", "tokio", "debug"] }
iced_native = { path = "../../native" }
async-std = { version = "1.0", features = ["unstable"] }
rand = "0.7" rand = "0.7"

View File

@ -8,8 +8,8 @@
//! [1]: https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Basic_animations#An_animated_solar_system //! [1]: https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Basic_animations#An_animated_solar_system
use iced::{ use iced::{
canvas::{self, Cursor, Path, Stroke}, canvas::{self, Cursor, Path, Stroke},
executor, window, Application, Canvas, Color, Command, Element, Length, executor, time, window, Application, Canvas, Color, Command, Element,
Point, Rectangle, Settings, Size, Subscription, Vector, Length, Point, Rectangle, Settings, Size, Subscription, Vector,
}; };
use std::time::Instant; use std::time::Instant;
@ -212,39 +212,3 @@ impl<Message> canvas::Program<Message> for State {
vec![background, system] vec![background, system]
} }
} }
mod time {
use iced::futures;
use std::time::Instant;
pub fn every(duration: std::time::Duration) -> iced::Subscription<Instant> {
iced::Subscription::from_recipe(Every(duration))
}
struct Every(std::time::Duration);
impl<H, I> iced_native::subscription::Recipe<H, I> for Every
where
H: std::hash::Hasher,
{
type Output = Instant;
fn hash(&self, state: &mut H) {
use std::hash::Hash;
std::any::TypeId::of::<Self>().hash(state);
self.0.hash(state);
}
fn stream(
self: Box<Self>,
_input: futures::stream::BoxStream<'static, I>,
) -> futures::stream::BoxStream<'static, Self::Output> {
use futures::stream::StreamExt;
async_std::stream::interval(self.0)
.map(|_| Instant::now())
.boxed()
}
}
}

View File

@ -6,7 +6,4 @@ edition = "2018"
publish = false publish = false
[dependencies] [dependencies]
iced = { path = "../.." } iced = { path = "../..", features = ["tokio"] }
iced_native = { path = "../../native" }
iced_futures = { path = "../../futures", features = ["async-std"] }
async-std = { version = "1.0", features = ["unstable"] }

View File

@ -1,6 +1,7 @@
use iced::{ use iced::{
button, Align, Application, Button, Column, Command, Container, Element, button, executor, time, Align, Application, Button, Column, Command,
HorizontalAlignment, Length, Row, Settings, Subscription, Text, Container, Element, HorizontalAlignment, Length, Row, Settings,
Subscription, Text,
}; };
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
@ -28,7 +29,7 @@ enum Message {
} }
impl Application for Stopwatch { impl Application for Stopwatch {
type Executor = iced_futures::executor::AsyncStd; type Executor = executor::Default;
type Message = Message; type Message = Message;
type Flags = (); type Flags = ();
@ -143,43 +144,6 @@ impl Application for Stopwatch {
} }
} }
mod time {
use iced::futures;
pub fn every(
duration: std::time::Duration,
) -> iced::Subscription<std::time::Instant> {
iced::Subscription::from_recipe(Every(duration))
}
struct Every(std::time::Duration);
impl<H, I> iced_native::subscription::Recipe<H, I> for Every
where
H: std::hash::Hasher,
{
type Output = std::time::Instant;
fn hash(&self, state: &mut H) {
use std::hash::Hash;
std::any::TypeId::of::<Self>().hash(state);
self.0.hash(state);
}
fn stream(
self: Box<Self>,
_input: futures::stream::BoxStream<'static, I>,
) -> futures::stream::BoxStream<'static, Self::Output> {
use futures::stream::StreamExt;
async_std::stream::interval(self.0)
.map(|_| std::time::Instant::now())
.boxed()
}
}
}
mod style { mod style {
use iced::{button, Background, Color, Vector}; use iced::{button, Background, Color, Vector};

View File

@ -22,11 +22,12 @@ version = "0.3"
[target.'cfg(not(target_arch = "wasm32"))'.dependencies.tokio] [target.'cfg(not(target_arch = "wasm32"))'.dependencies.tokio]
version = "0.2" version = "0.2"
optional = true optional = true
features = ["rt-core", "rt-threaded"] features = ["rt-core", "rt-threaded", "time", "stream"]
[target.'cfg(not(target_arch = "wasm32"))'.dependencies.async-std] [target.'cfg(not(target_arch = "wasm32"))'.dependencies.async-std]
version = "1.0" version = "1.0"
optional = true optional = true
features = ["unstable"]
[target.'cfg(target_arch = "wasm32")'.dependencies] [target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen-futures = "0.4" wasm-bindgen-futures = "0.4"

View File

@ -14,6 +14,10 @@ mod runtime;
pub mod executor; pub mod executor;
pub mod subscription; pub mod subscription;
#[cfg(any(feature = "tokio", feature = "async-std"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "tokio", feature = "async-std"))))]
pub mod time;
pub use command::Command; pub use command::Command;
pub use executor::Executor; pub use executor::Executor;
pub use runtime::Runtime; pub use runtime::Runtime;

70
futures/src/time.rs Normal file
View File

@ -0,0 +1,70 @@
//! Listen and react to time.
use crate::subscription::{self, Subscription};
/// Returns a [`Subscription`] that produces messages at a set interval.
///
/// The first message is produced after a `duration`, and then continues to
/// produce more messages every `duration` after that.
///
/// [`Subscription`]: ../subscription/struct.Subscription.html
pub fn every<H: std::hash::Hasher, E>(
duration: std::time::Duration,
) -> Subscription<H, E, std::time::Instant> {
Subscription::from_recipe(Every(duration))
}
struct Every(std::time::Duration);
#[cfg(feature = "async-std")]
impl<H, E> subscription::Recipe<H, E> for Every
where
H: std::hash::Hasher,
{
type Output = std::time::Instant;
fn hash(&self, state: &mut H) {
use std::hash::Hash;
std::any::TypeId::of::<Self>().hash(state);
self.0.hash(state);
}
fn stream(
self: Box<Self>,
_input: futures::stream::BoxStream<'static, E>,
) -> futures::stream::BoxStream<'static, Self::Output> {
use futures::stream::StreamExt;
async_std::stream::interval(self.0)
.map(|_| std::time::Instant::now())
.boxed()
}
}
#[cfg(all(feature = "tokio", not(feature = "async-std")))]
impl<H, E> subscription::Recipe<H, E> for Every
where
H: std::hash::Hasher,
{
type Output = std::time::Instant;
fn hash(&self, state: &mut H) {
use std::hash::Hash;
std::any::TypeId::of::<Self>().hash(state);
self.0.hash(state);
}
fn stream(
self: Box<Self>,
_input: futures::stream::BoxStream<'static, E>,
) -> futures::stream::BoxStream<'static, Self::Output> {
use futures::stream::StreamExt;
let start = tokio::time::Instant::now() + self.0;
tokio::time::interval_at(start, self.0)
.map(|_| std::time::Instant::now())
.boxed()
}
}

View File

@ -190,6 +190,10 @@ pub mod settings;
pub mod widget; pub mod widget;
pub mod window; pub mod window;
#[cfg(any(feature = "tokio", feature = "async-std"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "tokio", feature = "async-std"))))]
pub mod time;
#[doc(no_inline)] #[doc(no_inline)]
pub use widget::*; pub use widget::*;

14
src/time.rs Normal file
View File

@ -0,0 +1,14 @@
//! Listen and react to time.
use crate::Subscription;
/// Returns a [`Subscription`] that produces messages at a set interval.
///
/// The first message is produced after a `duration`, and then continues to
/// produce more messages every `duration` after that.
///
/// [`Subscription`]: ../subscription/struct.Subscription.html
pub fn every(
duration: std::time::Duration,
) -> Subscription<std::time::Instant> {
iced_futures::time::every(duration)
}