Skip to content
This repository has been archived by the owner on Jun 8, 2024. It is now read-only.

Commit

Permalink
split time module up
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Aug 24, 2023
1 parent 50a2f3e commit 5de6fcf
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 136 deletions.
14 changes: 4 additions & 10 deletions core/src/ambient.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
use crate::{
ctxt::Ctxt,
empty::Empty,
event::Event,
filter::Filter,
id::IdGen,
props::Props,
target::Target,
time::{Clock, Timestamp},
clock::Clock, ctxt::Ctxt, empty::Empty, event::Event, filter::Filter, id::IdGen, props::Props,
target::Target, timestamp::Timestamp,
};

#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -190,8 +184,8 @@ mod std_support {
use std::sync::OnceLock;

use crate::{
ctxt::ErasedCtxt, filter::ErasedFilter, id::ErasedIdGen, target::ErasedTarget,
time::ErasedClock,
clock::ErasedClock, ctxt::ErasedCtxt, filter::ErasedFilter, id::ErasedIdGen,
target::ErasedTarget,
};

use super::*;
Expand Down
108 changes: 108 additions & 0 deletions core/src/clock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
use crate::{
empty::Empty,
extent::{Extent, ToExtent},
timestamp::Timestamp,
};

pub trait Clock {
fn now(&self) -> Option<Timestamp>;
}

impl<'a, T: Clock + ?Sized> Clock for &'a T {
fn now(&self) -> Option<Timestamp> {
(**self).now()
}
}

impl<'a, T: Clock> Clock for Option<T> {
fn now(&self) -> Option<Timestamp> {
if let Some(time) = self {
time.now()
} else {
Empty.now()
}
}
}

#[cfg(feature = "alloc")]
impl<'a, T: Clock + ?Sized + 'a> Clock for alloc::boxed::Box<T> {
fn now(&self) -> Option<Timestamp> {
(**self).now()
}
}

impl Clock for Empty {
fn now(&self) -> Option<Timestamp> {
None
}
}

mod internal {
use super::Timestamp;

pub trait DispatchClock {
fn dispatch_now(&self) -> Option<Timestamp>;
}

pub trait SealedClock {
fn erase_clock(&self) -> crate::internal::Erased<&dyn DispatchClock>;
}
}

pub trait ErasedClock: internal::SealedClock {}

impl<T: Clock> ErasedClock for T {}

impl<T: Clock> internal::SealedClock for T {
fn erase_clock(&self) -> crate::internal::Erased<&dyn internal::DispatchClock> {
crate::internal::Erased(self)
}
}

impl<T: Clock> internal::DispatchClock for T {
fn dispatch_now(&self) -> Option<Timestamp> {
self.now()
}
}

impl<'a> Clock for dyn ErasedClock + 'a {
fn now(&self) -> Option<Timestamp> {
self.erase_clock().0.dispatch_now()
}
}

impl<'a> Clock for dyn ErasedClock + Send + Sync + 'a {
fn now(&self) -> Option<Timestamp> {
(self as &(dyn ErasedClock + 'a)).now()
}
}

#[derive(Clone, Copy)]
pub struct Timer<C> {
start: Option<Timestamp>,
clock: C,
}

impl<C: Clock> Timer<C> {
pub fn start(clock: C) -> Self {
Timer {
start: clock.now(),
clock,
}
}

pub fn extent(&self) -> Extent {
let end = self.clock.now();

match (self.start, end) {
(Some(start), Some(end)) => Extent::new(start..end),
_ => Extent::empty(),
}
}
}

impl<C: Clock> ToExtent for Timer<C> {
fn to_extent(&self) -> Extent {
self.extent()
}
}
8 changes: 1 addition & 7 deletions core/src/extent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
empty::Empty,
key::{Key, ToKey},
props::{ControlFlow, Props},
time::{Clock, Timer, Timestamp},
timestamp::Timestamp,
value::{ToValue, Value},
well_known::{TIMESTAMP_KEY, TIMESTAMP_START_KEY},
};
Expand Down Expand Up @@ -149,9 +149,3 @@ impl ToExtent for Range<Timestamp> {
Extent::new(self.clone())
}
}

impl<C: Clock> ToExtent for Timer<C> {
fn to_extent(&self) -> Extent {
self.extent()
}
}
3 changes: 2 additions & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ extern crate alloc;
extern crate core;

pub mod ambient;
pub mod clock;
pub mod ctxt;
pub mod empty;
pub mod event;
Expand All @@ -17,7 +18,7 @@ pub mod level;
pub mod props;
pub mod target;
pub mod template;
pub mod time;
pub mod timestamp;
pub mod value;
pub mod well_known;

Expand Down
103 changes: 1 addition & 102 deletions core/src/time.rs → core/src/timestamp.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use core::{cmp, fmt, ops::Sub, str, str::FromStr, time::Duration};

use crate::{
empty::Empty,
extent::Extent,
value::{ToValue, Value},
};
use crate::value::{ToValue, Value};

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Timestamp(Duration);
Expand Down Expand Up @@ -74,103 +70,6 @@ impl<'v> Value<'v> {
}
}

pub trait Clock {
fn now(&self) -> Option<Timestamp>;
}

impl<'a, T: Clock + ?Sized> Clock for &'a T {
fn now(&self) -> Option<Timestamp> {
(**self).now()
}
}

impl<'a, T: Clock> Clock for Option<T> {
fn now(&self) -> Option<Timestamp> {
if let Some(time) = self {
time.now()
} else {
Empty.now()
}
}
}

#[cfg(feature = "alloc")]
impl<'a, T: Clock + ?Sized + 'a> Clock for alloc::boxed::Box<T> {
fn now(&self) -> Option<Timestamp> {
(**self).now()
}
}

impl Clock for Empty {
fn now(&self) -> Option<Timestamp> {
None
}
}

mod internal {
use super::Timestamp;

pub trait DispatchClock {
fn dispatch_now(&self) -> Option<Timestamp>;
}

pub trait SealedClock {
fn erase_clock(&self) -> crate::internal::Erased<&dyn DispatchClock>;
}
}

pub trait ErasedClock: internal::SealedClock {}

impl<T: Clock> ErasedClock for T {}

impl<T: Clock> internal::SealedClock for T {
fn erase_clock(&self) -> crate::internal::Erased<&dyn internal::DispatchClock> {
crate::internal::Erased(self)
}
}

impl<T: Clock> internal::DispatchClock for T {
fn dispatch_now(&self) -> Option<Timestamp> {
self.now()
}
}

impl<'a> Clock for dyn ErasedClock + 'a {
fn now(&self) -> Option<Timestamp> {
self.erase_clock().0.dispatch_now()
}
}

impl<'a> Clock for dyn ErasedClock + Send + Sync + 'a {
fn now(&self) -> Option<Timestamp> {
(self as &(dyn ErasedClock + 'a)).now()
}
}

#[derive(Clone, Copy)]
pub struct Timer<C> {
start: Option<Timestamp>,
clock: C,
}

impl<C: Clock> Timer<C> {
pub fn start(clock: C) -> Self {
Timer {
start: clock.now(),
clock,
}
}

pub fn extent(&self) -> Extent {
let end = self.clock.now();

match (self.start, end) {
(Some(start), Some(end)) => Extent::new(start..end),
_ => Extent::empty(),
}
}
}

#[derive(Debug)]
pub struct ParseTimestampError {}

Expand Down
2 changes: 1 addition & 1 deletion core/src/well_known.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
id::{SpanId, TraceId},
level::Level,
props::Props,
time::Timestamp,
timestamp::Timestamp,
value::Value,
};

Expand Down
4 changes: 2 additions & 2 deletions macros/src/optional.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::hook;
use proc_macro2::{Span, TokenStream};
use syn::{parse::Parse, punctuated::Punctuated, spanned::Spanned, token::Comma, Expr, Ident};
use proc_macro2::TokenStream;
use syn::{parse::Parse, punctuated::Punctuated, token::Comma, Expr, Ident};

pub struct Args;

Expand Down
19 changes: 10 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,36 @@
#[cfg(feature = "alloc")]
extern crate alloc;

use core::{future::Future, ops::Range};
use core::future::Future;

use emit_core::extent::ToExtent;

use crate::local_frame::{LocalFrame, LocalFrameFuture};

#[doc(inline)]
pub use emit_macros::*;

use emit_core::extent::{Extent, ToExtent};
#[doc(inline)]
pub use emit_core::{
ctxt, empty, event, filter, id, key, level, props, target, template, time, value, well_known,
clock, ctxt, empty, event, extent, filter, id, key, level, props, target, template, timestamp,
value, well_known,
};

pub mod local_frame;

pub use self::{
clock::{Clock, Timer},
ctxt::Ctxt,
event::Event,
extent::Extent,
filter::Filter,
id::{IdGen, SpanId, TraceId},
key::Key,
level::Level,
props::Props,
target::Target,
template::Template,
time::{Clock, Timer, Timestamp},
timestamp::Timestamp,
value::Value,
well_known::WellKnown,
};
Expand All @@ -38,6 +42,8 @@ mod platform;

#[cfg(feature = "std")]
mod setup;
#[cfg(feature = "std")]
pub use setup::*;

#[track_caller]
fn base_emit(
Expand Down Expand Up @@ -134,11 +140,6 @@ pub fn current_trace_id() -> Option<TraceId> {
trace_id
}

#[cfg(feature = "std")]
pub fn setup() -> setup::Setup {
setup::Setup::default()
}

#[doc(hidden)]
pub mod __private {
pub use crate::macro_hooks::*;
Expand Down
2 changes: 1 addition & 1 deletion src/macro_hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use core::{any::Any, fmt, future::Future, ops::ControlFlow, panic::Location};

use emit_core::{
ambient,
clock::Clock,
ctxt::Ctxt,
filter::Filter,
id::{SpanId, TraceId},
Expand All @@ -10,7 +11,6 @@ use emit_core::{
props::Props,
target::Target,
template::Template,
time::Clock,
value::{ToValue, Value},
};

Expand Down
4 changes: 2 additions & 2 deletions src/platform.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::{id::IdGen, time::Clock, Timestamp};
use crate::{clock::Clock, id::IdGen, Timestamp};

#[cfg(not(feature = "std"))]
use emit_core::empty::Empty;

#[cfg(feature = "std")]
use emit_core::{id::ErasedIdGen, time::ErasedClock};
use emit_core::{clock::ErasedClock, id::ErasedIdGen};

#[cfg(feature = "std")]
pub(crate) mod system_clock;
Expand Down
Loading

0 comments on commit 5de6fcf

Please sign in to comment.