diff options
Diffstat (limited to 'crates/retry-error')
| -rw-r--r-- | crates/retry-error/Cargo.toml | 4 | ||||
| -rw-r--r-- | crates/retry-error/src/lib.rs | 37 |
2 files changed, 39 insertions, 2 deletions
diff --git a/crates/retry-error/Cargo.toml b/crates/retry-error/Cargo.toml index f4fc34493..5e439a25f 100644 --- a/crates/retry-error/Cargo.toml +++ b/crates/retry-error/Cargo.toml @@ -22,5 +22,9 @@ humantime = "2" [features] full = [] + [package.metadata.docs.rs] all-features = true + +[target.'cfg(all(target_arch="wasm32", target_os="unknown"))'.dependencies] +web-time = "1.1.0" diff --git a/crates/retry-error/src/lib.rs b/crates/retry-error/src/lib.rs index 5d00eb4aa..2ca7a9893 100644 --- a/crates/retry-error/src/lib.rs +++ b/crates/retry-error/src/lib.rs @@ -49,7 +49,13 @@ use std::error::Error; use std::fmt::{self, Debug, Display, Error as FmtError, Formatter}; use std::iter; -use std::time::{Duration, Instant, SystemTime}; +use std::time::{Duration, SystemTime}; + +#[cfg(all(target_arch = "wasm32", target_os = "unknown"))] +use web_time::Instant; + +#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))] +use std::time::Instant; /// An error type for use when we're going to do something a few times, /// and they might all fail. @@ -135,6 +141,7 @@ impl<E> RetryError<E> { /// /// # Example /// ``` + /// # #![allow(clippy::disallowed_methods)] /// # use retry_error::RetryError; /// # use std::time::{Instant, SystemTime}; /// let mut retry_err: RetryError<&str> = RetryError::in_attempt_to("connect"); @@ -169,7 +176,7 @@ impl<E> RetryError<E> { where T: Into<E>, { - self.push_timed(err, Instant::now(), Some(SystemTime::now())); + self.push_timed(err, current_instant(), Some(current_system_time())); } /// Return an iterator over all of the reasons that the attempt @@ -515,6 +522,32 @@ pub fn fmt_error_with_sources(mut e: &dyn Error, f: &mut fmt::Formatter) -> fmt: Ok(()) } +/// Return the current system time. +/// +/// (This is a separate method for compatibility with wasm32.) +#[cfg(all(target_arch = "wasm32", target_os = "unknown"))] +fn current_system_time() -> SystemTime { + use web_time::web::SystemTimeExt as _; + web_time::SystemTime::now().to_std() +} + +/// Return the current system time. +/// +/// (This is a separate method for compatibility with wasm32.) +#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))] +fn current_system_time() -> SystemTime { + #![allow(clippy::disallowed_methods)] + SystemTime::now() +} + +/// Return the current Instant. +/// +/// (This is a separate method for compatibility with wasm32.) +fn current_instant() -> Instant { + #![allow(clippy::disallowed_methods)] + Instant::now() +} + #[cfg(test)] mod test { // @@ begin test lint list maintained by maint/add_warning @@ |
