From 87b3e03d187237f665b376018ea5af0cc5f05814 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Fri, 28 Jan 2022 17:05:09 +0700 Subject: [PATCH] Enable `instant` only for `wasm32` targets ... and hide the dependency under a `time` module in `iced_native` --- native/Cargo.toml | 5 ++++- native/src/debug/basic.rs | 4 +++- native/src/lib.rs | 1 + native/src/mouse/click.rs | 2 +- native/src/time.rs | 7 +++++++ src/time.rs | 6 +++--- 6 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 native/src/time.rs diff --git a/native/Cargo.toml b/native/Cargo.toml index d8e75a4ecd..499ad29ee5 100644 --- a/native/Cargo.toml +++ b/native/Cargo.toml @@ -14,7 +14,6 @@ debug = [] twox-hash = { version = "1.5", default-features = false } unicode-segmentation = "1.6" num-traits = "0.2" -instant = { version="0.1", features=["wasm-bindgen"] } [dependencies.iced_core] version = "0.4" @@ -28,3 +27,7 @@ features = ["thread-pool"] [dependencies.iced_style] version = "0.3" path = "../style" + +[target.'cfg(target_arch = "wasm32")'.dependencies.instant] +version = "0.1" +features = ["wasm-bindgen"] diff --git a/native/src/debug/basic.rs b/native/src/debug/basic.rs index a42f66ea11..d706bb0091 100644 --- a/native/src/debug/basic.rs +++ b/native/src/debug/basic.rs @@ -1,5 +1,7 @@ #![allow(missing_docs)] -use std::{collections::VecDeque, time}; +use crate::time; + +use std::collections::VecDeque; /// A bunch of time measurements for debugging purposes. #[derive(Debug)] diff --git a/native/src/lib.rs b/native/src/lib.rs index a5526e6d49..bd50c6b242 100644 --- a/native/src/lib.rs +++ b/native/src/lib.rs @@ -49,6 +49,7 @@ pub mod renderer; pub mod subscription; pub mod svg; pub mod text; +pub mod time; pub mod touch; pub mod user_interface; pub mod widget; diff --git a/native/src/mouse/click.rs b/native/src/mouse/click.rs index 58cfda615e..ec321387f3 100644 --- a/native/src/mouse/click.rs +++ b/native/src/mouse/click.rs @@ -1,6 +1,6 @@ //! Track mouse clicks. +use crate::time::Instant; use crate::Point; -use instant::Instant; /// A mouse click. #[derive(Debug, Clone, Copy)] diff --git a/native/src/time.rs b/native/src/time.rs new file mode 100644 index 0000000000..5f95ee86e0 --- /dev/null +++ b/native/src/time.rs @@ -0,0 +1,7 @@ +//! Keep track of time, both in native and web platforms! + +#[cfg(target_arch = "wasm32")] +pub use instant::{Duration, Instant}; + +#[cfg(not(target_arch = "wasm32"))] +pub use std::time::{Duration, Instant}; diff --git a/src/time.rs b/src/time.rs index b8432895d6..943aa42f52 100644 --- a/src/time.rs +++ b/src/time.rs @@ -1,12 +1,12 @@ //! Listen and react to time. +pub use crate::runtime::time::{Duration, Instant}; + 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. -pub fn every( - duration: std::time::Duration, -) -> Subscription { +pub fn every(duration: Duration) -> Subscription { iced_futures::time::every(duration) }