diff --git a/biscuit-auth/Cargo.toml b/biscuit-auth/Cargo.toml index a7756e39..68c79481 100644 --- a/biscuit-auth/Cargo.toml +++ b/biscuit-auth/Cargo.toml @@ -13,7 +13,7 @@ repository = "https://github.com/biscuit-auth/biscuit-rust" [features] default = ["regex-full", "datalog-macro", "pem"] regex-full = ["regex/perf", "regex/unicode"] -wasm = ["wasm-bindgen"] +wasm-bindgen = ["dep:wasm-bindgen"] # used by biscuit-wasm to serialize errors to JSON serde-error = ["serde", "biscuit-parser/serde-error"] # used by biscuit-quote to parse datalog at compile-time diff --git a/biscuit-auth/src/time.rs b/biscuit-auth/src/time.rs index 69b87f24..f71862e0 100644 --- a/biscuit-auth/src/time.rs +++ b/biscuit-auth/src/time.rs @@ -6,10 +6,10 @@ //! //! code from -#[cfg(feature = "wasm")] +#[cfg(target_arch = "wasm32")] use std::convert::TryInto; use std::ops::{Add, AddAssign, Sub, SubAssign}; -#[cfg(feature = "wasm")] +#[cfg(feature = "wasm-bindgen")] use wasm_bindgen::prelude::*; pub use std::time::*; @@ -38,25 +38,30 @@ impl Instant { } } -#[cfg(target_arch = "wasm32")] -#[cfg(feature = "wasm")] -#[wasm_bindgen(inline_js = r#" -export function performance_now() { - return performance.now(); -}"#)] -extern "C" { - fn performance_now() -> f64; -} - #[cfg(target_arch = "wasm32")] #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Instant(u64); #[cfg(target_arch = "wasm32")] impl Instant { + #[cfg(feature = "wasm-bindgen")] pub fn now() -> Self { + #[wasm_bindgen(inline_js = r#" + export function performance_now() { + return performance.now(); + }"#)] + extern "C" { + fn performance_now() -> f64; + } Self((performance_now() * 1000.0) as u64) } + #[cfg(not(feature = "wasm-bindgen"))] + pub fn now() -> Self { + extern "C" { + fn instant_now() -> u64; + } + Self(unsafe { instant_now() }) + } pub fn duration_since(&self, earlier: Instant) -> Duration { Duration::from_micros(self.0 - earlier.0) }