summaryrefslogtreecommitdiffstats
path: root/examples/game_of_life/src/time.rs
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2020-04-29 08:25:42 +0200
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2020-04-29 08:25:42 +0200
commit70f86f998b6db102d5b77f756750414efd53aa9e (patch)
treef3771264767d2c3f87c098ac41b35b113116ce6b /examples/game_of_life/src/time.rs
parentafa0bca4fd1a5499fd24549eb49a44f9837597c6 (diff)
downloadiced-70f86f998b6db102d5b77f756750414efd53aa9e.tar.gz
iced-70f86f998b6db102d5b77f756750414efd53aa9e.tar.bz2
iced-70f86f998b6db102d5b77f756750414efd53aa9e.zip
Add `game_of_life` example
RIP John Conway
Diffstat (limited to 'examples/game_of_life/src/time.rs')
-rw-r--r--examples/game_of_life/src/time.rs34
1 files changed, 34 insertions, 0 deletions
diff --git a/examples/game_of_life/src/time.rs b/examples/game_of_life/src/time.rs
new file mode 100644
index 00000000..7b475ecd
--- /dev/null
+++ b/examples/game_of_life/src/time.rs
@@ -0,0 +1,34 @@
+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()
+ }
+}