Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion biscuit-auth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 17 additions & 12 deletions biscuit-auth/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
//!
//! code from <https://github.com/rust-lang/rust/issues/48564#issuecomment-698712971>

#[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::*;
Expand Down Expand Up @@ -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)
}
Expand Down