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

Commit

Permalink
start filling in some missing pieces
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Aug 21, 2023
1 parent f682ed3 commit 36c3906
Show file tree
Hide file tree
Showing 13 changed files with 513 additions and 178 deletions.
82 changes: 43 additions & 39 deletions core/src/ambient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ use crate::{
};

#[derive(Debug, Clone, Copy)]
pub struct Ambient<TTarget = Empty, TFilter = Empty, TCtxt = Empty, TClock = Empty, TGenId = Empty>
pub struct Ambient<TTarget = Empty, TFilter = Empty, TCtxt = Empty, TClock = Empty, TIdGen = Empty>
{
target: TTarget,
filter: TFilter,
ctxt: TCtxt,
clock: TClock,
gen_id: TGenId,
id_gen: TIdGen,
}

impl Default for Ambient {
Expand All @@ -32,85 +32,85 @@ impl Ambient {
filter: Empty,
ctxt: Empty,
clock: Empty,
gen_id: Empty,
id_gen: Empty,
}
}
}

impl<TTarget, TFilter, TCtxt, TClock, TGenId> Ambient<TTarget, TFilter, TCtxt, TClock, TGenId> {
impl<TTarget, TFilter, TCtxt, TClock, TIdGen> Ambient<TTarget, TFilter, TCtxt, TClock, TIdGen> {
pub fn target(&self) -> &TTarget {
&self.target
}

pub fn with_target<U>(self, target: U) -> Ambient<U, TFilter, TCtxt, TClock, TGenId> {
pub fn with_target<U>(self, target: U) -> Ambient<U, TFilter, TCtxt, TClock, TIdGen> {
Ambient {
target,
filter: self.filter,
ctxt: self.ctxt,
clock: self.clock,
gen_id: self.gen_id,
id_gen: self.id_gen,
}
}

pub fn filter(&self) -> &TFilter {
&self.filter
}

pub fn with_filter<U>(self, filter: U) -> Ambient<TTarget, U, TCtxt, TClock, TGenId> {
pub fn with_filter<U>(self, filter: U) -> Ambient<TTarget, U, TCtxt, TClock, TIdGen> {
Ambient {
target: self.target,
filter,
ctxt: self.ctxt,
clock: self.clock,
gen_id: self.gen_id,
id_gen: self.id_gen,
}
}

pub fn ctxt(&self) -> &TCtxt {
&self.ctxt
}

pub fn with_ctxt<U>(self, ctxt: U) -> Ambient<TTarget, TFilter, U, TClock, TGenId> {
pub fn with_ctxt<U>(self, ctxt: U) -> Ambient<TTarget, TFilter, U, TClock, TIdGen> {
Ambient {
target: self.target,
filter: self.filter,
ctxt,
clock: self.clock,
gen_id: self.gen_id,
id_gen: self.id_gen,
}
}

pub fn clock(&self) -> &TClock {
&self.clock
}

pub fn with_clock<U>(self, clock: U) -> Ambient<TTarget, TFilter, TCtxt, U, TGenId> {
pub fn with_clock<U>(self, clock: U) -> Ambient<TTarget, TFilter, TCtxt, U, TIdGen> {
Ambient {
target: self.target,
filter: self.filter,
ctxt: self.ctxt,
clock,
gen_id: self.gen_id,
id_gen: self.id_gen,
}
}

pub fn gen_id(&self) -> &TGenId {
&self.gen_id
pub fn id_gen(&self) -> &TIdGen {
&self.id_gen
}

pub fn with_gen_id<U>(self, gen_id: U) -> Ambient<TTarget, TFilter, TCtxt, TClock, U> {
pub fn with_id_gen<U>(self, id_gen: U) -> Ambient<TTarget, TFilter, TCtxt, TClock, U> {
Ambient {
target: self.target,
filter: self.filter,
ctxt: self.ctxt,
clock: self.clock,
gen_id,
id_gen,
}
}
}

impl<TTarget: Target, TFilter, TCtxt, TClock, TGenId> Target
for Ambient<TTarget, TFilter, TCtxt, TClock, TGenId>
impl<TTarget: Target, TFilter, TCtxt, TClock, TIdGen> Target
for Ambient<TTarget, TFilter, TCtxt, TClock, TIdGen>
{
fn event<P: Props>(&self, evt: &Event<P>) {
self.target.event(evt)
Expand All @@ -121,16 +121,16 @@ impl<TTarget: Target, TFilter, TCtxt, TClock, TGenId> Target
}
}

impl<TTarget, TFilter: Filter, TCtxt, TClock, TGenId> Filter
for Ambient<TTarget, TFilter, TCtxt, TClock, TGenId>
impl<TTarget, TFilter: Filter, TCtxt, TClock, TIdGen> Filter
for Ambient<TTarget, TFilter, TCtxt, TClock, TIdGen>
{
fn matches<P: Props>(&self, evt: &Event<P>) -> bool {
self.filter.matches(evt)
}
}

impl<TTarget, TFilter, TCtxt: Ctxt, TClock, TGenId> Ctxt
for Ambient<TTarget, TFilter, TCtxt, TClock, TGenId>
impl<TTarget, TFilter, TCtxt: Ctxt, TClock, TIdGen> Ctxt
for Ambient<TTarget, TFilter, TCtxt, TClock, TIdGen>
{
type CurrentProps = TCtxt::CurrentProps;
type LocalFrame = TCtxt::LocalFrame;
Expand All @@ -156,29 +156,31 @@ impl<TTarget, TFilter, TCtxt: Ctxt, TClock, TGenId> Ctxt
}
}

impl<TTarget, TFilter, TCtxt, TClock: Clock, TGenId> Clock
for Ambient<TTarget, TFilter, TCtxt, TClock, TGenId>
impl<TTarget, TFilter, TCtxt, TClock: Clock, TIdGen> Clock
for Ambient<TTarget, TFilter, TCtxt, TClock, TIdGen>
{
fn now(&self) -> Option<Timestamp> {
self.clock.now()
}
}

impl<TTarget, TFilter, TCtxt, TClock, TGenId: IdGen> IdGen
for Ambient<TTarget, TFilter, TCtxt, TClock, TGenId>
impl<TTarget, TFilter, TCtxt, TClock, TIdGen: IdGen> IdGen
for Ambient<TTarget, TFilter, TCtxt, TClock, TIdGen>
{
fn new_trace_id(&self) -> Option<crate::id::TraceId> {
self.gen_id.new_trace_id()
self.id_gen.new_trace_id()
}

fn new_span_id(&self) -> Option<crate::id::SpanId> {
self.gen_id.new_span_id()
self.id_gen.new_span_id()
}
}

#[cfg(not(feature = "std"))]
pub fn get() -> Option<&'static Ambient<impl Target, impl Filter, impl Ctxt, impl Clock, impl IdGen>>
{
pub type Get = Option<&'static Ambient>;

#[cfg(not(feature = "std"))]
pub fn get() -> Get {
None::<&'static Ambient>
}

Expand Down Expand Up @@ -279,15 +281,15 @@ mod std_support {
>,
> = OnceLock::new();

pub fn init<TTarget, TFilter, TCtxt, TClock, TGenId>(
ambient: Ambient<TTarget, TFilter, TCtxt, TClock, TGenId>,
pub fn init<TTarget, TFilter, TCtxt, TClock, TIdGen>(
ambient: Ambient<TTarget, TFilter, TCtxt, TClock, TIdGen>,
) -> Option<
Ambient<
&'static TTarget,
&'static TFilter,
&'static TCtxt,
&'static TClock,
&'static TGenId,
&'static TIdGen,
>,
>
where
Expand All @@ -296,15 +298,15 @@ mod std_support {
TCtxt: Ctxt + Send + Sync + 'static,
TCtxt::LocalFrame: Send + 'static,
TClock: Clock + Send + Sync + 'static,
TGenId: IdGen + Send + Sync + 'static,
TIdGen: IdGen + Send + Sync + 'static,
{
AMBIENT
.set(Ambient {
target: Box::new(ambient.target),
filter: Box::new(ambient.filter),
ctxt: Box::new(ambient.ctxt),
clock: Box::new(ambient.clock),
gen_id: Box::new(ambient.gen_id),
id_gen: Box::new(ambient.id_gen),
})
.ok()?;

Expand All @@ -315,27 +317,29 @@ mod std_support {
filter: ambient.filter.as_any().downcast_ref()?,
ctxt: ambient.ctxt.as_any().downcast_ref()?,
clock: ambient.clock.as_any().downcast_ref()?,
gen_id: ambient.gen_id.as_any().downcast_ref()?,
id_gen: ambient.id_gen.as_any().downcast_ref()?,
})
}

pub fn get() -> Option<
pub type Get = Option<
Ambient<
&'static (dyn ErasedTarget + Send + Sync),
&'static (dyn ErasedFilter + Send + Sync),
&'static (dyn ErasedCtxt + Send + Sync),
&'static (dyn ErasedClock + Send + Sync),
&'static (dyn ErasedIdGen + Send + Sync),
>,
> {
>;

pub fn get() -> Get {
let ambient = AMBIENT.get()?;

Some(Ambient {
target: ambient.target.as_super(),
filter: ambient.filter.as_super(),
ctxt: ambient.ctxt.as_super(),
clock: ambient.clock.as_super(),
gen_id: ambient.gen_id.as_super(),
id_gen: ambient.id_gen.as_super(),
})
}
}
Expand Down
50 changes: 2 additions & 48 deletions core/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use core::{
};

use crate::{
empty::Empty,
extent::Extent,
key::{Key, ToKey},
props::{ByRef, Chain, ErasedProps, Props},
template::{Render, Template},
time::{Clock, Timer, Timestamp},
time::Timestamp,
value::{ToValue, Value},
well_known::{MSG_KEY, TPL_KEY, TSS_KEY, TS_KEY},
};
Expand All @@ -20,52 +20,6 @@ pub struct Event<'a, P> {
props: P,
}

pub trait Extent {
fn extent(&self) -> Option<Range<Timestamp>>;
}

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

impl Extent for Empty {
fn extent(&self) -> Option<Range<Timestamp>> {
None
}
}

impl<'a, P> Extent for Event<'a, P> {
fn extent(&self) -> Option<Range<Timestamp>> {
self.extent().cloned()
}
}

impl Extent for Timestamp {
fn extent(&self) -> Option<Range<Timestamp>> {
Some(*self..*self)
}
}

impl Extent for Range<Timestamp> {
fn extent(&self) -> Option<Range<Timestamp>> {
Some(self.clone())
}
}

impl<C: Clock> Extent for Timer<C> {
fn extent(&self) -> Option<Range<Timestamp>> {
self.extent()
}
}

impl<T: Extent> Extent for Option<T> {
fn extent(&self) -> Option<Range<Timestamp>> {
self.as_ref().and_then(|ts| ts.extent())
}
}

impl<'a, P: Props> fmt::Debug for Event<'a, P> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut f = f.debug_struct("Event");
Expand Down
52 changes: 52 additions & 0 deletions core/src/extent.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use crate::{
empty::Empty,
event::Event,
time::{Clock, Timer, Timestamp},
};
use core::ops::Range;

pub trait Extent {
fn extent(&self) -> Option<Range<Timestamp>>;
}

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

impl Extent for Empty {
fn extent(&self) -> Option<Range<Timestamp>> {
None
}
}

impl<'a, P> Extent for Event<'a, P> {
fn extent(&self) -> Option<Range<Timestamp>> {
self.extent().cloned()
}
}

impl Extent for Timestamp {
fn extent(&self) -> Option<Range<Timestamp>> {
Some(*self..*self)
}
}

impl Extent for Range<Timestamp> {
fn extent(&self) -> Option<Range<Timestamp>> {
Some(self.clone())
}
}

impl<C: Clock> Extent for Timer<C> {
fn extent(&self) -> Option<Range<Timestamp>> {
self.extent()
}
}

impl<T: Extent> Extent for Option<T> {
fn extent(&self) -> Option<Range<Timestamp>> {
self.as_ref().and_then(|ts| ts.extent())
}
}
Loading

0 comments on commit 36c3906

Please sign in to comment.