Skip to content

Commit

Permalink
Remove Input associated type
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Mar 16, 2024
1 parent 911efec commit ff8c0cc
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 138 deletions.
43 changes: 11 additions & 32 deletions crates/musli-common/src/context/alloc_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ use musli::{Allocator, Context};

use super::access::{self, Access};
use super::rich_error::{RichError, Step};
use super::{Error, ErrorMarker};
use super::ErrorMarker;

/// A rich context which uses allocations and tracks the exact location of every
/// error.
pub struct AllocContext<A, M, E> {
pub struct AllocContext<A, M> {
access: Access,
mark: Cell<usize>,
alloc: A,
errors: UnsafeCell<Vec<(Vec<Step<String>>, Range<usize>, E)>>,
errors: UnsafeCell<Vec<(Vec<Step<String>>, Range<usize>, String)>>,
path: UnsafeCell<Vec<Step<String>>>,
include_type: bool,
_marker: PhantomData<M>,
}

impl<A, M, E> AllocContext<A, M, E> {
impl<A, M> AllocContext<A, M> {
/// Construct a new context which uses allocations to store arbitrary
/// amounts of diagnostics about decoding.
///
Expand All @@ -49,7 +49,7 @@ impl<A, M, E> AllocContext<A, M, E> {
}

/// Iterate over all collected errors.
pub fn errors(&self) -> Errors<'_, E> {
pub fn errors(&self) -> Errors<'_, String> {
let access = self.access.shared();

// SAFETY: We've checked above that we have shared access.
Expand All @@ -61,12 +61,11 @@ impl<A, M, E> AllocContext<A, M, E> {
}
}

impl<A, M, E> AllocContext<A, M, E>
impl<A, M> AllocContext<A, M>
where
A: Allocator,
E: Error,
{
fn push_error(&self, range: Range<usize>, message: E) {
fn push_error(&self, range: Range<usize>, message: String) {
let _access = self.access.exclusive();

// SAFETY: We've restricted access to the context, so this is safe.
Expand Down Expand Up @@ -95,13 +94,11 @@ where
}
}

impl<A, M, E> Context for AllocContext<A, M, E>
impl<A, M> Context for AllocContext<A, M>
where
A: Allocator,
E: Error,
{
type Mode = M;
type Input = E;
type Error = ErrorMarker;
type Mark = usize;
type Buf<'this> = A::Buf<'this> where Self: 'this;
Expand All @@ -111,30 +108,12 @@ where
self.alloc.alloc()
}

#[inline(always)]
fn report<T>(&self, error: T) -> Self::Error
where
E: From<T>,
{
self.push_error(self.mark.get()..self.mark.get(), E::from(error));
ErrorMarker
}

#[inline]
fn marked_report<T>(&self, mark: Self::Mark, message: T) -> Self::Error
where
E: From<T>,
{
self.push_error(mark..self.mark.get(), E::from(message));
ErrorMarker
}

#[inline(always)]
fn custom<T>(&self, message: T) -> Self::Error
where
T: 'static + Send + Sync + fmt::Display + fmt::Debug,
{
self.push_error(self.mark.get()..self.mark.get(), E::custom(message));
self.push_error(self.mark.get()..self.mark.get(), message.to_string());
ErrorMarker
}

Expand All @@ -143,7 +122,7 @@ where
where
T: fmt::Display,
{
self.push_error(self.mark.get()..self.mark.get(), E::message(message));
self.push_error(self.mark.get()..self.mark.get(), message.to_string());
ErrorMarker
}

Expand All @@ -152,7 +131,7 @@ where
where
T: fmt::Display,
{
self.push_error(mark..self.mark.get(), E::message(message));
self.push_error(mark..self.mark.get(), message.to_string());
ErrorMarker
}

Expand Down
34 changes: 0 additions & 34 deletions crates/musli-common/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ where
E: Error,
{
type Mode = M;
type Input = E;
type Error = E;
type Mark = ();
type Buf<'this> = A::Buf<'this> where Self: 'this;
Expand All @@ -82,14 +81,6 @@ where
self.alloc.alloc()
}

#[inline(always)]
fn report<T>(&self, error: T) -> Self::Error
where
E: From<T>,
{
E::from(error)
}

#[inline(always)]
fn custom<T>(&self, message: T) -> Self::Error
where
Expand Down Expand Up @@ -166,7 +157,6 @@ where
A: Allocator,
{
type Mode = M;
type Input = E;
type Error = ErrorMarker;
type Mark = ();
type Buf<'this> = A::Buf<'this> where Self: 'this;
Expand All @@ -176,15 +166,6 @@ where
self.alloc.alloc()
}

#[inline(always)]
fn report<T>(&self, _: T) -> ErrorMarker
where
E: From<T>,
{
self.error.set(true);
ErrorMarker
}

#[inline(always)]
fn custom<T>(&self, _: T) -> ErrorMarker
where
Expand Down Expand Up @@ -237,7 +218,6 @@ where
E: Error,
{
type Mode = M;
type Input = E;
type Error = ErrorMarker;
type Mark = ();
type Buf<'this> = A::Buf<'this> where Self: 'this;
Expand All @@ -247,20 +227,6 @@ where
self.alloc.alloc()
}

#[inline(always)]
fn report<T>(&self, error: T) -> ErrorMarker
where
E: From<T>,
{
// SAFETY: We're restricting access to the context, so that this is
// safe.
unsafe {
self.error.get().replace(Some(E::from(error)));
}

ErrorMarker
}

#[inline(always)]
fn custom<T>(&self, error: T) -> ErrorMarker
where
Expand Down
61 changes: 23 additions & 38 deletions crates/musli-common/src/context/no_std_context.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use core::cell::{Cell, UnsafeCell};
use core::fmt;
use core::fmt::{self, Write};
use core::marker::PhantomData;
use core::ops::Range;

Expand All @@ -9,7 +9,7 @@ use crate::fixed::{FixedString, FixedVec};

use super::access::{Access, Shared};
use super::rich_error::{RichError, Step};
use super::{Error, ErrorMarker};
use super::ErrorMarker;

/// A rich context which uses allocations and tracks the exact location of every
/// error.
Expand All @@ -20,18 +20,18 @@ use super::{Error, ErrorMarker};
/// indicator is used instead.
/// * The `S` parameter indicates the maximum size in bytes (UTF-8) of a stored
/// map key.
pub struct NoStdContext<const P: usize, const S: usize, A, M, E> {
pub struct NoStdContext<const P: usize, const S: usize, A, M> {
access: Access,
mark: Cell<usize>,
alloc: A,
error: UnsafeCell<Option<(Range<usize>, E)>>,
error: UnsafeCell<Option<(Range<usize>, FixedString<S>)>>,
path: UnsafeCell<FixedVec<Step<FixedString<S>>, P>>,
path_cap: Cell<usize>,
include_type: bool,
_marker: PhantomData<M>,
}

impl<A, M, E> NoStdContext<16, 32, A, M, E> {
impl<A, M> NoStdContext<16, 32, A, M> {
/// Construct a new context which uses allocations to a fixed number of
/// diagnostics.
///
Expand All @@ -43,7 +43,7 @@ impl<A, M, E> NoStdContext<16, 32, A, M, E> {
}
}

impl<const P: usize, const S: usize, A, M, E> NoStdContext<P, S, A, M, E> {
impl<const P: usize, const S: usize, A, M> NoStdContext<P, S, A, M> {
/// Construct a new context which uses allocations to a fixed but
/// configurable number of diagnostics.
pub fn new_with(alloc: A) -> Self {
Expand All @@ -67,7 +67,7 @@ impl<const P: usize, const S: usize, A, M, E> NoStdContext<P, S, A, M, E> {
}

/// Iterate over all collected errors.
pub fn errors(&self) -> Errors<'_, S, E> {
pub fn errors(&self) -> Errors<'_, S> {
let access = self.access.shared();

Errors {
Expand All @@ -79,7 +79,7 @@ impl<const P: usize, const S: usize, A, M, E> NoStdContext<P, S, A, M, E> {
}

/// Push an error into the collection.
fn push_error(&self, range: Range<usize>, error: E) {
fn push_error(&self, range: Range<usize>, error: FixedString<S>) {
// SAFETY: We've restricted access to the context, so this is safe.
unsafe {
self.error.get().replace(Some((range, error)));
Expand Down Expand Up @@ -116,13 +116,11 @@ impl<const P: usize, const S: usize, A, M, E> NoStdContext<P, S, A, M, E> {
}
}

impl<const V: usize, const S: usize, A, M, E> Context for NoStdContext<V, S, A, M, E>
impl<const V: usize, const S: usize, A, M> Context for NoStdContext<V, S, A, M>
where
A: Allocator,
E: Error,
{
type Mode = M;
type Input = E;
type Error = ErrorMarker;
type Mark = usize;
type Buf<'this> = A::Buf<'this> where Self: 'this;
Expand All @@ -132,30 +130,14 @@ where
self.alloc.alloc()
}

#[inline(always)]
fn report<T>(&self, error: T) -> Self::Error
where
E: From<T>,
{
self.push_error(self.mark.get()..self.mark.get(), E::from(error));
ErrorMarker
}

#[inline]
fn marked_report<T>(&self, mark: Self::Mark, message: T) -> Self::Error
where
E: From<T>,
{
self.push_error(mark..self.mark.get(), E::from(message));
ErrorMarker
}

#[inline(always)]
fn custom<T>(&self, message: T) -> Self::Error
where
T: 'static + Send + Sync + fmt::Display + fmt::Debug,
{
self.push_error(self.mark.get()..self.mark.get(), E::custom(message));
let mut s = FixedString::new();
_ = write!(s, "{message}");
self.push_error(self.mark.get()..self.mark.get(), s);
ErrorMarker
}

Expand All @@ -164,7 +146,9 @@ where
where
T: fmt::Display,
{
self.push_error(self.mark.get()..self.mark.get(), E::message(message));
let mut s = FixedString::new();
_ = write!(s, "{message}");
self.push_error(self.mark.get()..self.mark.get(), s);
ErrorMarker
}

Expand All @@ -173,7 +157,9 @@ where
where
T: fmt::Display,
{
self.push_error(mark..self.mark.get(), E::message(message));
let mut s = FixedString::new();
_ = write!(s, "{message}");
self.push_error(mark..self.mark.get(), s);
ErrorMarker
}

Expand Down Expand Up @@ -261,9 +247,8 @@ where
where
T: fmt::Display,
{
use core::fmt::Write;
let mut string = FixedString::new();
let _ = write!(string, "{}", field);
_ = write!(string, "{}", field);
self.push_path(Step::Key(string));
}

Expand All @@ -274,15 +259,15 @@ where
}

/// An iterator over available errors.
pub struct Errors<'a, const S: usize, E> {
pub struct Errors<'a, const S: usize> {
path: &'a [Step<FixedString<S>>],
error: Option<&'a (Range<usize>, E)>,
error: Option<&'a (Range<usize>, FixedString<S>)>,
path_cap: usize,
_access: Shared<'a>,
}

impl<'a, const S: usize, E: 'a> Iterator for Errors<'a, S, E> {
type Item = RichError<'a, FixedString<S>, E>;
impl<'a, const S: usize> Iterator for Errors<'a, S> {
type Item = RichError<'a, FixedString<S>, FixedString<S>>;

#[inline]
fn next(&mut self) -> Option<Self::Item> {
Expand Down
Loading

0 comments on commit ff8c0cc

Please sign in to comment.