Skip to content

Commit

Permalink
Upgrade quanta to v0.10.0
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsuya6502 committed May 19, 2022
1 parent b9c03c4 commit ef82926
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ uuid = { version = "0.8", features = ["v4"] }
triomphe = { version = "0.1", default-features = false }

# Optional dependencies (quanta, enabled by default)
quanta = { version = "0.9.3", optional = true }
quanta = { version = "0.10.0", optional = true }

# Optional dependencies (dashmap)
dashmap = { version = "5.2", optional = true }
Expand Down
2 changes: 1 addition & 1 deletion src/common/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub(crate) use clock::Mock;
/// a wrapper type over Instant to force checked additions and prevent
/// unintentional overflow. The type preserve the Copy semantics for the wrapped
#[derive(PartialEq, PartialOrd, Clone, Copy)]
pub(crate) struct Instant(pub clock::Instant);
pub(crate) struct Instant(clock::Instant);

pub(crate) trait CheckedTimeOps {
fn checked_add(&self, duration: Duration) -> Option<Self>
Expand Down
21 changes: 18 additions & 3 deletions src/common/time/atomic_time.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use super::Instant;

use std::sync::atomic::{AtomicU64, Ordering};
use std::{
any::TypeId,
sync::atomic::{AtomicU64, Ordering},
};

pub(crate) struct AtomicInstant {
instant: AtomicU64,
Expand All @@ -14,6 +17,9 @@ impl Default for AtomicInstant {
}
}

// TODO: Need a safe way to convert between `quanta::Instant` and `u64`.
// quanta v0.10.0 no longer provides `quanta::Instant::as_u64` method.

impl AtomicInstant {
pub(crate) fn reset(&self) {
self.instant.store(std::u64::MAX, Ordering::Release);
Expand All @@ -28,11 +34,20 @@ impl AtomicInstant {
if ts == u64::MAX {
None
} else {
Some(unsafe { std::mem::transmute(ts) })
debug_assert_eq!(
TypeId::of::<super::clock::Instant>(),
TypeId::of::<quanta::Instant>()
);
Some(Instant(unsafe { std::mem::transmute(ts) }))
}
}

pub(crate) fn set_instant(&self, instant: Instant) {
self.instant.store(instant.0.as_u64(), Ordering::Release);
debug_assert_eq!(
TypeId::of::<super::clock::Instant>(),
TypeId::of::<quanta::Instant>()
);
let ts = unsafe { std::mem::transmute(instant.0) };
self.instant.store(ts, Ordering::Release);
}
}

0 comments on commit ef82926

Please sign in to comment.