From ff8c0cc34165eca16c5ee067682570cc7f15f9b5 Mon Sep 17 00:00:00 2001 From: John-John Tedro Date: Sat, 16 Mar 2024 22:07:01 +0100 Subject: [PATCH] Remove Input associated type --- .../musli-common/src/context/alloc_context.rs | 43 ++++--------- crates/musli-common/src/context/mod.rs | 34 ----------- .../src/context/no_std_context.rs | 61 +++++++------------ crates/musli-common/src/macros.rs | 10 +-- crates/musli-json/src/encoding.rs | 4 +- crates/musli-serde/src/lib.rs | 10 --- crates/musli/src/context.rs | 17 ------ 7 files changed, 41 insertions(+), 138 deletions(-) diff --git a/crates/musli-common/src/context/alloc_context.rs b/crates/musli-common/src/context/alloc_context.rs index 16cad7931..cceda4edd 100644 --- a/crates/musli-common/src/context/alloc_context.rs +++ b/crates/musli-common/src/context/alloc_context.rs @@ -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 { +pub struct AllocContext { access: Access, mark: Cell, alloc: A, - errors: UnsafeCell>, Range, E)>>, + errors: UnsafeCell>, Range, String)>>, path: UnsafeCell>>, include_type: bool, _marker: PhantomData, } -impl AllocContext { +impl AllocContext { /// Construct a new context which uses allocations to store arbitrary /// amounts of diagnostics about decoding. /// @@ -49,7 +49,7 @@ impl AllocContext { } /// 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. @@ -61,12 +61,11 @@ impl AllocContext { } } -impl AllocContext +impl AllocContext where A: Allocator, - E: Error, { - fn push_error(&self, range: Range, message: E) { + fn push_error(&self, range: Range, message: String) { let _access = self.access.exclusive(); // SAFETY: We've restricted access to the context, so this is safe. @@ -95,13 +94,11 @@ where } } -impl Context for AllocContext +impl Context for AllocContext 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; @@ -111,30 +108,12 @@ where self.alloc.alloc() } - #[inline(always)] - fn report(&self, error: T) -> Self::Error - where - E: From, - { - self.push_error(self.mark.get()..self.mark.get(), E::from(error)); - ErrorMarker - } - - #[inline] - fn marked_report(&self, mark: Self::Mark, message: T) -> Self::Error - where - E: From, - { - self.push_error(mark..self.mark.get(), E::from(message)); - ErrorMarker - } - #[inline(always)] fn custom(&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 } @@ -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 } @@ -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 } diff --git a/crates/musli-common/src/context/mod.rs b/crates/musli-common/src/context/mod.rs index e2810caf4..af3053c81 100644 --- a/crates/musli-common/src/context/mod.rs +++ b/crates/musli-common/src/context/mod.rs @@ -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; @@ -82,14 +81,6 @@ where self.alloc.alloc() } - #[inline(always)] - fn report(&self, error: T) -> Self::Error - where - E: From, - { - E::from(error) - } - #[inline(always)] fn custom(&self, message: T) -> Self::Error where @@ -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; @@ -176,15 +166,6 @@ where self.alloc.alloc() } - #[inline(always)] - fn report(&self, _: T) -> ErrorMarker - where - E: From, - { - self.error.set(true); - ErrorMarker - } - #[inline(always)] fn custom(&self, _: T) -> ErrorMarker where @@ -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; @@ -247,20 +227,6 @@ where self.alloc.alloc() } - #[inline(always)] - fn report(&self, error: T) -> ErrorMarker - where - E: From, - { - // 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(&self, error: T) -> ErrorMarker where diff --git a/crates/musli-common/src/context/no_std_context.rs b/crates/musli-common/src/context/no_std_context.rs index 2ab3ca2fa..83d699aec 100644 --- a/crates/musli-common/src/context/no_std_context.rs +++ b/crates/musli-common/src/context/no_std_context.rs @@ -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; @@ -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. @@ -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 { +pub struct NoStdContext { access: Access, mark: Cell, alloc: A, - error: UnsafeCell, E)>>, + error: UnsafeCell, FixedString)>>, path: UnsafeCell>, P>>, path_cap: Cell, include_type: bool, _marker: PhantomData, } -impl NoStdContext<16, 32, A, M, E> { +impl NoStdContext<16, 32, A, M> { /// Construct a new context which uses allocations to a fixed number of /// diagnostics. /// @@ -43,7 +43,7 @@ impl NoStdContext<16, 32, A, M, E> { } } -impl NoStdContext { +impl NoStdContext { /// Construct a new context which uses allocations to a fixed but /// configurable number of diagnostics. pub fn new_with(alloc: A) -> Self { @@ -67,7 +67,7 @@ impl NoStdContext { } /// Iterate over all collected errors. - pub fn errors(&self) -> Errors<'_, S, E> { + pub fn errors(&self) -> Errors<'_, S> { let access = self.access.shared(); Errors { @@ -79,7 +79,7 @@ impl NoStdContext { } /// Push an error into the collection. - fn push_error(&self, range: Range, error: E) { + fn push_error(&self, range: Range, error: FixedString) { // SAFETY: We've restricted access to the context, so this is safe. unsafe { self.error.get().replace(Some((range, error))); @@ -116,13 +116,11 @@ impl NoStdContext { } } -impl Context for NoStdContext +impl Context for NoStdContext 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; @@ -132,30 +130,14 @@ where self.alloc.alloc() } - #[inline(always)] - fn report(&self, error: T) -> Self::Error - where - E: From, - { - self.push_error(self.mark.get()..self.mark.get(), E::from(error)); - ErrorMarker - } - - #[inline] - fn marked_report(&self, mark: Self::Mark, message: T) -> Self::Error - where - E: From, - { - self.push_error(mark..self.mark.get(), E::from(message)); - ErrorMarker - } - #[inline(always)] fn custom(&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 } @@ -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 } @@ -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 } @@ -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)); } @@ -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>], - error: Option<&'a (Range, E)>, + error: Option<&'a (Range, FixedString)>, path_cap: usize, _access: Shared<'a>, } -impl<'a, const S: usize, E: 'a> Iterator for Errors<'a, S, E> { - type Item = RichError<'a, FixedString, E>; +impl<'a, const S: usize> Iterator for Errors<'a, S> { + type Item = RichError<'a, FixedString, FixedString>; #[inline] fn next(&mut self) -> Option { diff --git a/crates/musli-common/src/macros.rs b/crates/musli-common/src/macros.rs index 12921ecf9..2b5209cc1 100644 --- a/crates/musli-common/src/macros.rs +++ b/crates/musli-common/src/macros.rs @@ -50,7 +50,7 @@ macro_rules! encode_with_extensions { #[inline] pub fn to_vec_with(self, cx: &C, value: &T) -> Result, C::Error> where - C: Context, + C: Context, T: ?Sized + Encode<$mode>, { let mut vec = Vec::new(); @@ -80,7 +80,7 @@ macro_rules! encode_with_extensions { value: &T, ) -> Result, C::Error> where - C: Context, + C: Context, T: ?Sized + Encode<$mode>, { let mut bytes = FixedBytes::new(); @@ -116,7 +116,7 @@ macro_rules! encoding_from_slice_impls { #[inline] pub fn from_slice_with<'de, C, T>(self, cx: &C, bytes: &'de [u8]) -> Result where - C: Context, + C: Context, T: Decode<'de, $mode>, { let reader = SliceReader::new(bytes); @@ -138,7 +138,7 @@ macro_rules! encoding_impls { #[inline] pub fn encode_with(self, cx: &C, writer: W, value: &T) -> Result<(), C::Error> where - C: Context, + C: Context, W: Writer, T: ?Sized + Encode<$mode>, { @@ -153,7 +153,7 @@ macro_rules! encoding_impls { #[inline] pub fn decode_with<'de, C, R, T>(self, cx: &C, reader: R) -> Result where - C: Context, + C: Context, R: Reader<'de>, T: Decode<'de, $mode>, { diff --git a/crates/musli-json/src/encoding.rs b/crates/musli-json/src/encoding.rs index 33bff568a..422df733d 100644 --- a/crates/musli-json/src/encoding.rs +++ b/crates/musli-json/src/encoding.rs @@ -258,7 +258,7 @@ impl Encoding { #[inline] pub fn from_str_with<'de, C, T>(self, cx: &C, string: &'de str) -> Result where - C: Context, + C: Context, T: Decode<'de, M>, { self.from_slice_with(cx, string.as_bytes()) @@ -285,7 +285,7 @@ impl Encoding { #[inline] pub fn from_slice_with<'de, C, T>(self, cx: &C, bytes: &'de [u8]) -> Result where - C: Context, + C: Context, T: Decode<'de, M>, { let mut reader = SliceParser::new(bytes); diff --git a/crates/musli-serde/src/lib.rs b/crates/musli-serde/src/lib.rs index 177b5cf1d..9502dd086 100644 --- a/crates/musli-serde/src/lib.rs +++ b/crates/musli-serde/src/lib.rs @@ -64,7 +64,6 @@ where C: Context, { type Mode = C::Mode; - type Input = C::Input; type Error = error::SerdeError; type Mark = C::Mark; @@ -82,15 +81,6 @@ where self.inner.alloc() } - #[inline] - fn report(&self, error: T) -> Self::Error - where - Self::Input: From, - { - *self.error.borrow_mut() = Some(self.inner.report(error)); - error::SerdeError::Captured - } - #[inline] fn custom(&self, error: T) -> Self::Error where diff --git a/crates/musli/src/context.rs b/crates/musli/src/context.rs index 76ce43d11..4e19cc757 100644 --- a/crates/musli/src/context.rs +++ b/crates/musli/src/context.rs @@ -10,8 +10,6 @@ use crate::{Buf, Decode, Decoder}; pub trait Context { /// Mode of the context. type Mode; - /// The error type which is collected by the context. - type Input: 'static; /// Error produced by context. type Error: 'static; /// A mark during processing. @@ -34,11 +32,6 @@ pub trait Context { /// Allocate a buffer. fn alloc(&self) -> Option>; - /// Report the given context error. - fn report(&self, error: T) -> Self::Error - where - Self::Input: From; - /// Generate a map function which maps an error using the `report` function. fn map(&self) -> impl FnOnce(T) -> Self::Error + '_ where @@ -62,16 +55,6 @@ pub trait Context { where T: fmt::Display; - /// Report the given encoding error from the given mark. - #[allow(unused_variables)] - #[inline(always)] - fn marked_report(&self, mark: Self::Mark, error: T) -> Self::Error - where - Self::Input: From, - { - self.report(error) - } - /// Report an error based on a mark. /// /// A mark is generated using [Context::mark] and indicates a prior state.