Skip to content

Commit

Permalink
Convert trace config to marker types
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Jan 5, 2025
1 parent 6b12a0b commit 2d3ea44
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 70 deletions.
59 changes: 29 additions & 30 deletions crates/musli/src/context/default_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,36 @@ use core::fmt;
use crate::alloc::System;
use crate::{Allocator, Context};

use super::{ErrorMarker, Errors, NoTrace, Report, Trace, WithTrace};
use super::{ErrorMarker, Errors, NoTrace, Report, Trace, TraceConfig, TraceImpl};

/// The default context which uses an allocator to track the location of errors.
///
/// This uses the provided allocator to allocate memory for the collected
/// diagnostics. The allocator to use can be provided using [`new_in`].
/// This is typically constructed using [`new`] and by default uses the
/// [`System`] allocator to allocate memory. To customized the allocator to use
/// [`new_in`] can be used during construction.
///
/// The default constructor is only available when the `alloc` feature is
/// enabled, and will use the [`System`] allocator.
///
/// [`new`]: super::new
/// [`new_in`]: super::new_in
pub struct DefaultContext<A, T>
pub struct DefaultContext<A, B>
where
A: Allocator,
T: Trace<A>,
A: Clone + Allocator,
B: TraceConfig,
{
alloc: A,
trace: T,
trace: B::Impl<A>,
}

#[cfg(feature = "alloc")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
impl DefaultContext<System, NoTrace> {
/// Construct a new fully featured context which uses the [`System`]
/// allocator for memory.
///
/// [`System`]: crate::alloc::System
/// Construct the default context which uses the [`System`] allocator for
/// memory.
#[inline]
pub fn new() -> Self {
Self::new_in(crate::alloc::System::new())
Self::new_in(System::new())
}
}

Expand All @@ -53,10 +53,9 @@ where
/// configurable number of diagnostics.
#[inline]
pub(super) fn new_in(alloc: A) -> Self {
Self {
alloc,
trace: NoTrace::new(),
}
let trace = NoTrace::new_in(alloc.clone());

Self { alloc, trace }
}

/// Unwrap the error marker or panic if there is no error.
Expand All @@ -66,7 +65,7 @@ where
}
}

impl<A> DefaultContext<A, WithTrace<A>>
impl<A> DefaultContext<A, Trace>
where
A: Clone + Allocator,
{
Expand Down Expand Up @@ -98,7 +97,7 @@ where
impl<A, B> DefaultContext<A, B>
where
A: Clone + Allocator,
B: Trace<A>,
B: TraceConfig,
{
/// Enable tracing through the current allocator `A`.
///
Expand All @@ -112,8 +111,8 @@ where
/// [`errors`]: DefaultContext::errors
/// [`Disabled`]: crate::alloc::Disabled
#[inline]
pub fn with_trace(self) -> DefaultContext<A, WithTrace<A>> {
let trace = WithTrace::new_in(self.alloc.clone());
pub fn with_trace(self) -> DefaultContext<A, Trace> {
let trace = Trace::new_in(self.alloc.clone());

DefaultContext {
alloc: self.alloc,
Expand All @@ -125,10 +124,10 @@ where
impl<A, B> Context for &DefaultContext<A, B>
where
A: Clone + Allocator,
B: Trace<A>,
B: TraceConfig,
{
type Error = ErrorMarker;
type Mark = B::Mark;
type Mark = <<B as TraceConfig>::Impl<A> as TraceImpl>::Mark;
type Allocator = A;

#[inline]
Expand All @@ -146,7 +145,7 @@ where
where
T: 'static + Send + Sync + fmt::Display + fmt::Debug,
{
self.trace.custom(&self.alloc, message);
self.trace.custom(&self.alloc, &message);
ErrorMarker
}

Expand All @@ -155,7 +154,7 @@ where
where
T: fmt::Display,
{
self.trace.message(&self.alloc, message);
self.trace.message(&self.alloc, &message);
ErrorMarker
}

Expand All @@ -164,7 +163,7 @@ where
where
T: fmt::Display,
{
self.trace.marked_message(&self.alloc, mark, message);
self.trace.marked_message(&self.alloc, mark, &message);
ErrorMarker
}

Expand All @@ -173,7 +172,7 @@ where
where
T: 'static + Send + Sync + fmt::Display + fmt::Debug,
{
self.trace.marked_custom(&self.alloc, mark, message);
self.trace.marked_custom(&self.alloc, mark, &message);
ErrorMarker
}

Expand All @@ -192,15 +191,15 @@ where
where
T: fmt::Display,
{
self.trace.enter_named_field(name, field);
self.trace.enter_named_field(name, &field);
}

#[inline]
fn enter_unnamed_field<T>(self, index: u32, name: T)
where
T: fmt::Display,
{
self.trace.enter_unnamed_field(index, name);
self.trace.enter_unnamed_field(index, &name);
}

#[inline]
Expand Down Expand Up @@ -233,7 +232,7 @@ where
where
T: fmt::Display,
{
self.trace.enter_variant(name, tag);
self.trace.enter_variant(name, &tag);
}

#[inline]
Expand All @@ -256,7 +255,7 @@ where
where
T: fmt::Display,
{
self.trace.enter_map_key(&self.alloc, field);
self.trace.enter_map_key(&self.alloc, &field);
}

#[inline]
Expand Down
9 changes: 4 additions & 5 deletions crates/musli/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use self::access::{Access, Shared};

mod trace;
#[doc(inline)]
pub use self::trace::{Error, Errors, NoTrace, Report, Trace, WithTrace};
pub use self::trace::{Error, Errors, NoTrace, Report, Trace, TraceConfig, TraceImpl};

mod error_marker;
#[doc(inline)]
Expand Down Expand Up @@ -68,17 +68,15 @@ use crate::Allocator;
/// assert_eq!(string, "42");
/// # Ok::<_, musli::context::ErrorMarker>(())
/// ```
///
#[inline]
pub fn new_in<A>(alloc: A) -> DefaultContext<A, NoTrace>
where
A: Clone + Allocator,
{
DefaultContext::new_in(alloc)
}

/// Construct a new default context using the static [`System`] allocator.
///
/// [`System`]: crate::alloc::System
/// Construct a new default context using the [`System`] allocator.
///
/// # Examples
///
Expand All @@ -96,6 +94,7 @@ where
/// ```
#[cfg(feature = "alloc")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
#[inline]
pub fn new() -> DefaultContext<System, NoTrace> {
DefaultContext::new()
}
Loading

0 comments on commit 2d3ea44

Please sign in to comment.