#[cfg(all(feature = "browser", target_arch = "wasm32"))]
pub fn now_millis() -> u64 {
js_sys::Date::now() as u64
}
#[cfg(not(all(feature = "browser", target_arch = "wasm32")))]
pub fn now_millis() -> u64 {
#[cfg(any(feature = "native", feature = "browser"))]
{
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_millis() as u64)
.unwrap_or(0)
}
#[cfg(not(any(feature = "native", feature = "browser")))]
{
0
}
}
#[cfg(feature = "native")]
pub use tokio::sync::Mutex;
#[cfg(not(feature = "native"))]
pub use futures_util::lock::Mutex;
#[cfg(feature = "native")]
pub use tokio::sync::RwLock;
#[cfg(not(feature = "native"))]
pub mod rw_lock_wrapper {
use std::ops::{Deref, DerefMut};
pub struct RwLock<T>(std::sync::RwLock<T>);
impl<T> RwLock<T> {
pub fn new(value: T) -> Self {
Self(std::sync::RwLock::new(value))
}
pub async fn read(&self) -> RwLockReadGuard<'_, T> {
RwLockReadGuard(self.0.read().expect("RwLock poisoned"))
}
pub async fn write(&self) -> RwLockWriteGuard<'_, T> {
RwLockWriteGuard(self.0.write().expect("RwLock poisoned"))
}
}
pub struct RwLockReadGuard<'a, T>(std::sync::RwLockReadGuard<'a, T>);
impl<T> Deref for RwLockReadGuard<'_, T> {
type Target = T;
fn deref(&self) -> &T {
&self.0
}
}
pub struct RwLockWriteGuard<'a, T>(std::sync::RwLockWriteGuard<'a, T>);
impl<T> Deref for RwLockWriteGuard<'_, T> {
type Target = T;
fn deref(&self) -> &T {
&self.0
}
}
impl<T> DerefMut for RwLockWriteGuard<'_, T> {
fn deref_mut(&mut self) -> &mut T {
&mut self.0
}
}
}
#[cfg(not(feature = "native"))]
pub use rw_lock_wrapper::RwLock;