From c5ea51f7406a8fe51f642937b0956800f337d95c Mon Sep 17 00:00:00 2001 From: John-John Tedro Date: Wed, 1 Jan 2025 10:54:32 +0100 Subject: [PATCH] Simplify how Context is passed around --- crates/musli-core/src/context.rs | 86 ++-- .../musli-core/src/de/decode_slice_builder.rs | 16 +- crates/musli-core/src/de/decoder.rs | 458 ++++++++---------- crates/musli-core/src/de/unsized_visitor.rs | 23 +- crates/musli-core/src/de/utils.rs | 27 +- crates/musli-core/src/de/visitor.rs | 72 +-- crates/musli-core/src/en/encoder.rs | 333 +++++-------- crates/musli-core/src/en/sequence_encoder.rs | 11 +- crates/musli-core/src/en/utils.rs | 95 ++-- crates/musli-core/src/impls/alloc.rs | 298 ++++++------ crates/musli-core/src/impls/mod.rs | 111 +++-- crates/musli-core/src/impls/net.rs | 20 +- crates/musli-core/src/lib.rs | 34 +- crates/musli-core/src/never.rs | 71 ++- crates/musli-macros/src/de.rs | 16 +- crates/musli-macros/src/en.rs | 18 +- crates/musli-macros/src/types.rs | 11 +- crates/musli/src/context/capture.rs | 16 +- crates/musli/src/context/default_context.rs | 46 +- crates/musli/src/context/ignore.rs | 16 +- crates/musli/src/context/same.rs | 16 +- crates/musli/src/descriptive/de.rs | 109 ++--- crates/musli/src/descriptive/en.rs | 101 ++-- .../musli/src/descriptive/integer_encoding.rs | 24 +- crates/musli/src/fixed.rs | 16 +- crates/musli/src/int/continuation.rs | 8 +- crates/musli/src/int/encoding.rs | 24 +- crates/musli/src/int/traits.rs | 16 +- crates/musli/src/json/de/key_decoder.rs | 29 +- .../musli/src/json/de/key_signed_visitor.rs | 4 +- .../musli/src/json/de/key_unsigned_visitor.rs | 4 +- crates/musli/src/json/de/mod.rs | 43 +- crates/musli/src/json/de/object_decoder.rs | 28 +- .../musli/src/json/de/object_pair_decoder.rs | 16 +- crates/musli/src/json/de/sequence_decoder.rs | 16 +- crates/musli/src/json/de/variant_decoder.rs | 18 +- crates/musli/src/json/en/array_encoder.rs | 25 +- crates/musli/src/json/en/mod.rs | 49 +- crates/musli/src/json/en/object_encoder.rs | 26 +- .../musli/src/json/en/object_key_encoder.rs | 27 +- .../musli/src/json/en/object_pair_encoder.rs | 16 +- crates/musli/src/json/en/variant_encoder.rs | 18 +- crates/musli/src/json/encoding.rs | 4 +- crates/musli/src/json/parser/integer.rs | 52 +- .../musli/src/json/parser/mut_slice_parser.rs | 28 +- crates/musli/src/json/parser/parser.rs | 84 ++-- crates/musli/src/json/parser/slice_parser.rs | 28 +- crates/musli/src/json/parser/string.rs | 13 +- crates/musli/src/lib.rs | 34 +- crates/musli/src/macros/internal.rs | 32 +- crates/musli/src/reader.rs | 160 +++--- crates/musli/src/serde/deserializer.rs | 207 ++++---- crates/musli/src/serde/error.rs | 4 +- crates/musli/src/serde/mod.rs | 100 ++-- crates/musli/src/serde/serializer.rs | 18 +- crates/musli/src/storage/de.rs | 95 ++-- crates/musli/src/storage/en.rs | 88 ++-- crates/musli/src/value/de.rs | 124 ++--- crates/musli/src/value/en.rs | 174 ++++--- crates/musli/src/value/mod.rs | 4 +- crates/musli/src/value/value.rs | 88 ++-- crates/musli/src/wire/de.rs | 111 ++--- crates/musli/src/wire/en.rs | 107 ++-- crates/musli/src/wire/int.rs | 24 +- crates/musli/src/wrap.rs | 12 +- crates/musli/src/writer.rs | 60 +-- crates/musli/src/writer/slice_mut_writer.rs | 16 +- crates/musli/tests/ui/visitor_attribute_ok.rs | 2 +- crates/musli/tests/visitors.rs | 22 +- tests/benches/comparison.rs | 4 +- 70 files changed, 1916 insertions(+), 2190 deletions(-) diff --git a/crates/musli-core/src/context.rs b/crates/musli-core/src/context.rs index 7697260b9..af2e98e2d 100644 --- a/crates/musli-core/src/context.rs +++ b/crates/musli-core/src/context.rs @@ -9,7 +9,7 @@ use crate::alloc::Allocator; /// Provides ergonomic access to the serialization context. /// /// This is used to among other things report diagnostics. -pub trait Context { +pub trait Context: Copy { /// Mode of the context. type Mode: 'static; /// Error produced by context. @@ -22,13 +22,13 @@ pub trait Context { type String: AsRef; /// Clear the state of the context, allowing it to be re-used. - fn clear(&self); + fn clear(self); /// Advance the context by `n` bytes of input. /// /// This is typically used to move the mark forward as produced by /// [Context::mark]. - fn advance(&self, n: usize); + fn advance(self, n: usize); /// Return a mark which acts as a checkpoint at the current encoding state. /// @@ -37,21 +37,21 @@ pub trait Context { /// /// This typically indicates a byte offset, and is used by /// [`marked_message`][Context::marked_message] to report a spanned error. - fn mark(&self) -> Self::Mark; + fn mark(self) -> Self::Mark; /// Access the underlying allocator. - fn alloc(&self) -> Self::Allocator; + fn alloc(self) -> Self::Allocator; /// Collect and allocate a string from a [`Display`] implementation. /// /// [`Display`]: fmt::Display - fn collect_string(&self, value: &T) -> Result + fn collect_string(self, value: &T) -> Result where T: ?Sized + fmt::Display; /// Generate a map function which maps an error using the `custom` function. #[inline] - fn map(&self) -> impl FnOnce(T) -> Self::Error + '_ + fn map(self) -> impl FnOnce(T) -> Self::Error where T: 'static + Send + Sync + Error, { @@ -61,7 +61,7 @@ pub trait Context { /// Report a custom error, which is not encapsulated by the error type /// expected by the context. This is essentially a type-erased way of /// reporting error-like things out from the context. - fn custom(&self, error: T) -> Self::Error + fn custom(self, error: T) -> Self::Error where T: 'static + Send + Sync + Error; @@ -69,7 +69,7 @@ pub trait Context { /// /// This is made available to format custom error messages in `no_std` /// environments. The error message is to be collected by formatting `T`. - fn message(&self, message: T) -> Self::Error + fn message(self, message: T) -> Self::Error where T: fmt::Display; @@ -78,7 +78,7 @@ pub trait Context { /// A mark is generated using [Context::mark] and indicates a prior state. #[allow(unused_variables)] #[inline(always)] - fn marked_message(&self, mark: &Self::Mark, message: T) -> Self::Error + fn marked_message(self, mark: &Self::Mark, message: T) -> Self::Error where T: fmt::Display, { @@ -90,7 +90,7 @@ pub trait Context { /// A mark is generated using [Context::mark] and indicates a prior state. #[allow(unused_variables)] #[inline(always)] - fn marked_custom(&self, mark: &Self::Mark, message: T) -> Self::Error + fn marked_custom(self, mark: &Self::Mark, message: T) -> Self::Error where T: 'static + Send + Sync + Error, { @@ -99,7 +99,7 @@ pub trait Context { /// Report that an invalid variant tag was encountered. #[inline(always)] - fn invalid_variant_tag(&self, type_name: &'static str, tag: &T) -> Self::Error + fn invalid_variant_tag(self, type_name: &'static str, tag: &T) -> Self::Error where T: ?Sized + fmt::Debug, { @@ -110,7 +110,7 @@ pub trait Context { /// The value for the given tag could not be collected. #[inline(always)] - fn expected_tag(&self, type_name: &'static str, tag: &T) -> Self::Error + fn expected_tag(self, type_name: &'static str, tag: &T) -> Self::Error where T: ?Sized + fmt::Debug, { @@ -119,7 +119,7 @@ pub trait Context { /// Trying to decode an uninhabitable type. #[inline(always)] - fn uninhabitable(&self, type_name: &'static str) -> Self::Error { + fn uninhabitable(self, type_name: &'static str) -> Self::Error { self.message(format_args!( "Type {type_name} cannot be decoded since it's uninhabitable" )) @@ -127,7 +127,7 @@ pub trait Context { /// Encountered an unsupported field tag. #[inline(always)] - fn invalid_field_tag(&self, type_name: &'static str, tag: &T) -> Self::Error + fn invalid_field_tag(self, type_name: &'static str, tag: &T) -> Self::Error where T: ?Sized + fmt::Debug, { @@ -139,7 +139,7 @@ pub trait Context { /// Expected another field to decode. #[inline(always)] fn expected_field_adjacent( - &self, + self, type_name: &'static str, tag: &T, content: &C, @@ -155,7 +155,7 @@ pub trait Context { /// Missing adjacent tag when decoding. #[inline(always)] - fn missing_adjacent_tag(&self, type_name: &'static str, tag: &T) -> Self::Error + fn missing_adjacent_tag(self, type_name: &'static str, tag: &T) -> Self::Error where T: ?Sized + fmt::Debug, { @@ -166,11 +166,7 @@ pub trait Context { /// Encountered an unsupported field tag. #[inline(always)] - fn invalid_field_string_tag( - &self, - type_name: &'static str, - field: Self::String, - ) -> Self::Error { + fn invalid_field_string_tag(self, type_name: &'static str, field: Self::String) -> Self::Error { let field = field.as_ref(); self.message(format_args!( @@ -179,9 +175,8 @@ pub trait Context { } /// Missing variant field required to decode. - #[allow(unused_variables)] #[inline(always)] - fn missing_variant_field(&self, type_name: &'static str, tag: &T) -> Self::Error + fn missing_variant_field(self, type_name: &'static str, tag: &T) -> Self::Error where T: ?Sized + fmt::Debug, { @@ -192,15 +187,14 @@ pub trait Context { /// Indicate that a variant tag could not be determined. #[inline(always)] - fn missing_variant_tag(&self, type_name: &'static str) -> Self::Error { + fn missing_variant_tag(self, type_name: &'static str) -> Self::Error { self.message(format_args!("Type {type_name} is missing variant tag")) } /// Encountered an unsupported variant field. - #[allow(unused_variables)] #[inline(always)] fn invalid_variant_field_tag( - &self, + self, type_name: &'static str, variant: &V, tag: &T, @@ -216,7 +210,7 @@ pub trait Context { /// Missing variant field required to decode. #[inline(always)] - fn alloc_failed(&self) -> Self::Error { + fn alloc_failed(self) -> Self::Error { self.message("Failed to allocate") } @@ -228,13 +222,13 @@ pub trait Context { /// /// [`leave_struct`]: Context::leave_struct #[inline(always)] - fn enter_struct(&self, type_name: &'static str) { + fn enter_struct(self, type_name: &'static str) { _ = type_name; } /// Trace that we've left the last struct that was entered. #[inline(always)] - fn leave_struct(&self) {} + fn leave_struct(self) {} /// Indicate that we've entered an enum with the given `name`. /// @@ -244,13 +238,13 @@ pub trait Context { /// /// [`leave_enum`]: Context::leave_enum #[inline(always)] - fn enter_enum(&self, type_name: &'static str) { + fn enter_enum(self, type_name: &'static str) { _ = type_name; } /// Trace that we've left the last enum that was entered. #[inline(always)] - fn leave_enum(&self) {} + fn leave_enum(self) {} /// Trace that we've entered the given named field. /// @@ -275,7 +269,7 @@ pub trait Context { /// /// [`leave_field`]: Context::leave_field #[inline(always)] - fn enter_named_field(&self, type_name: &'static str, tag: &T) + fn enter_named_field(self, type_name: &'static str, tag: &T) where T: ?Sized + fmt::Display, { @@ -302,12 +296,13 @@ pub trait Context { /// ``` /// /// [`leave_field`]: Context::leave_field - #[allow(unused_variables)] #[inline(always)] - fn enter_unnamed_field(&self, index: u32, name: &T) + fn enter_unnamed_field(self, index: u32, name: &T) where T: ?Sized + fmt::Display, { + _ = index; + _ = name; } /// Trace that we've left the last field that was entered. @@ -317,9 +312,8 @@ pub trait Context { /// /// [`enter_named_field`]: Context::enter_named_field /// [`enter_unnamed_field`]: Context::enter_unnamed_field - #[allow(unused_variables)] #[inline(always)] - fn leave_field(&self) {} + fn leave_field(self) {} /// Trace that we've entered the given variant in an enum. /// @@ -345,12 +339,13 @@ pub trait Context { /// ``` /// /// [`leave_variant`]: Context::leave_variant - #[allow(unused_variables)] #[inline(always)] - fn enter_variant(&self, type_name: &'static str, tag: T) + fn enter_variant(self, type_name: &'static str, tag: T) where T: fmt::Display, { + _ = type_name; + _ = tag; } /// Trace that we've left the last variant that was entered. @@ -359,17 +354,16 @@ pub trait Context { /// [`enter_variant`]. /// /// [`enter_variant`]: Context::enter_variant - #[allow(unused_variables)] #[inline(always)] - fn leave_variant(&self) {} + fn leave_variant(self) {} /// Trace a that a map key has been entered. - #[allow(unused_variables)] #[inline(always)] - fn enter_map_key(&self, field: T) + fn enter_map_key(self, field: T) where T: fmt::Display, { + _ = field; } /// Trace that we've left the last map field that was entered. @@ -380,12 +374,12 @@ pub trait Context { /// [`enter_map_key`]: Context::enter_map_key #[allow(unused_variables)] #[inline(always)] - fn leave_map_key(&self) {} + fn leave_map_key(self) {} /// Trace a sequence field. #[allow(unused_variables)] #[inline(always)] - fn enter_sequence_index(&self, index: usize) {} + fn enter_sequence_index(self, index: usize) {} /// Trace that we've left the last sequence index that was entered. /// @@ -395,5 +389,5 @@ pub trait Context { /// [`enter_sequence_index`]: Context::enter_sequence_index #[allow(unused_variables)] #[inline(always)] - fn leave_sequence_index(&self) {} + fn leave_sequence_index(self) {} } diff --git a/crates/musli-core/src/de/decode_slice_builder.rs b/crates/musli-core/src/de/decode_slice_builder.rs index 192d54998..c7df06077 100644 --- a/crates/musli-core/src/de/decode_slice_builder.rs +++ b/crates/musli-core/src/de/decode_slice_builder.rs @@ -3,24 +3,24 @@ use crate::Context; /// Trait used to decode a slice into a type. pub trait DecodeSliceBuilder: Sized { /// Construct a new empty container. - fn new(cx: &C) -> Result + fn new(cx: C) -> Result where - C: ?Sized + Context; + C: Context; /// Construct a new container with the given capacity hint. - fn with_capacity(cx: &C, hint: usize) -> Result + fn with_capacity(cx: C, hint: usize) -> Result where - C: ?Sized + Context; + C: Context; /// Push a value into the container. - fn push(&mut self, cx: &C, value: T) -> Result<(), C::Error> + fn push(&mut self, cx: C, value: T) -> Result<(), C::Error> where - C: ?Sized + Context; + C: Context; /// Reserve additional space for `capacity` elements in the collection. - fn reserve(&mut self, cx: &C, capacity: usize) -> Result<(), C::Error> + fn reserve(&mut self, cx: C, capacity: usize) -> Result<(), C::Error> where - C: ?Sized + Context; + C: Context; /// Mark the given length as initialized. /// diff --git a/crates/musli-core/src/de/decoder.rs b/crates/musli-core/src/de/decoder.rs index f134152c3..bd5b127a4 100644 --- a/crates/musli-core/src/de/decoder.rs +++ b/crates/musli-core/src/de/decoder.rs @@ -31,9 +31,9 @@ pub trait Decoder<'de>: Sized { type Mode: 'static; /// [`Decoder`] with a different context returned by /// [`Decoder::with_context`] - type WithContext<'this, U>: Decoder<'de, Cx = U, Error = U::Error, Mode = U::Mode> + type WithContext: Decoder<'de, Cx = U, Error = U::Error, Mode = U::Mode> where - U: 'this + Context; + U: Context; /// Decoder returned by [`Decoder::decode_buffer`]. type DecodeBuffer: AsDecoder; /// Decoder returned by [`Decoder::decode_option`]. @@ -56,24 +56,17 @@ pub trait Decoder<'de>: Sized { type __UseMusliDecoderAttributeMacro; /// Perform an operation while accessing the context. - fn cx(self, f: F) -> O - where - F: FnOnce(&Self::Cx, Self) -> O; + fn cx(&self) -> Self::Cx; /// Construct an decoder with a different context. - fn with_context( - self, - cx: &U, - ) -> Result, ::Error> + fn with_context(self, cx: U) -> Result, ::Error> where U: Context, { - self.cx(|cx, this| { - Err(cx.message(format_args!( - "Context switch not supported, expected {}", - ExpectingWrapper::new(&this).format() - ))) - }) + Err(self.cx().message(format_args!( + "Context switch not supported, expected {}", + ExpectingWrapper::new(&self).format() + ))) } /// Format the human-readable message that should occur if the decoder was @@ -88,20 +81,20 @@ pub trait Decoder<'de>: Sized { /// use musli::Context; /// use musli::de::{self, Decoder, Decode}; /// - /// struct MyDecoder<'a, C: ?Sized> { - /// cx: &'a C, + /// struct MyDecoder { + /// cx: C, /// } /// /// #[musli::decoder] - /// impl<'de, C: ?Sized + Context> Decoder<'de> for MyDecoder<'_, C> { + /// impl<'de, C> Decoder<'de> for MyDecoder + /// where + /// C: Context, + /// { /// type Cx = C; /// /// #[inline] - /// fn cx(self, f: F) -> O - /// where - /// F: FnOnce(&Self::Cx, Self) -> O, - /// { - /// f(self.cx, self) + /// fn cx(&self) -> Self::Cx { + /// self.cx /// } /// /// #[inline] @@ -170,12 +163,10 @@ pub trait Decoder<'de>: Sized { /// Skip over the current next value. #[inline] fn skip(self) -> Result<(), ::Error> { - self.cx(|cx, this| { - Err(cx.message(format_args!( - "Skipping is not supported, expected {}", - ExpectingWrapper::new(&this).format() - ))) - }) + Err(self.cx().message(format_args!( + "Skipping is not supported, expected {}", + ExpectingWrapper::new(&self).format() + ))) } /// This is a variant of [`Decoder::skip`], but instead of erroring in case @@ -219,42 +210,40 @@ pub trait Decoder<'de>: Sized { /// where /// D: Decoder<'de, Mode = Binary>, /// { - /// decoder.cx(|cx, decoder| { - /// let mut buffer = decoder.decode_buffer()?; + /// let cx = decoder.cx(); /// - /// let discriminant = buffer.as_decoder()?.decode_map(|st| { - /// loop { - /// let Some(mut e) = st.decode_entry()? else { - /// return Err(cx.missing_variant_tag("Enum")); - /// }; + /// let mut buffer = decoder.decode_buffer()?; /// - /// let found = e.decode_key()?.decode_unsized(|string: &str| { - /// Ok(string == "type") - /// })?; + /// let discriminant = buffer.as_decoder()?.decode_map(|st| { + /// loop { + /// let Some(mut e) = st.decode_entry()? else { + /// return Err(cx.missing_variant_tag("Enum")); + /// }; /// - /// if found { - /// break Ok(e.decode_value()?.decode()?); - /// } - /// } - /// })?; + /// let found = e.decode_key()?.decode_unsized(|string: &str| { + /// Ok(string == "type") + /// })?; /// - /// match discriminant { - /// 0 => Ok(Enum::Empty), - /// 1 => Ok(Enum::Person(buffer.as_decoder()?.decode()?)), - /// other => Err(cx.invalid_variant_tag("Enum", &other)), + /// if found { + /// break Ok(e.decode_value()?.decode()?); + /// } /// } - /// }) + /// })?; + /// + /// match discriminant { + /// 0 => Ok(Enum::Empty), + /// 1 => Ok(Enum::Person(buffer.as_decoder()?.decode()?)), + /// other => Err(cx.invalid_variant_tag("Enum", &other)), + /// } /// } /// } /// ``` #[inline] fn decode_buffer(self) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(format_args!( - "Decode buffering not supported, expected {}", - ExpectingWrapper::new(&this).format() - ))) - }) + Err(self.cx().message(format_args!( + "Decode buffering not supported, expected {}", + ExpectingWrapper::new(&self).format() + ))) } /// Decode a unit. @@ -289,12 +278,10 @@ pub trait Decoder<'de>: Sized { /// ``` #[inline] fn decode_empty(self) -> Result<(), ::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Empty, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Empty, + ExpectingWrapper::new(&self), + ))) } /// Decode a boolean. @@ -333,12 +320,10 @@ pub trait Decoder<'de>: Sized { /// ``` #[inline] fn decode_bool(self) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Bool, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Bool, + ExpectingWrapper::new(&self), + ))) } /// Decode a character. @@ -377,12 +362,10 @@ pub trait Decoder<'de>: Sized { /// ``` #[inline] fn decode_char(self) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Char, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Char, + ExpectingWrapper::new(&self), + ))) } /// Decode a 8-bit unsigned integer (a.k.a. a byte). @@ -421,12 +404,10 @@ pub trait Decoder<'de>: Sized { /// ``` #[inline] fn decode_u8(self) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Unsigned8, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Unsigned8, + ExpectingWrapper::new(&self), + ))) } /// Decode a 16-bit unsigned integer. @@ -465,12 +446,10 @@ pub trait Decoder<'de>: Sized { /// ``` #[inline] fn decode_u16(self) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Unsigned16, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Unsigned16, + ExpectingWrapper::new(&self), + ))) } /// Decode a 32-bit unsigned integer. @@ -509,12 +488,10 @@ pub trait Decoder<'de>: Sized { /// ``` #[inline] fn decode_u32(self) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Unsigned32, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Unsigned32, + ExpectingWrapper::new(&self), + ))) } /// Decode a 64-bit unsigned integer. @@ -553,12 +530,10 @@ pub trait Decoder<'de>: Sized { /// ``` #[inline] fn decode_u64(self) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Unsigned64, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Unsigned64, + ExpectingWrapper::new(&self), + ))) } /// Decode a 128-bit unsigned integer. @@ -597,12 +572,10 @@ pub trait Decoder<'de>: Sized { /// ``` #[inline] fn decode_u128(self) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Unsigned128, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Unsigned128, + ExpectingWrapper::new(&self), + ))) } /// Decode a 8-bit signed integer. @@ -641,12 +614,10 @@ pub trait Decoder<'de>: Sized { /// ``` #[inline] fn decode_i8(self) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Signed8, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Signed8, + ExpectingWrapper::new(&self), + ))) } /// Decode a 16-bit signed integer. @@ -685,12 +656,10 @@ pub trait Decoder<'de>: Sized { /// ``` #[inline] fn decode_i16(self) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Signed16, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Signed16, + ExpectingWrapper::new(&self), + ))) } /// Decode a 32-bit signed integer. @@ -729,12 +698,10 @@ pub trait Decoder<'de>: Sized { /// ``` #[inline] fn decode_i32(self) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Signed32, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Signed32, + ExpectingWrapper::new(&self), + ))) } /// Decode a 64-bit signed integer. @@ -773,12 +740,10 @@ pub trait Decoder<'de>: Sized { /// ``` #[inline] fn decode_i64(self) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Signed64, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Signed64, + ExpectingWrapper::new(&self), + ))) } /// Decode a 128-bit signed integer. @@ -817,12 +782,10 @@ pub trait Decoder<'de>: Sized { /// ``` #[inline] fn decode_i128(self) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Signed128, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Signed128, + ExpectingWrapper::new(&self), + ))) } /// Decode a [`usize`]. @@ -861,12 +824,10 @@ pub trait Decoder<'de>: Sized { /// ``` #[inline] fn decode_usize(self) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Usize, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Usize, + ExpectingWrapper::new(&self), + ))) } /// Decode a [`isize`]. @@ -905,12 +866,10 @@ pub trait Decoder<'de>: Sized { /// ``` #[inline] fn decode_isize(self) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Isize, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Isize, + ExpectingWrapper::new(&self), + ))) } /// Decode a 32-bit floating point value. @@ -949,12 +908,10 @@ pub trait Decoder<'de>: Sized { /// ``` #[inline] fn decode_f32(self) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Float32, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Float32, + ExpectingWrapper::new(&self), + ))) } /// Decode a 64-bit floating point value. @@ -993,12 +950,10 @@ pub trait Decoder<'de>: Sized { /// ``` #[inline] fn decode_f64(self) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Float64, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Float64, + ExpectingWrapper::new(&self), + ))) } /// Decode a fixed-length array. @@ -1037,12 +992,10 @@ pub trait Decoder<'de>: Sized { /// ``` #[inline] fn decode_array(self) -> Result<[u8; N], ::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Array, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Array, + ExpectingWrapper::new(&self), + ))) } /// Decode a sequence of bytes whos length is encoded in the payload. @@ -1078,10 +1031,7 @@ pub trait Decoder<'de>: Sized { /// { /// struct Visitor; /// - /// impl<'de, C> UnsizedVisitor<'de, C, [u8]> for Visitor - /// where - /// C: ?Sized + Context, - /// { + /// impl<'de, C> UnsizedVisitor<'de, C, [u8]> for Visitor where C: Context { /// type Ok = &'de [u8]; /// /// #[inline] @@ -1090,7 +1040,7 @@ pub trait Decoder<'de>: Sized { /// } /// /// #[inline] - /// fn visit_borrowed(self, _: &C, bytes: &'de [u8]) -> Result { + /// fn visit_borrowed(self, cx: C, bytes: &'de [u8]) -> Result { /// Ok(bytes) /// } /// } @@ -1106,12 +1056,10 @@ pub trait Decoder<'de>: Sized { where V: UnsizedVisitor<'de, Self::Cx, [u8]>, { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Bytes, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Bytes, + ExpectingWrapper::new(&self), + ))) } /// Decode a string slice from the current decoder. @@ -1149,7 +1097,7 @@ pub trait Decoder<'de>: Sized { /// /// impl<'de, C> UnsizedVisitor<'de, C, str> for Visitor /// where - /// C: ?Sized + Context, + /// C: Context, /// { /// type Ok = &'de str; /// @@ -1159,7 +1107,7 @@ pub trait Decoder<'de>: Sized { /// } /// /// #[inline] - /// fn visit_borrowed(self, _: &C, bytes: &'de str) -> Result { + /// fn visit_borrowed(self, _: C, bytes: &'de str) -> Result { /// Ok(bytes) /// } /// } @@ -1175,12 +1123,10 @@ pub trait Decoder<'de>: Sized { where V: UnsizedVisitor<'de, Self::Cx, str>, { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::String, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::String, + ExpectingWrapper::new(&self), + ))) } /// Decode an optional value. @@ -1223,12 +1169,10 @@ pub trait Decoder<'de>: Sized { #[inline] #[must_use = "Decoders must be consumed"] fn decode_option(self) -> Result, ::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Option, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Option, + ExpectingWrapper::new(&self), + ))) } /// Construct an unpack that can decode more than one element at a time. @@ -1278,12 +1222,10 @@ pub trait Decoder<'de>: Sized { where F: FnOnce(&mut Self::DecodePack) -> Result::Error>, { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Pack, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Pack, + ExpectingWrapper::new(&self), + ))) } /// Decode a sequence of values. @@ -1376,12 +1318,10 @@ pub trait Decoder<'de>: Sized { where F: FnOnce(&mut Self::DecodeSequence) -> Result::Error>, { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::UnsizedSequence, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::UnsizedSequence, + ExpectingWrapper::new(&self), + ))) } /// Decode a sequence with a `hint` indicating its expected characteristics. @@ -1453,12 +1393,10 @@ pub trait Decoder<'de>: Sized { where F: FnOnce(&mut Self::DecodeMap) -> Result::Error>, { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::UnsizedMap, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::UnsizedMap, + ExpectingWrapper::new(&self), + ))) } /// Decode a map using a simplified function. @@ -1502,32 +1440,32 @@ pub trait Decoder<'de>: Sized { /// { /// static HINT: MapHint = MapHint::with_size(2); /// - /// decoder.cx(|cx, decoder| { - /// decoder.decode_map_hint(&HINT, |st| { - /// let mut string = None; - /// let mut integer = None; - /// - /// while let Some(mut field) = st.decode_entry()? { - /// // Note: to avoid allocating `decode_string` needs to be used with a visitor. - /// let tag = field.decode_key()?.decode::()?; - /// - /// match tag.as_str() { - /// "string" => { - /// string = Some(field.decode_value()?.decode()?); - /// } - /// "integer" => { - /// integer = Some(field.decode_value()?.decode()?); - /// } - /// tag => { - /// return Err(cx.invalid_field_tag("Struct", tag)); - /// } + /// let cx = decoder.cx(); + /// + /// decoder.decode_map_hint(&HINT, |st| { + /// let mut string = None; + /// let mut integer = None; + /// + /// while let Some(mut field) = st.decode_entry()? { + /// // Note: to avoid allocating `decode_string` needs to be used with a visitor. + /// let tag = field.decode_key()?.decode::()?; + /// + /// match tag.as_str() { + /// "string" => { + /// string = Some(field.decode_value()?.decode()?); + /// } + /// "integer" => { + /// integer = Some(field.decode_value()?.decode()?); + /// } + /// tag => { + /// return Err(cx.invalid_field_tag("Struct", tag)); /// } /// } + /// } /// - /// Ok(Self { - /// string: string.ok_or_else(|| cx.expected_tag("Struct", "string"))?, - /// integer: integer.ok_or_else(|| cx.expected_tag("Struct", "integer"))?, - /// }) + /// Ok(Self { + /// string: string.ok_or_else(|| cx.expected_tag("Struct", "string"))?, + /// integer: integer.ok_or_else(|| cx.expected_tag("Struct", "integer"))?, /// }) /// }) /// } @@ -1550,12 +1488,10 @@ pub trait Decoder<'de>: Sized { where F: FnOnce(&mut Self::DecodeMapEntries) -> Result::Error>, { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::MapEntries, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::MapEntries, + ExpectingWrapper::new(&self), + ))) } /// Decode a variant using a closure. @@ -1577,17 +1513,17 @@ pub trait Decoder<'de>: Sized { /// where /// D: Decoder<'de>, /// { - /// decoder.cx(|cx, decoder| { - /// decoder.decode_variant(|variant| { - /// let tag = variant.decode_tag()?.decode()?; - /// let value = variant.decode_value()?; - /// - /// match tag { - /// 0 => Ok(Self::Number(value.decode()?)), - /// 1 => Ok(Self::String(value.decode()?)), - /// tag => Err(cx.invalid_variant_tag("Enum", &tag)), - /// } - /// }) + /// let cx = decoder.cx(); + /// + /// decoder.decode_variant(|variant| { + /// let tag = variant.decode_tag()?.decode()?; + /// let value = variant.decode_value()?; + /// + /// match tag { + /// 0 => Ok(Self::Number(value.decode()?)), + /// 1 => Ok(Self::String(value.decode()?)), + /// tag => Err(cx.invalid_variant_tag("Enum", &tag)), + /// } /// }) /// } /// } @@ -1597,12 +1533,10 @@ pub trait Decoder<'de>: Sized { where F: FnOnce(&mut Self::DecodeVariant) -> Result::Error>, { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Variant, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Variant, + ExpectingWrapper::new(&self), + ))) } /// Decode an unknown number using a visitor. @@ -1611,12 +1545,10 @@ pub trait Decoder<'de>: Sized { where V: Visitor<'de, Self::Cx>, { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Number, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Number, + ExpectingWrapper::new(&self), + ))) } /// Decode dynamically through a [`Visitor`]. @@ -1625,12 +1557,10 @@ pub trait Decoder<'de>: Sized { where V: Visitor<'de, Self::Cx>, { - self.cx(|cx, this| { - Err(cx.message(format_args!( - "Any type not supported, expected {}", - ExpectingWrapper::new(&this).format() - ))) - }) + Err(self.cx().message(format_args!( + "Any type not supported, expected {}", + ExpectingWrapper::new(&self).format() + ))) } } diff --git a/crates/musli-core/src/de/unsized_visitor.rs b/crates/musli-core/src/de/unsized_visitor.rs index 0dd96fcdd..2d7e736b2 100644 --- a/crates/musli-core/src/de/unsized_visitor.rs +++ b/crates/musli-core/src/de/unsized_visitor.rs @@ -26,8 +26,9 @@ use crate::Context; /// [`Decoder::decode_bytes`]: crate::de::Decoder::decode_bytes /// [`Decoder::decode_string`]: crate::de::Decoder::decode_string /// [`Decoder::decode_unsized`]: crate::de::Decoder::decode_unsized -pub trait UnsizedVisitor<'de, C: ?Sized + Context, T>: Sized +pub trait UnsizedVisitor<'de, C, T>: Sized where + C: Context, T: ?Sized + ToOwned, { /// The value produced. @@ -40,20 +41,20 @@ where /// Visit an owned value. #[inline(always)] - fn visit_owned(self, cx: &C, value: T::Owned) -> Result { + fn visit_owned(self, cx: C, value: T::Owned) -> Result { self.visit_ref(cx, value.borrow()) } /// Visit a string that is borrowed directly from the source data. #[inline(always)] - fn visit_borrowed(self, cx: &C, value: &'de T) -> Result { + fn visit_borrowed(self, cx: C, value: &'de T) -> Result { self.visit_ref(cx, value) } /// Visit a value reference that is provided from the decoder in any manner /// possible. Which might require additional decoding work. #[inline(always)] - fn visit_ref(self, cx: &C, _: &T) -> Result { + fn visit_ref(self, cx: C, _: &T) -> Result { Err(cx.message(expecting::bad_visitor_type( &expecting::AnyValue, ExpectingWrapper::new(&self), @@ -62,12 +63,18 @@ where } #[repr(transparent)] -struct ExpectingWrapper<'a, T, C: ?Sized, I: ?Sized> { +struct ExpectingWrapper<'a, T, C, I> +where + I: ?Sized, +{ inner: T, - _marker: PhantomData<(&'a C, &'a I)>, + _marker: PhantomData<(C, &'a I)>, } -impl ExpectingWrapper<'_, T, C, U> { +impl ExpectingWrapper<'_, T, C, U> +where + U: ?Sized, +{ #[inline(always)] fn new(value: &T) -> &Self { // SAFETY: `ExpectingWrapper` is repr(transparent) over `T`. @@ -78,7 +85,7 @@ impl ExpectingWrapper<'_, T, C, U> { impl<'de, T, C, U> Expecting for ExpectingWrapper<'_, T, C, U> where T: UnsizedVisitor<'de, C, U>, - C: ?Sized + Context, + C: Context, U: ?Sized + ToOwned, { #[inline(always)] diff --git a/crates/musli-core/src/de/utils.rs b/crates/musli-core/src/de/utils.rs index 4c9c9e979..1fc850d3c 100644 --- a/crates/musli-core/src/de/utils.rs +++ b/crates/musli-core/src/de/utils.rs @@ -10,21 +10,20 @@ where { use crate::Context; - decoder.cx(|cx, decoder| { - decoder.decode_sequence(move |seq| { - let mut out = - V::with_capacity(cx, crate::internal::size_hint::cautious(seq.size_hint()))?; - let mut index = 0usize; + let cx = decoder.cx(); - while let Some(value) = seq.try_decode_next()? { - cx.enter_sequence_index(index); - let value = T::decode(value)?; - out.push(cx, value)?; - cx.leave_sequence_index(); - index = index.wrapping_add(1); - } + decoder.decode_sequence(move |seq| { + let mut out = V::with_capacity(cx, crate::internal::size_hint::cautious(seq.size_hint()))?; + let mut index = 0usize; - Ok(out) - }) + while let Some(value) = seq.try_decode_next()? { + cx.enter_sequence_index(index); + let value = T::decode(value)?; + out.push(cx, value)?; + cx.leave_sequence_index(); + index = index.wrapping_add(1); + } + + Ok(out) }) } diff --git a/crates/musli-core/src/de/visitor.rs b/crates/musli-core/src/de/visitor.rs index 1b8eafc51..62599e055 100644 --- a/crates/musli-core/src/de/visitor.rs +++ b/crates/musli-core/src/de/visitor.rs @@ -11,7 +11,11 @@ use super::{Decoder, MapDecoder, SequenceDecoder, SizeHint, UnsizedVisitor, Vari /// Each callback on this visitor indicates the type that should be decoded from /// the passed in decoder. A typical implementation would simply call the /// corresponding decoder function for the type being visited. -pub trait Visitor<'de, C: ?Sized + Context>: Sized { +pub trait Visitor<'de, C> +where + Self: Sized, + C: Context, +{ /// The value produced by the visitor. type Ok; /// String decoder to use. @@ -31,7 +35,7 @@ pub trait Visitor<'de, C: ?Sized + Context>: Sized { /// Indicates that the visited type is empty. #[inline] - fn visit_empty(self, cx: &C) -> Result { + fn visit_empty(self, cx: C) -> Result { Err(cx.message(expecting::unsupported_type( &expecting::Empty, ExpectingWrapper::new(&self), @@ -40,7 +44,7 @@ pub trait Visitor<'de, C: ?Sized + Context>: Sized { /// Indicates that the visited type is a `bool`. #[inline] - fn visit_bool(self, cx: &C, _: bool) -> Result { + fn visit_bool(self, cx: C, _: bool) -> Result { Err(cx.message(expecting::unsupported_type( &expecting::Bool, ExpectingWrapper::new(&self), @@ -49,7 +53,7 @@ pub trait Visitor<'de, C: ?Sized + Context>: Sized { /// Indicates that the visited type is a `char`. #[inline] - fn visit_char(self, cx: &C, _: char) -> Result { + fn visit_char(self, cx: C, _: char) -> Result { Err(cx.message(expecting::unsupported_type( &expecting::Char, ExpectingWrapper::new(&self), @@ -58,7 +62,7 @@ pub trait Visitor<'de, C: ?Sized + Context>: Sized { /// Indicates that the visited type is a `u8`. #[inline] - fn visit_u8(self, cx: &C, _: u8) -> Result { + fn visit_u8(self, cx: C, _: u8) -> Result { Err(cx.message(expecting::unsupported_type( &expecting::Unsigned8, ExpectingWrapper::new(&self), @@ -67,7 +71,7 @@ pub trait Visitor<'de, C: ?Sized + Context>: Sized { /// Indicates that the visited type is a `u16`. #[inline] - fn visit_u16(self, cx: &C, _: u16) -> Result { + fn visit_u16(self, cx: C, _: u16) -> Result { Err(cx.message(expecting::unsupported_type( &expecting::Unsigned16, ExpectingWrapper::new(&self), @@ -76,7 +80,7 @@ pub trait Visitor<'de, C: ?Sized + Context>: Sized { /// Indicates that the visited type is a `u32`. #[inline] - fn visit_u32(self, cx: &C, _: u32) -> Result { + fn visit_u32(self, cx: C, _: u32) -> Result { Err(cx.message(expecting::unsupported_type( &expecting::Unsigned32, ExpectingWrapper::new(&self), @@ -85,7 +89,7 @@ pub trait Visitor<'de, C: ?Sized + Context>: Sized { /// Indicates that the visited type is a `u64`. #[inline] - fn visit_u64(self, cx: &C, _: u64) -> Result { + fn visit_u64(self, cx: C, _: u64) -> Result { Err(cx.message(expecting::unsupported_type( &expecting::Unsigned64, ExpectingWrapper::new(&self), @@ -94,7 +98,7 @@ pub trait Visitor<'de, C: ?Sized + Context>: Sized { /// Indicates that the visited type is a `u128`. #[inline] - fn visit_u128(self, cx: &C, _: u128) -> Result { + fn visit_u128(self, cx: C, _: u128) -> Result { Err(cx.message(expecting::unsupported_type( &expecting::Unsigned128, ExpectingWrapper::new(&self), @@ -103,7 +107,7 @@ pub trait Visitor<'de, C: ?Sized + Context>: Sized { /// Indicates that the visited type is a `i8`. #[inline] - fn visit_i8(self, cx: &C, _: i8) -> Result { + fn visit_i8(self, cx: C, _: i8) -> Result { Err(cx.message(expecting::unsupported_type( &expecting::Signed8, ExpectingWrapper::new(&self), @@ -112,7 +116,7 @@ pub trait Visitor<'de, C: ?Sized + Context>: Sized { /// Indicates that the visited type is a `i16`. #[inline] - fn visit_i16(self, cx: &C, _: i16) -> Result { + fn visit_i16(self, cx: C, _: i16) -> Result { Err(cx.message(expecting::unsupported_type( &expecting::Signed16, ExpectingWrapper::new(&self), @@ -121,7 +125,7 @@ pub trait Visitor<'de, C: ?Sized + Context>: Sized { /// Indicates that the visited type is a `i32`. #[inline] - fn visit_i32(self, cx: &C, _: i32) -> Result { + fn visit_i32(self, cx: C, _: i32) -> Result { Err(cx.message(expecting::unsupported_type( &expecting::Signed32, ExpectingWrapper::new(&self), @@ -130,7 +134,7 @@ pub trait Visitor<'de, C: ?Sized + Context>: Sized { /// Indicates that the visited type is a `i64`. #[inline] - fn visit_i64(self, cx: &C, _: i64) -> Result { + fn visit_i64(self, cx: C, _: i64) -> Result { Err(cx.message(expecting::unsupported_type( &expecting::Signed64, ExpectingWrapper::new(&self), @@ -139,7 +143,7 @@ pub trait Visitor<'de, C: ?Sized + Context>: Sized { /// Indicates that the visited type is a `i128`. #[inline] - fn visit_i128(self, cx: &C, _: i128) -> Result { + fn visit_i128(self, cx: C, _: i128) -> Result { Err(cx.message(expecting::unsupported_type( &expecting::Signed128, ExpectingWrapper::new(&self), @@ -148,7 +152,7 @@ pub trait Visitor<'de, C: ?Sized + Context>: Sized { /// Indicates that the visited type is a `usize`. #[inline] - fn visit_usize(self, cx: &C, _: usize) -> Result { + fn visit_usize(self, cx: C, _: usize) -> Result { Err(cx.message(expecting::unsupported_type( &expecting::Usize, ExpectingWrapper::new(&self), @@ -157,7 +161,7 @@ pub trait Visitor<'de, C: ?Sized + Context>: Sized { /// Indicates that the visited type is a `isize`. #[inline] - fn visit_isize(self, cx: &C, _: isize) -> Result { + fn visit_isize(self, cx: C, _: isize) -> Result { Err(cx.message(expecting::unsupported_type( &expecting::Isize, ExpectingWrapper::new(&self), @@ -166,7 +170,7 @@ pub trait Visitor<'de, C: ?Sized + Context>: Sized { /// Indicates that the visited type is a `f32`. #[inline] - fn visit_f32(self, cx: &C, _: f32) -> Result { + fn visit_f32(self, cx: C, _: f32) -> Result { Err(cx.message(expecting::unsupported_type( &expecting::Float32, ExpectingWrapper::new(&self), @@ -175,7 +179,7 @@ pub trait Visitor<'de, C: ?Sized + Context>: Sized { /// Indicates that the visited type is a `f64`. #[inline] - fn visit_f64(self, cx: &C, _: f64) -> Result { + fn visit_f64(self, cx: C, _: f64) -> Result { Err(cx.message(expecting::unsupported_type( &expecting::Float64, ExpectingWrapper::new(&self), @@ -184,7 +188,7 @@ pub trait Visitor<'de, C: ?Sized + Context>: Sized { /// Indicates that the visited type is an optional type. #[inline] - fn visit_option(self, cx: &C, _: Option) -> Result + fn visit_option(self, cx: C, _: Option) -> Result where D: Decoder<'de, Cx = C, Error = C::Error, Mode = C::Mode>, { @@ -196,7 +200,7 @@ pub trait Visitor<'de, C: ?Sized + Context>: Sized { /// Indicates that the visited type is a sequence. #[inline] - fn visit_sequence(self, cx: &C, decoder: &mut D) -> Result + fn visit_sequence(self, cx: C, decoder: &mut D) -> Result where D: ?Sized + SequenceDecoder<'de, Cx = C>, { @@ -208,7 +212,7 @@ pub trait Visitor<'de, C: ?Sized + Context>: Sized { /// Indicates that the visited type is a map. #[inline] - fn visit_map(self, cx: &C, decoder: &mut D) -> Result::Error> + fn visit_map(self, cx: C, decoder: &mut D) -> Result::Error> where D: ?Sized + MapDecoder<'de, Cx = C>, { @@ -220,7 +224,7 @@ pub trait Visitor<'de, C: ?Sized + Context>: Sized { /// Indicates that the visited type is `string`. #[inline] - fn visit_string(self, cx: &C, hint: SizeHint) -> Result { + fn visit_string(self, cx: C, hint: SizeHint) -> Result { Err(cx.message(expecting::unsupported_type( &expecting::StringWith(hint), ExpectingWrapper::new(&self), @@ -229,7 +233,7 @@ pub trait Visitor<'de, C: ?Sized + Context>: Sized { /// Indicates that the visited type is `bytes`. #[inline] - fn visit_bytes(self, cx: &C, hint: SizeHint) -> Result { + fn visit_bytes(self, cx: C, hint: SizeHint) -> Result { Err(cx.message(expecting::unsupported_type( &expecting::BytesWith(hint), ExpectingWrapper::new(&self), @@ -238,7 +242,7 @@ pub trait Visitor<'de, C: ?Sized + Context>: Sized { /// Indicates that the visited type is a variant. #[inline] - fn visit_variant(self, cx: &C, _: &mut D) -> Result + fn visit_variant(self, cx: C, _: &mut D) -> Result where D: VariantDecoder<'de, Cx = C>, { @@ -254,31 +258,29 @@ pub trait Visitor<'de, C: ?Sized + Context>: Sized { where D: Decoder<'de, Cx = C, Error = C::Error, Mode = C::Mode>, { - decoder.cx(|cx, _| { - Err(cx.message(expecting::unsupported_type( - &expecting::Any, - ExpectingWrapper::new(&self), - ))) - }) + Err(decoder.cx().message(expecting::unsupported_type( + &expecting::Any, + ExpectingWrapper::new(&self), + ))) } } #[repr(transparent)] -struct ExpectingWrapper<'a, T, C: ?Sized> { +struct ExpectingWrapper { inner: T, - _marker: PhantomData<&'a C>, + _marker: PhantomData, } -impl ExpectingWrapper<'_, T, C> { +impl ExpectingWrapper { fn new(inner: &T) -> &Self { // SAFETY: `ExpectingWrapper` is repr(transparent) over `T`. unsafe { &*(inner as *const T as *const Self) } } } -impl<'de, T, C> Expecting for ExpectingWrapper<'_, T, C> +impl<'de, T, C> Expecting for ExpectingWrapper where - C: ?Sized + Context, + C: Context, T: Visitor<'de, C>, { #[inline] diff --git a/crates/musli-core/src/en/encoder.rs b/crates/musli-core/src/en/encoder.rs index 5c1b26249..0ac23eb9e 100644 --- a/crates/musli-core/src/en/encoder.rs +++ b/crates/musli-core/src/en/encoder.rs @@ -34,9 +34,9 @@ pub trait Encoder: Sized { /// Mode associated with encoding. type Mode: 'static; /// Constructed [`Encoder`] with a different context. - type WithContext<'this, U>: Encoder + type WithContext: Encoder where - U: 'this + Context; + U: Context; /// A simple pack that packs a sequence of elements. type EncodePack: SequenceEncoder; /// Encoder returned when encoding an optional value which is present. @@ -61,24 +61,17 @@ pub trait Encoder: Sized { type __UseMusliEncoderAttributeMacro; /// Perform an operation while accessing the context. - fn cx(self, f: F) -> O - where - F: FnOnce(&Self::Cx, Self) -> O; + fn cx(&self) -> Self::Cx; /// Construct an encoder with a different context. - fn with_context( - self, - _: &U, - ) -> Result, ::Error> + fn with_context(self, _: U) -> Result, ::Error> where U: Context, { - self.cx(|cx, this| { - Err(cx.message(format_args!( - "Context switch not supported, expected {}", - ExpectingWrapper::new(&this).format() - ))) - }) + Err(self.cx().message(format_args!( + "Context switch not supported, expected {}", + ExpectingWrapper::new(&self).format() + ))) } /// An expectation error. Every other implementation defers to this to @@ -152,12 +145,10 @@ pub trait Encoder: Sized { /// ``` #[inline] fn encode_empty(self) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Empty, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Empty, + ExpectingWrapper::new(&self), + ))) } /// Encode a boolean value. @@ -201,12 +192,10 @@ pub trait Encoder: Sized { /// ``` #[inline] fn encode_bool(self, v: bool) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Bool, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Bool, + ExpectingWrapper::new(&self), + ))) } /// Encode a character. @@ -250,12 +239,10 @@ pub trait Encoder: Sized { /// ``` #[inline] fn encode_char(self, v: char) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Char, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Char, + ExpectingWrapper::new(&self), + ))) } /// Encode a 8-bit unsigned integer. @@ -299,12 +286,10 @@ pub trait Encoder: Sized { /// ``` #[inline] fn encode_u8(self, v: u8) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Unsigned8, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Unsigned8, + ExpectingWrapper::new(&self), + ))) } /// Encode a 16-bit unsigned integer. @@ -348,12 +333,10 @@ pub trait Encoder: Sized { /// ``` #[inline] fn encode_u16(self, v: u16) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Unsigned16, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Unsigned16, + ExpectingWrapper::new(&self), + ))) } /// Encode a 32-bit unsigned integer. @@ -397,12 +380,10 @@ pub trait Encoder: Sized { /// ``` #[inline] fn encode_u32(self, v: u32) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Unsigned32, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Unsigned32, + ExpectingWrapper::new(&self), + ))) } /// Encode a 64-bit unsigned integer. @@ -446,12 +427,10 @@ pub trait Encoder: Sized { /// ``` #[inline] fn encode_u64(self, v: u64) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Unsigned64, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Unsigned64, + ExpectingWrapper::new(&self), + ))) } /// Encode a 128-bit unsigned integer. @@ -495,12 +474,10 @@ pub trait Encoder: Sized { /// ``` #[inline] fn encode_u128(self, v: u128) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Unsigned128, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Unsigned128, + ExpectingWrapper::new(&self), + ))) } /// Encode a 8-bit signed integer. @@ -544,12 +521,10 @@ pub trait Encoder: Sized { /// ``` #[inline] fn encode_i8(self, v: i8) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Signed8, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Signed8, + ExpectingWrapper::new(&self), + ))) } /// Encode a 16-bit signed integer. @@ -593,12 +568,10 @@ pub trait Encoder: Sized { /// ``` #[inline] fn encode_i16(self, v: i16) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Signed16, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Signed16, + ExpectingWrapper::new(&self), + ))) } /// Encode a 32-bit signed integer. @@ -642,12 +615,10 @@ pub trait Encoder: Sized { /// ``` #[inline] fn encode_i32(self, v: i32) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Signed32, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Signed32, + ExpectingWrapper::new(&self), + ))) } /// Encode a 64-bit signed integer. @@ -691,12 +662,10 @@ pub trait Encoder: Sized { /// ``` #[inline] fn encode_i64(self, v: i64) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Signed64, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Signed64, + ExpectingWrapper::new(&self), + ))) } /// Encode a 128-bit signed integer. @@ -740,12 +709,10 @@ pub trait Encoder: Sized { /// ``` #[inline] fn encode_i128(self, v: i128) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Signed128, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Signed128, + ExpectingWrapper::new(&self), + ))) } /// Encode a [`usize`]. @@ -789,12 +756,10 @@ pub trait Encoder: Sized { /// ``` #[inline] fn encode_usize(self, v: usize) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Usize, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Usize, + ExpectingWrapper::new(&self), + ))) } /// Encode a [`isize`]. @@ -838,12 +803,10 @@ pub trait Encoder: Sized { /// ``` #[inline] fn encode_isize(self, v: isize) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Isize, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Isize, + ExpectingWrapper::new(&self), + ))) } /// Encode a 32-bit floating point value. @@ -887,12 +850,10 @@ pub trait Encoder: Sized { /// ``` #[inline] fn encode_f32(self, v: f32) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Float32, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Float32, + ExpectingWrapper::new(&self), + ))) } /// Encode a 64-bit floating point value. @@ -936,12 +897,10 @@ pub trait Encoder: Sized { /// ``` #[inline] fn encode_f64(self, v: f64) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Float64, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Float64, + ExpectingWrapper::new(&self), + ))) } /// Encode fixed-length array. @@ -988,12 +947,10 @@ pub trait Encoder: Sized { self, array: &[u8; N], ) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Array, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Array, + ExpectingWrapper::new(&self), + ))) } /// Encode a sequence of bytes. @@ -1038,12 +995,10 @@ pub trait Encoder: Sized { /// ``` #[inline] fn encode_bytes(self, bytes: &[u8]) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Bytes, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Bytes, + ExpectingWrapper::new(&self), + ))) } /// Encode the given slices of bytes in sequence, with one following another @@ -1106,12 +1061,10 @@ pub trait Encoder: Sized { where I: IntoIterator>, { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Bytes, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Bytes, + ExpectingWrapper::new(&self), + ))) } /// Encode a string. @@ -1155,12 +1108,10 @@ pub trait Encoder: Sized { /// ``` #[inline] fn encode_string(self, string: &str) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::String, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::String, + ExpectingWrapper::new(&self), + ))) } /// Encode a value that implements [`Display`] as a string. @@ -1198,12 +1149,10 @@ pub trait Encoder: Sized { where T: ?Sized + fmt::Display, { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::CollectString, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::CollectString, + ExpectingWrapper::new(&self), + ))) } /// Encode an optional value that is present. @@ -1254,12 +1203,10 @@ pub trait Encoder: Sized { /// ``` #[inline] fn encode_some(self) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Option, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Option, + ExpectingWrapper::new(&self), + ))) } /// Encode an optional value that is absent. @@ -1310,12 +1257,10 @@ pub trait Encoder: Sized { /// ``` #[inline] fn encode_none(self) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Option, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Option, + ExpectingWrapper::new(&self), + ))) } /// Construct a pack that can encode more than one element at a time. @@ -1358,12 +1303,10 @@ pub trait Encoder: Sized { /// ``` #[inline] fn encode_pack(self) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Pack, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Pack, + ExpectingWrapper::new(&self), + ))) } /// Encodes a pack using a closure. @@ -1614,12 +1557,10 @@ pub trait Encoder: Sized { self, hint: &SequenceHint, ) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::SequenceWith(hint.size_hint()), - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::SequenceWith(hint.size_hint()), + ExpectingWrapper::new(&self), + ))) } /// Encode a sequence using a closure. @@ -1752,12 +1693,10 @@ pub trait Encoder: Sized { /// ``` #[inline] fn encode_map(self, hint: &MapHint) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Map, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Map, + ExpectingWrapper::new(&self), + ))) } /// Encode a map using a closure. @@ -1857,12 +1796,10 @@ pub trait Encoder: Sized { self, hint: &MapHint, ) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::MapWith(hint.size_hint()), - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::MapWith(hint.size_hint()), + ExpectingWrapper::new(&self), + ))) } /// Encode a variant. @@ -1939,12 +1876,10 @@ pub trait Encoder: Sized { /// ``` #[inline] fn encode_variant(self) -> Result::Error> { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::Variant, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::Variant, + ExpectingWrapper::new(&self), + ))) } /// Encode a variant using a closure. @@ -2137,12 +2072,10 @@ pub trait Encoder: Sized { where T: ?Sized + Encode<::Mode>, { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::SequenceVariant, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::SequenceVariant, + ExpectingWrapper::new(&self), + ))) } /// Simplified encoding for a struct variant. @@ -2206,12 +2139,10 @@ pub trait Encoder: Sized { where T: ?Sized + Encode<::Mode>, { - self.cx(|cx, this| { - Err(cx.message(expecting::unsupported_type( - &expecting::MapVariant, - ExpectingWrapper::new(&this), - ))) - }) + Err(self.cx().message(expecting::unsupported_type( + &expecting::MapVariant, + ExpectingWrapper::new(&self), + ))) } } diff --git a/crates/musli-core/src/en/sequence_encoder.rs b/crates/musli-core/src/en/sequence_encoder.rs index 833df3714..f9aac8b21 100644 --- a/crates/musli-core/src/en/sequence_encoder.rs +++ b/crates/musli-core/src/en/sequence_encoder.rs @@ -5,7 +5,7 @@ use super::{utils, Encode, Encoder}; /// Trait governing how to encode a sequence. pub trait SequenceEncoder { /// Context associated with the encoder. - type Cx: ?Sized + Context; + type Cx: Context; /// Result type of the encoder. type Ok; /// The encoder returned when advancing the sequence encoder. @@ -18,13 +18,8 @@ pub trait SequenceEncoder { where Self: 'this; - /// Perform an operation while accessing the context. - /// - /// This access the context while providing a mutable reference to the - /// sequence encoder. - fn cx_mut(&mut self, f: F) -> O - where - F: FnOnce(&Self::Cx, &mut Self) -> O; + /// Access the associated context. + fn cx(&self) -> Self::Cx; /// Return encoder for the next element. #[must_use = "Encoders must be consumed"] diff --git a/crates/musli-core/src/en/utils.rs b/crates/musli-core/src/en/utils.rs index bbde778dd..eaa6803c1 100644 --- a/crates/musli-core/src/en/utils.rs +++ b/crates/musli-core/src/en/utils.rs @@ -10,19 +10,18 @@ where E: Encoder, T: Encode, { - encoder.cx(|cx, encoder| { - let slice = slice.as_ref(); - let hint = SequenceHint::with_size(slice.len()); - let mut seq = encoder.encode_sequence(&hint)?; + let cx = encoder.cx(); + let slice = slice.as_ref(); + let hint = SequenceHint::with_size(slice.len()); + let mut seq = encoder.encode_sequence(&hint)?; - for (index, item) in slice.iter().enumerate() { - cx.enter_sequence_index(index); - seq.push(item)?; - cx.leave_sequence_index(); - } + for (index, item) in slice.iter().enumerate() { + cx.enter_sequence_index(index); + seq.push(item)?; + cx.leave_sequence_index(); + } - seq.finish_sequence() - }) + seq.finish_sequence() } /// The default implementation of [`Encoder::encode_slices`]. @@ -36,23 +35,23 @@ where E: Encoder, T: Encode, { - encoder.cx(|cx, encoder| { - let hint = SequenceHint::with_size(len); - let mut seq = encoder.encode_sequence(&hint)?; - - let mut index = 0; - - for slice in slices { - for item in slice.as_ref() { - cx.enter_sequence_index(index); - seq.push(item)?; - cx.leave_sequence_index(); - index = index.wrapping_add(1); - } + let cx = encoder.cx(); + + let hint = SequenceHint::with_size(len); + let mut seq = encoder.encode_sequence(&hint)?; + + let mut index = 0; + + for slice in slices { + for item in slice.as_ref() { + cx.enter_sequence_index(index); + seq.push(item)?; + cx.leave_sequence_index(); + index = index.wrapping_add(1); } + } - seq.finish_sequence() - }) + seq.finish_sequence() } /// The default implementation of [`SequenceEncoder::encode_slice`]. @@ -65,18 +64,18 @@ where E: ?Sized + SequenceEncoder, T: Encode<::Mode>, { - seq.cx_mut(|cx, this| { - let mut index = 0usize; + let cx = seq.cx(); - for value in slice.as_ref() { - cx.enter_sequence_index(index); - this.push(value)?; - cx.leave_sequence_index(); - index = index.wrapping_add(1); - } + let mut index = 0usize; + + for value in slice.as_ref() { + cx.enter_sequence_index(index); + seq.push(value)?; + cx.leave_sequence_index(); + index = index.wrapping_add(1); + } - Ok(()) - }) + Ok(()) } /// The default implementation of [`SequenceEncoder::encode_slices`]. @@ -89,18 +88,18 @@ where E: ?Sized + SequenceEncoder, T: Encode<::Mode>, { - seq.cx_mut(|cx, seq| { - let mut index = 0usize; - - for slice in slices { - for value in slice.as_ref() { - cx.enter_sequence_index(index); - seq.push(value)?; - cx.leave_sequence_index(); - index = index.wrapping_add(1); - } + let cx = seq.cx(); + + let mut index = 0usize; + + for slice in slices { + for value in slice.as_ref() { + cx.enter_sequence_index(index); + seq.push(value)?; + cx.leave_sequence_index(); + index = index.wrapping_add(1); } + } - Ok(()) - }) + Ok(()) } diff --git a/crates/musli-core/src/impls/alloc.rs b/crates/musli-core/src/impls/alloc.rs index 3de847b7b..90fc5af4e 100644 --- a/crates/musli-core/src/impls/alloc.rs +++ b/crates/musli-core/src/impls/alloc.rs @@ -66,7 +66,7 @@ impl<'de, M> Decode<'de, M> for String { impl<'de, C> UnsizedVisitor<'de, C, str> for Visitor where - C: ?Sized + Context, + C: Context, { type Ok = String; @@ -76,17 +76,17 @@ impl<'de, M> Decode<'de, M> for String { } #[inline] - fn visit_owned(self, _: &C, value: String) -> Result { + fn visit_owned(self, _: C, value: String) -> Result { Ok(value) } #[inline] - fn visit_borrowed(self, cx: &C, string: &'de str) -> Result { + fn visit_borrowed(self, cx: C, string: &'de str) -> Result { self.visit_ref(cx, string) } #[inline] - fn visit_ref(self, _: &C, string: &str) -> Result { + fn visit_ref(self, _: C, string: &str) -> Result { Ok(string.to_owned()) } } @@ -166,7 +166,7 @@ macro_rules! cow { impl<'de, C> UnsizedVisitor<'de, C, $source> for Visitor where - C: ?Sized + Context, + C: Context, { type Ok = Cow<'de, $ty>; @@ -178,7 +178,7 @@ macro_rules! cow { #[inline] fn visit_owned( self, - $cx: &C, + $cx: C, $owned: <$source as ToOwned>::Owned, ) -> Result { Ok($owned_expr) @@ -187,18 +187,14 @@ macro_rules! cow { #[inline] fn visit_borrowed( self, - $cx: &C, + $cx: C, $borrowed: &'de $source, ) -> Result { Ok($borrowed_expr) } #[inline] - fn visit_ref( - self, - $cx: &C, - $reference: &$source, - ) -> Result { + fn visit_ref(self, $cx: C, $reference: &$source) -> Result { Ok($reference_expr) } } @@ -298,34 +294,34 @@ macro_rules! slice_sequence { $($extra: $extra_bound0 $(+ $extra_bound)*),* { #[inline] - fn new(_: &C) -> Result + fn new(_: C) -> Result where - C: ?Sized + Context, + C: Context, { Ok(Builder($ty::new(), PhantomData)) } #[inline] - fn with_capacity(_: &C, size: usize) -> Result + fn with_capacity(_: C, size: usize) -> Result where - C: ?Sized + Context, + C: Context, { Ok(Builder($ty::with_capacity(size), PhantomData)) } #[inline] - fn push(&mut self, _: &C, value: T) -> Result<(), C::Error> + fn push(&mut self, _: C, value: T) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { self.0.$insert(value); Ok(()) } #[inline] - fn reserve(&mut self, _: &C, capacity: usize) -> Result<(), C::Error> + fn reserve(&mut self, _: C, capacity: usize) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { self.0.reserve(capacity); Ok(()) @@ -392,19 +388,19 @@ macro_rules! sequence { { let hint = SequenceHint::with_size(self.len()); - encoder.cx(|$cx, encoder| { - encoder.encode_sequence_fn(&hint, |seq| { - let mut index = 0; + let $cx = encoder.cx(); - for value in self { - $cx.enter_sequence_index(index); - seq.push(value)?; - $cx.leave_sequence_index(); - index = index.wrapping_add(1); - } + encoder.encode_sequence_fn(&hint, |seq| { + let mut index = 0; - Ok(()) - }) + for value in self { + $cx.enter_sequence_index(index); + seq.push(value)?; + $cx.leave_sequence_index(); + index = index.wrapping_add(1); + } + + Ok(()) }) } @@ -427,21 +423,21 @@ macro_rules! sequence { where D: Decoder<'de, Mode = M>, { - decoder.cx(|$cx, decoder| { - decoder.decode_sequence(|$access| { - let mut out = $factory; + let $cx = decoder.cx(); - let mut index = 0; + decoder.decode_sequence(|$access| { + let mut out = $factory; - while let Some(value) = $access.try_decode_next()? { - $cx.enter_sequence_index(index); - out.$insert(value.decode()?); - $cx.leave_sequence_index(); - index = index.wrapping_add(1); - } + let mut index = 0; + + while let Some(value) = $access.try_decode_next()? { + $cx.enter_sequence_index(index); + out.$insert(value.decode()?); + $cx.leave_sequence_index(); + index = index.wrapping_add(1); + } - Ok(out) - }) + Ok(out) }) } } @@ -457,19 +453,19 @@ macro_rules! sequence { where E: Encoder, { - encoder.cx(|$cx, encoder| { - encoder.encode_pack_fn(|pack| { - let mut index = 0; - - for value in self { - $cx.enter_sequence_index(index); - pack.push(value)?; - $cx.leave_sequence_index(); - index = index.wrapping_add(1); - } + let $cx = encoder.cx(); - Ok(()) - }) + encoder.encode_pack_fn(|pack| { + let mut index = 0; + + for value in self { + $cx.enter_sequence_index(index); + pack.push(value)?; + $cx.leave_sequence_index(); + index = index.wrapping_add(1); + } + + Ok(()) }) } } @@ -605,20 +601,20 @@ macro_rules! map { { let hint = MapHint::with_size(self.len()); - encoder.cx(|$cx, encoder| { - encoder.encode_map_fn(&hint, |map| { - for (k, v) in self { - $cx.enter_map_key(k); - map.encode_entry_fn(|entry| { - entry.encode_key()?.encode(k)?; - entry.encode_value()?.encode(v)?; - Ok(()) - })?; - $cx.leave_map_key(); - } + let $cx = encoder.cx(); - Ok(()) - }) + encoder.encode_map_fn(&hint, |map| { + for (k, v) in self { + $cx.enter_map_key(k); + map.encode_entry_fn(|entry| { + entry.encode_key()?.encode(k)?; + entry.encode_value()?.encode(v)?; + Ok(()) + })?; + $cx.leave_map_key(); + } + + Ok(()) }) } } @@ -661,20 +657,20 @@ macro_rules! map { where D: Decoder<'de, Mode = M>, { - decoder.cx(|$cx, decoder| { - decoder.decode_map(|$access| { - let mut out = $with_capacity; - - while let Some(mut entry) = $access.decode_entry()? { - let key = entry.decode_key()?.decode()?; - $cx.enter_map_key(&key); - let value = entry.decode_value()?.decode()?; - out.insert(key, value); - $cx.leave_map_key(); - } + let $cx = decoder.cx(); - Ok(out) - }) + decoder.decode_map(|$access| { + let mut out = $with_capacity; + + while let Some(mut entry) = $access.decode_entry()? { + let key = entry.decode_key()?.decode()?; + $cx.enter_map_key(&key); + let value = entry.decode_value()?.decode()?; + out.insert(key, value); + $cx.leave_map_key(); + } + + Ok(out) }) } } @@ -723,7 +719,7 @@ impl<'de, M> Decode<'de, M> for CString { impl<'de, C> UnsizedVisitor<'de, C, [u8]> for Visitor where - C: ?Sized + Context, + C: Context, { type Ok = CString; @@ -733,17 +729,17 @@ impl<'de, M> Decode<'de, M> for CString { } #[inline] - fn visit_owned(self, cx: &C, value: Vec) -> Result { + fn visit_owned(self, cx: C, value: Vec) -> Result { CString::from_vec_with_nul(value).map_err(cx.map()) } #[inline] - fn visit_borrowed(self, cx: &C, bytes: &'de [u8]) -> Result { + fn visit_borrowed(self, cx: C, bytes: &'de [u8]) -> Result { self.visit_ref(cx, bytes) } #[inline] - fn visit_ref(self, cx: &C, bytes: &[u8]) -> Result { + fn visit_ref(self, cx: C, bytes: &[u8]) -> Result { Ok(CStr::from_bytes_with_nul(bytes) .map_err(cx.map())? .to_owned()) @@ -889,35 +885,35 @@ where use crate::alloc::{Allocator, RawVec}; use crate::en::VariantEncoder; - encoder.cx(|cx, encoder| { - encoder.encode_variant_fn(|variant| { - let mut buf = cx.alloc().new_raw_vec::(); - let mut len = 0; + let cx = encoder.cx(); - for w in self.encode_wide() { - let bytes = w.to_le_bytes(); + encoder.encode_variant_fn(|variant| { + let mut buf = cx.alloc().new_raw_vec::(); + let mut len = 0; - if !buf.resize(len, bytes.len()) { - return Err(cx.message("Allocation failed")); - } + for w in self.encode_wide() { + let bytes = w.to_le_bytes(); - // SAFETY: We've just resized the above buffer. - unsafe { - buf.as_mut_ptr() - .add(len) - .copy_from_nonoverlapping(bytes.as_ptr(), bytes.len()); - } + if !buf.resize(len, bytes.len()) { + return Err(cx.message("Allocation failed")); + } - len += bytes.len(); + // SAFETY: We've just resized the above buffer. + unsafe { + buf.as_mut_ptr() + .add(len) + .copy_from_nonoverlapping(bytes.as_ptr(), bytes.len()); } - // SAFETY: Slice does not outlive the buffer it references. - let bytes = unsafe { core::slice::from_raw_parts(buf.as_ptr(), len) }; + len += bytes.len(); + } + + // SAFETY: Slice does not outlive the buffer it references. + let bytes = unsafe { core::slice::from_raw_parts(buf.as_ptr(), len) }; - variant.encode_tag()?.encode(&PlatformTag::Windows)?; - variant.encode_data()?.encode_bytes(bytes)?; - Ok(()) - }) + variant.encode_tag()?.encode(&PlatformTag::Windows)?; + variant.encode_data()?.encode_bytes(bytes)?; + Ok(()) }) } @@ -966,59 +962,57 @@ where { use crate::de::VariantDecoder; - decoder.cx(|cx, decoder| { - decoder.decode_variant(|variant| { - let tag = variant.decode_tag()?.decode::()?; - - match tag { - #[cfg(not(unix))] - PlatformTag::Unix => Err(cx.message("Unsupported OsString::Unix variant")), - #[cfg(unix)] - PlatformTag::Unix => { - use std::os::unix::ffi::OsStringExt; - Ok(OsString::from_vec(variant.decode_value()?.decode()?)) - } - #[cfg(not(windows))] - PlatformTag::Windows => { - Err(cx.message("Unsupported OsString::Windows variant")) - } - #[cfg(windows)] - PlatformTag::Windows => { - use std::os::windows::ffi::OsStringExt; + let cx = decoder.cx(); - struct Visitor; + decoder.decode_variant(|variant| { + let tag = variant.decode_tag()?.decode::()?; - impl UnsizedVisitor<'_, C, [u8]> for Visitor - where - C: ?Sized + Context, - { - type Ok = OsString; + match tag { + #[cfg(not(unix))] + PlatformTag::Unix => Err(cx.message("Unsupported OsString::Unix variant")), + #[cfg(unix)] + PlatformTag::Unix => { + use std::os::unix::ffi::OsStringExt; + Ok(OsString::from_vec(variant.decode_value()?.decode()?)) + } + #[cfg(not(windows))] + PlatformTag::Windows => Err(cx.message("Unsupported OsString::Windows variant")), + #[cfg(windows)] + PlatformTag::Windows => { + use std::os::windows::ffi::OsStringExt; - #[inline] - fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "a literal byte reference") - } + struct Visitor; - #[inline] - fn visit_ref(self, _: &C, bytes: &[u8]) -> Result { - let mut buf = Vec::with_capacity(bytes.len() / 2); + impl UnsizedVisitor<'_, C, [u8]> for Visitor + where + C: Context, + { + type Ok = OsString; - for pair in bytes.chunks_exact(2) { - let &[a, b] = pair else { - continue; - }; + #[inline] + fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "a literal byte reference") + } + + #[inline] + fn visit_ref(self, _: C, bytes: &[u8]) -> Result { + let mut buf = Vec::with_capacity(bytes.len() / 2); - buf.push(u16::from_le_bytes([a, b])); - } + for pair in bytes.chunks_exact(2) { + let &[a, b] = pair else { + continue; + }; - Ok(OsString::from_wide(&buf)) + buf.push(u16::from_le_bytes([a, b])); } - } - variant.decode_value()?.decode_bytes(Visitor) + Ok(OsString::from_wide(&buf)) + } } + + variant.decode_value()?.decode_bytes(Visitor) } - }) + } }) } } @@ -1138,7 +1132,7 @@ impl<'de, M> DecodeBytes<'de, M> for Vec { impl<'de, C> UnsizedVisitor<'de, C, [u8]> for Visitor where - C: ?Sized + Context, + C: Context, { type Ok = Vec; @@ -1148,12 +1142,12 @@ impl<'de, M> DecodeBytes<'de, M> for Vec { } #[inline] - fn visit_borrowed(self, _: &C, bytes: &'de [u8]) -> Result { + fn visit_borrowed(self, _: C, bytes: &'de [u8]) -> Result { Ok(bytes.to_vec()) } #[inline] - fn visit_ref(self, _: &C, bytes: &[u8]) -> Result { + fn visit_ref(self, _: C, bytes: &[u8]) -> Result { Ok(bytes.to_vec()) } } diff --git a/crates/musli-core/src/impls/mod.rs b/crates/musli-core/src/impls/mod.rs index 05d940a8e..a8698619e 100644 --- a/crates/musli-core/src/impls/mod.rs +++ b/crates/musli-core/src/impls/mod.rs @@ -151,17 +151,16 @@ macro_rules! non_zero { where D: Decoder<'de>, { - decoder.cx(|cx, decoder| { - let value = decoder.decode()?; - - match Self::new(value) { - Some(value) => Ok(value), - None => Err(cx.message(NonZeroUnsupportedValue { - type_name: stringify!($ty), - value, - })), - } - }) + let cx = decoder.cx(); + let value = decoder.decode()?; + + match Self::new(value) { + Some(value) => Ok(value), + None => Err(cx.message(NonZeroUnsupportedValue { + type_name: stringify!($ty), + value, + })), + } } } }; @@ -233,28 +232,27 @@ where where D: Decoder<'de, Mode = M>, { - decoder.cx(|cx, decoder| { - let mark = cx.mark(); + let cx = decoder.cx(); + let mark = cx.mark(); - decoder.decode_sequence(|seq| { - let mut array = crate::internal::FixedVec::new(); + decoder.decode_sequence(|seq| { + let mut array = crate::internal::FixedVec::new(); - while let Some(item) = seq.try_decode_next()? { - array.try_push(item.decode()?).map_err(cx.map())?; - } + while let Some(item) = seq.try_decode_next()? { + array.try_push(item.decode()?).map_err(cx.map())?; + } - if array.len() != N { - return Err(cx.marked_message( - &mark, - format_args!( - "Array with length {} does not have the expected {N} number of elements", - array.len() - ), - )); - } + if array.len() != N { + return Err(cx.marked_message( + &mark, + format_args!( + "Array with length {} does not have the expected {N} number of elements", + array.len() + ), + )); + } - Ok(array.into_inner()) - }) + Ok(array.into_inner()) }) } } @@ -287,17 +285,17 @@ where where D: Decoder<'de, Mode = M>, { - decoder.cx(|cx, decoder| { - decoder.decode_pack(|pack| { - let mut array = crate::internal::FixedVec::new(); + let cx = decoder.cx(); - while array.len() < N { - let item = pack.decode_next()?; - array.try_push(item.decode()?).map_err(cx.map())?; - } + decoder.decode_pack(|pack| { + let mut array = crate::internal::FixedVec::new(); - Ok(array.into_inner()) - }) + while array.len() < N { + let item = pack.decode_next()?; + array.try_push(item.decode()?).map_err(cx.map())?; + } + + Ok(array.into_inner()) }) } } @@ -452,7 +450,7 @@ impl<'de, M> Decode<'de, M> for &'de str { impl<'de, C> UnsizedVisitor<'de, C, str> for Visitor where - C: ?Sized + Context, + C: Context, { type Ok = &'de str; @@ -462,7 +460,7 @@ impl<'de, M> Decode<'de, M> for &'de str { } #[inline] - fn visit_borrowed(self, _: &C, string: &'de str) -> Result { + fn visit_borrowed(self, _: C, string: &'de str) -> Result { Ok(string) } } @@ -482,7 +480,7 @@ impl<'de, M> DecodeUnsized<'de, M> for str { impl UnsizedVisitor<'_, C, str> for Visitor where - C: ?Sized + Context, + C: Context, F: FnOnce(&str) -> Result, { type Ok = O; @@ -493,7 +491,7 @@ impl<'de, M> DecodeUnsized<'de, M> for str { } #[inline] - fn visit_ref(self, _: &C, string: &str) -> Result { + fn visit_ref(self, _: C, string: &str) -> Result { (self.0)(string) } } @@ -513,7 +511,7 @@ impl<'de, M> DecodeUnsized<'de, M> for [u8] { impl UnsizedVisitor<'_, C, [u8]> for Visitor where - C: ?Sized + Context, + C: Context, F: FnOnce(&[u8]) -> Result, { type Ok = O; @@ -524,7 +522,7 @@ impl<'de, M> DecodeUnsized<'de, M> for [u8] { } #[inline] - fn visit_ref(self, _: &C, bytes: &[u8]) -> Result { + fn visit_ref(self, _: C, bytes: &[u8]) -> Result { (self.0)(bytes) } } @@ -567,7 +565,7 @@ impl<'de, M> Decode<'de, M> for &'de [u8] { impl<'de, C> UnsizedVisitor<'de, C, [u8]> for Visitor where - C: ?Sized + Context, + C: Context, { type Ok = &'de [u8]; @@ -577,7 +575,7 @@ impl<'de, M> Decode<'de, M> for &'de [u8] { } #[inline] - fn visit_borrowed(self, _: &C, bytes: &'de [u8]) -> Result { + fn visit_borrowed(self, _: C, bytes: &'de [u8]) -> Result { Ok(bytes) } } @@ -597,7 +595,7 @@ impl<'de, M> DecodeUnsizedBytes<'de, M> for [u8] { impl UnsizedVisitor<'_, C, [u8]> for Visitor where - C: ?Sized + Context, + C: Context, F: FnOnce(&[u8]) -> Result, { type Ok = O; @@ -608,7 +606,7 @@ impl<'de, M> DecodeUnsizedBytes<'de, M> for [u8] { } #[inline] - fn visit_ref(self, _: &C, bytes: &[u8]) -> Result { + fn visit_ref(self, _: C, bytes: &[u8]) -> Result { (self.0)(bytes) } } @@ -785,10 +783,9 @@ impl<'de, M> Decode<'de, M> for &'de CStr { where D: Decoder<'de>, { - decoder.cx(|cx, decoder| { - let bytes = decoder.decode()?; - CStr::from_bytes_with_nul(bytes).map_err(cx.map()) - }) + let cx = decoder.cx(); + let bytes = decoder.decode()?; + CStr::from_bytes_with_nul(bytes).map_err(cx.map()) } } @@ -799,11 +796,11 @@ impl<'de, M> DecodeUnsized<'de, M> for CStr { D: Decoder<'de, Mode = M>, F: FnOnce(&Self) -> Result, { - decoder.cx(|cx, decoder| { - DecodeUnsizedBytes::decode_unsized_bytes(decoder, |bytes: &[u8]| { - let cstr = CStr::from_bytes_with_nul(bytes).map_err(cx.map())?; - f(cstr) - }) + let cx = decoder.cx(); + + DecodeUnsizedBytes::decode_unsized_bytes(decoder, |bytes: &[u8]| { + let cstr = CStr::from_bytes_with_nul(bytes).map_err(cx.map())?; + f(cstr) }) } } diff --git a/crates/musli-core/src/impls/net.rs b/crates/musli-core/src/impls/net.rs index b7d2a52ec..1f0e02508 100644 --- a/crates/musli-core/src/impls/net.rs +++ b/crates/musli-core/src/impls/net.rs @@ -88,9 +88,8 @@ impl<'de> Decode<'de, Text> for Ipv4Addr { where D: Decoder<'de>, { - decoder.cx(|cx, decoder| { - decoder.decode_unsized(|string: &str| Ipv4Addr::from_str(string).map_err(cx.map())) - }) + let cx = decoder.cx(); + decoder.decode_unsized(|string: &str| Ipv4Addr::from_str(string).map_err(cx.map())) } } @@ -160,9 +159,8 @@ impl<'de> Decode<'de, Text> for Ipv6Addr { where D: Decoder<'de>, { - decoder.cx(|cx, decoder| { - decoder.decode_unsized(|string: &str| Ipv6Addr::from_str(string).map_err(cx.map())) - }) + let cx = decoder.cx(); + decoder.decode_unsized(|string: &str| Ipv6Addr::from_str(string).map_err(cx.map())) } } @@ -289,9 +287,8 @@ impl<'de> Decode<'de, Text> for SocketAddrV4 { where D: Decoder<'de>, { - decoder.cx(|cx, decoder| { - decoder.decode_unsized(|string: &str| SocketAddrV4::from_str(string).map_err(cx.map())) - }) + let cx = decoder.cx(); + decoder.decode_unsized(|string: &str| SocketAddrV4::from_str(string).map_err(cx.map())) } } @@ -367,9 +364,8 @@ impl<'de> Decode<'de, Text> for SocketAddrV6 { where D: Decoder<'de>, { - decoder.cx(|cx, decoder| { - decoder.decode_unsized(|string: &str| SocketAddrV6::from_str(string).map_err(cx.map())) - }) + let cx = decoder.cx(); + decoder.decode_unsized(|string: &str| SocketAddrV6::from_str(string).map_err(cx.map())) } } diff --git a/crates/musli-core/src/lib.rs b/crates/musli-core/src/lib.rs index 33ba4a612..6acdc533b 100644 --- a/crates/musli-core/src/lib.rs +++ b/crates/musli-core/src/lib.rs @@ -60,25 +60,22 @@ mod never; /// use musli_core::Context; /// use musli_core::en::{Encoder, Encode}; /// -/// struct MyEncoder<'a, C: ?Sized> { +/// struct MyEncoder<'a, C> { /// value: &'a mut Option, -/// cx: &'a C, +/// cx: C, /// } /// /// #[musli_core::encoder(crate = musli_core)] /// impl Encoder for MyEncoder<'_, C> /// where -/// C: ?Sized + Context, +/// C: Context, /// { /// type Cx = C; /// type Ok = (); /// /// #[inline] -/// fn cx(self, f: F) -> O -/// where -/// F: FnOnce(&Self::Cx, Self) -> O, -/// { -/// f(self.cx, self) +/// fn cx(&self) -> Self::Cx { +/// self.cx /// } /// /// fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -122,20 +119,20 @@ pub use musli_macros::encoder; /// use musli_core::Context; /// use musli_core::de::{Decoder, Decode}; /// -/// struct MyDecoder<'a, C: ?Sized> { -/// cx: &'a C, +/// struct MyDecoder { +/// cx: C, /// } /// /// #[musli_core::decoder(crate = musli_core)] -/// impl<'de, C: ?Sized + Context> Decoder<'de> for MyDecoder<'_, C> { +/// impl<'de, C> Decoder<'de> for MyDecoder +/// where +/// C: Context, +/// { /// type Cx = C; /// /// #[inline] -/// fn cx(self, f: F) -> O -/// where -/// F: FnOnce(&Self::Cx, Self) -> O, -/// { -/// f(self.cx, self) +/// fn cx(&self) -> Self::Cx { +/// self.cx /// } /// /// fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -175,7 +172,10 @@ pub use musli_macros::decoder; /// struct AnyVisitor; /// /// #[musli_core::visitor(crate = musli_core)] -/// impl<'de, C: ?Sized + Context> Visitor<'de, C> for AnyVisitor { +/// impl<'de, C> Visitor<'de, C> for AnyVisitor +/// where +/// C: ?Sized + Context, +/// { /// type Ok = (); /// /// #[inline] diff --git a/crates/musli-core/src/never.rs b/crates/musli-core/src/never.rs index 575a7c25c..df85d9a05 100644 --- a/crates/musli-core/src/never.rs +++ b/crates/musli-core/src/never.rs @@ -36,21 +36,21 @@ pub enum NeverMarker {} /// use musli::Context; /// use musli::de::{Decoder, Decode}; /// -/// struct MyDecoder<'a, C: ?Sized> { -/// cx: &'a C, +/// struct MyDecoder { +/// cx: C, /// number: u32, /// } /// /// #[musli::decoder] -/// impl<'de, C: ?Sized + Context> Decoder<'de> for MyDecoder<'_, C> where { +/// impl<'de, C> Decoder<'de> for MyDecoder +/// where +/// C: Context, +/// { /// type Cx = C; /// /// #[inline] -/// fn cx(self, f: F) -> O -/// where -/// F: FnOnce(&Self::Cx, Self) -> O, -/// { -/// f(self.cx, self) +/// fn cx(&self) -> Self::Cx { +/// self.cx /// } /// /// fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -99,15 +99,15 @@ impl RawVec for Never { impl<'de, C> Decoder<'de> for Never<(), C> where - C: ?Sized + Context, + C: Context, { type Cx = C; type Error = C::Error; type Mode = C::Mode; - type WithContext<'this, U> + type WithContext = Never<(), U> where - U: 'this + Context; + U: Context; type DecodeBuffer = Self; type DecodePack = Self; type DecodeSequence = Self; @@ -118,15 +118,12 @@ where type __UseMusliDecoderAttributeMacro = (); #[inline] - fn cx(self, _: F) -> O - where - F: FnOnce(&Self::Cx, Self) -> O, - { + fn cx(&self) -> Self::Cx { match self._never {} } #[inline] - fn with_context(self, _: &U) -> Result, C::Error> + fn with_context(self, _: U) -> Result, C::Error> where U: Context, { @@ -167,7 +164,7 @@ where impl AsDecoder for Never<(), C> where - C: ?Sized + Context, + C: Context, { type Cx = C; type Decoder<'this> @@ -183,7 +180,7 @@ where impl EntriesDecoder<'_> for Never<(), C> where - C: ?Sized + Context, + C: Context, { type Cx = C; type DecodeEntryKey<'this> @@ -213,7 +210,7 @@ where impl VariantDecoder<'_> for Never<(), C> where - C: ?Sized + Context, + C: Context, { type Cx = C; type DecodeTag<'this> @@ -238,7 +235,7 @@ where impl MapDecoder<'_> for Never<(), C> where - C: ?Sized + Context, + C: Context, { type Cx = C; type DecodeEntry<'this> @@ -270,7 +267,7 @@ where impl EntryDecoder<'_> for Never<(), C> where - C: ?Sized + Context, + C: Context, { type Cx = C; type DecodeKey<'this> @@ -292,7 +289,7 @@ where impl SequenceDecoder<'_> for Never<(), C> where - C: ?Sized + Context, + C: Context, { type Cx = C; type DecodeNext<'this> @@ -313,17 +310,17 @@ where impl Encoder for Never where - C: ?Sized + Context, + C: Context, A: 'static, { type Cx = C; type Error = C::Error; type Ok = A; type Mode = C::Mode; - type WithContext<'this, U> + type WithContext = Never where - U: 'this + Context; + U: Context; type EncodePack = Self; type EncodeSome = Self; type EncodeSequence = Self; @@ -335,15 +332,12 @@ where type __UseMusliEncoderAttributeMacro = (); #[inline] - fn cx(self, _: F) -> O - where - F: FnOnce(&Self::Cx, Self) -> O, - { + fn cx(&self) -> Self::Cx { match self._never {} } #[inline] - fn with_context(self, _: &U) -> Result, C::Error> + fn with_context(self, _: U) -> Result, C::Error> where U: Context, { @@ -366,7 +360,7 @@ where impl UnsizedVisitor<'_, C, T> for Never where - C: ?Sized + Context, + C: Context, T: ?Sized + ToOwned, { type Ok = A; @@ -379,7 +373,7 @@ where impl SequenceEncoder for Never where A: 'static, - C: ?Sized + Context, + C: Context, { type Cx = C; type Ok = A; @@ -389,10 +383,7 @@ where Self: 'this; #[inline] - fn cx_mut(&mut self, _: F) -> O - where - F: FnOnce(&Self::Cx, &mut Self) -> O, - { + fn cx(&self) -> Self::Cx { match self._never {} } @@ -410,7 +401,7 @@ where impl MapEncoder for Never where A: 'static, - C: ?Sized + Context, + C: Context, { type Cx = C; type Ok = A; @@ -432,7 +423,7 @@ where impl EntryEncoder for Never where A: 'static, - C: ?Sized + Context, + C: Context, { type Cx = C; type Ok = A; @@ -464,7 +455,7 @@ where impl EntriesEncoder for Never where A: 'static, - C: ?Sized + Context, + C: Context, { type Cx = C; type Ok = A; @@ -496,7 +487,7 @@ where impl VariantEncoder for Never where A: 'static, - C: ?Sized + Context, + C: Context, { type Cx = C; type Ok = A; diff --git a/crates/musli-macros/src/de.rs b/crates/musli-macros/src/de.rs index 9cdf123a8..11d5cf23c 100644 --- a/crates/musli-macros/src/de.rs +++ b/crates/musli-macros/src/de.rs @@ -118,15 +118,15 @@ pub(crate) fn expand_decode_entry(e: Build<'_>) -> Result { where #d_param: #decoder_t<#lt, Mode = #mode_ident>, { - #decoder_t::cx(#decoder_var, |#ctx_var, #decoder_var| { - let #decoder_var = match #decoder_t::try_fast_decode(#decoder_var)? { - #try_fast_decode::Ok(value) => return #result::Ok(value), - #try_fast_decode::Unsupported(#decoder_var) => #decoder_var, - _ => return #result::Err(#context_t::message(#ctx_var, "Fast decoding failed")), - }; + let #ctx_var = #decoder_t::cx(&#decoder_var); - #body - }) + let #decoder_var = match #decoder_t::try_fast_decode(#decoder_var)? { + #try_fast_decode::Ok(value) => return #result::Ok(value), + #try_fast_decode::Unsupported(#decoder_var) => #decoder_var, + _ => return #result::Err(#context_t::message(#ctx_var, "Fast decoding failed")), + }; + + #body } } }; diff --git a/crates/musli-macros/src/en.rs b/crates/musli-macros/src/en.rs index 487f1ce21..9eb51f1b1 100644 --- a/crates/musli-macros/src/en.rs +++ b/crates/musli-macros/src/en.rs @@ -98,15 +98,15 @@ pub(crate) fn expand_insert_entry(e: Build<'_>) -> Result { where #e_param: #encoder_t, { - #encoder_t::cx(#encoder_var, |#ctx_var, #encoder_var| { - let #encoder_var = match #encoder_t::try_fast_encode(#encoder_var, self)? { - #try_fast_encode::Ok(value) => return #result::Ok(value), - #try_fast_encode::Unsupported(_, #encoder_var) => #encoder_var, - _ => return #result::Err(#context_t::message(#ctx_var, "Fast encoding failed")), - }; - - #body - }) + let #ctx_var = #encoder_t::cx(&#encoder_var); + + let #encoder_var = match #encoder_t::try_fast_encode(#encoder_var, self)? { + #try_fast_encode::Ok(value) => return #result::Ok(value), + #try_fast_encode::Unsupported(_, #encoder_var) => #encoder_var, + _ => return #result::Err(#context_t::message(#ctx_var, "Fast encoding failed")), + }; + + #body } #[inline] diff --git a/crates/musli-macros/src/types.rs b/crates/musli-macros/src/types.rs index 42a10dca9..23b3eb859 100644 --- a/crates/musli-macros/src/types.rs +++ b/crates/musli-macros/src/types.rs @@ -217,15 +217,6 @@ impl Types { let mut params = Punctuated::default(); - let this_lifetime = syn::Lifetime::new("'this", Span::call_site()); - - params.push(syn::GenericParam::Lifetime(syn::LifetimeParam { - attrs: Vec::new(), - lifetime: this_lifetime.clone(), - colon_token: None, - bounds: Punctuated::default(), - })); - params.push(syn::GenericParam::Type(syn::TypeParam { attrs: Vec::new(), ident: u_param.clone(), @@ -247,7 +238,7 @@ impl Types { where_clause .predicates - .push(syn::parse_quote!(#u_param: #this_lifetime + #crate_path::Context)); + .push(syn::parse_quote!(#u_param: #crate_path::Context)); generics = syn::Generics { lt_token: Some(::default()), diff --git a/crates/musli/src/context/capture.rs b/crates/musli/src/context/capture.rs index ba8740e95..8010285ca 100644 --- a/crates/musli/src/context/capture.rs +++ b/crates/musli/src/context/capture.rs @@ -63,7 +63,7 @@ where } } -impl Context for Capture +impl Context for &Capture where M: 'static, E: ContextError, @@ -76,7 +76,7 @@ where type String = String; #[inline] - fn clear(&self) { + fn clear(self) { // SAFETY: We're restricting access to the context, so that this is // safe. unsafe { @@ -85,18 +85,18 @@ where } #[inline] - fn mark(&self) -> Self::Mark {} + fn mark(self) -> Self::Mark {} #[inline] - fn advance(&self, _: usize) {} + fn advance(self, _: usize) {} #[inline] - fn alloc(&self) -> Self::Allocator { + fn alloc(self) -> Self::Allocator { self.alloc.clone() } #[inline] - fn collect_string(&self, value: &T) -> Result + fn collect_string(self, value: &T) -> Result where T: ?Sized + fmt::Display, { @@ -107,7 +107,7 @@ where } #[inline] - fn custom(&self, error: T) -> ErrorMarker + fn custom(self, error: T) -> ErrorMarker where T: 'static + Send + Sync + Error, { @@ -123,7 +123,7 @@ where } #[inline] - fn message(&self, message: T) -> ErrorMarker + fn message(self, message: T) -> ErrorMarker where T: fmt::Display, { diff --git a/crates/musli/src/context/default_context.rs b/crates/musli/src/context/default_context.rs index b6a6576ad..d43f73e5a 100644 --- a/crates/musli/src/context/default_context.rs +++ b/crates/musli/src/context/default_context.rs @@ -166,7 +166,7 @@ where } } -impl Context for DefaultContext +impl Context for &DefaultContext where A: Clone + Allocator, M: 'static, @@ -178,7 +178,7 @@ where type String = String; #[inline] - fn clear(&self) { + fn clear(self) { self.mark.set(0); let _access = self.access.exclusive(); @@ -190,12 +190,12 @@ where } #[inline] - fn alloc(&self) -> Self::Allocator { + fn alloc(self) -> Self::Allocator { self.alloc.clone() } #[inline] - fn collect_string(&self, value: &T) -> Result + fn collect_string(self, value: &T) -> Result where T: ?Sized + fmt::Display, { @@ -206,7 +206,7 @@ where } #[inline] - fn custom(&self, message: T) -> Self::Error + fn custom(self, message: T) -> Self::Error where T: 'static + Send + Sync + fmt::Display + fmt::Debug, { @@ -218,7 +218,7 @@ where } #[inline] - fn message(&self, message: T) -> Self::Error + fn message(self, message: T) -> Self::Error where T: fmt::Display, { @@ -230,7 +230,7 @@ where } #[inline] - fn marked_message(&self, mark: &Self::Mark, message: T) -> Self::Error + fn marked_message(self, mark: &Self::Mark, message: T) -> Self::Error where T: fmt::Display, { @@ -242,7 +242,7 @@ where } #[inline] - fn marked_custom(&self, mark: &Self::Mark, message: T) -> Self::Error + fn marked_custom(self, mark: &Self::Mark, message: T) -> Self::Error where T: 'static + Send + Sync + fmt::Display + fmt::Debug, { @@ -254,17 +254,17 @@ where } #[inline] - fn mark(&self) -> Self::Mark { + fn mark(self) -> Self::Mark { self.mark.get() } #[inline] - fn advance(&self, n: usize) { + fn advance(self, n: usize) { self.mark.set(self.mark.get().wrapping_add(n)); } #[inline] - fn enter_named_field(&self, name: &'static str, _: &T) + fn enter_named_field(self, name: &'static str, _: &T) where T: ?Sized + fmt::Display, { @@ -272,7 +272,7 @@ where } #[inline] - fn enter_unnamed_field(&self, index: u32, _: &T) + fn enter_unnamed_field(self, index: u32, _: &T) where T: ?Sized + fmt::Display, { @@ -280,60 +280,60 @@ where } #[inline] - fn leave_field(&self) { + fn leave_field(self) { self.pop_path(); } #[inline] - fn enter_struct(&self, name: &'static str) { + fn enter_struct(self, name: &'static str) { if self.include_type { self.push_path(Step::Struct(name)); } } #[inline] - fn leave_struct(&self) { + fn leave_struct(self) { if self.include_type { self.pop_path(); } } #[inline] - fn enter_enum(&self, name: &'static str) { + fn enter_enum(self, name: &'static str) { if self.include_type { self.push_path(Step::Enum(name)); } } #[inline] - fn leave_enum(&self) { + fn leave_enum(self) { if self.include_type { self.pop_path(); } } #[inline] - fn enter_variant(&self, name: &'static str, _: T) { + fn enter_variant(self, name: &'static str, _: T) { self.push_path(Step::Variant(name)); } #[inline] - fn leave_variant(&self) { + fn leave_variant(self) { self.pop_path(); } #[inline] - fn enter_sequence_index(&self, index: usize) { + fn enter_sequence_index(self, index: usize) { self.push_path(Step::Index(index)); } #[inline] - fn leave_sequence_index(&self) { + fn leave_sequence_index(self) { self.pop_path(); } #[inline] - fn enter_map_key(&self, field: T) + fn enter_map_key(self, field: T) where T: fmt::Display, { @@ -343,7 +343,7 @@ where } #[inline] - fn leave_map_key(&self) { + fn leave_map_key(self) { self.pop_path(); } } diff --git a/crates/musli/src/context/ignore.rs b/crates/musli/src/context/ignore.rs index 52e00cb44..7c5ecca7e 100644 --- a/crates/musli/src/context/ignore.rs +++ b/crates/musli/src/context/ignore.rs @@ -62,7 +62,7 @@ impl Ignore { } } -impl Context for Ignore +impl Context for &Ignore where M: 'static, A: Clone + Allocator, @@ -74,21 +74,21 @@ where type String = String; #[inline] - fn clear(&self) {} + fn clear(self) {} #[inline] - fn mark(&self) -> Self::Mark {} + fn mark(self) -> Self::Mark {} #[inline] - fn advance(&self, _: usize) {} + fn advance(self, _: usize) {} #[inline] - fn alloc(&self) -> Self::Allocator { + fn alloc(self) -> Self::Allocator { self.alloc.clone() } #[inline] - fn collect_string(&self, value: &T) -> Result + fn collect_string(self, value: &T) -> Result where T: ?Sized + fmt::Display, { @@ -99,7 +99,7 @@ where } #[inline] - fn custom(&self, _: T) -> ErrorMarker + fn custom(self, _: T) -> ErrorMarker where T: 'static + Send + Sync + fmt::Display + fmt::Debug, { @@ -108,7 +108,7 @@ where } #[inline] - fn message(&self, _: T) -> ErrorMarker + fn message(self, _: T) -> ErrorMarker where T: fmt::Display, { diff --git a/crates/musli/src/context/same.rs b/crates/musli/src/context/same.rs index a65dfa2e9..046cb9230 100644 --- a/crates/musli/src/context/same.rs +++ b/crates/musli/src/context/same.rs @@ -70,7 +70,7 @@ where } } -impl Context for Same +impl Context for &Same where M: 'static, E: ContextError, @@ -83,21 +83,21 @@ where type String = String; #[inline] - fn clear(&self) {} + fn clear(self) {} #[inline] - fn mark(&self) -> Self::Mark {} + fn mark(self) -> Self::Mark {} #[inline] - fn advance(&self, _: usize) {} + fn advance(self, _: usize) {} #[inline] - fn alloc(&self) -> Self::Allocator { + fn alloc(self) -> Self::Allocator { self.alloc.clone() } #[inline] - fn collect_string(&self, value: &T) -> Result + fn collect_string(self, value: &T) -> Result where T: ?Sized + fmt::Display, { @@ -108,7 +108,7 @@ where } #[inline] - fn custom(&self, message: T) -> Self::Error + fn custom(self, message: T) -> Self::Error where T: 'static + Send + Sync + Error, { @@ -116,7 +116,7 @@ where } #[inline] - fn message(&self, message: T) -> Self::Error + fn message(self, message: T) -> Self::Error where T: fmt::Display, { diff --git a/crates/musli/src/descriptive/de.rs b/crates/musli/src/descriptive/de.rs index fe182c28e..d6f122888 100644 --- a/crates/musli/src/descriptive/de.rs +++ b/crates/musli/src/descriptive/de.rs @@ -23,23 +23,23 @@ use super::tag::{Kind, Mark, Tag, F32, F64, I128, I16, I32, I64, I8, U128, U16, const BUFFER_OPTIONS: Options = options::new().build(); /// A very simple decoder. -pub struct SelfDecoder<'a, R, const OPT: Options, C: ?Sized> { - cx: &'a C, +pub struct SelfDecoder { + cx: C, reader: R, } -impl<'a, R, const OPT: Options, C: ?Sized> SelfDecoder<'a, R, OPT, C> { +impl SelfDecoder { /// Construct a new fixed width message encoder. #[inline] - pub(crate) fn new(cx: &'a C, reader: R) -> Self { + pub(crate) fn new(cx: C, reader: R) -> Self { Self { cx, reader } } } -impl<'de, R, const OPT: Options, C> SelfDecoder<'_, Limit, OPT, C> +impl<'de, R, const OPT: Options, C> SelfDecoder, OPT, C> where R: Reader<'de>, - C: ?Sized + Context, + C: Context, { #[inline] fn end(mut self) -> Result<(), C::Error> { @@ -51,10 +51,10 @@ where } } -impl<'a, 'de, R, const OPT: Options, C> SelfDecoder<'a, R, OPT, C> +impl<'de, R, const OPT: Options, C> SelfDecoder where R: Reader<'de>, - C: ?Sized + Context, + C: Context, { /// Skip over any sequences of values. pub(crate) fn skip_any(mut self) -> Result<(), C::Error> { @@ -111,7 +111,7 @@ where // Standard function for decoding a pair sequence. #[inline] - fn shared_decode_map(mut self) -> Result, C::Error> { + fn shared_decode_map(mut self) -> Result, C::Error> { let pos = self.cx.mark(); let len = self.decode_prefix(Kind::Map, &pos)?; Ok(RemainingSelfDecoder::new(self.cx, self.reader, len)) @@ -119,7 +119,7 @@ where // Standard function for decoding a pair sequence. #[inline] - fn shared_decode_sequence(mut self) -> Result, C::Error> { + fn shared_decode_sequence(mut self) -> Result, C::Error> { let pos = self.cx.mark(); let len = self.decode_prefix(Kind::Sequence, &pos)?; Ok(RemainingSelfDecoder::new(self.cx, self.reader, len)) @@ -169,19 +169,19 @@ where /// This simplifies implementing decoders that do not have any special handling /// for length-prefixed types. #[doc(hidden)] -pub struct RemainingSelfDecoder<'a, R, const OPT: Options, C: ?Sized> { - cx: &'a C, +pub struct RemainingSelfDecoder { + cx: C, reader: R, remaining: usize, } -impl<'a, 'de, R, const OPT: Options, C> RemainingSelfDecoder<'a, R, OPT, C> +impl<'de, R, const OPT: Options, C> RemainingSelfDecoder where R: Reader<'de>, - C: ?Sized + Context, + C: Context, { #[inline] - fn new(cx: &'a C, reader: R, remaining: usize) -> Self { + fn new(cx: C, reader: R, remaining: usize) -> Self { Self { cx, reader, @@ -214,37 +214,34 @@ where } #[crate::decoder(crate)] -impl<'a, 'de, R, const OPT: Options, C> Decoder<'de> for SelfDecoder<'a, R, OPT, C> +impl<'de, R, const OPT: Options, C> Decoder<'de> for SelfDecoder where R: Reader<'de>, - C: ?Sized + Context, + C: Context, { type Cx = C; type Error = C::Error; type Mode = C::Mode; - type WithContext<'this, U> - = SelfDecoder<'this, R, OPT, U> + type WithContext + = SelfDecoder where - U: 'this + Context; + U: Context; #[cfg(feature = "value")] - type DecodeBuffer = crate::value::AsValueDecoder<'a, BUFFER_OPTIONS, C>; - type DecodePack = SelfDecoder<'a, Limit, OPT, C>; + type DecodeBuffer = crate::value::AsValueDecoder; + type DecodePack = SelfDecoder, OPT, C>; type DecodeSome = Self; - type DecodeSequence = RemainingSelfDecoder<'a, R, OPT, C>; - type DecodeMap = RemainingSelfDecoder<'a, R, OPT, C>; - type DecodeMapEntries = RemainingSelfDecoder<'a, R, OPT, C>; + type DecodeSequence = RemainingSelfDecoder; + type DecodeMap = RemainingSelfDecoder; + type DecodeMapEntries = RemainingSelfDecoder; type DecodeVariant = Self; #[inline] - fn cx(self, f: F) -> O - where - F: FnOnce(&Self::Cx, Self) -> O, - { - f(self.cx, self) + fn cx(&self) -> Self::Cx { + self.cx } #[inline] - fn with_context(self, cx: &U) -> Result, C::Error> + fn with_context(self, cx: U) -> Result, C::Error> where U: Context, { @@ -329,7 +326,7 @@ where impl<'de, C, V> UnsizedVisitor<'de, C, [u8]> for Visitor where - C: ?Sized + Context, + C: Context, V: UnsizedVisitor<'de, C, str>, { type Ok = V::Ok; @@ -341,19 +338,19 @@ where #[cfg(feature = "alloc")] #[inline] - fn visit_owned(self, cx: &C, bytes: Vec) -> Result { + fn visit_owned(self, cx: C, bytes: Vec) -> Result { let string = crate::str::from_utf8_owned(bytes).map_err(cx.map())?; self.0.visit_owned(cx, string) } #[inline] - fn visit_borrowed(self, cx: &C, bytes: &'de [u8]) -> Result { + fn visit_borrowed(self, cx: C, bytes: &'de [u8]) -> Result { let string = crate::str::from_utf8(bytes).map_err(cx.map())?; self.0.visit_borrowed(cx, string) } #[inline] - fn visit_ref(self, cx: &C, bytes: &[u8]) -> Result { + fn visit_ref(self, cx: C, bytes: &[u8]) -> Result { let string = crate::str::from_utf8(bytes).map_err(cx.map())?; self.0.visit_ref(cx, string) } @@ -740,14 +737,14 @@ where } } -impl<'a, 'de, R, const OPT: Options, C> SequenceDecoder<'de> for SelfDecoder<'a, Limit, OPT, C> +impl<'de, R, const OPT: Options, C> SequenceDecoder<'de> for SelfDecoder, OPT, C> where R: Reader<'de>, - C: ?Sized + Context, + C: Context, { type Cx = C; type DecodeNext<'this> - = StorageDecoder<'a, as Reader<'de>>::Mut<'this>, OPT, C> + = StorageDecoder< as Reader<'de>>::Mut<'this>, OPT, C> where Self: 'this; @@ -762,14 +759,14 @@ where } } -impl<'a, 'de, R, const OPT: Options, C> SequenceDecoder<'de> for RemainingSelfDecoder<'a, R, OPT, C> +impl<'de, R, const OPT: Options, C> SequenceDecoder<'de> for RemainingSelfDecoder where R: Reader<'de>, - C: ?Sized + Context, + C: Context, { type Cx = C; type DecodeNext<'this> - = SelfDecoder<'a, R::Mut<'this>, OPT, C> + = SelfDecoder, OPT, C> where Self: 'this; @@ -800,18 +797,18 @@ where } } -impl<'a, 'de, R, const OPT: Options, C> MapDecoder<'de> for RemainingSelfDecoder<'a, R, OPT, C> +impl<'de, R, const OPT: Options, C> MapDecoder<'de> for RemainingSelfDecoder where R: Reader<'de>, - C: ?Sized + Context, + C: Context, { type Cx = C; type DecodeEntry<'this> - = SelfDecoder<'a, R::Mut<'this>, OPT, C> + = SelfDecoder, OPT, C> where Self: 'this; type DecodeRemainingEntries<'this> - = RemainingSelfDecoder<'a, R::Mut<'this>, OPT, C> + = RemainingSelfDecoder, OPT, C> where Self: 'this; @@ -840,18 +837,18 @@ where } } -impl<'a, 'de, R, const OPT: Options, C> EntriesDecoder<'de> for RemainingSelfDecoder<'a, R, OPT, C> +impl<'de, R, const OPT: Options, C> EntriesDecoder<'de> for RemainingSelfDecoder where R: Reader<'de>, - C: ?Sized + Context, + C: Context, { type Cx = C; type DecodeEntryKey<'this> - = SelfDecoder<'a, R::Mut<'this>, OPT, C> + = SelfDecoder, OPT, C> where Self: 'this; type DecodeEntryValue<'this> - = SelfDecoder<'a, R::Mut<'this>, OPT, C> + = SelfDecoder, OPT, C> where Self: 'this; @@ -877,14 +874,14 @@ where } } -impl<'a, 'de, R, const OPT: Options, C> EntryDecoder<'de> for SelfDecoder<'a, R, OPT, C> +impl<'de, R, const OPT: Options, C> EntryDecoder<'de> for SelfDecoder where R: Reader<'de>, - C: ?Sized + Context, + C: Context, { type Cx = C; type DecodeKey<'this> - = SelfDecoder<'a, R::Mut<'this>, OPT, C> + = SelfDecoder, OPT, C> where Self: 'this; type DecodeValue = Self; @@ -900,18 +897,18 @@ where } } -impl<'a, 'de, R, const OPT: Options, C> VariantDecoder<'de> for SelfDecoder<'a, R, OPT, C> +impl<'de, R, const OPT: Options, C> VariantDecoder<'de> for SelfDecoder where R: Reader<'de>, - C: ?Sized + Context, + C: Context, { type Cx = C; type DecodeTag<'this> - = SelfDecoder<'a, R::Mut<'this>, OPT, C> + = SelfDecoder, OPT, C> where Self: 'this; type DecodeValue<'this> - = SelfDecoder<'a, R::Mut<'this>, OPT, C> + = SelfDecoder, OPT, C> where Self: 'this; diff --git a/crates/musli/src/descriptive/en.rs b/crates/musli/src/descriptive/en.rs index db5762faf..38670c049 100644 --- a/crates/musli/src/descriptive/en.rs +++ b/crates/musli/src/descriptive/en.rs @@ -17,35 +17,35 @@ use super::tag::{ const VARIANT: Tag = Tag::from_mark(Mark::Variant); /// A very simple encoder. -pub struct SelfEncoder<'a, W, const OPT: Options, C: ?Sized> { - cx: &'a C, +pub struct SelfEncoder { + cx: C, writer: W, } -impl<'a, W, const OPT: Options, C: ?Sized> SelfEncoder<'a, W, OPT, C> { +impl SelfEncoder { /// Construct a new fixed width message encoder. #[inline] - pub(crate) fn new(cx: &'a C, writer: W) -> Self { + pub(crate) fn new(cx: C, writer: W) -> Self { Self { cx, writer } } } -pub struct SelfPackEncoder<'a, W, const OPT: Options, C> +pub struct SelfPackEncoder where - C: ?Sized + Context, + C: Context, { - cx: &'a C, + cx: C, writer: W, buffer: BufWriter, } -impl<'a, W, const OPT: Options, C> SelfPackEncoder<'a, W, OPT, C> +impl SelfPackEncoder where - C: ?Sized + Context, + C: Context, { /// Construct a new fixed width message encoder. #[inline] - pub(crate) fn new(cx: &'a C, writer: W) -> Self { + pub(crate) fn new(cx: C, writer: W) -> Self { Self { cx, writer, @@ -55,20 +55,20 @@ where } #[crate::encoder(crate)] -impl<'a, W, const OPT: Options, C> Encoder for SelfEncoder<'a, W, OPT, C> +impl Encoder for SelfEncoder where W: Writer, - C: ?Sized + Context, + C: Context, { type Cx = C; type Error = C::Error; type Ok = (); type Mode = C::Mode; - type WithContext<'this, U> - = SelfEncoder<'this, W, OPT, U> + type WithContext + = SelfEncoder where - U: 'this + Context; - type EncodePack = SelfPackEncoder<'a, W, OPT, C>; + U: Context; + type EncodePack = SelfPackEncoder; type EncodeSome = Self; type EncodeSequence = Self; type EncodeMap = Self; @@ -78,15 +78,12 @@ where type EncodeMapVariant = Self; #[inline] - fn cx(self, f: F) -> O - where - F: FnOnce(&Self::Cx, Self) -> O, - { - f(self.cx, self) + fn cx(&self) -> Self::Cx { + self.cx } #[inline] - fn with_context(self, cx: &U) -> Result, C::Error> + fn with_context(self, cx: U) -> Result, C::Error> where U: Context, { @@ -330,24 +327,21 @@ where } } -impl<'a, W, const OPT: Options, C> SequenceEncoder for SelfPackEncoder<'a, W, OPT, C> +impl SequenceEncoder for SelfPackEncoder where W: Writer, - C: ?Sized + Context, + C: Context, { type Cx = C; type Ok = (); type EncodeNext<'this> - = StorageEncoder<'a, &'this mut BufWriter, OPT, C> + = StorageEncoder<&'this mut BufWriter, OPT, C> where Self: 'this; #[inline] - fn cx_mut(&mut self, f: F) -> O - where - F: FnOnce(&Self::Cx, &mut Self) -> O, - { - f(self.cx, self) + fn cx(&self) -> Self::Cx { + self.cx } #[inline] @@ -364,24 +358,21 @@ where } } -impl<'a, W, const OPT: Options, C> SequenceEncoder for SelfEncoder<'a, W, OPT, C> +impl SequenceEncoder for SelfEncoder where W: Writer, - C: ?Sized + Context, + C: Context, { type Cx = C; type Ok = (); type EncodeNext<'this> - = SelfEncoder<'a, W::Mut<'this>, OPT, C> + = SelfEncoder, OPT, C> where Self: 'this; #[inline] - fn cx_mut(&mut self, f: F) -> O - where - F: FnOnce(&Self::Cx, &mut Self) -> O, - { - f(self.cx, self) + fn cx(&self) -> Self::Cx { + self.cx } #[inline] @@ -395,15 +386,15 @@ where } } -impl<'a, W, const OPT: Options, C> MapEncoder for SelfEncoder<'a, W, OPT, C> +impl MapEncoder for SelfEncoder where W: Writer, - C: ?Sized + Context, + C: Context, { type Cx = C; type Ok = (); type EncodeEntry<'this> - = SelfEncoder<'a, W::Mut<'this>, OPT, C> + = SelfEncoder, OPT, C> where Self: 'this; @@ -418,19 +409,19 @@ where } } -impl<'a, W, const OPT: Options, C> EntryEncoder for SelfEncoder<'a, W, OPT, C> +impl EntryEncoder for SelfEncoder where W: Writer, - C: ?Sized + Context, + C: Context, { type Cx = C; type Ok = (); type EncodeKey<'this> - = SelfEncoder<'a, W::Mut<'this>, OPT, C> + = SelfEncoder, OPT, C> where Self: 'this; type EncodeValue<'this> - = SelfEncoder<'a, W::Mut<'this>, OPT, C> + = SelfEncoder, OPT, C> where Self: 'this; @@ -450,19 +441,19 @@ where } } -impl<'a, W, const OPT: Options, C> EntriesEncoder for SelfEncoder<'a, W, OPT, C> +impl EntriesEncoder for SelfEncoder where W: Writer, - C: ?Sized + Context, + C: Context, { type Cx = C; type Ok = (); type EncodeEntryKey<'this> - = SelfEncoder<'a, W::Mut<'this>, OPT, C> + = SelfEncoder, OPT, C> where Self: 'this; type EncodeEntryValue<'this> - = SelfEncoder<'a, W::Mut<'this>, OPT, C> + = SelfEncoder, OPT, C> where Self: 'this; @@ -482,19 +473,19 @@ where } } -impl<'a, W, const OPT: Options, C> VariantEncoder for SelfEncoder<'a, W, OPT, C> +impl VariantEncoder for SelfEncoder where W: Writer, - C: ?Sized + Context, + C: Context, { type Cx = C; type Ok = (); type EncodeTag<'this> - = SelfEncoder<'a, W::Mut<'this>, OPT, C> + = SelfEncoder, OPT, C> where Self: 'this; type EncodeData<'this> - = SelfEncoder<'a, W::Mut<'this>, OPT, C> + = SelfEncoder, OPT, C> where Self: 'this; @@ -517,13 +508,13 @@ where /// Encode a length prefix. #[inline] fn encode_prefix( - cx: &C, + cx: C, mut writer: W, kind: Kind, len: usize, ) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, W: Writer, { let (tag, embedded) = Tag::with_len(kind, len); diff --git a/crates/musli/src/descriptive/integer_encoding.rs b/crates/musli/src/descriptive/integer_encoding.rs index 9fe46f56f..39aa2219c 100644 --- a/crates/musli/src/descriptive/integer_encoding.rs +++ b/crates/musli/src/descriptive/integer_encoding.rs @@ -7,13 +7,13 @@ use super::tag::{Kind, NumberKind, Tag}; #[inline] pub(crate) fn encode_typed_unsigned( - cx: &C, + cx: C, writer: W, bits: u8, value: T, ) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, W: Writer, T: Unsigned, { @@ -21,9 +21,9 @@ where } #[inline] -pub(crate) fn decode_typed_unsigned<'de, C, R, T>(cx: &C, reader: R) -> Result +pub(crate) fn decode_typed_unsigned<'de, C, R, T>(cx: C, reader: R) -> Result where - C: ?Sized + Context, + C: Context, R: Reader<'de>, T: Unsigned + TryFrom, { @@ -48,9 +48,9 @@ where } #[inline] -fn encode_typed(cx: &C, mut writer: W, bits: u8, value: T) -> Result<(), C::Error> +fn encode_typed(cx: C, mut writer: W, bits: u8, value: T) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, W: Writer, T: Unsigned, { @@ -59,9 +59,9 @@ where } #[inline] -fn decode_typed<'de, C, R, T>(cx: &C, mut reader: R) -> Result<(T, NumberKind), C::Error> +fn decode_typed<'de, C, R, T>(cx: C, mut reader: R) -> Result<(T, NumberKind), C::Error> where - C: ?Sized + Context, + C: Context, R: Reader<'de>, T: Unsigned, { @@ -77,13 +77,13 @@ where #[inline] pub(crate) fn encode_typed_signed( - cx: &C, + cx: C, writer: W, bits: u8, value: T, ) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, W: Writer, T: Signed, { @@ -91,9 +91,9 @@ where } #[inline] -pub(crate) fn decode_typed_signed<'de, C, R, T>(cx: &C, reader: R) -> Result +pub(crate) fn decode_typed_signed<'de, C, R, T>(cx: C, reader: R) -> Result where - C: ?Sized + Context, + C: Context, R: Reader<'de>, T: Signed + TryFrom<::Unsigned>, { diff --git a/crates/musli/src/fixed.rs b/crates/musli/src/fixed.rs index 80fa39d6b..79f6c5a6c 100644 --- a/crates/musli/src/fixed.rs +++ b/crates/musli/src/fixed.rs @@ -146,9 +146,9 @@ impl FixedBytes { /// Try and extend from the given slice. #[inline] - pub fn write_bytes(&mut self, cx: &C, source: &[u8]) -> Result<(), C::Error> + pub fn write_bytes(&mut self, cx: C, source: &[u8]) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { if !self.extend_from_slice(source) { return Err(cx.message(FixedBytesOverflow { @@ -193,9 +193,9 @@ impl Writer for FixedBytes { Self: 'this; #[inline] - fn finish(&mut self, _: &C) -> Result + fn finish(&mut self, _: C) -> Result where - C: ?Sized + Context, + C: Context, { Ok(()) } @@ -206,18 +206,18 @@ impl Writer for FixedBytes { } #[inline] - fn extend(&mut self, cx: &C, buffer: Vec) -> Result<(), C::Error> + fn extend(&mut self, cx: C, buffer: Vec) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { // SAFETY: the buffer never outlives this function call. self.write_bytes(cx, buffer.as_slice()) } #[inline] - fn write_bytes(&mut self, cx: &C, bytes: &[u8]) -> Result<(), C::Error> + fn write_bytes(&mut self, cx: C, bytes: &[u8]) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { FixedBytes::write_bytes(self, cx, bytes)?; cx.advance(bytes.len()); diff --git a/crates/musli/src/int/continuation.rs b/crates/musli/src/int/continuation.rs index c800ffcba..5dd13139c 100644 --- a/crates/musli/src/int/continuation.rs +++ b/crates/musli/src/int/continuation.rs @@ -11,9 +11,9 @@ const CONT_BYTE: u8 = 0b1000_0000; /// Decode the given length using variable int encoding. #[inline] -pub fn decode<'de, C, R, T>(cx: &C, mut r: R) -> Result +pub fn decode<'de, C, R, T>(cx: C, mut r: R) -> Result where - C: ?Sized + Context, + C: Context, R: Reader<'de>, T: int::Unsigned, { @@ -42,9 +42,9 @@ where /// Encode the given length using variable length encoding. #[inline] -pub fn encode(cx: &C, mut w: W, mut value: T) -> Result<(), C::Error> +pub fn encode(cx: C, mut w: W, mut value: T) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, W: Writer, T: int::Unsigned, { diff --git a/crates/musli/src/int/encoding.rs b/crates/musli/src/int/encoding.rs index 0eb2b42f6..e272b91e1 100644 --- a/crates/musli/src/int/encoding.rs +++ b/crates/musli/src/int/encoding.rs @@ -6,12 +6,12 @@ use crate::{Context, Options, Reader, Writer}; /// Governs how unsigned integers are encoded into a [`Writer`]. #[inline] pub(crate) fn encode_unsigned( - cx: &C, + cx: C, writer: W, value: T, ) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, W: Writer, T: Unsigned + UnsignedOps, { @@ -28,11 +28,11 @@ where /// passed in through `F`. #[inline] pub(crate) fn decode_unsigned<'de, C, R, T, const OPT: Options>( - cx: &C, + cx: C, reader: R, ) -> Result where - C: ?Sized + Context, + C: Context, R: Reader<'de>, T: UnsignedOps, { @@ -48,12 +48,12 @@ where /// Governs how signed integers are encoded into a [`Writer`]. #[inline] pub(crate) fn encode_signed( - cx: &C, + cx: C, writer: W, value: T, ) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, W: Writer, T: Signed, T::Unsigned: UnsignedOps, @@ -70,11 +70,11 @@ where /// Governs how signed integers are decoded from a [`Reader`]. #[inline] pub(crate) fn decode_signed<'de, C, R, T, const OPT: Options>( - cx: &C, + cx: C, reader: R, ) -> Result where - C: ?Sized + Context, + C: Context, R: Reader<'de>, T: Signed, T::Unsigned: UnsignedOps, @@ -94,12 +94,12 @@ where /// Governs how usize lengths are encoded into a [`Writer`]. #[inline] pub(crate) fn encode_usize( - cx: &C, + cx: C, writer: W, value: usize, ) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, W: Writer, { match crate::options::length::() { @@ -125,11 +125,11 @@ where /// Governs how usize lengths are decoded from a [`Reader`]. #[inline] pub(crate) fn decode_usize<'de, C, R, const OPT: Options>( - cx: &C, + cx: C, reader: R, ) -> Result where - C: ?Sized + Context, + C: Context, R: Reader<'de>, { match crate::options::length::() { diff --git a/crates/musli/src/int/traits.rs b/crates/musli/src/int/traits.rs index 359a7b6ea..562c51ac8 100644 --- a/crates/musli/src/int/traits.rs +++ b/crates/musli/src/int/traits.rs @@ -52,15 +52,15 @@ pub(crate) trait Unsigned: pub(crate) trait UnsignedOps: Unsigned { /// Write the current byte array to the given writer in little-endian /// encoding. - fn write_bytes(self, cx: &C, writer: W, byte_order: ByteOrder) -> Result<(), C::Error> + fn write_bytes(self, cx: C, writer: W, byte_order: ByteOrder) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, W: Writer; /// Read the current value from the reader in little-endian encoding. - fn read_bytes<'de, C, R>(cx: &C, reader: R, byte_order: ByteOrder) -> Result + fn read_bytes<'de, C, R>(cx: C, reader: R, byte_order: ByteOrder) -> Result where - C: ?Sized + Context, + C: Context, R: Reader<'de>; } @@ -149,12 +149,12 @@ macro_rules! implement_ops { #[inline] fn write_bytes( self, - cx: &C, + cx: C, mut writer: W, byte_order: ByteOrder, ) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, W: Writer, { let bytes = match byte_order { @@ -168,12 +168,12 @@ macro_rules! implement_ops { #[inline] fn read_bytes<'de, C, R>( - cx: &C, + cx: C, mut reader: R, byte_order: ByteOrder, ) -> Result where - C: ?Sized + Context, + C: Context, R: Reader<'de>, { let bytes = reader.read_array(cx)?; diff --git a/crates/musli/src/json/de/key_decoder.rs b/crates/musli/src/json/de/key_decoder.rs index e8c993eac..5f14e2558 100644 --- a/crates/musli/src/json/de/key_decoder.rs +++ b/crates/musli/src/json/de/key_decoder.rs @@ -8,19 +8,19 @@ use super::super::parser::{Parser, Token}; use super::{JsonDecoder, KeySignedVisitor, KeyUnsignedVisitor, StringReference}; /// A JSON object key decoder for Müsli. -pub(crate) struct JsonKeyDecoder<'a, P, C: ?Sized> { - cx: &'a C, +pub(crate) struct JsonKeyDecoder { + cx: C, parser: P, } -impl<'a, 'de, P, C> JsonKeyDecoder<'a, P, C> +impl<'de, P, C> JsonKeyDecoder where P: Parser<'de>, - C: ?Sized + Context, + C: Context, { /// Construct a new fixed width message encoder. #[inline] - pub(crate) fn new(cx: &'a C, parser: P) -> Self { + pub(crate) fn new(cx: C, parser: P) -> Self { Self { cx, parser } } @@ -39,29 +39,26 @@ where } #[crate::decoder(crate)] -impl<'de, P, C> Decoder<'de> for JsonKeyDecoder<'_, P, C> +impl<'de, P, C> Decoder<'de> for JsonKeyDecoder where P: Parser<'de>, - C: ?Sized + Context, + C: Context, { type Cx = C; type Error = C::Error; type Mode = C::Mode; - type WithContext<'this, U> - = JsonKeyDecoder<'this, P, U> + type WithContext + = JsonKeyDecoder where - U: 'this + Context; + U: Context; #[inline] - fn cx(self, f: F) -> O - where - F: FnOnce(&Self::Cx, Self) -> O, - { - f(self.cx, self) + fn cx(&self) -> Self::Cx { + self.cx } #[inline] - fn with_context(self, cx: &U) -> Result, C::Error> + fn with_context(self, cx: U) -> Result, C::Error> where U: Context, { diff --git a/crates/musli/src/json/de/key_signed_visitor.rs b/crates/musli/src/json/de/key_signed_visitor.rs index bc1fa5143..79474564b 100644 --- a/crates/musli/src/json/de/key_signed_visitor.rs +++ b/crates/musli/src/json/de/key_signed_visitor.rs @@ -23,7 +23,7 @@ impl KeySignedVisitor { impl UnsizedVisitor<'_, C, [u8]> for KeySignedVisitor where - C: ?Sized + Context, + C: Context, T: Signed, { type Ok = T; @@ -34,7 +34,7 @@ where } #[inline] - fn visit_ref(self, cx: &C, bytes: &[u8]) -> Result { + fn visit_ref(self, cx: C, bytes: &[u8]) -> Result { parse_signed(cx, &mut SliceParser::new(bytes)) } } diff --git a/crates/musli/src/json/de/key_unsigned_visitor.rs b/crates/musli/src/json/de/key_unsigned_visitor.rs index df589c0ba..882e54611 100644 --- a/crates/musli/src/json/de/key_unsigned_visitor.rs +++ b/crates/musli/src/json/de/key_unsigned_visitor.rs @@ -22,7 +22,7 @@ impl KeyUnsignedVisitor { impl UnsizedVisitor<'_, C, [u8]> for KeyUnsignedVisitor where - C: ?Sized + Context, + C: Context, T: Unsigned, { type Ok = T; @@ -33,7 +33,7 @@ where } #[inline] - fn visit_ref(self, cx: &C, bytes: &[u8]) -> Result { + fn visit_ref(self, cx: C, bytes: &[u8]) -> Result { parse_unsigned(cx, &mut SliceParser::new(bytes)) } } diff --git a/crates/musli/src/json/de/mod.rs b/crates/musli/src/json/de/mod.rs index 883e96e55..4e489ae35 100644 --- a/crates/musli/src/json/de/mod.rs +++ b/crates/musli/src/json/de/mod.rs @@ -45,19 +45,19 @@ use super::parser::{integer, Parser, StringReference, Token}; const BUFFER_OPTIONS: Options = options::new().map_keys_as_numbers().build(); /// A JSON decoder for Müsli. -pub(crate) struct JsonDecoder<'a, P, C: ?Sized> { - cx: &'a C, +pub(crate) struct JsonDecoder { + cx: C, parser: P, } -impl<'a, 'de, P, C> JsonDecoder<'a, P, C> +impl<'de, P, C> JsonDecoder where P: Parser<'de>, - C: ?Sized + Context, + C: Context, { /// Construct a new fixed width message encoder. #[inline] - pub(crate) fn new(cx: &'a C, parser: P) -> Self { + pub(crate) fn new(cx: C, parser: P) -> Self { Self { cx, parser } } @@ -101,37 +101,34 @@ where } #[crate::decoder(crate)] -impl<'a, 'de, P, C> Decoder<'de> for JsonDecoder<'a, P, C> +impl<'de, P, C> Decoder<'de> for JsonDecoder where P: Parser<'de>, - C: ?Sized + Context, + C: Context, { type Cx = C; type Error = C::Error; type Mode = C::Mode; - type WithContext<'this, U> - = JsonDecoder<'this, P, U> + type WithContext + = JsonDecoder where - U: 'this + Context; + U: Context; #[cfg(feature = "value")] - type DecodeBuffer = crate::value::AsValueDecoder<'a, BUFFER_OPTIONS, C>; - type DecodePack = JsonSequenceDecoder<'a, P, C>; - type DecodeSequence = JsonSequenceDecoder<'a, P, C>; - type DecodeMap = JsonObjectDecoder<'a, P, C>; - type DecodeMapEntries = JsonObjectDecoder<'a, P, C>; - type DecodeSome = JsonDecoder<'a, P, C>; - type DecodeVariant = JsonVariantDecoder<'a, P, C>; + type DecodeBuffer = crate::value::AsValueDecoder; + type DecodePack = JsonSequenceDecoder; + type DecodeSequence = JsonSequenceDecoder; + type DecodeMap = JsonObjectDecoder; + type DecodeMapEntries = JsonObjectDecoder; + type DecodeSome = JsonDecoder; + type DecodeVariant = JsonVariantDecoder; #[inline] - fn cx(self, f: F) -> O - where - F: FnOnce(&Self::Cx, Self) -> O, - { - f(self.cx, self) + fn cx(&self) -> Self::Cx { + self.cx } #[inline] - fn with_context(self, cx: &U) -> Result, C::Error> + fn with_context(self, cx: U) -> Result, C::Error> where U: Context, { diff --git a/crates/musli/src/json/de/object_decoder.rs b/crates/musli/src/json/de/object_decoder.rs index c3a068314..25a381a7b 100644 --- a/crates/musli/src/json/de/object_decoder.rs +++ b/crates/musli/src/json/de/object_decoder.rs @@ -7,21 +7,21 @@ use crate::Context; use super::{JsonDecoder, JsonKeyDecoder, JsonObjectPairDecoder}; #[must_use = "Must call skip_object_remaining to complete decoding"] -pub(crate) struct JsonObjectDecoder<'a, P, C: ?Sized> { - cx: &'a C, +pub(crate) struct JsonObjectDecoder { + cx: C, first: bool, len: Option, parser: P, finalized: bool, } -impl<'a, 'de, P, C> JsonObjectDecoder<'a, P, C> +impl<'de, P, C> JsonObjectDecoder where P: Parser<'de>, - C: ?Sized + Context, + C: Context, { pub(super) fn new_in( - cx: &'a C, + cx: C, first: bool, len: Option, parser: P, @@ -36,7 +36,7 @@ where } #[inline] - pub(super) fn new(cx: &'a C, len: Option, mut parser: P) -> Result { + pub(super) fn new(cx: C, len: Option, mut parser: P) -> Result { let actual = parser.lex(cx); if !matches!(actual, Token::OpenBrace) { @@ -110,18 +110,18 @@ where } } -impl<'a, 'de, P, C> MapDecoder<'de> for JsonObjectDecoder<'a, P, C> +impl<'de, P, C> MapDecoder<'de> for JsonObjectDecoder where P: Parser<'de>, - C: ?Sized + Context, + C: Context, { type Cx = C; type DecodeEntry<'this> - = JsonObjectPairDecoder<'a, P::Mut<'this>, C> + = JsonObjectPairDecoder, C> where Self: 'this; type DecodeRemainingEntries<'this> - = JsonObjectDecoder<'a, P::Mut<'this>, C> + = JsonObjectDecoder, C> where Self: 'this; @@ -156,18 +156,18 @@ where } } -impl<'a, 'de, P, C> EntriesDecoder<'de> for JsonObjectDecoder<'a, P, C> +impl<'de, P, C> EntriesDecoder<'de> for JsonObjectDecoder where P: Parser<'de>, - C: ?Sized + Context, + C: Context, { type Cx = C; type DecodeEntryKey<'this> - = JsonKeyDecoder<'a, P::Mut<'this>, C> + = JsonKeyDecoder, C> where Self: 'this; type DecodeEntryValue<'this> - = JsonDecoder<'a, P::Mut<'this>, C> + = JsonDecoder, C> where Self: 'this; diff --git a/crates/musli/src/json/de/object_pair_decoder.rs b/crates/musli/src/json/de/object_pair_decoder.rs index 57e889173..1e2d33677 100644 --- a/crates/musli/src/json/de/object_pair_decoder.rs +++ b/crates/musli/src/json/de/object_pair_decoder.rs @@ -4,29 +4,29 @@ use crate::Context; use super::{JsonDecoder, JsonKeyDecoder}; -pub(crate) struct JsonObjectPairDecoder<'a, P, C: ?Sized> { - cx: &'a C, +pub(crate) struct JsonObjectPairDecoder { + cx: C, parser: P, } -impl<'a, P, C: ?Sized> JsonObjectPairDecoder<'a, P, C> { +impl JsonObjectPairDecoder { #[inline] - pub(super) fn new(cx: &'a C, parser: P) -> Self { + pub(super) fn new(cx: C, parser: P) -> Self { Self { cx, parser } } } -impl<'a, 'de, P, C> EntryDecoder<'de> for JsonObjectPairDecoder<'a, P, C> +impl<'de, P, C> EntryDecoder<'de> for JsonObjectPairDecoder where P: Parser<'de>, - C: ?Sized + Context, + C: Context, { type Cx = C; type DecodeKey<'this> - = JsonKeyDecoder<'a, P::Mut<'this>, C> + = JsonKeyDecoder, C> where Self: 'this; - type DecodeValue = JsonDecoder<'a, P, C>; + type DecodeValue = JsonDecoder; #[inline] fn decode_key(&mut self) -> Result, C::Error> { diff --git a/crates/musli/src/json/de/sequence_decoder.rs b/crates/musli/src/json/de/sequence_decoder.rs index 17ae944b2..9fdd00987 100644 --- a/crates/musli/src/json/de/sequence_decoder.rs +++ b/crates/musli/src/json/de/sequence_decoder.rs @@ -7,21 +7,21 @@ use crate::Context; use super::JsonDecoder; #[must_use = "Must call skip_sequence_remaining"] -pub(crate) struct JsonSequenceDecoder<'a, P, C: ?Sized> { - cx: &'a C, +pub(crate) struct JsonSequenceDecoder { + cx: C, len: Option, first: bool, parser: P, finalized: bool, } -impl<'a, 'de, P, C> JsonSequenceDecoder<'a, P, C> +impl<'de, P, C> JsonSequenceDecoder where P: Parser<'de>, - C: ?Sized + Context, + C: Context, { #[inline] - pub(super) fn new(cx: &'a C, len: Option, mut parser: P) -> Result { + pub(super) fn new(cx: C, len: Option, mut parser: P) -> Result { let actual = parser.lex(cx); if !matches!(actual, Token::OpenBracket) { @@ -89,14 +89,14 @@ where } } -impl<'a, 'de, P, C> SequenceDecoder<'de> for JsonSequenceDecoder<'a, P, C> +impl<'de, P, C> SequenceDecoder<'de> for JsonSequenceDecoder where P: Parser<'de>, - C: ?Sized + Context, + C: Context, { type Cx = C; type DecodeNext<'this> - = JsonDecoder<'a, P::Mut<'this>, C> + = JsonDecoder, C> where Self: 'this; diff --git a/crates/musli/src/json/de/variant_decoder.rs b/crates/musli/src/json/de/variant_decoder.rs index 92ba4f84f..d30d81463 100644 --- a/crates/musli/src/json/de/variant_decoder.rs +++ b/crates/musli/src/json/de/variant_decoder.rs @@ -4,18 +4,18 @@ use crate::Context; use super::{JsonDecoder, JsonKeyDecoder}; -pub(crate) struct JsonVariantDecoder<'a, P, C: ?Sized> { - cx: &'a C, +pub(crate) struct JsonVariantDecoder { + cx: C, parser: P, } -impl<'a, 'de, P, C> JsonVariantDecoder<'a, P, C> +impl<'de, P, C> JsonVariantDecoder where P: Parser<'de>, - C: ?Sized + Context, + C: Context, { #[inline] - pub(super) fn new(cx: &'a C, mut parser: P) -> Result { + pub(super) fn new(cx: C, mut parser: P) -> Result { let actual = parser.lex(cx); if !matches!(actual, Token::OpenBrace) { @@ -41,18 +41,18 @@ where } } -impl<'a, 'de, P, C> VariantDecoder<'de> for JsonVariantDecoder<'a, P, C> +impl<'de, P, C> VariantDecoder<'de> for JsonVariantDecoder where P: Parser<'de>, - C: ?Sized + Context, + C: Context, { type Cx = C; type DecodeTag<'this> - = JsonKeyDecoder<'a, P::Mut<'this>, C> + = JsonKeyDecoder, C> where Self: 'this; type DecodeValue<'this> - = JsonDecoder<'a, P::Mut<'this>, C> + = JsonDecoder, C> where Self: 'this; diff --git a/crates/musli/src/json/en/array_encoder.rs b/crates/musli/src/json/en/array_encoder.rs index 8c3217c64..62c102a5f 100644 --- a/crates/musli/src/json/en/array_encoder.rs +++ b/crates/musli/src/json/en/array_encoder.rs @@ -6,25 +6,25 @@ use crate::{Context, Writer}; use super::JsonEncoder; /// Encoder for a JSON array. -pub(crate) struct JsonArrayEncoder<'a, W, C: ?Sized> { - cx: &'a C, +pub(crate) struct JsonArrayEncoder { + cx: C, first: bool, end: &'static [u8], writer: W, } -impl<'a, W, C> JsonArrayEncoder<'a, W, C> +impl JsonArrayEncoder where W: Writer, - C: ?Sized + Context, + C: Context, { #[inline] - pub(super) fn new(cx: &'a C, writer: W) -> Result { + pub(super) fn new(cx: C, writer: W) -> Result { Self::with_end(cx, writer, b"]") } #[inline] - pub(super) fn with_end(cx: &'a C, mut writer: W, end: &'static [u8]) -> Result { + pub(super) fn with_end(cx: C, mut writer: W, end: &'static [u8]) -> Result { writer.write_byte(cx, b'[')?; Ok(Self { @@ -36,24 +36,21 @@ where } } -impl<'a, W, C> SequenceEncoder for JsonArrayEncoder<'a, W, C> +impl SequenceEncoder for JsonArrayEncoder where W: Writer, - C: ?Sized + Context, + C: Context, { type Cx = C; type Ok = (); type EncodeNext<'this> - = JsonEncoder<'a, W::Mut<'this>, C> + = JsonEncoder, C> where Self: 'this; #[inline] - fn cx_mut(&mut self, f: F) -> O - where - F: FnOnce(&Self::Cx, &mut Self) -> O, - { - f(self.cx, self) + fn cx(&self) -> Self::Cx { + self.cx } #[inline] diff --git a/crates/musli/src/json/en/mod.rs b/crates/musli/src/json/en/mod.rs index 585ca0479..9b1a7e712 100644 --- a/crates/musli/src/json/en/mod.rs +++ b/crates/musli/src/json/en/mod.rs @@ -20,52 +20,49 @@ use crate::hint::{MapHint, SequenceHint}; use crate::{Context, Writer}; /// A JSON encoder for Müsli. -pub(crate) struct JsonEncoder<'a, W, C: ?Sized> { - cx: &'a C, +pub(crate) struct JsonEncoder { + cx: C, writer: W, } -impl<'a, W, C: ?Sized> JsonEncoder<'a, W, C> { +impl JsonEncoder { /// Construct a new fixed width message encoder. #[inline] - pub(crate) fn new(cx: &'a C, writer: W) -> Self { + pub(crate) fn new(cx: C, writer: W) -> Self { Self { cx, writer } } } #[crate::encoder(crate)] -impl<'a, C, W> Encoder for JsonEncoder<'a, W, C> +impl Encoder for JsonEncoder where W: Writer, - C: ?Sized + Context, + C: Context, { type Cx = C; type Error = C::Error; type Ok = (); type Mode = C::Mode; - type WithContext<'this, U> - = JsonEncoder<'this, W, U> + type WithContext + = JsonEncoder where - U: 'this + Context; - type EncodePack = JsonArrayEncoder<'a, W, C>; + U: Context; + type EncodePack = JsonArrayEncoder; type EncodeSome = Self; - type EncodeSequence = JsonArrayEncoder<'a, W, C>; - type EncodeMap = JsonObjectEncoder<'a, W, C>; - type EncodeMapEntries = JsonObjectEncoder<'a, W, C>; - type EncodeVariant = JsonVariantEncoder<'a, W, C>; - type EncodeSequenceVariant = JsonArrayEncoder<'a, W, C>; - type EncodeMapVariant = JsonObjectEncoder<'a, W, C>; + type EncodeSequence = JsonArrayEncoder; + type EncodeMap = JsonObjectEncoder; + type EncodeMapEntries = JsonObjectEncoder; + type EncodeVariant = JsonVariantEncoder; + type EncodeSequenceVariant = JsonArrayEncoder; + type EncodeMapVariant = JsonObjectEncoder; #[inline] - fn cx(self, f: F) -> O - where - F: FnOnce(&Self::Cx, Self) -> O, - { - f(self.cx, self) + fn cx(&self) -> Self::Cx { + self.cx } #[inline] - fn with_context(self, cx: &U) -> Result, C::Error> + fn with_context(self, cx: U) -> Result, C::Error> where U: Context, { @@ -329,9 +326,9 @@ where /// Encode a sequence of chars as a string. #[inline] -fn encode_string(cx: &C, mut w: W, bytes: &[u8]) -> Result<(), C::Error> +fn encode_string(cx: C, mut w: W, bytes: &[u8]) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, W: Writer, { w.write_byte(cx, b'"')?; @@ -400,9 +397,9 @@ static ESCAPE: [u8; 256] = [ // Hex digits. static HEX_DIGITS: [u8; 16] = *b"0123456789abcdef"; -fn write_escape(cx: &C, mut writer: W, escape: u8, byte: u8) -> Result<(), C::Error> +fn write_escape(cx: C, mut writer: W, escape: u8, byte: u8) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, W: Writer, { let s = match escape { diff --git a/crates/musli/src/json/en/object_encoder.rs b/crates/musli/src/json/en/object_encoder.rs index c55ee9216..70ea5d19a 100644 --- a/crates/musli/src/json/en/object_encoder.rs +++ b/crates/musli/src/json/en/object_encoder.rs @@ -4,25 +4,25 @@ use crate::{Context, Writer}; use super::{JsonEncoder, JsonObjectKeyEncoder, JsonObjectPairEncoder}; /// An object encoder for JSON. -pub(crate) struct JsonObjectEncoder<'a, W, C: ?Sized> { - cx: &'a C, +pub(crate) struct JsonObjectEncoder { + cx: C, len: usize, end: &'static [u8], writer: W, } -impl<'a, W, C> JsonObjectEncoder<'a, W, C> +impl JsonObjectEncoder where W: Writer, - C: ?Sized + Context, + C: Context, { #[inline] - pub(super) fn new(cx: &'a C, writer: W) -> Result { + pub(super) fn new(cx: C, writer: W) -> Result { Self::with_end(cx, writer, b"}") } #[inline] - pub(super) fn with_end(cx: &'a C, mut writer: W, end: &'static [u8]) -> Result { + pub(super) fn with_end(cx: C, mut writer: W, end: &'static [u8]) -> Result { writer.write_byte(cx, b'{')?; Ok(Self { @@ -34,15 +34,15 @@ where } } -impl<'a, W, C> MapEncoder for JsonObjectEncoder<'a, W, C> +impl MapEncoder for JsonObjectEncoder where W: Writer, - C: ?Sized + Context, + C: Context, { type Cx = C; type Ok = (); type EncodeEntry<'this> - = JsonObjectPairEncoder<'a, W::Mut<'this>, C> + = JsonObjectPairEncoder, C> where Self: 'this; @@ -63,19 +63,19 @@ where } } -impl<'a, W, C> EntriesEncoder for JsonObjectEncoder<'a, W, C> +impl EntriesEncoder for JsonObjectEncoder where W: Writer, - C: ?Sized + Context, + C: Context, { type Cx = C; type Ok = (); type EncodeEntryKey<'this> - = JsonObjectKeyEncoder<'a, W::Mut<'this>, C> + = JsonObjectKeyEncoder, C> where Self: 'this; type EncodeEntryValue<'this> - = JsonEncoder<'a, W::Mut<'this>, C> + = JsonEncoder, C> where Self: 'this; diff --git a/crates/musli/src/json/en/object_key_encoder.rs b/crates/musli/src/json/en/object_key_encoder.rs index a73ffc783..23b616611 100644 --- a/crates/musli/src/json/en/object_key_encoder.rs +++ b/crates/musli/src/json/en/object_key_encoder.rs @@ -3,14 +3,14 @@ use core::fmt; use crate::en::{Encode, Encoder}; use crate::{Context, Writer}; -pub(crate) struct JsonObjectKeyEncoder<'a, W, C: ?Sized> { - cx: &'a C, +pub(crate) struct JsonObjectKeyEncoder { + cx: C, writer: W, } -impl<'a, W, C: ?Sized> JsonObjectKeyEncoder<'a, W, C> { +impl JsonObjectKeyEncoder { #[inline] - pub(super) fn new(cx: &'a C, writer: W) -> Self { + pub(super) fn new(cx: C, writer: W) -> Self { Self { cx, writer } } } @@ -27,30 +27,27 @@ macro_rules! format_integer { } #[crate::encoder(crate)] -impl Encoder for JsonObjectKeyEncoder<'_, W, C> +impl Encoder for JsonObjectKeyEncoder where W: Writer, - C: ?Sized + Context, + C: Context, { type Cx = C; type Error = C::Error; type Ok = (); type Mode = C::Mode; - type WithContext<'this, U> - = JsonObjectKeyEncoder<'this, W, U> + type WithContext + = JsonObjectKeyEncoder where - U: 'this + Context; + U: Context; #[inline] - fn cx(self, f: F) -> O - where - F: FnOnce(&Self::Cx, Self) -> O, - { - f(self.cx, self) + fn cx(&self) -> Self::Cx { + self.cx } #[inline] - fn with_context(self, cx: &U) -> Result, C::Error> + fn with_context(self, cx: U) -> Result, C::Error> where U: Context, { diff --git a/crates/musli/src/json/en/object_pair_encoder.rs b/crates/musli/src/json/en/object_pair_encoder.rs index 29f4dcffe..25c303881 100644 --- a/crates/musli/src/json/en/object_pair_encoder.rs +++ b/crates/musli/src/json/en/object_pair_encoder.rs @@ -4,32 +4,32 @@ use crate::{Context, Writer}; use super::{JsonEncoder, JsonObjectKeyEncoder}; /// Encoder for a JSON object pair. -pub(crate) struct JsonObjectPairEncoder<'a, W, C: ?Sized> { - cx: &'a C, +pub(crate) struct JsonObjectPairEncoder { + cx: C, empty: bool, writer: W, } -impl<'a, W, C: ?Sized> JsonObjectPairEncoder<'a, W, C> { +impl JsonObjectPairEncoder { #[inline] - pub(super) const fn new(cx: &'a C, empty: bool, writer: W) -> Self { + pub(super) const fn new(cx: C, empty: bool, writer: W) -> Self { Self { cx, empty, writer } } } -impl<'a, W, C> EntryEncoder for JsonObjectPairEncoder<'a, W, C> +impl EntryEncoder for JsonObjectPairEncoder where W: Writer, - C: ?Sized + Context, + C: Context, { type Cx = C; type Ok = (); type EncodeKey<'this> - = JsonObjectKeyEncoder<'a, W::Mut<'this>, C> + = JsonObjectKeyEncoder, C> where Self: 'this; type EncodeValue<'this> - = JsonEncoder<'a, W::Mut<'this>, C> + = JsonEncoder, C> where Self: 'this; diff --git a/crates/musli/src/json/en/variant_encoder.rs b/crates/musli/src/json/en/variant_encoder.rs index 901e02e06..93150bc21 100644 --- a/crates/musli/src/json/en/variant_encoder.rs +++ b/crates/musli/src/json/en/variant_encoder.rs @@ -4,36 +4,36 @@ use crate::{Context, Writer}; use super::{JsonEncoder, JsonObjectKeyEncoder}; /// A JSON variant encoder. -pub(crate) struct JsonVariantEncoder<'a, W, C: ?Sized> { - cx: &'a C, +pub(crate) struct JsonVariantEncoder { + cx: C, writer: W, } -impl<'a, W, C> JsonVariantEncoder<'a, W, C> +impl JsonVariantEncoder where W: Writer, - C: ?Sized + Context, + C: Context, { #[inline] - pub(super) fn new(cx: &'a C, mut writer: W) -> Result { + pub(super) fn new(cx: C, mut writer: W) -> Result { writer.write_byte(cx, b'{')?; Ok(Self { cx, writer }) } } -impl<'a, W, C> VariantEncoder for JsonVariantEncoder<'a, W, C> +impl VariantEncoder for JsonVariantEncoder where W: Writer, - C: ?Sized + Context, + C: Context, { type Cx = C; type Ok = (); type EncodeTag<'this> - = JsonObjectKeyEncoder<'a, W::Mut<'this>, C> + = JsonObjectKeyEncoder, C> where Self: 'this; type EncodeData<'this> - = JsonEncoder<'a, W::Mut<'this>, C> + = JsonEncoder, C> where Self: 'this; diff --git a/crates/musli/src/json/encoding.rs b/crates/musli/src/json/encoding.rs index b3129800c..cdf41fce1 100644 --- a/crates/musli/src/json/encoding.rs +++ b/crates/musli/src/json/encoding.rs @@ -262,9 +262,9 @@ where #[cfg(feature = "alloc")] #[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))] #[inline] - pub fn to_string_with(self, cx: &C, value: &T) -> Result + pub fn to_string_with(self, cx: C, value: &T) -> Result where - C: ?Sized + Context, + C: Context, T: ?Sized + Encode, { cx.clear(); diff --git a/crates/musli/src/json/parser/integer.rs b/crates/musli/src/json/parser/integer.rs index f9fecb130..136d10902 100644 --- a/crates/musli/src/json/parser/integer.rs +++ b/crates/musli/src/json/parser/integer.rs @@ -214,10 +214,10 @@ where } /// Implementation to skip over a well-formed JSON number. -pub(crate) fn skip_number<'de, P, C>(cx: &C, mut p: P) -> Result<(), C::Error> +pub(crate) fn skip_number<'de, P, C>(cx: C, mut p: P) -> Result<(), C::Error> where P: Parser<'de>, - C: ?Sized + Context, + C: Context, { p.skip_whitespace(cx); @@ -264,11 +264,11 @@ where /// Partially parse an unsigned value. #[cfg_attr(feature = "parse-full", allow(unused))] #[inline(never)] -pub(crate) fn parse_unsigned_base<'de, T, C, P>(cx: &C, mut p: P) -> Result +pub(crate) fn parse_unsigned_base<'de, T, C, P>(cx: C, mut p: P) -> Result where T: Unsigned, P: Parser<'de>, - C: ?Sized + Context, + C: Context, { p.skip_whitespace(cx); @@ -279,11 +279,11 @@ where /// Fully parse an unsigned value. #[cfg_attr(not(feature = "parse-full"), allow(unused))] #[inline(never)] -pub(crate) fn parse_unsigned_full<'de, T, C, P>(cx: &C, mut p: P) -> Result +pub(crate) fn parse_unsigned_full<'de, T, C, P>(cx: C, mut p: P) -> Result where T: Unsigned, P: Parser<'de>, - C: ?Sized + Context, + C: Context, { p.skip_whitespace(cx); @@ -297,9 +297,9 @@ where /// Decode a signed integer. #[inline] -fn decode_signed_base<'de, T, C, P>(cx: &C, mut p: P) -> Result, C::Error> +fn decode_signed_base<'de, T, C, P>(cx: C, mut p: P) -> Result, C::Error> where - C: ?Sized + Context, + C: Context, T: Signed, P: Parser<'de>, { @@ -322,11 +322,11 @@ where /// Decode a full signed integer. pub(crate) fn decode_signed_full<'de, T, C, P>( - cx: &C, + cx: C, p: &mut P, ) -> Result, C::Error> where - C: ?Sized + Context, + C: Context, T: Signed, P: ?Sized + Parser<'de>, { @@ -337,9 +337,9 @@ where /// Decode a full signed integer. #[inline] -fn decode_signed_full_inner<'de, T, C, P>(cx: &C, mut p: P) -> Result, C::Error> +fn decode_signed_full_inner<'de, T, C, P>(cx: C, mut p: P) -> Result, C::Error> where - C: ?Sized + Context, + C: Context, T: Signed, P: Parser<'de>, { @@ -363,11 +363,11 @@ where /// Fully parse a signed value. #[cfg_attr(feature = "parse-full", allow(unused))] #[inline(never)] -pub(crate) fn parse_signed_base<'de, T, C, P>(cx: &C, mut p: P) -> Result +pub(crate) fn parse_signed_base<'de, T, C, P>(cx: C, mut p: P) -> Result where T: Signed, P: Parser<'de>, - C: ?Sized + Context, + C: Context, { p.skip_whitespace(cx); @@ -382,11 +382,11 @@ where /// Fully parse a signed value. #[cfg_attr(not(feature = "parse-full"), allow(unused))] #[inline(never)] -pub(crate) fn parse_signed_full<'de, T, C, P>(cx: &C, mut p: P) -> Result +pub(crate) fn parse_signed_full<'de, T, C, P>(cx: C, mut p: P) -> Result where T: Signed, P: Parser<'de>, - C: ?Sized + Context, + C: Context, { p.skip_whitespace(cx); @@ -401,11 +401,11 @@ where /// Generically decode a single (whole) integer from a stream of bytes abiding /// by JSON convention for format. #[inline] -fn decode_unsigned_base<'de, T, C, P>(cx: &C, mut p: P, start: &C::Mark) -> Result +fn decode_unsigned_base<'de, T, C, P>(cx: C, mut p: P, start: &C::Mark) -> Result where T: Unsigned, P: Parser<'de>, - C: ?Sized + Context, + C: Context, { let base = match p.read_byte(cx)? { b'0' => T::ZERO, @@ -430,14 +430,14 @@ where /// by JSON convention for format. #[inline] fn decode_unsigned_full<'de, T, C, P>( - cx: &C, + cx: C, mut p: P, start: &C::Mark, ) -> Result, C::Error> where T: Unsigned, P: Parser<'de>, - C: ?Sized + Context, + C: Context, { let base = decode_unsigned_base(cx, p.borrow_mut(), start)?; @@ -484,10 +484,10 @@ where /// Decode an exponent. #[inline] -fn decode_exponent<'de, P, C>(cx: &C, mut p: P, start: &C::Mark) -> Result +fn decode_exponent<'de, P, C>(cx: C, mut p: P, start: &C::Mark) -> Result where P: Parser<'de>, - C: ?Sized + Context, + C: Context, { let mut is_negative = false; let mut e = 0u32; @@ -515,11 +515,11 @@ where /// Decode a single digit into `out`. #[inline] -fn digit<'de, T, C, P>(cx: &C, out: T, mut p: P, start: &C::Mark) -> Result +fn digit<'de, T, C, P>(cx: C, out: T, mut p: P, start: &C::Mark) -> Result where T: Unsigned, P: Parser<'de>, - C: ?Sized + Context, + C: Context, { let Some(out) = out.checked_mul10() else { return Err(cx.marked_message(start, IntegerError::IntegerOverflow)); @@ -530,10 +530,10 @@ where /// Decode sequence of zeros. #[inline] -fn decode_zeros<'de, P, C>(cx: &C, mut p: P) -> Result +fn decode_zeros<'de, P, C>(cx: C, mut p: P) -> Result where P: Parser<'de>, - C: ?Sized + Context, + C: Context, { let mut count = 0i32; diff --git a/crates/musli/src/json/parser/mut_slice_parser.rs b/crates/musli/src/json/parser/mut_slice_parser.rs index 8c8a2d481..715622e49 100644 --- a/crates/musli/src/json/parser/mut_slice_parser.rs +++ b/crates/musli/src/json/parser/mut_slice_parser.rs @@ -42,12 +42,12 @@ impl<'de> Parser<'de> for MutSliceParser<'_, 'de> { #[inline] fn parse_string<'scratch, C>( &mut self, - cx: &C, + cx: C, validate: bool, scratch: &'scratch mut Vec, ) -> Result, C::Error> where - C: ?Sized + Context, + C: Context, { let start = cx.mark(); let actual = self.lex(cx); @@ -66,9 +66,9 @@ impl<'de> Parser<'de> for MutSliceParser<'_, 'de> { } #[inline] - fn skip_string(&mut self, cx: &C) -> Result<(), C::Error> + fn skip_string(&mut self, cx: C) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { let mut access = SliceAccess::new(cx, self.slice, 0); let out = access.skip_string(); @@ -77,9 +77,9 @@ impl<'de> Parser<'de> for MutSliceParser<'_, 'de> { } #[inline] - fn skip(&mut self, cx: &C, n: usize) -> Result<(), C::Error> + fn skip(&mut self, cx: C, n: usize) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { if self.slice.len() < n { return Err(cx.custom(SliceUnderflow::new(n, self.slice.len()))); @@ -91,9 +91,9 @@ impl<'de> Parser<'de> for MutSliceParser<'_, 'de> { } #[inline] - fn read(&mut self, cx: &C, buf: &mut [u8]) -> Result<(), C::Error> + fn read(&mut self, cx: C, buf: &mut [u8]) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { if self.slice.len() < buf.len() { return Err(cx.custom(SliceUnderflow::new(buf.len(), self.slice.len()))); @@ -107,9 +107,9 @@ impl<'de> Parser<'de> for MutSliceParser<'_, 'de> { } #[inline] - fn skip_whitespace(&mut self, cx: &C) + fn skip_whitespace(&mut self, cx: C) where - C: ?Sized + Context, + C: Context, { let n = 0; @@ -134,9 +134,9 @@ impl<'de> Parser<'de> for MutSliceParser<'_, 'de> { self.slice.first().copied() } - fn parse_f32(&mut self, cx: &C) -> Result + fn parse_f32(&mut self, cx: C) -> Result where - C: ?Sized + Context, + C: Context, { let Some((value, read)) = crate::dec2flt::dec2flt(self.slice) else { return Err(cx.message(ErrorMessage::ParseFloat)); @@ -147,9 +147,9 @@ impl<'de> Parser<'de> for MutSliceParser<'_, 'de> { Ok(value) } - fn parse_f64(&mut self, cx: &C) -> Result + fn parse_f64(&mut self, cx: C) -> Result where - C: ?Sized + Context, + C: Context, { let Some((value, read)) = crate::dec2flt::dec2flt(self.slice) else { return Err(cx.message(ErrorMessage::ParseFloat)); diff --git a/crates/musli/src/json/parser/parser.rs b/crates/musli/src/json/parser/parser.rs index 693397c89..5b4f31e67 100644 --- a/crates/musli/src/json/parser/parser.rs +++ b/crates/musli/src/json/parser/parser.rs @@ -34,23 +34,23 @@ pub trait Parser<'de>: private::Sealed { #[doc(hidden)] fn parse_string<'scratch, C>( &mut self, - cx: &C, + cx: C, validate: bool, scratch: &'scratch mut Vec, ) -> Result, C::Error> where - C: ?Sized + Context; + C: Context; /// Skip a string. #[doc(hidden)] - fn skip_string(&mut self, cx: &C) -> Result<(), C::Error> + fn skip_string(&mut self, cx: C) -> Result<(), C::Error> where - C: ?Sized + Context; + C: Context; #[doc(hidden)] - fn read_byte(&mut self, cx: &C) -> Result + fn read_byte(&mut self, cx: C) -> Result where - C: ?Sized + Context, + C: Context, { let mut byte = [0]; self.read(cx, &mut byte[..])?; @@ -58,25 +58,25 @@ pub trait Parser<'de>: private::Sealed { } #[doc(hidden)] - fn skip(&mut self, cx: &C, n: usize) -> Result<(), C::Error> + fn skip(&mut self, cx: C, n: usize) -> Result<(), C::Error> where - C: ?Sized + Context; + C: Context; #[doc(hidden)] - fn read(&mut self, cx: &C, buf: &mut [u8]) -> Result<(), C::Error> + fn read(&mut self, cx: C, buf: &mut [u8]) -> Result<(), C::Error> where - C: ?Sized + Context; + C: Context; /// Skip over whitespace. #[doc(hidden)] - fn skip_whitespace(&mut self, cx: &C) + fn skip_whitespace(&mut self, cx: C) where - C: ?Sized + Context; + C: Context; #[doc(hidden)] - fn consume_while(&mut self, cx: &C, m: fn(u8) -> bool) -> Result + fn consume_while(&mut self, cx: C, m: fn(u8) -> bool) -> Result where - C: ?Sized + Context, + C: Context, { let mut c = 0; @@ -97,9 +97,9 @@ pub trait Parser<'de>: private::Sealed { fn peek(&mut self) -> Option; #[doc(hidden)] - fn lex(&mut self, cx: &C) -> Token + fn lex(&mut self, cx: C) -> Token where - C: ?Sized + Context, + C: Context, { self.skip_whitespace(cx); @@ -111,19 +111,19 @@ pub trait Parser<'de>: private::Sealed { } /// Parse a 32-bit floating point number. - fn parse_f32(&mut self, cx: &C) -> Result + fn parse_f32(&mut self, cx: C) -> Result where - C: ?Sized + Context; + C: Context; /// Parse a 64-bit floating point number. - fn parse_f64(&mut self, cx: &C) -> Result + fn parse_f64(&mut self, cx: C) -> Result where - C: ?Sized + Context; + C: Context; #[doc(hidden)] - fn parse_exact(&mut self, cx: &C, exact: &str) -> Result<(), C::Error> + fn parse_exact(&mut self, cx: C, exact: &str) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { debug_assert!(exact.len() <= 5); @@ -144,9 +144,9 @@ pub trait Parser<'de>: private::Sealed { /// Parse an unknown number and try to coerce it into the best fit type /// through [Visitor]. #[doc(hidden)] - fn parse_number(&mut self, cx: &C, visitor: V) -> Result + fn parse_number(&mut self, cx: C, visitor: V) -> Result where - C: ?Sized + Context, + C: Context, V: Visitor<'de, C>, { let signed = decode_signed_full::(cx, self)?; @@ -232,28 +232,28 @@ where #[inline] fn parse_string<'scratch, C>( &mut self, - cx: &C, + cx: C, validate: bool, scratch: &'scratch mut Vec, ) -> Result, C::Error> where - C: ?Sized + Context, + C: Context, { (**self).parse_string(cx, validate, scratch) } #[inline] - fn skip_string(&mut self, cx: &C) -> Result<(), C::Error> + fn skip_string(&mut self, cx: C) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { (**self).skip_string(cx) } #[inline] - fn read_byte(&mut self, cx: &C) -> Result + fn read_byte(&mut self, cx: C) -> Result where - C: ?Sized + Context, + C: Context, { (**self).read_byte(cx) } @@ -264,49 +264,49 @@ where } #[inline] - fn lex(&mut self, cx: &C) -> Token + fn lex(&mut self, cx: C) -> Token where - C: ?Sized + Context, + C: Context, { (**self).lex(cx) } #[inline] - fn skip_whitespace(&mut self, cx: &C) + fn skip_whitespace(&mut self, cx: C) where - C: ?Sized + Context, + C: Context, { (**self).skip_whitespace(cx); } #[inline] - fn skip(&mut self, cx: &C, n: usize) -> Result<(), C::Error> + fn skip(&mut self, cx: C, n: usize) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { (**self).skip(cx, n) } #[inline] - fn read(&mut self, cx: &C, buf: &mut [u8]) -> Result<(), C::Error> + fn read(&mut self, cx: C, buf: &mut [u8]) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { (**self).read(cx, buf) } #[inline] - fn parse_f32(&mut self, cx: &C) -> Result + fn parse_f32(&mut self, cx: C) -> Result where - C: ?Sized + Context, + C: Context, { (**self).parse_f32(cx) } #[inline] - fn parse_f64(&mut self, cx: &C) -> Result + fn parse_f64(&mut self, cx: C) -> Result where - C: ?Sized + Context, + C: Context, { (**self).parse_f64(cx) } diff --git a/crates/musli/src/json/parser/slice_parser.rs b/crates/musli/src/json/parser/slice_parser.rs index fe513366d..4c1d9930e 100644 --- a/crates/musli/src/json/parser/slice_parser.rs +++ b/crates/musli/src/json/parser/slice_parser.rs @@ -34,12 +34,12 @@ impl<'de> Parser<'de> for SliceParser<'de> { #[inline] fn parse_string<'scratch, C>( &mut self, - cx: &C, + cx: C, validate: bool, scratch: &'scratch mut Vec, ) -> Result, C::Error> where - C: ?Sized + Context, + C: Context, { let start = cx.mark(); let actual = self.lex(cx); @@ -58,9 +58,9 @@ impl<'de> Parser<'de> for SliceParser<'de> { } #[inline] - fn skip_string(&mut self, cx: &C) -> Result<(), C::Error> + fn skip_string(&mut self, cx: C) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { let mut access = SliceAccess::new(cx, self.slice, self.index); let out = access.skip_string(); @@ -69,9 +69,9 @@ impl<'de> Parser<'de> for SliceParser<'de> { } #[inline] - fn skip(&mut self, cx: &C, n: usize) -> Result<(), C::Error> + fn skip(&mut self, cx: C, n: usize) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { let outcome = self.index.wrapping_add(n); @@ -85,9 +85,9 @@ impl<'de> Parser<'de> for SliceParser<'de> { } #[inline] - fn read(&mut self, cx: &C, buf: &mut [u8]) -> Result<(), C::Error> + fn read(&mut self, cx: C, buf: &mut [u8]) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { let outcome = self.index.wrapping_add(buf.len()); @@ -105,9 +105,9 @@ impl<'de> Parser<'de> for SliceParser<'de> { } #[inline] - fn skip_whitespace(&mut self, cx: &C) + fn skip_whitespace(&mut self, cx: C) where - C: ?Sized + Context, + C: Context, { while matches!( self.slice.get(self.index), @@ -123,9 +123,9 @@ impl<'de> Parser<'de> for SliceParser<'de> { self.slice.get(self.index).copied() } - fn parse_f32(&mut self, cx: &C) -> Result + fn parse_f32(&mut self, cx: C) -> Result where - C: ?Sized + Context, + C: Context, { let Some((value, read)) = crate::dec2flt::dec2flt(&self.slice[self.index..]) else { return Err(cx.message(ErrorMessage::ParseFloat)); @@ -136,9 +136,9 @@ impl<'de> Parser<'de> for SliceParser<'de> { Ok(value) } - fn parse_f64(&mut self, cx: &C) -> Result + fn parse_f64(&mut self, cx: C) -> Result where - C: ?Sized + Context, + C: Context, { let Some((value, read)) = crate::dec2flt::dec2flt(&self.slice[self.index..]) else { return Err(cx.message(ErrorMessage::ParseFloat)); diff --git a/crates/musli/src/json/parser/string.rs b/crates/musli/src/json/parser/string.rs index 88ddefba4..ed3e42f62 100644 --- a/crates/musli/src/json/parser/string.rs +++ b/crates/musli/src/json/parser/string.rs @@ -44,21 +44,18 @@ pub enum StringReference<'de, 'scratch> { } /// Accessor for a slice. -pub(crate) struct SliceAccess<'a, 'de, C> -where - C: ?Sized, -{ - cx: &'a C, +pub(crate) struct SliceAccess<'de, C> { + cx: C, slice: &'de [u8], pub(crate) index: usize, } -impl<'a, 'de, C> SliceAccess<'a, 'de, C> +impl<'de, C> SliceAccess<'de, C> where - C: ?Sized + Context, + C: Context, { #[inline] - pub(crate) fn new(cx: &'a C, slice: &'de [u8], index: usize) -> Self { + pub(crate) fn new(cx: C, slice: &'de [u8], index: usize) -> Self { Self { cx, slice, index } } diff --git a/crates/musli/src/lib.rs b/crates/musli/src/lib.rs index 2b57f14fe..1ac81574c 100644 --- a/crates/musli/src/lib.rs +++ b/crates/musli/src/lib.rs @@ -548,25 +548,22 @@ pub use musli_core::mode; /// use musli::Context; /// use musli::en::{Encoder, Encode}; /// -/// struct MyEncoder<'a, C: ?Sized> { +/// struct MyEncoder<'a, C> { +/// cx: C, /// value: &'a mut Option, -/// cx: &'a C, /// } /// /// #[musli::encoder] /// impl Encoder for MyEncoder<'_, C> /// where -/// C: ?Sized + Context, +/// C: Context, /// { /// type Cx = C; /// type Ok = (); /// /// #[inline] -/// fn cx(self, f: F) -> O -/// where -/// F: FnOnce(&Self::Cx, Self) -> O, -/// { -/// f(self.cx, self) +/// fn cx(&self) -> Self::Cx { +/// self.cx /// } /// /// fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -607,20 +604,20 @@ pub use musli_core::encoder; /// use musli::Context; /// use musli::de::{Decoder, Decode}; /// -/// struct MyDecoder<'a, C: ?Sized> { -/// cx: &'a C, +/// struct MyDecoder { +/// cx: C, /// } /// /// #[musli::decoder] -/// impl<'de, C: ?Sized + Context> Decoder<'de> for MyDecoder<'_, C> { +/// impl<'de, C> Decoder<'de> for MyDecoder +/// where +/// C: Context, +/// { /// type Cx = C; /// /// #[inline] -/// fn cx(self, f: F) -> O -/// where -/// F: FnOnce(&Self::Cx, Self) -> O, -/// { -/// f(self.cx, self) +/// fn cx(&self) -> Self::Cx { +/// self.cx /// } /// /// fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -658,7 +655,10 @@ pub use musli_core::decoder; /// struct AnyVisitor; /// /// #[musli::visitor] -/// impl<'de, C: ?Sized + Context> Visitor<'de, C> for AnyVisitor { +/// impl<'de, C> Visitor<'de, C> for AnyVisitor +/// where +/// C: Context, +/// { /// type Ok = (); /// /// #[inline] diff --git a/crates/musli/src/macros/internal.rs b/crates/musli/src/macros/internal.rs index bca9c745d..48834c318 100644 --- a/crates/musli/src/macros/internal.rs +++ b/crates/musli/src/macros/internal.rs @@ -654,9 +654,9 @@ macro_rules! encoding_impls { /// # Ok::<(), Error>(()) /// ``` #[inline] - pub fn encode_with(self, cx: &C, writer: W, value: &T) -> Result + pub fn encode_with(self, cx: C, writer: W, value: &T) -> Result where - C: ?Sized + Context, + C: Context, W: $writer_trait, T: ?Sized + Encode, { @@ -712,12 +712,12 @@ macro_rules! encoding_impls { #[inline] pub fn to_slice_with( self, - cx: &C, + cx: C, out: &mut [u8], value: &T, ) -> Result where - C: ?Sized + Context, + C: Context, T: ?Sized + Encode, { let len = out.len(); @@ -767,11 +767,11 @@ macro_rules! encoding_impls { #[inline] pub fn to_vec_with( self, - cx: &C, + cx: C, value: &T, ) -> Result, C::Error> where - C: ?Sized + Context, + C: Context, T: ?Sized + Encode, { let mut vec = rust_alloc::vec::Vec::new(); @@ -814,11 +814,11 @@ macro_rules! encoding_impls { #[inline] pub fn to_fixed_bytes_with( self, - cx: &C, + cx: C, value: &T, ) -> Result<$crate::FixedBytes, C::Error> where - C: ?Sized + Context, + C: Context, T: ?Sized + Encode, { let mut bytes = $crate::FixedBytes::new(); @@ -864,9 +864,9 @@ macro_rules! encoding_impls { /// ``` #[cfg(feature = "std")] #[inline] - pub fn to_writer_with(self, cx: &C, write: W, value: &T) -> Result<(), C::Error> + pub fn to_writer_with(self, cx: C, write: W, value: &T) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, W: std::io::Write, T: ?Sized + Encode, { @@ -914,9 +914,9 @@ macro_rules! encoding_impls { /// # Ok::<(), Error>(()) /// ``` #[inline] - pub fn decode_with<'de, C, R, T>(self, cx: &C, reader: R) -> Result + pub fn decode_with<'de, C, R, T>(self, cx: C, reader: R) -> Result where - C: ?Sized + Context, + C: Context, R: $reader_trait<'de>, T: Decode<'de, C::Mode>, { @@ -963,9 +963,9 @@ macro_rules! encoding_impls { /// # Ok::<(), Error>(()) /// ``` #[inline] - pub fn from_slice_with<'de, C, T>(self, cx: &C, bytes: &'de [u8]) -> Result + pub fn from_slice_with<'de, C, T>(self, cx: C, bytes: &'de [u8]) -> Result where - C: ?Sized + Context, + C: Context, T: Decode<'de, $mode>, { self.decode_with(cx, bytes) @@ -982,9 +982,9 @@ macro_rules! encoding_impls { /// /// [`Context`]: crate::Context #[inline] - pub fn from_str_with<'de, C, T>(self, cx: &C, string: &'de str) -> Result + pub fn from_str_with<'de, C, T>(self, cx: C, string: &'de str) -> Result where - C: ?Sized + Context, + C: Context, T: Decode<'de, M>, { self.from_slice_with(cx, string.as_bytes()) diff --git a/crates/musli/src/reader.rs b/crates/musli/src/reader.rs index 03814e22a..db5bab556 100644 --- a/crates/musli/src/reader.rs +++ b/crates/musli/src/reader.rs @@ -52,24 +52,24 @@ pub trait Reader<'de>: self::sealed::Sealed { fn borrow_mut(&mut self) -> Self::Mut<'_>; /// Skip over the given number of bytes. - fn skip(&mut self, cx: &C, n: usize) -> Result<(), C::Error> + fn skip(&mut self, cx: C, n: usize) -> Result<(), C::Error> where - C: ?Sized + Context; + C: Context; /// Peek the next value. fn peek(&mut self) -> Option; /// Read a slice into the given buffer. #[inline] - fn read(&mut self, cx: &C, buf: &mut [u8]) -> Result<(), C::Error> + fn read(&mut self, cx: C, buf: &mut [u8]) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { struct Visitor<'a>(&'a mut [u8]); impl<'de, C> UnsizedVisitor<'de, C, [u8]> for Visitor<'_> where - C: ?Sized + Context, + C: Context, { type Ok = (); @@ -79,12 +79,12 @@ pub trait Reader<'de>: self::sealed::Sealed { } #[inline] - fn visit_borrowed(self, cx: &C, bytes: &'de [u8]) -> Result { + fn visit_borrowed(self, cx: C, bytes: &'de [u8]) -> Result { self.visit_ref(cx, bytes) } #[inline] - fn visit_ref(self, _: &C, bytes: &[u8]) -> Result { + fn visit_ref(self, _: C, bytes: &[u8]) -> Result { self.0.copy_from_slice(bytes); Ok(()) } @@ -94,9 +94,9 @@ pub trait Reader<'de>: self::sealed::Sealed { } /// Read a slice out of the current reader. - fn read_bytes(&mut self, cx: &C, n: usize, visitor: V) -> Result + fn read_bytes(&mut self, cx: C, n: usize, visitor: V) -> Result where - C: ?Sized + Context, + C: Context, V: UnsizedVisitor<'de, C, [u8]>; /// Read into the given buffer which might not have been initialized. @@ -107,18 +107,18 @@ pub trait Reader<'de>: self::sealed::Sealed { /// `len`. unsafe fn read_bytes_uninit( &mut self, - cx: &C, + cx: C, ptr: *mut u8, len: usize, ) -> Result<(), C::Error> where - C: ?Sized + Context; + C: Context; /// Read a single byte. #[inline] - fn read_byte(&mut self, cx: &C) -> Result + fn read_byte(&mut self, cx: C) -> Result where - C: ?Sized + Context, + C: Context, { let [byte] = self.read_array::(cx)?; Ok(byte) @@ -126,15 +126,15 @@ pub trait Reader<'de>: self::sealed::Sealed { /// Read an array out of the current reader. #[inline] - fn read_array(&mut self, cx: &C) -> Result<[u8; N], C::Error> + fn read_array(&mut self, cx: C) -> Result<[u8; N], C::Error> where - C: ?Sized + Context, + C: Context, { struct Visitor([u8; N]); impl<'de, const N: usize, C> UnsizedVisitor<'de, C, [u8]> for Visitor where - C: ?Sized + Context, + C: Context, { type Ok = [u8; N]; @@ -144,12 +144,12 @@ pub trait Reader<'de>: self::sealed::Sealed { } #[inline] - fn visit_borrowed(self, cx: &C, bytes: &'de [u8]) -> Result { + fn visit_borrowed(self, cx: C, bytes: &'de [u8]) -> Result { self.visit_ref(cx, bytes) } #[inline] - fn visit_ref(mut self, cx: &C, bytes: &[u8]) -> Result { + fn visit_ref(mut self, cx: C, bytes: &[u8]) -> Result { self.0.copy_from_slice(bytes); cx.advance(bytes.len()); Ok(self.0) @@ -193,9 +193,9 @@ impl<'de> Reader<'de> for &'de [u8] { } #[inline] - fn skip(&mut self, cx: &C, n: usize) -> Result<(), C::Error> + fn skip(&mut self, cx: C, n: usize) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { if self.len() < n { return Err(cx.message(SliceUnderflow { @@ -211,9 +211,9 @@ impl<'de> Reader<'de> for &'de [u8] { } #[inline] - fn read(&mut self, cx: &C, buf: &mut [u8]) -> Result<(), C::Error> + fn read(&mut self, cx: C, buf: &mut [u8]) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { if self.len() < buf.len() { return Err(cx.custom(SliceUnderflow::new(buf.len(), self.len()))); @@ -227,9 +227,9 @@ impl<'de> Reader<'de> for &'de [u8] { } #[inline] - fn read_bytes(&mut self, cx: &C, n: usize, visitor: V) -> Result + fn read_bytes(&mut self, cx: C, n: usize, visitor: V) -> Result where - C: ?Sized + Context, + C: Context, V: UnsizedVisitor<'de, C, [u8]>, { if self.len() < n { @@ -244,14 +244,9 @@ impl<'de> Reader<'de> for &'de [u8] { } #[inline] - unsafe fn read_bytes_uninit( - &mut self, - cx: &C, - ptr: *mut u8, - n: usize, - ) -> Result<(), C::Error> + unsafe fn read_bytes_uninit(&mut self, cx: C, ptr: *mut u8, n: usize) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { if self.len() < n { return Err(cx.custom(SliceUnderflow::new(n, self.len()))); @@ -264,9 +259,9 @@ impl<'de> Reader<'de> for &'de [u8] { } #[inline] - fn read_byte(&mut self, cx: &C) -> Result + fn read_byte(&mut self, cx: C) -> Result where - C: ?Sized + Context, + C: Context, { let &[first, ref tail @ ..] = *self else { return Err(cx.custom(SliceUnderflow::new(1, self.len()))); @@ -278,9 +273,9 @@ impl<'de> Reader<'de> for &'de [u8] { } #[inline] - fn read_array(&mut self, cx: &C) -> Result<[u8; N], C::Error> + fn read_array(&mut self, cx: C) -> Result<[u8; N], C::Error> where - C: ?Sized + Context, + C: Context, { if self.len() < N { return Err(cx.custom(SliceUnderflow::new(N, self.len()))); @@ -338,9 +333,9 @@ impl<'de> SliceReader<'de> { /// use musli::Context; /// use musli::reader::{Reader, SliceReader}; /// - /// fn process(cx: &C) -> Result<(), C::Error> + /// fn process(cx: C) -> Result<(), C::Error> /// where - /// C: ?Sized + Context + /// C: Context /// { /// let mut reader = SliceReader::new(&[1, 2, 3, 4]); /// assert_eq!(reader.as_slice(), &[1, 2, 3, 4]); @@ -362,9 +357,9 @@ impl<'de> SliceReader<'de> { /// use musli::Context; /// use musli::reader::{Reader, SliceReader}; /// - /// fn process(cx: &C) -> Result<(), C::Error> + /// fn process(cx: C) -> Result<(), C::Error> /// where - /// C: ?Sized + Context + /// C: Context /// { /// let mut reader = SliceReader::new(&[1, 2, 3, 4]); /// assert_eq!(reader.remaining(), 4); @@ -390,9 +385,9 @@ impl<'de> Reader<'de> for SliceReader<'de> { } #[inline] - fn skip(&mut self, cx: &C, n: usize) -> Result<(), C::Error> + fn skip(&mut self, cx: C, n: usize) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { self.range.start = bounds_check_add(cx, &self.range, n)?; cx.advance(n); @@ -400,9 +395,9 @@ impl<'de> Reader<'de> for SliceReader<'de> { } #[inline] - fn read_bytes(&mut self, cx: &C, n: usize, visitor: V) -> Result + fn read_bytes(&mut self, cx: C, n: usize, visitor: V) -> Result where - C: ?Sized + Context, + C: Context, V: UnsizedVisitor<'de, C, [u8]>, { let outcome = bounds_check_add(cx, &self.range, n)?; @@ -418,14 +413,9 @@ impl<'de> Reader<'de> for SliceReader<'de> { } #[inline] - unsafe fn read_bytes_uninit( - &mut self, - cx: &C, - ptr: *mut u8, - n: usize, - ) -> Result<(), C::Error> + unsafe fn read_bytes_uninit(&mut self, cx: C, ptr: *mut u8, n: usize) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { let outcome = bounds_check_add(cx, &self.range, n)?; ptr.copy_from_nonoverlapping(self.range.start, n); @@ -445,9 +435,9 @@ impl<'de> Reader<'de> for SliceReader<'de> { } #[inline] - fn read(&mut self, cx: &C, buf: &mut [u8]) -> Result<(), C::Error> + fn read(&mut self, cx: C, buf: &mut [u8]) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { let outcome = bounds_check_add(cx, &self.range, buf.len())?; @@ -465,9 +455,9 @@ impl<'de> Reader<'de> for SliceReader<'de> { } #[inline] -fn bounds_check_add(cx: &C, range: &Range<*const u8>, len: usize) -> Result<*const u8, C::Error> +fn bounds_check_add(cx: C, range: &Range<*const u8>, len: usize) -> Result<*const u8, C::Error> where - C: ?Sized + Context, + C: Context, { let outcome = range.start.wrapping_add(len); @@ -502,9 +492,9 @@ where R: Reader<'de>, { #[inline] - fn bounds_check(&mut self, cx: &C, n: usize) -> Result<(), C::Error> + fn bounds_check(&mut self, cx: C, n: usize) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { match self.remaining.checked_sub(n) { Some(remaining) => { @@ -531,18 +521,18 @@ where } #[inline] - fn skip(&mut self, cx: &C, n: usize) -> Result<(), C::Error> + fn skip(&mut self, cx: C, n: usize) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { self.bounds_check(cx, n)?; self.reader.skip(cx, n) } #[inline] - fn read_bytes(&mut self, cx: &C, n: usize, visitor: V) -> Result + fn read_bytes(&mut self, cx: C, n: usize, visitor: V) -> Result where - C: ?Sized + Context, + C: Context, V: UnsizedVisitor<'de, C, [u8]>, { self.bounds_check(cx, n)?; @@ -550,14 +540,9 @@ where } #[inline] - unsafe fn read_bytes_uninit( - &mut self, - cx: &C, - ptr: *mut u8, - n: usize, - ) -> Result<(), C::Error> + unsafe fn read_bytes_uninit(&mut self, cx: C, ptr: *mut u8, n: usize) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { self.bounds_check(cx, n)?; self.reader.read_bytes_uninit(cx, ptr, n) @@ -573,27 +558,27 @@ where } #[inline] - fn read(&mut self, cx: &C, buf: &mut [u8]) -> Result<(), C::Error> + fn read(&mut self, cx: C, buf: &mut [u8]) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { self.bounds_check(cx, buf.len())?; self.reader.read(cx, buf) } #[inline] - fn read_byte(&mut self, cx: &C) -> Result + fn read_byte(&mut self, cx: C) -> Result where - C: ?Sized + Context, + C: Context, { self.bounds_check(cx, 1)?; self.reader.read_byte(cx) } #[inline] - fn read_array(&mut self, cx: &C) -> Result<[u8; N], C::Error> + fn read_array(&mut self, cx: C) -> Result<[u8; N], C::Error> where - C: ?Sized + Context, + C: Context, { self.bounds_check(cx, N)?; self.reader.read_array(cx) @@ -627,31 +612,26 @@ where } #[inline] - fn skip(&mut self, cx: &C, n: usize) -> Result<(), C::Error> + fn skip(&mut self, cx: C, n: usize) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { (**self).skip(cx, n) } #[inline] - fn read_bytes(&mut self, cx: &C, n: usize, visitor: V) -> Result + fn read_bytes(&mut self, cx: C, n: usize, visitor: V) -> Result where - C: ?Sized + Context, + C: Context, V: UnsizedVisitor<'de, C, [u8]>, { (**self).read_bytes(cx, n, visitor) } #[inline] - unsafe fn read_bytes_uninit( - &mut self, - cx: &C, - ptr: *mut u8, - n: usize, - ) -> Result<(), C::Error> + unsafe fn read_bytes_uninit(&mut self, cx: C, ptr: *mut u8, n: usize) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { (**self).read_bytes_uninit(cx, ptr, n) } @@ -662,25 +642,25 @@ where } #[inline] - fn read(&mut self, cx: &C, buf: &mut [u8]) -> Result<(), C::Error> + fn read(&mut self, cx: C, buf: &mut [u8]) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { (**self).read(cx, buf) } #[inline] - fn read_byte(&mut self, cx: &C) -> Result + fn read_byte(&mut self, cx: C) -> Result where - C: ?Sized + Context, + C: Context, { (**self).read_byte(cx) } #[inline] - fn read_array(&mut self, cx: &C) -> Result<[u8; N], C::Error> + fn read_array(&mut self, cx: C) -> Result<[u8; N], C::Error> where - C: ?Sized + Context, + C: Context, { (**self).read_array(cx) } diff --git a/crates/musli/src/serde/deserializer.rs b/crates/musli/src/serde/deserializer.rs index 819611faf..023351332 100644 --- a/crates/musli/src/serde/deserializer.rs +++ b/crates/musli/src/serde/deserializer.rs @@ -15,25 +15,18 @@ use rust_alloc::string::String; #[cfg(feature = "alloc")] use rust_alloc::vec::Vec; -pub struct Deserializer<'de, 'a, D> -where - D: Decoder<'de>, -{ - cx: &'a D::Cx, +pub struct Deserializer { decoder: D, } -impl<'de, 'a, D> Deserializer<'de, 'a, D> -where - D: Decoder<'de>, -{ +impl Deserializer { /// Construct a new deserializer out of a decoder. - pub fn new(cx: &'a D::Cx, decoder: D) -> Self { - Self { cx, decoder } + pub fn new(decoder: D) -> Self { + Self { decoder } } } -impl<'de, D> de::Deserializer<'de> for Deserializer<'de, '_, D> +impl<'de, D> de::Deserializer<'de> for Deserializer where D: Decoder<'de, Cx: Context>, { @@ -209,7 +202,7 @@ where V: de::Visitor<'de>, { match self.decoder.decode_option()? { - Some(decoder) => visitor.visit_some(Deserializer::new(self.cx, decoder)), + Some(decoder) => visitor.visit_some(Deserializer::new(decoder)), None => visitor.visit_none(), } } @@ -245,7 +238,7 @@ where where V: de::Visitor<'de>, { - visitor.visit_newtype_struct(Deserializer::new(self.cx, self.decoder)) + visitor.visit_newtype_struct(Deserializer::new(self.decoder)) } #[inline] @@ -254,7 +247,7 @@ where V: de::Visitor<'de>, { self.decoder - .decode_sequence(|d| visitor.visit_seq(SeqAccess::new(self.cx, d))) + .decode_sequence(|d| visitor.visit_seq(SeqAccess::new(d))) } #[inline] @@ -264,9 +257,8 @@ where { let hint = SequenceHint::with_size(len); - self.decoder.decode_sequence_hint(&hint, |d| { - visitor.visit_seq(SequenceAccess::new(self.cx, d)) - }) + self.decoder + .decode_sequence_hint(&hint, |d| visitor.visit_seq(SequenceAccess::new(d))) } #[inline] @@ -288,7 +280,7 @@ where V: de::Visitor<'de>, { self.decoder - .decode_map_entries(|decoder| visitor.visit_map(MapAccess::new(self.cx, decoder))) + .decode_map_entries(|decoder| visitor.visit_map(MapAccess::new(decoder))) } #[inline] @@ -302,7 +294,7 @@ where V: de::Visitor<'de>, { self.decoder - .decode_map_entries(|decoder| visitor.visit_map(StructAccess::new(self.cx, decoder))) + .decode_map_entries(|decoder| visitor.visit_map(StructAccess::new(decoder))) } #[inline] @@ -316,7 +308,7 @@ where V: de::Visitor<'de>, { self.decoder - .decode_variant(|decoder| visitor.visit_enum(EnumAccess::new(self.cx, decoder))) + .decode_variant(|decoder| visitor.visit_enum(EnumAccess::new(decoder))) } #[inline] @@ -342,24 +334,23 @@ where } } -struct SequenceAccess<'de, 'a, D> +struct SequenceAccess<'a, D> where - D: SequenceDecoder<'de>, + D: ?Sized, { - cx: &'a D::Cx, decoder: &'a mut D, } -impl<'de, 'a, D> SequenceAccess<'de, 'a, D> +impl<'a, D> SequenceAccess<'a, D> where - D: SequenceDecoder<'de>, + D: ?Sized, { - fn new(cx: &'a D::Cx, decoder: &'a mut D) -> Self { - SequenceAccess { cx, decoder } + fn new(decoder: &'a mut D) -> Self { + SequenceAccess { decoder } } } -impl<'de, D> de::SeqAccess<'de> for SequenceAccess<'de, '_, D> +impl<'de, D> de::SeqAccess<'de> for SequenceAccess<'_, D> where D: SequenceDecoder<'de, Cx: Context>, { @@ -374,7 +365,7 @@ where return Ok(None); }; - let output = seed.deserialize(Deserializer::new(self.cx, decoder))?; + let output = seed.deserialize(Deserializer::new(decoder))?; Ok(Some(output)) } @@ -383,27 +374,26 @@ where self.decoder.size_hint().into_option() } } -struct StructAccess<'de, 'a, D> +struct StructAccess<'a, D> where - D: EntriesDecoder<'de>, + D: ?Sized, { - cx: &'a D::Cx, decoder: &'a mut D, } -impl<'de, 'a, D> StructAccess<'de, 'a, D> +impl<'a, D> StructAccess<'a, D> where - D: EntriesDecoder<'de>, + D: ?Sized, { #[inline] - fn new(cx: &'a D::Cx, decoder: &'a mut D) -> Self { - StructAccess { cx, decoder } + fn new(decoder: &'a mut D) -> Self { + StructAccess { decoder } } } -impl<'de, D> de::MapAccess<'de> for StructAccess<'de, '_, D> +impl<'de, D> de::MapAccess<'de> for StructAccess<'_, D> where - D: EntriesDecoder<'de, Cx: Context>, + D: ?Sized + EntriesDecoder<'de, Cx: Context>, { type Error = ::Error; @@ -416,7 +406,7 @@ where return Ok(None); }; - let output = seed.deserialize(Deserializer::new(self.cx, decoder))?; + let output = seed.deserialize(Deserializer::new(decoder))?; Ok(Some(output)) } @@ -426,7 +416,7 @@ where V: de::DeserializeSeed<'de>, { let decoder = self.decoder.decode_entry_value()?; - let output = seed.deserialize(Deserializer::new(self.cx, decoder))?; + let output = seed.deserialize(Deserializer::new(decoder))?; Ok(output) } @@ -449,7 +439,7 @@ impl BytesVisitor { impl<'de, C, V> crate::de::UnsizedVisitor<'de, C, [u8]> for BytesVisitor where - C: ?Sized + Context, + C: Context, V: de::Visitor<'de>, { type Ok = V::Value; @@ -461,39 +451,38 @@ where #[inline] #[cfg(feature = "alloc")] - fn visit_owned(self, _: &C, value: Vec) -> Result { + fn visit_owned(self, _: C, value: Vec) -> Result { de::Visitor::visit_byte_buf(self.visitor, value) } #[inline] - fn visit_borrowed(self, _: &C, value: &'de [u8]) -> Result { + fn visit_borrowed(self, _: C, value: &'de [u8]) -> Result { de::Visitor::visit_borrowed_bytes(self.visitor, value) } #[inline] - fn visit_ref(self, _: &C, value: &[u8]) -> Result { + fn visit_ref(self, _: C, value: &[u8]) -> Result { de::Visitor::visit_bytes(self.visitor, value) } } -struct SeqAccess<'de, 'a, D> +struct SeqAccess<'a, D> where - D: ?Sized + SequenceDecoder<'de>, + D: ?Sized, { - cx: &'a D::Cx, decoder: &'a mut D, } -impl<'de, 'a, D> SeqAccess<'de, 'a, D> +impl<'a, D> SeqAccess<'a, D> where - D: ?Sized + SequenceDecoder<'de>, + D: ?Sized, { - fn new(cx: &'a D::Cx, decoder: &'a mut D) -> Self { - Self { cx, decoder } + fn new(decoder: &'a mut D) -> Self { + Self { decoder } } } -impl<'de, D> de::SeqAccess<'de> for SeqAccess<'de, '_, D> +impl<'de, D> de::SeqAccess<'de> for SeqAccess<'_, D> where D: ?Sized + SequenceDecoder<'de, Cx: Context>, { @@ -508,7 +497,7 @@ where return Ok(None); }; - let output = seed.deserialize(Deserializer::new(self.cx, decoder))?; + let output = seed.deserialize(Deserializer::new(decoder))?; Ok(Some(output)) } @@ -518,24 +507,23 @@ where } } -struct MapAccess<'de, 'a, D> +struct MapAccess<'a, D> where - D: ?Sized + EntriesDecoder<'de>, + D: ?Sized, { - cx: &'a D::Cx, decoder: &'a mut D, } -impl<'de, 'a, D> MapAccess<'de, 'a, D> +impl<'a, D> MapAccess<'a, D> where - D: ?Sized + EntriesDecoder<'de>, + D: ?Sized, { - fn new(cx: &'a D::Cx, decoder: &'a mut D) -> Self { - Self { cx, decoder } + fn new(decoder: &'a mut D) -> Self { + Self { decoder } } } -impl<'de, D> de::MapAccess<'de> for MapAccess<'de, '_, D> +impl<'de, D> de::MapAccess<'de> for MapAccess<'_, D> where D: ?Sized + EntriesDecoder<'de, Cx: Context>, { @@ -550,7 +538,7 @@ where return Ok(None); }; - let output = seed.deserialize(Deserializer::new(self.cx, decoder))?; + let output = seed.deserialize(Deserializer::new(decoder))?; Ok(Some(output)) } @@ -560,7 +548,7 @@ where V: de::DeserializeSeed<'de>, { let decoder = self.decoder.decode_entry_value()?; - let output = seed.deserialize(Deserializer::new(self.cx, decoder))?; + let output = seed.deserialize(Deserializer::new(decoder))?; Ok(output) } } @@ -577,7 +565,7 @@ impl StringVisitor { impl<'de, C, V> crate::de::UnsizedVisitor<'de, C, str> for StringVisitor where - C: ?Sized + Context, + C: Context, V: de::Visitor<'de>, { type Ok = V::Value; @@ -589,41 +577,40 @@ where #[inline] #[cfg(feature = "alloc")] - fn visit_owned(self, _: &C, value: String) -> Result { + fn visit_owned(self, _: C, value: String) -> Result { de::Visitor::visit_string(self.visitor, value) } #[inline] - fn visit_borrowed(self, _: &C, value: &'de str) -> Result { + fn visit_borrowed(self, _: C, value: &'de str) -> Result { de::Visitor::visit_borrowed_str(self.visitor, value) } #[inline] - fn visit_ref(self, _: &C, value: &str) -> Result { + fn visit_ref(self, _: C, value: &str) -> Result { de::Visitor::visit_str(self.visitor, value) } } -struct EnumAccess<'de, 'a, D> +struct EnumAccess<'a, D> where - D: VariantDecoder<'de>, + D: ?Sized, { - cx: &'a D::Cx, decoder: &'a mut D, } -impl<'de, 'a, D> EnumAccess<'de, 'a, D> +impl<'a, D> EnumAccess<'a, D> where - D: VariantDecoder<'de>, + D: ?Sized, { - fn new(cx: &'a D::Cx, decoder: &'a mut D) -> Self { - Self { cx, decoder } + fn new(decoder: &'a mut D) -> Self { + Self { decoder } } } -impl<'de, D> de::VariantAccess<'de> for EnumAccess<'de, '_, D> +impl<'de, D> de::VariantAccess<'de> for EnumAccess<'_, D> where - D: VariantDecoder<'de, Cx: Context>, + D: ?Sized + VariantDecoder<'de, Cx: Context>, { type Error = ::Error; @@ -637,7 +624,7 @@ where where T: de::DeserializeSeed<'de>, { - seed.deserialize(Deserializer::new(self.cx, self.decoder.decode_value()?)) + seed.deserialize(Deserializer::new(self.decoder.decode_value()?)) } #[inline] @@ -649,9 +636,7 @@ where self.decoder .decode_value()? - .decode_sequence_hint(&hint, |tuple| { - visitor.visit_seq(SequenceAccess::new(self.cx, tuple)) - }) + .decode_sequence_hint(&hint, |tuple| visitor.visit_seq(SequenceAccess::new(tuple))) } #[inline] @@ -665,11 +650,11 @@ where { self.decoder .decode_value()? - .decode_map_entries(|decoder| visitor.visit_map(StructAccess::new(self.cx, decoder))) + .decode_map_entries(|decoder| visitor.visit_map(StructAccess::new(decoder))) } } -impl<'de, D> de::EnumAccess<'de> for EnumAccess<'de, '_, D> +impl<'de, D> de::EnumAccess<'de> for EnumAccess<'_, D> where D: VariantDecoder<'de, Cx: Context>, { @@ -682,7 +667,7 @@ where V: de::DeserializeSeed<'de>, { let tag = self.decoder.decode_tag()?; - let value = seed.deserialize(Deserializer::new(self.cx, tag))?; + let value = seed.deserialize(Deserializer::new(tag))?; Ok((value, self)) } } @@ -700,7 +685,7 @@ impl AnyVisitor { #[crate::visitor(crate)] impl<'de, C, V> Visitor<'de, C> for AnyVisitor where - C: ?Sized + Context, + C: Context, V: de::Visitor<'de>, { type Ok = V::Value; @@ -714,42 +699,42 @@ where } #[inline] - fn visit_empty(self, _: &C) -> Result { + fn visit_empty(self, _: C) -> Result { self.visitor.visit_unit() } #[inline] - fn visit_bool(self, _: &C, v: bool) -> Result { + fn visit_bool(self, _: C, v: bool) -> Result { self.visitor.visit_bool(v) } #[inline] - fn visit_char(self, _: &C, v: char) -> Result { + fn visit_char(self, _: C, v: char) -> Result { self.visitor.visit_char(v) } #[inline] - fn visit_u8(self, _: &C, v: u8) -> Result { + fn visit_u8(self, _: C, v: u8) -> Result { self.visitor.visit_u8(v) } #[inline] - fn visit_u16(self, _: &C, v: u16) -> Result { + fn visit_u16(self, _: C, v: u16) -> Result { self.visitor.visit_u16(v) } #[inline] - fn visit_u32(self, _: &C, v: u32) -> Result { + fn visit_u32(self, _: C, v: u32) -> Result { self.visitor.visit_u32(v) } #[inline] - fn visit_u64(self, _: &C, v: u64) -> Result { + fn visit_u64(self, _: C, v: u64) -> Result { self.visitor.visit_u64(v) } #[inline] - fn visit_u128(self, _: &C, v: u128) -> Result { + fn visit_u128(self, _: C, v: u128) -> Result { // Serde's 128-bit support is very broken, so just try to avoid it if we can. // See: https://github.com/serde-rs/serde/issues/2576 if let Ok(v) = u64::try_from(v) { @@ -760,27 +745,27 @@ where } #[inline] - fn visit_i8(self, _: &C, v: i8) -> Result { + fn visit_i8(self, _: C, v: i8) -> Result { self.visitor.visit_i8(v) } #[inline] - fn visit_i16(self, _: &C, v: i16) -> Result { + fn visit_i16(self, _: C, v: i16) -> Result { self.visitor.visit_i16(v) } #[inline] - fn visit_i32(self, _: &C, v: i32) -> Result { + fn visit_i32(self, _: C, v: i32) -> Result { self.visitor.visit_i32(v) } #[inline] - fn visit_i64(self, _: &C, v: i64) -> Result { + fn visit_i64(self, _: C, v: i64) -> Result { self.visitor.visit_i64(v) } #[inline] - fn visit_i128(self, _: &C, v: i128) -> Result { + fn visit_i128(self, _: C, v: i128) -> Result { // Serde's 128-bit support is very broken, so just try to avoid it if we can. // See: https://github.com/serde-rs/serde/issues/2576 if let Ok(v) = i64::try_from(v) { @@ -791,7 +776,7 @@ where } #[inline] - fn visit_usize(self, cx: &C, v: usize) -> Result { + fn visit_usize(self, cx: C, v: usize) -> Result { if let Some(value) = unsigned_value(self.visitor, v)? { return Ok(value); } @@ -800,7 +785,7 @@ where } #[inline] - fn visit_isize(self, cx: &C, v: isize) -> Result { + fn visit_isize(self, cx: C, v: isize) -> Result { if let Some(value) = signed_value(self.visitor, v)? { return Ok(value); } @@ -809,54 +794,52 @@ where } #[inline] - fn visit_f32(self, _: &C, v: f32) -> Result { + fn visit_f32(self, _: C, v: f32) -> Result { self.visitor.visit_f32(v) } #[inline] - fn visit_f64(self, _: &C, v: f64) -> Result { + fn visit_f64(self, _: C, v: f64) -> Result { self.visitor.visit_f64(v) } #[inline] - fn visit_option(self, cx: &C, v: Option) -> Result + fn visit_option(self, _: C, v: Option) -> Result where D: Decoder<'de, Cx = C>, { match v { - Some(v) => self.visitor.visit_some(Deserializer::new(cx, v)), + Some(v) => self.visitor.visit_some(Deserializer::new(v)), None => self.visitor.visit_none(), } } #[inline] - fn visit_sequence(self, cx: &C, decoder: &mut D) -> Result + fn visit_sequence(self, _: C, decoder: &mut D) -> Result where D: ?Sized + SequenceDecoder<'de, Cx = C>, { - self.visitor.visit_seq(SeqAccess::new(cx, decoder)) + self.visitor.visit_seq(SeqAccess::new(decoder)) } #[inline] - fn visit_map(self, cx: &C, decoder: &mut D) -> Result + fn visit_map(self, _: C, decoder: &mut D) -> Result where D: ?Sized + MapDecoder<'de, Cx = C>, { let mut map_decoder = decoder.decode_remaining_entries()?; - let value = self - .visitor - .visit_map(MapAccess::new(cx, &mut map_decoder))?; + let value = self.visitor.visit_map(MapAccess::new(&mut map_decoder))?; map_decoder.end_entries()?; Ok(value) } #[inline] - fn visit_string(self, _: &C, _: SizeHint) -> Result { + fn visit_string(self, _: C, _: SizeHint) -> Result { Ok(StringVisitor::new(self.visitor)) } #[inline] - fn visit_bytes(self, _: &C, _: SizeHint) -> Result { + fn visit_bytes(self, _: C, _: SizeHint) -> Result { Ok(BytesVisitor::new(self.visitor)) } } diff --git a/crates/musli/src/serde/error.rs b/crates/musli/src/serde/error.rs index 4b6fa0140..b6732a895 100644 --- a/crates/musli/src/serde/error.rs +++ b/crates/musli/src/serde/error.rs @@ -17,9 +17,9 @@ pub enum SerdeError { } impl SerdeError { - pub(super) fn report(self, cx: &C) -> Option + pub(super) fn report(self, cx: C) -> Option where - C: ?Sized + Context, + C: Context, { match self { SerdeError::Captured => None, diff --git a/crates/musli/src/serde/mod.rs b/crates/musli/src/serde/mod.rs index a1b93dcd3..52ea0be6c 100644 --- a/crates/musli/src/serde/mod.rs +++ b/crates/musli/src/serde/mod.rs @@ -106,17 +106,17 @@ use self::serializer::Serializer; use crate::alloc::{self, String}; use crate::{Context, Decoder, Encoder}; -struct SerdeContext<'a, C> +struct SerdeContext where - C: ?Sized + Context, + C: Context, { error: RefCell>, - inner: &'a C, + inner: C, } -impl Context for SerdeContext<'_, C> +impl Context for &SerdeContext where - C: ?Sized + Context, + C: Context, { type Mode = C::Mode; type Error = error::SerdeError; @@ -125,28 +125,28 @@ where type String = String; #[inline] - fn clear(&self) { + fn clear(self) { self.inner.clear(); *self.error.borrow_mut() = None; } #[inline] - fn mark(&self) -> Self::Mark { + fn mark(self) -> Self::Mark { self.inner.mark() } #[inline] - fn advance(&self, n: usize) { + fn advance(self, n: usize) { self.inner.advance(n) } #[inline] - fn alloc(&self) -> Self::Allocator { + fn alloc(self) -> Self::Allocator { self.inner.alloc() } #[inline] - fn collect_string(&self, value: &T) -> Result + fn collect_string(self, value: &T) -> Result where T: ?Sized + fmt::Display, { @@ -157,7 +157,7 @@ where } #[inline] - fn custom(&self, error: T) -> Self::Error + fn custom(self, error: T) -> Self::Error where T: 'static + Send + Sync + Error, { @@ -166,7 +166,7 @@ where } #[inline] - fn message(&self, message: T) -> Self::Error + fn message(self, message: T) -> Self::Error where T: fmt::Display, { @@ -182,31 +182,31 @@ where E: Encoder, T: Serialize, { - encoder.cx(|cx, encoder| { - let cx = SerdeContext { - error: RefCell::new(None), - inner: cx, - }; + let cx = encoder.cx(); - let encoder = encoder.with_context(&cx)?; + let cx = SerdeContext { + error: RefCell::new(None), + inner: cx, + }; - let serializer = Serializer::new(encoder); + let encoder = encoder.with_context(&cx)?; - let error = match value.serialize(serializer) { - Ok(value) => return Ok(value), - Err(error) => error, - }; + let serializer = Serializer::new(encoder); - if let Some(error) = error.report(cx.inner) { - return Err(error); - } + let error = match value.serialize(serializer) { + Ok(value) => return Ok(value), + Err(error) => error, + }; + + if let Some(error) = error.report(cx.inner) { + return Err(error); + } - let Some(error) = cx.error.borrow_mut().take() else { - return Err(cx.inner.message("error during encoding (no information)")); - }; + let Some(error) = cx.error.borrow_mut().take() else { + return Err(cx.inner.message("error during encoding (no information)")); + }; - Err(error) - }) + Err(error) } /// Decode the given serde value `T` from the given [Decoder] using the serde @@ -216,29 +216,29 @@ where D: Decoder<'de>, T: Deserialize<'de>, { - decoder.cx(|cx, decoder| { - let cx = SerdeContext { - error: RefCell::new(None), - inner: cx, - }; + let cx = decoder.cx(); - let decoder = decoder.with_context(&cx)?; + let cx = SerdeContext { + error: RefCell::new(None), + inner: cx, + }; - let deserializer = Deserializer::new(&cx, decoder); + let decoder = decoder.with_context(&cx)?; - let error = match T::deserialize(deserializer) { - Ok(value) => return Ok(value), - Err(error) => error, - }; + let deserializer = Deserializer::new(decoder); - if let Some(error) = error.report(cx.inner) { - return Err(error); - } + let error = match T::deserialize(deserializer) { + Ok(value) => return Ok(value), + Err(error) => error, + }; + + if let Some(error) = error.report(cx.inner) { + return Err(error); + } - let Some(error) = cx.error.borrow_mut().take() else { - return Err(cx.inner.message("error during encoding (no information)")); - }; + let Some(error) = cx.error.borrow_mut().take() else { + return Err(cx.inner.message("error during encoding (no information)")); + }; - Err(error) - }) + Err(error) } diff --git a/crates/musli/src/serde/serializer.rs b/crates/musli/src/serde/serializer.rs index 413ad586c..fcd699292 100644 --- a/crates/musli/src/serde/serializer.rs +++ b/crates/musli/src/serde/serializer.rs @@ -229,15 +229,15 @@ where #[inline] fn serialize_map(self, len: Option) -> Result { - self.encoder.cx(|cx, encoder| { - let Some(len) = len else { - return Err(cx.message("Can only serialize maps with known lengths")); - }; - - let hint = MapHint::with_size(len); - let encoder = encoder.encode_map_entries(&hint)?; - Ok(SerializeMap::new(encoder)) - }) + let cx = self.encoder.cx(); + + let Some(len) = len else { + return Err(cx.message("Can only serialize maps with known lengths")); + }; + + let hint = MapHint::with_size(len); + let encoder = self.encoder.encode_map_entries(&hint)?; + Ok(SerializeMap::new(encoder)) } #[inline] diff --git a/crates/musli/src/storage/de.rs b/crates/musli/src/storage/de.rs index cd1d765d9..b01c66183 100644 --- a/crates/musli/src/storage/de.rs +++ b/crates/musli/src/storage/de.rs @@ -23,15 +23,15 @@ macro_rules! is_bitwise_slice { } /// A very simple decoder suitable for storage decoding. -pub struct StorageDecoder<'a, R, const OPT: Options, C: ?Sized> { - cx: &'a C, +pub struct StorageDecoder { + cx: C, reader: R, } -impl<'a, R, const OPT: Options, C: ?Sized> StorageDecoder<'a, R, OPT, C> { +impl StorageDecoder { /// Construct a new fixed width message encoder. #[inline] - pub fn new(cx: &'a C, reader: R) -> Self { + pub fn new(cx: C, reader: R) -> Self { Self { cx, reader } } } @@ -41,42 +41,39 @@ impl<'a, R, const OPT: Options, C: ?Sized> StorageDecoder<'a, R, OPT, C> { /// This simplifies implementing decoders that do not have any special handling /// for length-prefixed types. #[doc(hidden)] -pub struct LimitedStorageDecoder<'a, R, const OPT: Options, C: ?Sized> { +pub struct LimitedStorageDecoder { remaining: usize, - cx: &'a C, + cx: C, reader: R, } #[crate::decoder(crate)] -impl<'a, 'de, R, const OPT: Options, C: ?Sized + Context> Decoder<'de> - for StorageDecoder<'a, R, OPT, C> +impl<'de, R, const OPT: Options, C> Decoder<'de> for StorageDecoder where R: Reader<'de>, + C: Context, { type Cx = C; type Error = C::Error; type Mode = C::Mode; - type WithContext<'this, U> - = StorageDecoder<'this, R, OPT, U> + type WithContext + = StorageDecoder where - U: 'this + Context; + U: Context; type DecodePack = Self; type DecodeSome = Self; - type DecodeSequence = LimitedStorageDecoder<'a, R, OPT, C>; - type DecodeMap = LimitedStorageDecoder<'a, R, OPT, C>; - type DecodeMapEntries = LimitedStorageDecoder<'a, R, OPT, C>; + type DecodeSequence = LimitedStorageDecoder; + type DecodeMap = LimitedStorageDecoder; + type DecodeMapEntries = LimitedStorageDecoder; type DecodeVariant = Self; #[inline] - fn cx(self, f: F) -> O - where - F: FnOnce(&Self::Cx, Self) -> O, - { - f(self.cx, self) + fn cx(&self) -> Self::Cx { + self.cx } #[inline] - fn with_context(self, cx: &U) -> Result, C::Error> + fn with_context(self, cx: U) -> Result, C::Error> where U: Context, { @@ -154,7 +151,7 @@ where impl<'de, C, V> UnsizedVisitor<'de, C, [u8]> for Visitor where - C: ?Sized + Context, + C: Context, V: UnsizedVisitor<'de, C, str>, { type Ok = V::Ok; @@ -166,19 +163,19 @@ where #[cfg(feature = "alloc")] #[inline] - fn visit_owned(self, cx: &C, bytes: Vec) -> Result { + fn visit_owned(self, cx: C, bytes: Vec) -> Result { let string = crate::str::from_utf8_owned(bytes).map_err(cx.map())?; self.0.visit_owned(cx, string) } #[inline] - fn visit_borrowed(self, cx: &C, bytes: &'de [u8]) -> Result { + fn visit_borrowed(self, cx: C, bytes: &'de [u8]) -> Result { let string = crate::str::from_utf8(bytes).map_err(cx.map())?; self.0.visit_borrowed(cx, string) } #[inline] - fn visit_ref(self, cx: &C, bytes: &[u8]) -> Result { + fn visit_ref(self, cx: C, bytes: &[u8]) -> Result { let string = crate::str::from_utf8(bytes).map_err(cx.map())?; self.0.visit_ref(cx, string) } @@ -415,14 +412,14 @@ where } } -impl<'a, 'de, R, const OPT: Options, C: ?Sized + Context> SequenceDecoder<'de> - for StorageDecoder<'a, R, OPT, C> +impl<'de, R, const OPT: Options, C> SequenceDecoder<'de> for StorageDecoder where R: Reader<'de>, + C: Context, { type Cx = C; type DecodeNext<'this> - = StorageDecoder<'a, R::Mut<'this>, OPT, C> + = StorageDecoder, OPT, C> where Self: 'this; @@ -439,13 +436,13 @@ where } } -impl<'a, 'de, R, const OPT: Options, C> LimitedStorageDecoder<'a, R, OPT, C> +impl<'de, R, const OPT: Options, C> LimitedStorageDecoder where - C: ?Sized + Context, + C: Context, R: Reader<'de>, { #[inline] - fn new(cx: &'a C, mut reader: R) -> Result { + fn new(cx: C, mut reader: R) -> Result { let remaining = crate::int::decode_usize::<_, _, OPT>(cx, reader.borrow_mut())?; Ok(Self { @@ -456,7 +453,7 @@ where } #[inline] - fn with_remaining(cx: &'a C, reader: R, remaining: usize) -> Self { + fn with_remaining(cx: C, reader: R, remaining: usize) -> Self { Self { cx, reader, @@ -465,14 +462,14 @@ where } } -impl<'a, 'de, R, const OPT: Options, C: ?Sized + Context> SequenceDecoder<'de> - for LimitedStorageDecoder<'a, R, OPT, C> +impl<'de, R, const OPT: Options, C> SequenceDecoder<'de> for LimitedStorageDecoder where R: Reader<'de>, + C: Context, { type Cx = C; type DecodeNext<'this> - = StorageDecoder<'a, R::Mut<'this>, OPT, C> + = StorageDecoder, OPT, C> where Self: 'this; @@ -503,18 +500,18 @@ where } } -impl<'a, 'de, R, const OPT: Options, C: ?Sized + Context> MapDecoder<'de> - for LimitedStorageDecoder<'a, R, OPT, C> +impl<'de, R, const OPT: Options, C> MapDecoder<'de> for LimitedStorageDecoder where R: Reader<'de>, + C: Context, { type Cx = C; type DecodeEntry<'this> - = StorageDecoder<'a, R::Mut<'this>, OPT, C> + = StorageDecoder, OPT, C> where Self: 'this; type DecodeRemainingEntries<'this> - = LimitedStorageDecoder<'a, R::Mut<'this>, OPT, C> + = LimitedStorageDecoder, OPT, C> where Self: 'this; @@ -545,14 +542,14 @@ where } } -impl<'a, 'de, R, const OPT: Options, C: ?Sized + Context> EntryDecoder<'de> - for StorageDecoder<'a, R, OPT, C> +impl<'de, R, const OPT: Options, C> EntryDecoder<'de> for StorageDecoder where R: Reader<'de>, + C: Context, { type Cx = C; type DecodeKey<'this> - = StorageDecoder<'a, R::Mut<'this>, OPT, C> + = StorageDecoder, OPT, C> where Self: 'this; type DecodeValue = Self; @@ -568,18 +565,18 @@ where } } -impl<'a, 'de, R, const OPT: Options, C: ?Sized + Context> EntriesDecoder<'de> - for LimitedStorageDecoder<'a, R, OPT, C> +impl<'de, R, const OPT: Options, C> EntriesDecoder<'de> for LimitedStorageDecoder where R: Reader<'de>, + C: Context, { type Cx = C; type DecodeEntryKey<'this> - = StorageDecoder<'a, R::Mut<'this>, OPT, C> + = StorageDecoder, OPT, C> where Self: 'this; type DecodeEntryValue<'this> - = StorageDecoder<'a, R::Mut<'this>, OPT, C> + = StorageDecoder, OPT, C> where Self: 'this; @@ -610,18 +607,18 @@ where } } -impl<'a, 'de, R, const OPT: Options, C: ?Sized + Context> VariantDecoder<'de> - for StorageDecoder<'a, R, OPT, C> +impl<'de, R, const OPT: Options, C> VariantDecoder<'de> for StorageDecoder where R: Reader<'de>, + C: Context, { type Cx = C; type DecodeTag<'this> - = StorageDecoder<'a, R::Mut<'this>, OPT, C> + = StorageDecoder, OPT, C> where Self: 'this; type DecodeValue<'this> - = StorageDecoder<'a, R::Mut<'this>, OPT, C> + = StorageDecoder, OPT, C> where Self: 'this; diff --git a/crates/musli/src/storage/en.rs b/crates/musli/src/storage/en.rs index b91c9dc53..34390b359 100644 --- a/crates/musli/src/storage/en.rs +++ b/crates/musli/src/storage/en.rs @@ -21,34 +21,34 @@ macro_rules! is_bitwise_slice { } /// A very simple encoder suitable for storage encoding. -pub struct StorageEncoder<'a, W, const OPT: Options, C: ?Sized> { - cx: &'a C, +pub struct StorageEncoder { + cx: C, writer: W, } -impl<'a, W, const OPT: Options, C: ?Sized> StorageEncoder<'a, W, OPT, C> { +impl StorageEncoder { /// Construct a new fixed width message encoder. #[inline] - pub fn new(cx: &'a C, writer: W) -> Self { + pub fn new(cx: C, writer: W) -> Self { Self { cx, writer } } } #[crate::encoder(crate)] -impl<'a, W, const OPT: Options, C> Encoder for StorageEncoder<'a, W, OPT, C> +impl Encoder for StorageEncoder where - C: ?Sized + Context, + C: Context, W: Writer, { type Cx = C; type Error = C::Error; type Ok = (); type Mode = C::Mode; - type WithContext<'this, U> - = StorageEncoder<'this, W, OPT, U> + type WithContext + = StorageEncoder where - U: 'this + Context; - type EncodePack = StorageEncoder<'a, W, OPT, C>; + U: Context; + type EncodePack = StorageEncoder; type EncodeSome = Self; type EncodeSequence = Self; type EncodeMap = Self; @@ -58,15 +58,12 @@ where type EncodeMapVariant = Self; #[inline] - fn cx(self, f: F) -> O - where - F: FnOnce(&Self::Cx, Self) -> O, - { - f(self.cx, self) + fn cx(&self) -> Self::Cx { + self.cx } #[inline] - fn with_context(self, cx: &U) -> Result, C::Error> + fn with_context(self, cx: U) -> Result, C::Error> where U: Context, { @@ -331,24 +328,21 @@ where } } -impl<'a, W, const OPT: Options, C> SequenceEncoder for StorageEncoder<'a, W, OPT, C> +impl SequenceEncoder for StorageEncoder where - C: ?Sized + Context, + C: Context, W: Writer, { type Cx = C; type Ok = (); type EncodeNext<'this> - = StorageEncoder<'a, W::Mut<'this>, OPT, C> + = StorageEncoder, OPT, C> where Self: 'this; #[inline] - fn cx_mut(&mut self, f: F) -> O - where - F: FnOnce(&Self::Cx, &mut Self) -> O, - { - f(self.cx, self) + fn cx(&self) -> Self::Cx { + self.cx } #[inline] @@ -398,15 +392,15 @@ where } } -impl<'a, W, const OPT: Options, C> MapEncoder for StorageEncoder<'a, W, OPT, C> +impl MapEncoder for StorageEncoder where - C: ?Sized + Context, + C: Context, W: Writer, { type Cx = C; type Ok = (); type EncodeEntry<'this> - = StorageEncoder<'a, W::Mut<'this>, OPT, C> + = StorageEncoder, OPT, C> where Self: 'this; @@ -421,19 +415,19 @@ where } } -impl<'a, W, const OPT: Options, C> EntryEncoder for StorageEncoder<'a, W, OPT, C> +impl EntryEncoder for StorageEncoder where - C: ?Sized + Context, + C: Context, W: Writer, { type Cx = C; type Ok = (); type EncodeKey<'this> - = StorageEncoder<'a, W::Mut<'this>, OPT, C> + = StorageEncoder, OPT, C> where Self: 'this; type EncodeValue<'this> - = StorageEncoder<'a, W::Mut<'this>, OPT, C> + = StorageEncoder, OPT, C> where Self: 'this; @@ -453,19 +447,19 @@ where } } -impl<'a, W, const OPT: Options, C> EntriesEncoder for StorageEncoder<'a, W, OPT, C> +impl EntriesEncoder for StorageEncoder where - C: ?Sized + Context, + C: Context, W: Writer, { type Cx = C; type Ok = (); type EncodeEntryKey<'this> - = StorageEncoder<'a, W::Mut<'this>, OPT, C> + = StorageEncoder, OPT, C> where Self: 'this; type EncodeEntryValue<'this> - = StorageEncoder<'a, W::Mut<'this>, OPT, C> + = StorageEncoder, OPT, C> where Self: 'this; @@ -485,19 +479,19 @@ where } } -impl<'a, W, const OPT: Options, C> VariantEncoder for StorageEncoder<'a, W, OPT, C> +impl VariantEncoder for StorageEncoder where - C: ?Sized + Context, + C: Context, W: Writer, { type Cx = C; type Ok = (); type EncodeTag<'this> - = StorageEncoder<'a, W::Mut<'this>, OPT, C> + = StorageEncoder, OPT, C> where Self: 'this; type EncodeData<'this> - = StorageEncoder<'a, W::Mut<'this>, OPT, C> + = StorageEncoder, OPT, C> where Self: 'this; @@ -526,12 +520,12 @@ where #[inline] unsafe fn encode_packed_len_slice( mut writer: W, - cx: &C, + cx: C, slice: impl AsRef<[T]>, ) -> Result<(), C::Error> where W: Writer, - C: ?Sized + Context, + C: Context, T: Encode, { let slice = slice.as_ref(); @@ -560,13 +554,13 @@ where #[inline] unsafe fn encode_packed_len_slices( mut writer: W, - cx: &C, + cx: C, len: usize, slices: I, ) -> Result<(), C::Error> where W: Writer, - C: ?Sized + Context, + C: Context, I: IntoIterator>, T: Encode, { @@ -599,12 +593,12 @@ where #[inline] unsafe fn encode_packed_slice( mut writer: W, - cx: &C, + cx: C, slice: impl AsRef<[T]>, ) -> Result<(), C::Error> where W: Writer, - C: ?Sized + Context, + C: Context, T: Encode, { if size_of::() > 0 { @@ -631,12 +625,12 @@ where #[inline] unsafe fn encode_packed_slices( mut writer: W, - cx: &C, + cx: C, slices: I, ) -> Result<(), C::Error> where W: Writer, - C: ?Sized + Context, + C: Context, I: IntoIterator>, T: Encode, { diff --git a/crates/musli/src/value/de.rs b/crates/musli/src/value/de.rs index 3b57de307..b55a69214 100644 --- a/crates/musli/src/value/de.rs +++ b/crates/musli/src/value/de.rs @@ -19,16 +19,16 @@ use super::value::{Number, Value}; use super::AsValueDecoder; /// Encoder for a single value. -pub struct ValueDecoder<'a, 'de, const OPT: Options, C: ?Sized> { - cx: &'a C, +pub struct ValueDecoder<'de, const OPT: Options, C> { + cx: C, value: &'de Value, #[cfg(feature = "alloc")] map_key: bool, } -impl<'a, 'de, const OPT: Options, C: ?Sized> ValueDecoder<'a, 'de, OPT, C> { +impl<'de, const OPT: Options, C> ValueDecoder<'de, OPT, C> { #[inline] - pub(crate) const fn new(cx: &'a C, value: &'de Value) -> Self { + pub(crate) const fn new(cx: C, value: &'de Value) -> Self { Self { cx, value, @@ -38,7 +38,7 @@ impl<'a, 'de, const OPT: Options, C: ?Sized> ValueDecoder<'a, 'de, OPT, C> { } #[inline] - pub(crate) const fn with_map_key(cx: &'a C, value: &'de Value) -> Self { + pub(crate) const fn with_map_key(cx: C, value: &'de Value) -> Self { Self { cx, value, @@ -80,34 +80,32 @@ macro_rules! ensure { } #[crate::decoder(crate)] -impl<'a, 'de, C: ?Sized + Context, const OPT: Options> Decoder<'de> - for ValueDecoder<'a, 'de, OPT, C> +impl<'de, C, const OPT: Options> Decoder<'de> for ValueDecoder<'de, OPT, C> +where + C: Context, { type Cx = C; type Error = C::Error; type Mode = C::Mode; - type WithContext<'this, U> - = ValueDecoder<'this, 'de, OPT, U> + type WithContext + = ValueDecoder<'de, OPT, U> where - U: 'this + Context; - type DecodeBuffer = AsValueDecoder<'a, OPT, C>; + U: Context; + type DecodeBuffer = AsValueDecoder; type DecodeSome = Self; - type DecodePack = StorageDecoder<'a, SliceReader<'de>, OPT, C>; - type DecodeSequence = IterValueDecoder<'a, 'de, OPT, C>; - type DecodeMap = IterValuePairsDecoder<'a, 'de, OPT, C>; - type DecodeMapEntries = IterValuePairsDecoder<'a, 'de, OPT, C>; - type DecodeVariant = IterValueVariantDecoder<'a, 'de, OPT, C>; + type DecodePack = StorageDecoder, OPT, C>; + type DecodeSequence = IterValueDecoder<'de, OPT, C>; + type DecodeMap = IterValuePairsDecoder<'de, OPT, C>; + type DecodeMapEntries = IterValuePairsDecoder<'de, OPT, C>; + type DecodeVariant = IterValueVariantDecoder<'de, OPT, C>; #[inline] - fn cx(self, f: F) -> O - where - F: FnOnce(&Self::Cx, Self) -> O, - { - f(self.cx, self) + fn cx(&self) -> Self::Cx { + self.cx } #[inline] - fn with_context(self, cx: &U) -> Result, C::Error> + fn with_context(self, cx: U) -> Result, C::Error> where U: Context, { @@ -382,10 +380,13 @@ impl<'a, 'de, C: ?Sized + Context, const OPT: Options> Decoder<'de> } } -impl<'a, C: ?Sized + Context, const OPT: Options> AsDecoder for ValueDecoder<'a, '_, OPT, C> { +impl AsDecoder for ValueDecoder<'_, OPT, C> +where + C: Context, +{ type Cx = C; type Decoder<'this> - = ValueDecoder<'a, 'this, OPT, C> + = ValueDecoder<'this, OPT, C> where Self: 'this; @@ -396,15 +397,15 @@ impl<'a, C: ?Sized + Context, const OPT: Options> AsDecoder for ValueDecoder<'a, } /// A decoder over a simple value iterator. -pub struct IterValueDecoder<'a, 'de, const OPT: Options, C: ?Sized> { - cx: &'a C, +pub struct IterValueDecoder<'de, const OPT: Options, C> { + cx: C, iter: slice::Iter<'de, Value>, } #[cfg(feature = "alloc")] -impl<'a, 'de, const OPT: Options, C: ?Sized> IterValueDecoder<'a, 'de, OPT, C> { +impl<'de, const OPT: Options, C> IterValueDecoder<'de, OPT, C> { #[inline] - fn new(cx: &'a C, values: &'de [Value]) -> Self { + fn new(cx: C, values: &'de [Value]) -> Self { Self { cx, iter: values.iter(), @@ -412,12 +413,13 @@ impl<'a, 'de, const OPT: Options, C: ?Sized> IterValueDecoder<'a, 'de, OPT, C> { } } -impl<'a, 'de, C: ?Sized + Context, const OPT: Options> SequenceDecoder<'de> - for IterValueDecoder<'a, 'de, OPT, C> +impl<'de, C, const OPT: Options> SequenceDecoder<'de> for IterValueDecoder<'de, OPT, C> +where + C: Context, { type Cx = C; type DecodeNext<'this> - = ValueDecoder<'a, 'de, OPT, C> + = ValueDecoder<'de, OPT, C> where Self: 'this; @@ -444,14 +446,14 @@ impl<'a, 'de, C: ?Sized + Context, const OPT: Options> SequenceDecoder<'de> } /// A decoder over a simple value pair iterator. -pub struct IterValuePairsDecoder<'a, 'de, const OPT: Options, C: ?Sized> { - cx: &'a C, +pub struct IterValuePairsDecoder<'de, const OPT: Options, C> { + cx: C, iter: slice::Iter<'de, (Value, Value)>, } -impl<'a, 'de, const OPT: Options, C: ?Sized> IterValuePairsDecoder<'a, 'de, OPT, C> { +impl<'de, const OPT: Options, C> IterValuePairsDecoder<'de, OPT, C> { #[inline] - fn new(cx: &'a C, values: &'de [(Value, Value)]) -> Self { + fn new(cx: C, values: &'de [(Value, Value)]) -> Self { Self { cx, iter: values.iter(), @@ -459,16 +461,17 @@ impl<'a, 'de, const OPT: Options, C: ?Sized> IterValuePairsDecoder<'a, 'de, OPT, } } -impl<'a, 'de, C: ?Sized + Context, const OPT: Options> MapDecoder<'de> - for IterValuePairsDecoder<'a, 'de, OPT, C> +impl<'de, C, const OPT: Options> MapDecoder<'de> for IterValuePairsDecoder<'de, OPT, C> +where + C: Context, { type Cx = C; type DecodeEntry<'this> - = IterValuePairDecoder<'a, 'de, OPT, C> + = IterValuePairDecoder<'de, OPT, C> where Self: 'this; type DecodeRemainingEntries<'this> - = IterValuePairsDecoder<'a, 'de, OPT, C> + = IterValuePairsDecoder<'de, OPT, C> where Self: 'this; @@ -494,16 +497,17 @@ impl<'a, 'de, C: ?Sized + Context, const OPT: Options> MapDecoder<'de> } } -impl<'a, 'de, C: ?Sized + Context, const OPT: Options> EntriesDecoder<'de> - for IterValuePairsDecoder<'a, 'de, OPT, C> +impl<'de, C, const OPT: Options> EntriesDecoder<'de> for IterValuePairsDecoder<'de, OPT, C> +where + C: Context, { type Cx = C; type DecodeEntryKey<'this> - = ValueDecoder<'a, 'de, OPT, C> + = ValueDecoder<'de, OPT, C> where Self: 'this; type DecodeEntryValue<'this> - = ValueDecoder<'a, 'de, OPT, C> + = ValueDecoder<'de, OPT, C> where Self: 'this; @@ -531,15 +535,16 @@ impl<'a, 'de, C: ?Sized + Context, const OPT: Options> EntriesDecoder<'de> } } -impl<'a, 'de, C: ?Sized + Context, const OPT: Options> EntryDecoder<'de> - for IterValuePairDecoder<'a, 'de, OPT, C> +impl<'de, C, const OPT: Options> EntryDecoder<'de> for IterValuePairDecoder<'de, OPT, C> +where + C: Context, { type Cx = C; type DecodeKey<'this> - = ValueDecoder<'a, 'de, OPT, C> + = ValueDecoder<'de, OPT, C> where Self: 'this; - type DecodeValue = ValueDecoder<'a, 'de, OPT, C>; + type DecodeValue = ValueDecoder<'de, OPT, C>; #[inline] fn decode_key(&mut self) -> Result, C::Error> { @@ -553,42 +558,43 @@ impl<'a, 'de, C: ?Sized + Context, const OPT: Options> EntryDecoder<'de> } /// A decoder over a simple value pair iterator. -pub struct IterValuePairDecoder<'a, 'de, const OPT: Options, C: ?Sized> { - cx: &'a C, +pub struct IterValuePairDecoder<'de, const OPT: Options, C> { + cx: C, pair: &'de (Value, Value), } -impl<'a, 'de, const OPT: Options, C: ?Sized> IterValuePairDecoder<'a, 'de, OPT, C> { +impl<'de, const OPT: Options, C> IterValuePairDecoder<'de, OPT, C> { #[inline] - const fn new(cx: &'a C, pair: &'de (Value, Value)) -> Self { + const fn new(cx: C, pair: &'de (Value, Value)) -> Self { Self { cx, pair } } } /// A decoder over a simple value pair as a variant. -pub struct IterValueVariantDecoder<'a, 'de, const OPT: Options, C: ?Sized> { - cx: &'a C, +pub struct IterValueVariantDecoder<'de, const OPT: Options, C> { + cx: C, pair: &'de (Value, Value), } #[cfg(feature = "alloc")] -impl<'a, 'de, const OPT: Options, C: ?Sized> IterValueVariantDecoder<'a, 'de, OPT, C> { +impl<'de, const OPT: Options, C> IterValueVariantDecoder<'de, OPT, C> { #[inline] - const fn new(cx: &'a C, pair: &'de (Value, Value)) -> Self { + const fn new(cx: C, pair: &'de (Value, Value)) -> Self { Self { cx, pair } } } -impl<'a, 'de, C: ?Sized + Context, const OPT: Options> VariantDecoder<'de> - for IterValueVariantDecoder<'a, 'de, OPT, C> +impl<'de, C, const OPT: Options> VariantDecoder<'de> for IterValueVariantDecoder<'de, OPT, C> +where + C: Context, { type Cx = C; type DecodeTag<'this> - = ValueDecoder<'a, 'de, OPT, C> + = ValueDecoder<'de, OPT, C> where Self: 'this; type DecodeValue<'this> - = ValueDecoder<'a, 'de, OPT, C> + = ValueDecoder<'de, OPT, C> where Self: 'this; diff --git a/crates/musli/src/value/en.rs b/crates/musli/src/value/en.rs index 903817427..5b963fd3d 100644 --- a/crates/musli/src/value/en.rs +++ b/crates/musli/src/value/en.rs @@ -55,59 +55,56 @@ where } /// Encoder for a single value. -pub struct ValueEncoder<'a, const OPT: Options, O, C: ?Sized> { - cx: &'a C, +pub struct ValueEncoder { + cx: C, output: O, } -impl<'a, const OPT: Options, O, C: ?Sized> ValueEncoder<'a, OPT, O, C> { +impl ValueEncoder { #[inline] - pub(crate) fn new(cx: &'a C, output: O) -> Self { + pub(crate) fn new(cx: C, output: O) -> Self { Self { cx, output } } } #[crate::encoder(crate)] -impl<'a, const OPT: Options, O, C> Encoder for ValueEncoder<'a, OPT, O, C> +impl Encoder for ValueEncoder where O: ValueOutput, - C: ?Sized + Context, + C: Context, { type Cx = C; type Error = C::Error; type Ok = (); type Mode = C::Mode; - type WithContext<'this, U> - = ValueEncoder<'this, OPT, O, U> + type WithContext + = ValueEncoder where - U: 'this + Context; + U: Context; #[cfg(feature = "alloc")] - type EncodeSome = ValueEncoder<'a, OPT, SomeValueWriter, C>; + type EncodeSome = ValueEncoder, C>; #[cfg(feature = "alloc")] - type EncodePack = PackValueEncoder<'a, OPT, O, C>; + type EncodePack = PackValueEncoder; #[cfg(feature = "alloc")] - type EncodeSequence = SequenceValueEncoder<'a, OPT, O, C>; + type EncodeSequence = SequenceValueEncoder; #[cfg(feature = "alloc")] - type EncodeMap = MapValueEncoder<'a, OPT, O, C>; + type EncodeMap = MapValueEncoder; #[cfg(feature = "alloc")] - type EncodeMapEntries = MapValueEncoder<'a, OPT, O, C>; + type EncodeMapEntries = MapValueEncoder; #[cfg(feature = "alloc")] - type EncodeVariant = VariantValueEncoder<'a, OPT, O, C>; + type EncodeVariant = VariantValueEncoder; #[cfg(feature = "alloc")] - type EncodeSequenceVariant = VariantSequenceEncoder<'a, OPT, O, C>; + type EncodeSequenceVariant = VariantSequenceEncoder; #[cfg(feature = "alloc")] - type EncodeMapVariant = VariantStructEncoder<'a, OPT, O, C>; + type EncodeMapVariant = VariantStructEncoder; #[inline] - fn cx(self, f: F) -> R - where - F: FnOnce(&Self::Cx, Self) -> R, - { - f(self.cx, self) + fn cx(&self) -> Self::Cx { + self.cx } #[inline] - fn with_context(self, cx: &U) -> Result, C::Error> + fn with_context(self, cx: U) -> Result, C::Error> where U: Context, { @@ -380,16 +377,16 @@ where /// A sequence encoder. #[cfg(feature = "alloc")] -pub struct SequenceValueEncoder<'a, const OPT: Options, O, C: ?Sized> { - cx: &'a C, +pub struct SequenceValueEncoder { + cx: C, output: O, values: Vec, } #[cfg(feature = "alloc")] -impl<'a, const OPT: Options, O, C: ?Sized> SequenceValueEncoder<'a, OPT, O, C> { +impl SequenceValueEncoder { #[inline] - fn new(cx: &'a C, output: O) -> Self { + fn new(cx: C, output: O) -> Self { Self { cx, output, @@ -399,25 +396,22 @@ impl<'a, const OPT: Options, O, C: ?Sized> SequenceValueEncoder<'a, OPT, O, C> { } #[cfg(feature = "alloc")] -impl<'a, const OPT: Options, O, C> SequenceEncoder for SequenceValueEncoder<'a, OPT, O, C> +impl SequenceEncoder for SequenceValueEncoder where O: ValueOutput, - C: ?Sized + Context, + C: Context, { type Cx = C; type Ok = (); type EncodeNext<'this> - = ValueEncoder<'a, OPT, &'this mut Vec, C> + = ValueEncoder, C> where Self: 'this; #[inline] - fn cx_mut(&mut self, f: F) -> U - where - F: FnOnce(&Self::Cx, &mut Self) -> U, - { - f(self.cx, self) + fn cx(&self) -> Self::Cx { + self.cx } #[inline] @@ -434,22 +428,22 @@ where /// A pack encoder. #[cfg(feature = "alloc")] -pub struct PackValueEncoder<'a, const OPT: Options, O, C> +pub struct PackValueEncoder where - C: ?Sized + Context, + C: Context, { - cx: &'a C, + cx: C, output: O, writer: BufWriter, } #[cfg(feature = "alloc")] -impl<'a, const OPT: Options, O, C> PackValueEncoder<'a, OPT, O, C> +impl PackValueEncoder where - C: ?Sized + Context, + C: Context, { #[inline] - fn new(cx: &'a C, output: O) -> Result { + fn new(cx: C, output: O) -> Result { Ok(Self { cx, output, @@ -459,24 +453,21 @@ where } #[cfg(feature = "alloc")] -impl<'a, const OPT: Options, O, C> SequenceEncoder for PackValueEncoder<'a, OPT, O, C> +impl SequenceEncoder for PackValueEncoder where O: ValueOutput, - C: ?Sized + Context, + C: Context, { type Cx = C; type Ok = (); type EncodeNext<'this> - = StorageEncoder<'a, &'this mut BufWriter, OPT, C> + = StorageEncoder<&'this mut BufWriter, OPT, C> where Self: 'this; #[inline] - fn cx_mut(&mut self, f: F) -> U - where - F: FnOnce(&Self::Cx, &mut Self) -> U, - { - f(self.cx, self) + fn cx(&self) -> Self::Cx { + self.cx } #[inline] @@ -494,16 +485,16 @@ where /// A pairs encoder. #[cfg(feature = "alloc")] -pub struct MapValueEncoder<'a, const OPT: Options, O, C: ?Sized> { - cx: &'a C, +pub struct MapValueEncoder { + cx: C, output: O, values: Vec<(Value, Value)>, } #[cfg(feature = "alloc")] -impl<'a, const OPT: Options, O, C: ?Sized> MapValueEncoder<'a, OPT, O, C> { +impl MapValueEncoder { #[inline] - fn new(cx: &'a C, output: O) -> Self { + fn new(cx: C, output: O) -> Self { Self { cx, output, @@ -513,10 +504,10 @@ impl<'a, const OPT: Options, O, C: ?Sized> MapValueEncoder<'a, OPT, O, C> { } #[cfg(feature = "alloc")] -impl MapEncoder for MapValueEncoder<'_, OPT, O, C> +impl MapEncoder for MapValueEncoder where O: ValueOutput, - C: ?Sized + Context, + C: Context, { type Cx = C; type Ok = (); @@ -538,19 +529,19 @@ where } #[cfg(feature = "alloc")] -impl<'a, const OPT: Options, O, C> EntriesEncoder for MapValueEncoder<'a, OPT, O, C> +impl EntriesEncoder for MapValueEncoder where O: ValueOutput, - C: ?Sized + Context, + C: Context, { type Cx = C; type Ok = (); type EncodeEntryKey<'this> - = ValueEncoder<'a, OPT, &'this mut Value, C> + = ValueEncoder where Self: 'this; type EncodeEntryValue<'this> - = ValueEncoder<'a, OPT, &'this mut Value, C> + = ValueEncoder where Self: 'this; @@ -583,16 +574,16 @@ where /// A pairs encoder. #[cfg(feature = "alloc")] -pub struct PairValueEncoder<'a, const OPT: Options, C: ?Sized> { - cx: &'a C, +pub struct PairValueEncoder<'a, const OPT: Options, C> { + cx: C, output: &'a mut Vec<(Value, Value)>, pair: (Value, Value), } #[cfg(feature = "alloc")] -impl<'a, const OPT: Options, C: ?Sized> PairValueEncoder<'a, OPT, C> { +impl<'a, const OPT: Options, C> PairValueEncoder<'a, OPT, C> { #[inline] - fn new(cx: &'a C, output: &'a mut Vec<(Value, Value)>) -> Self { + fn new(cx: C, output: &'a mut Vec<(Value, Value)>) -> Self { Self { cx, output, @@ -602,18 +593,18 @@ impl<'a, const OPT: Options, C: ?Sized> PairValueEncoder<'a, OPT, C> { } #[cfg(feature = "alloc")] -impl<'a, const OPT: Options, C> EntryEncoder for PairValueEncoder<'a, OPT, C> +impl EntryEncoder for PairValueEncoder<'_, OPT, C> where - C: ?Sized + Context, + C: Context, { type Cx = C; type Ok = (); type EncodeKey<'this> - = ValueEncoder<'a, OPT, &'this mut Value, C> + = ValueEncoder where Self: 'this; type EncodeValue<'this> - = ValueEncoder<'a, OPT, &'this mut Value, C> + = ValueEncoder where Self: 'this; @@ -636,16 +627,16 @@ where /// A pairs encoder. #[cfg(feature = "alloc")] -pub struct VariantValueEncoder<'a, const OPT: Options, O, C: ?Sized> { - cx: &'a C, +pub struct VariantValueEncoder { + cx: C, output: O, pair: (Value, Value), } #[cfg(feature = "alloc")] -impl<'a, const OPT: Options, O, C: ?Sized> VariantValueEncoder<'a, OPT, O, C> { +impl VariantValueEncoder { #[inline] - fn new(cx: &'a C, output: O) -> Self { + fn new(cx: C, output: O) -> Self { Self { cx, output, @@ -655,19 +646,19 @@ impl<'a, const OPT: Options, O, C: ?Sized> VariantValueEncoder<'a, OPT, O, C> { } #[cfg(feature = "alloc")] -impl<'a, const OPT: Options, O, C> VariantEncoder for VariantValueEncoder<'a, OPT, O, C> +impl VariantEncoder for VariantValueEncoder where O: ValueOutput, - C: ?Sized + Context, + C: Context, { type Cx = C; type Ok = (); type EncodeTag<'this> - = ValueEncoder<'a, OPT, &'this mut Value, C> + = ValueEncoder where Self: 'this; type EncodeData<'this> - = ValueEncoder<'a, OPT, &'this mut Value, C> + = ValueEncoder where Self: 'this; @@ -690,17 +681,17 @@ where /// A variant sequence encoder. #[cfg(feature = "alloc")] -pub struct VariantSequenceEncoder<'a, const OPT: Options, O, C: ?Sized> { - cx: &'a C, +pub struct VariantSequenceEncoder { + cx: C, output: O, variant: Value, values: Vec, } #[cfg(feature = "alloc")] -impl<'a, const OPT: Options, O, C: ?Sized> VariantSequenceEncoder<'a, OPT, O, C> { +impl VariantSequenceEncoder { #[inline] - fn new(cx: &'a C, output: O, variant: Value, len: usize) -> Self { + fn new(cx: C, output: O, variant: Value, len: usize) -> Self { Self { cx, output, @@ -711,25 +702,22 @@ impl<'a, const OPT: Options, O, C: ?Sized> VariantSequenceEncoder<'a, OPT, O, C> } #[cfg(feature = "alloc")] -impl<'a, const OPT: Options, O, C> SequenceEncoder for VariantSequenceEncoder<'a, OPT, O, C> +impl SequenceEncoder for VariantSequenceEncoder where O: ValueOutput, - C: ?Sized + Context, + C: Context, { type Cx = C; type Ok = (); type EncodeNext<'this> - = ValueEncoder<'a, OPT, &'this mut Vec, C> + = ValueEncoder, C> where Self: 'this; #[inline] - fn cx_mut(&mut self, f: F) -> U - where - F: FnOnce(&Self::Cx, &mut Self) -> U, - { - f(self.cx, self) + fn cx(&self) -> Self::Cx { + self.cx } #[inline] @@ -749,17 +737,17 @@ where /// A variant struct encoder. #[cfg(feature = "alloc")] -pub struct VariantStructEncoder<'a, const OPT: Options, O, C: ?Sized> { - cx: &'a C, +pub struct VariantStructEncoder { + cx: C, output: O, variant: Value, fields: Vec<(Value, Value)>, } #[cfg(feature = "alloc")] -impl<'a, const OPT: Options, O, C: ?Sized> VariantStructEncoder<'a, OPT, O, C> { +impl VariantStructEncoder { #[inline] - fn new(cx: &'a C, output: O, variant: Value, len: usize) -> Self { + fn new(cx: C, output: O, variant: Value, len: usize) -> Self { Self { cx, output, @@ -770,10 +758,10 @@ impl<'a, const OPT: Options, O, C: ?Sized> VariantStructEncoder<'a, OPT, O, C> { } #[cfg(feature = "alloc")] -impl MapEncoder for VariantStructEncoder<'_, OPT, O, C> +impl MapEncoder for VariantStructEncoder where O: ValueOutput, - C: ?Sized + Context, + C: Context, { type Cx = C; type Ok = (); diff --git a/crates/musli/src/value/mod.rs b/crates/musli/src/value/mod.rs index 0fb8c6761..7cd528e22 100644 --- a/crates/musli/src/value/mod.rs +++ b/crates/musli/src/value/mod.rs @@ -60,9 +60,9 @@ where /// Decode a [Value] into a type which implements [Decode] using a custom /// context. -pub fn decode_with<'de, C, T>(cx: &C, value: &'de Value) -> Result +pub fn decode_with<'de, C, T>(cx: C, value: &'de Value) -> Result where - C: ?Sized + crate::Context, + C: crate::Context, T: Decode<'de, C::Mode>, { use crate::de::Decoder; diff --git a/crates/musli/src/value/value.rs b/crates/musli/src/value/value.rs index 5680e7073..624ab4029 100644 --- a/crates/musli/src/value/value.rs +++ b/crates/musli/src/value/value.rs @@ -60,19 +60,13 @@ impl Value { /// Construct a [AsValueDecoder] implementation out of this value which /// emits the specified error `E`. #[inline] - pub fn into_value_decoder( - self, - cx: &C, - ) -> AsValueDecoder<'_, OPT, C> { + pub fn into_value_decoder(self, cx: C) -> AsValueDecoder { AsValueDecoder::new(cx, self) } /// Get a decoder associated with a value. #[inline] - pub(crate) fn decoder<'a, 'de, const OPT: Options, C: ?Sized>( - &'de self, - cx: &'a C, - ) -> ValueDecoder<'a, 'de, OPT, C> { + pub(crate) fn decoder(&self, cx: C) -> ValueDecoder<'_, OPT, C> { ValueDecoder::new(cx, self) } @@ -215,7 +209,10 @@ impl Number { struct AnyVisitor; #[crate::visitor(crate)] -impl<'de, C: ?Sized + Context> Visitor<'de, C> for AnyVisitor { +impl<'de, C> Visitor<'de, C> for AnyVisitor +where + C: Context, +{ type Ok = Value; #[cfg(feature = "alloc")] type String = StringVisitor; @@ -228,93 +225,93 @@ impl<'de, C: ?Sized + Context> Visitor<'de, C> for AnyVisitor { } #[inline] - fn visit_empty(self, _: &C) -> Result { + fn visit_empty(self, _: C) -> Result { Ok(Value::Unit) } #[inline] - fn visit_bool(self, _: &C, value: bool) -> Result { + fn visit_bool(self, _: C, value: bool) -> Result { Ok(Value::Bool(value)) } #[inline] - fn visit_char(self, _: &C, value: char) -> Result { + fn visit_char(self, _: C, value: char) -> Result { Ok(Value::Char(value)) } #[inline] - fn visit_u8(self, _: &C, value: u8) -> Result { + fn visit_u8(self, _: C, value: u8) -> Result { Ok(Value::Number(Number::U8(value))) } #[inline] - fn visit_u16(self, _: &C, value: u16) -> Result { + fn visit_u16(self, _: C, value: u16) -> Result { Ok(Value::Number(Number::U16(value))) } #[inline] - fn visit_u32(self, _: &C, value: u32) -> Result { + fn visit_u32(self, _: C, value: u32) -> Result { Ok(Value::Number(Number::U32(value))) } #[inline] - fn visit_u64(self, _: &C, value: u64) -> Result { + fn visit_u64(self, _: C, value: u64) -> Result { Ok(Value::Number(Number::U64(value))) } #[inline] - fn visit_u128(self, _: &C, value: u128) -> Result { + fn visit_u128(self, _: C, value: u128) -> Result { Ok(Value::Number(Number::U128(value))) } #[inline] - fn visit_i8(self, _: &C, value: i8) -> Result { + fn visit_i8(self, _: C, value: i8) -> Result { Ok(Value::Number(Number::I8(value))) } #[inline] - fn visit_i16(self, _: &C, value: i16) -> Result { + fn visit_i16(self, _: C, value: i16) -> Result { Ok(Value::Number(Number::I16(value))) } #[inline] - fn visit_i32(self, _: &C, value: i32) -> Result { + fn visit_i32(self, _: C, value: i32) -> Result { Ok(Value::Number(Number::I32(value))) } #[inline] - fn visit_i64(self, _: &C, value: i64) -> Result { + fn visit_i64(self, _: C, value: i64) -> Result { Ok(Value::Number(Number::I64(value))) } #[inline] - fn visit_i128(self, _: &C, value: i128) -> Result { + fn visit_i128(self, _: C, value: i128) -> Result { Ok(Value::Number(Number::I128(value))) } #[inline] - fn visit_usize(self, _: &C, value: usize) -> Result { + fn visit_usize(self, _: C, value: usize) -> Result { Ok(Value::Number(Number::Usize(value))) } #[inline] - fn visit_isize(self, _: &C, value: isize) -> Result { + fn visit_isize(self, _: C, value: isize) -> Result { Ok(Value::Number(Number::Isize(value))) } #[inline] - fn visit_f32(self, _: &C, value: f32) -> Result { + fn visit_f32(self, _: C, value: f32) -> Result { Ok(Value::Number(Number::F32(value))) } #[inline] - fn visit_f64(self, _: &C, value: f64) -> Result { + fn visit_f64(self, _: C, value: f64) -> Result { Ok(Value::Number(Number::F64(value))) } #[cfg(feature = "alloc")] #[inline] - fn visit_option(self, _: &C, decoder: Option) -> Result + fn visit_option(self, _: C, decoder: Option) -> Result where D: Decoder<'de, Cx = C, Error = C::Error>, { @@ -326,7 +323,7 @@ impl<'de, C: ?Sized + Context> Visitor<'de, C> for AnyVisitor { #[cfg(feature = "alloc")] #[inline] - fn visit_sequence(self, _: &C, seq: &mut D) -> Result + fn visit_sequence(self, _: C, seq: &mut D) -> Result where D: ?Sized + SequenceDecoder<'de, Cx = C>, { @@ -341,7 +338,7 @@ impl<'de, C: ?Sized + Context> Visitor<'de, C> for AnyVisitor { #[cfg(feature = "alloc")] #[inline] - fn visit_map(self, _: &C, map: &mut D) -> Result + fn visit_map(self, _: C, map: &mut D) -> Result where D: ?Sized + MapDecoder<'de, Cx = C>, { @@ -358,19 +355,19 @@ impl<'de, C: ?Sized + Context> Visitor<'de, C> for AnyVisitor { #[cfg(feature = "alloc")] #[inline] - fn visit_bytes(self, _: &C, _: SizeHint) -> Result { + fn visit_bytes(self, _: C, _: SizeHint) -> Result { Ok(BytesVisitor) } #[cfg(feature = "alloc")] #[inline] - fn visit_string(self, _: &C, _: SizeHint) -> Result { + fn visit_string(self, _: C, _: SizeHint) -> Result { Ok(StringVisitor) } #[cfg(feature = "alloc")] #[inline] - fn visit_variant(self, _: &C, variant: &mut D) -> Result + fn visit_variant(self, _: C, variant: &mut D) -> Result where D: VariantDecoder<'de, Cx = C>, { @@ -398,7 +395,7 @@ struct BytesVisitor; #[cfg(feature = "alloc")] impl UnsizedVisitor<'_, C, [u8]> for BytesVisitor where - C: ?Sized + Context, + C: Context, { type Ok = Value; @@ -409,12 +406,12 @@ where #[cfg(feature = "alloc")] #[inline] - fn visit_owned(self, _: &C, bytes: Vec) -> Result { + fn visit_owned(self, _: C, bytes: Vec) -> Result { Ok(Value::Bytes(bytes)) } #[inline] - fn visit_ref(self, _: &C, bytes: &[u8]) -> Result { + fn visit_ref(self, _: C, bytes: &[u8]) -> Result { Ok(Value::Bytes(bytes.to_vec())) } } @@ -425,7 +422,7 @@ struct StringVisitor; #[cfg(feature = "alloc")] impl UnsizedVisitor<'_, C, str> for StringVisitor where - C: ?Sized + Context, + C: Context, { type Ok = Value; @@ -435,12 +432,12 @@ where } #[inline] - fn visit_owned(self, _: &C, string: String) -> Result { + fn visit_owned(self, _: C, string: String) -> Result { Ok(Value::String(string)) } #[inline] - fn visit_ref(self, _: &C, string: &str) -> Result { + fn visit_ref(self, _: C, string: &str) -> Result { Ok(Value::String(string.to_owned())) } } @@ -512,23 +509,26 @@ impl Encode for Value { } /// Value's [AsDecoder] implementation. -pub struct AsValueDecoder<'a, const OPT: Options, C: ?Sized> { - cx: &'a C, +pub struct AsValueDecoder { + cx: C, value: Value, } -impl<'a, const OPT: Options, C: ?Sized> AsValueDecoder<'a, OPT, C> { +impl AsValueDecoder { /// Construct a new buffered value decoder. #[inline] - pub fn new(cx: &'a C, value: Value) -> Self { + pub fn new(cx: C, value: Value) -> Self { Self { cx, value } } } -impl<'a, const OPT: Options, C: ?Sized + Context> AsDecoder for AsValueDecoder<'a, OPT, C> { +impl AsDecoder for AsValueDecoder +where + C: Context, +{ type Cx = C; type Decoder<'this> - = ValueDecoder<'a, 'this, OPT, C> + = ValueDecoder<'this, OPT, C> where Self: 'this; diff --git a/crates/musli/src/wire/de.rs b/crates/musli/src/wire/de.rs index e074e5d9b..255937ce1 100644 --- a/crates/musli/src/wire/de.rs +++ b/crates/musli/src/wire/de.rs @@ -17,26 +17,26 @@ use crate::{Context, Options, Reader}; use super::tag::{Kind, Tag}; /// A very simple decoder. -pub struct WireDecoder<'a, R, const OPT: Options, C: ?Sized> { - cx: &'a C, +pub struct WireDecoder { + cx: C, reader: R, } -impl<'a, 'de, R, const OPT: Options, C> WireDecoder<'a, R, OPT, C> +impl<'de, R, const OPT: Options, C> WireDecoder where + C: Context, R: Reader<'de>, - C: ?Sized + Context, { /// Construct a new fixed width message encoder. #[inline] - pub(crate) fn new(cx: &'a C, reader: R) -> Self { + pub(crate) fn new(cx: C, reader: R) -> Self { Self { cx, reader } } } -impl<'de, R, const OPT: Options, C> WireDecoder<'_, Limit, OPT, C> +impl<'de, R, const OPT: Options, C> WireDecoder, OPT, C> where - C: ?Sized + Context, + C: Context, R: Reader<'de>, { #[inline] @@ -49,10 +49,10 @@ where } } -impl<'a, 'de, R, const OPT: Options, C> WireDecoder<'a, R, OPT, C> +impl<'de, R, const OPT: Options, C> WireDecoder where + C: Context, R: Reader<'de>, - C: ?Sized + Context, { /// Skip over any sequences of values. pub(crate) fn skip_any(mut self) -> Result<(), C::Error> { @@ -117,16 +117,14 @@ where // Standard function for decoding a pair sequence. #[inline] - fn shared_decode_pair_sequence( - mut self, - ) -> Result, C::Error> { + fn shared_decode_pair_sequence(mut self) -> Result, C::Error> { let len = self.decode_sequence_len()?; Ok(RemainingWireDecoder::new(self.cx, self.reader, len / 2)) } // Standard function for decoding a pair sequence. #[inline] - fn shared_decode_sequence(mut self) -> Result, C::Error> { + fn shared_decode_sequence(mut self) -> Result, C::Error> { let len = self.decode_sequence_len()?; Ok(RemainingWireDecoder::new(self.cx, self.reader, len)) } @@ -154,19 +152,19 @@ where /// This simplifies implementing decoders that do not have any special handling /// for length-prefixed types. #[doc(hidden)] -pub struct RemainingWireDecoder<'a, R, const OPT: Options, C: ?Sized> { - cx: &'a C, +pub struct RemainingWireDecoder { + cx: C, reader: R, remaining: usize, } -impl<'a, 'de, R, const OPT: Options, C> RemainingWireDecoder<'a, R, OPT, C> +impl<'de, R, const OPT: Options, C> RemainingWireDecoder where - C: ?Sized + Context, + C: Context, R: Reader<'de>, { #[inline] - fn new(cx: &'a C, reader: R, remaining: usize) -> Self { + fn new(cx: C, reader: R, remaining: usize) -> Self { Self { cx, reader, @@ -203,35 +201,32 @@ where } #[crate::decoder(crate)] -impl<'a, 'de, R, const OPT: Options, C> Decoder<'de> for WireDecoder<'a, R, OPT, C> +impl<'de, R, const OPT: Options, C> Decoder<'de> for WireDecoder where - C: ?Sized + Context, + C: Context, R: Reader<'de>, { type Cx = C; type Error = C::Error; type Mode = C::Mode; - type WithContext<'this, U> - = WireDecoder<'this, R, OPT, U> + type WithContext + = WireDecoder where - U: 'this + Context; - type DecodePack = WireDecoder<'a, Limit, OPT, C>; + U: Context; + type DecodePack = WireDecoder, OPT, C>; type DecodeSome = Self; - type DecodeSequence = RemainingWireDecoder<'a, R, OPT, C>; - type DecodeMap = RemainingWireDecoder<'a, R, OPT, C>; - type DecodeMapEntries = RemainingWireDecoder<'a, R, OPT, C>; + type DecodeSequence = RemainingWireDecoder; + type DecodeMap = RemainingWireDecoder; + type DecodeMapEntries = RemainingWireDecoder; type DecodeVariant = Self; #[inline] - fn cx(self, f: F) -> O - where - F: FnOnce(&Self::Cx, Self) -> O, - { - f(self.cx, self) + fn cx(&self) -> Self::Cx { + self.cx } #[inline] - fn with_context(self, cx: &U) -> Result, C::Error> + fn with_context(self, cx: U) -> Result, C::Error> where U: Context, { @@ -309,7 +304,7 @@ where impl<'de, C, V> UnsizedVisitor<'de, C, [u8]> for Visitor where - C: ?Sized + Context, + C: Context, V: UnsizedVisitor<'de, C, str>, { type Ok = V::Ok; @@ -321,19 +316,19 @@ where #[cfg(feature = "alloc")] #[inline] - fn visit_owned(self, cx: &C, bytes: Vec) -> Result { + fn visit_owned(self, cx: C, bytes: Vec) -> Result { let string = crate::str::from_utf8_owned(bytes).map_err(cx.map())?; self.0.visit_owned(cx, string) } #[inline] - fn visit_borrowed(self, cx: &C, bytes: &'de [u8]) -> Result { + fn visit_borrowed(self, cx: C, bytes: &'de [u8]) -> Result { let string = crate::str::from_utf8(bytes).map_err(cx.map())?; self.0.visit_borrowed(cx, string) } #[inline] - fn visit_ref(self, cx: &C, bytes: &[u8]) -> Result { + fn visit_ref(self, cx: C, bytes: &[u8]) -> Result { let string = crate::str::from_utf8(bytes).map_err(cx.map())?; self.0.visit_ref(cx, string) } @@ -522,14 +517,14 @@ where } } -impl<'a, 'de, R, const OPT: Options, C> SequenceDecoder<'de> for WireDecoder<'a, Limit, OPT, C> +impl<'de, R, const OPT: Options, C> SequenceDecoder<'de> for WireDecoder, OPT, C> where - C: ?Sized + Context, + C: Context, R: Reader<'de>, { type Cx = C; type DecodeNext<'this> - = StorageDecoder<'a, as Reader<'de>>::Mut<'this>, OPT, C> + = StorageDecoder< as Reader<'de>>::Mut<'this>, OPT, C> where Self: 'this; @@ -544,14 +539,14 @@ where } } -impl<'a, 'de, R, const OPT: Options, C> SequenceDecoder<'de> for RemainingWireDecoder<'a, R, OPT, C> +impl<'de, R, const OPT: Options, C> SequenceDecoder<'de> for RemainingWireDecoder where - C: ?Sized + Context, + C: Context, R: Reader<'de>, { type Cx = C; type DecodeNext<'this> - = WireDecoder<'a, R::Mut<'this>, OPT, C> + = WireDecoder, OPT, C> where Self: 'this; @@ -583,18 +578,18 @@ where } } -impl<'a, 'de, R, const OPT: Options, C> VariantDecoder<'de> for WireDecoder<'a, R, OPT, C> +impl<'de, R, const OPT: Options, C> VariantDecoder<'de> for WireDecoder where - C: ?Sized + Context, + C: Context, R: Reader<'de>, { type Cx = C; type DecodeTag<'this> - = WireDecoder<'a, R::Mut<'this>, OPT, C> + = WireDecoder, OPT, C> where Self: 'this; type DecodeValue<'this> - = WireDecoder<'a, R::Mut<'this>, OPT, C> + = WireDecoder, OPT, C> where Self: 'this; @@ -609,18 +604,18 @@ where } } -impl<'a, 'de, R, const OPT: Options, C> MapDecoder<'de> for RemainingWireDecoder<'a, R, OPT, C> +impl<'de, R, const OPT: Options, C> MapDecoder<'de> for RemainingWireDecoder where - C: ?Sized + Context, + C: Context, R: Reader<'de>, { type Cx = C; type DecodeEntry<'this> - = WireDecoder<'a, R::Mut<'this>, OPT, C> + = WireDecoder, OPT, C> where Self: 'this; type DecodeRemainingEntries<'this> - = RemainingWireDecoder<'a, R::Mut<'this>, OPT, C> + = RemainingWireDecoder, OPT, C> where Self: 'this; @@ -649,14 +644,14 @@ where } } -impl<'a, 'de, R, const OPT: Options, C> EntryDecoder<'de> for WireDecoder<'a, R, OPT, C> +impl<'de, R, const OPT: Options, C> EntryDecoder<'de> for WireDecoder where - C: ?Sized + Context, + C: Context, R: Reader<'de>, { type Cx = C; type DecodeKey<'this> - = WireDecoder<'a, R::Mut<'this>, OPT, C> + = WireDecoder, OPT, C> where Self: 'this; type DecodeValue = Self; @@ -672,18 +667,18 @@ where } } -impl<'a, 'de, R, const OPT: Options, C> EntriesDecoder<'de> for RemainingWireDecoder<'a, R, OPT, C> +impl<'de, R, const OPT: Options, C> EntriesDecoder<'de> for RemainingWireDecoder where - C: ?Sized + Context, + C: Context, R: Reader<'de>, { type Cx = C; type DecodeEntryKey<'this> - = WireDecoder<'a, R::Mut<'this>, OPT, C> + = WireDecoder, OPT, C> where Self: 'this; type DecodeEntryValue<'this> - = WireDecoder<'a, R::Mut<'this>, OPT, C> + = WireDecoder, OPT, C> where Self: 'this; diff --git a/crates/musli/src/wire/en.rs b/crates/musli/src/wire/en.rs index a61569a43..44cc8e056 100644 --- a/crates/musli/src/wire/en.rs +++ b/crates/musli/src/wire/en.rs @@ -11,19 +11,19 @@ use crate::{Context, Options, Writer}; use super::tag::{Kind, Tag}; /// A very simple encoder. -pub struct WireEncoder<'a, W, const OPT: Options, C: ?Sized> { - cx: &'a C, +pub struct WireEncoder { + cx: C, writer: W, } -impl<'a, W, const OPT: Options, C> WireEncoder<'a, W, OPT, C> +impl WireEncoder where + C: Context, W: Writer, - C: ?Sized + Context, { /// Construct a new fixed width message encoder. #[inline] - pub(crate) fn new(cx: &'a C, writer: W) -> Self { + pub(crate) fn new(cx: C, writer: W) -> Self { Self { cx, writer } } @@ -56,22 +56,22 @@ where } } -pub struct WireSequenceEncoder<'a, W, const OPT: Options, C> +pub struct WireSequenceEncoder where - C: ?Sized + Context, + C: Context, { - cx: &'a C, + cx: C, writer: W, buffer: BufWriter, } -impl<'a, W, const OPT: Options, C> WireSequenceEncoder<'a, W, OPT, C> +impl WireSequenceEncoder where - C: ?Sized + Context, + C: Context, { /// Construct a new fixed width message encoder. #[inline] - pub(crate) fn new(cx: &'a C, writer: W) -> Self { + pub(crate) fn new(cx: C, writer: W) -> Self { Self { cx, writer, @@ -81,20 +81,20 @@ where } #[crate::encoder(crate)] -impl<'a, W, const OPT: Options, C> Encoder for WireEncoder<'a, W, OPT, C> +impl Encoder for WireEncoder where - C: ?Sized + Context, + C: Context, W: Writer, { type Cx = C; type Error = C::Error; type Ok = (); type Mode = C::Mode; - type WithContext<'this, U> - = WireEncoder<'this, W, OPT, U> + type WithContext + = WireEncoder where - U: 'this + Context; - type EncodePack = WireSequenceEncoder<'a, W, OPT, C>; + U: Context; + type EncodePack = WireSequenceEncoder; type EncodeSome = Self; type EncodeSequence = Self; type EncodeMap = Self; @@ -104,15 +104,12 @@ where type EncodeMapVariant = Self; #[inline] - fn cx(self, f: F) -> O - where - F: FnOnce(&Self::Cx, Self) -> O, - { - f(self.cx, self) + fn cx(&self) -> Self::Cx { + self.cx } #[inline] - fn with_context(self, cx: &U) -> Result, C::Error> + fn with_context(self, cx: U) -> Result, C::Error> where U: Context, { @@ -340,24 +337,21 @@ where } } -impl<'a, W, const OPT: Options, C> SequenceEncoder for WireSequenceEncoder<'a, W, OPT, C> +impl SequenceEncoder for WireSequenceEncoder where - C: ?Sized + Context, + C: Context, W: Writer, { type Cx = C; type Ok = (); type EncodeNext<'this> - = StorageEncoder<'a, &'this mut BufWriter, OPT, C> + = StorageEncoder<&'this mut BufWriter, OPT, C> where Self: 'this; #[inline] - fn cx_mut(&mut self, f: F) -> O - where - F: FnOnce(&Self::Cx, &mut Self) -> O, - { - f(self.cx, self) + fn cx(&self) -> Self::Cx { + self.cx } #[inline] @@ -374,24 +368,21 @@ where } } -impl<'a, W, const OPT: Options, C> SequenceEncoder for WireEncoder<'a, W, OPT, C> +impl SequenceEncoder for WireEncoder where - C: ?Sized + Context, + C: Context, W: Writer, { type Cx = C; type Ok = (); type EncodeNext<'this> - = WireEncoder<'a, W::Mut<'this>, OPT, C> + = WireEncoder, OPT, C> where Self: 'this; #[inline] - fn cx_mut(&mut self, f: F) -> O - where - F: FnOnce(&Self::Cx, &mut Self) -> O, - { - f(self.cx, self) + fn cx(&self) -> Self::Cx { + self.cx } #[inline] @@ -405,15 +396,15 @@ where } } -impl<'a, W, const OPT: Options, C> MapEncoder for WireEncoder<'a, W, OPT, C> +impl MapEncoder for WireEncoder where - C: ?Sized + Context, + C: Context, W: Writer, { type Cx = C; type Ok = (); type EncodeEntry<'this> - = WireEncoder<'a, W::Mut<'this>, OPT, C> + = WireEncoder, OPT, C> where Self: 'this; @@ -428,19 +419,19 @@ where } } -impl<'a, W, const OPT: Options, C> EntriesEncoder for WireEncoder<'a, W, OPT, C> +impl EntriesEncoder for WireEncoder where - C: ?Sized + Context, + C: Context, W: Writer, { type Cx = C; type Ok = (); type EncodeEntryKey<'this> - = WireEncoder<'a, W::Mut<'this>, OPT, C> + = WireEncoder, OPT, C> where Self: 'this; type EncodeEntryValue<'this> - = WireEncoder<'a, W::Mut<'this>, OPT, C> + = WireEncoder, OPT, C> where Self: 'this; @@ -460,19 +451,19 @@ where } } -impl<'a, W, const OPT: Options, C> EntryEncoder for WireEncoder<'a, W, OPT, C> +impl EntryEncoder for WireEncoder where - C: ?Sized + Context, + C: Context, W: Writer, { type Cx = C; type Ok = (); type EncodeKey<'this> - = WireEncoder<'a, W::Mut<'this>, OPT, C> + = WireEncoder, OPT, C> where Self: 'this; type EncodeValue<'this> - = WireEncoder<'a, W::Mut<'this>, OPT, C> + = WireEncoder, OPT, C> where Self: 'this; @@ -492,19 +483,19 @@ where } } -impl<'a, W, const OPT: Options, C> VariantEncoder for WireEncoder<'a, W, OPT, C> +impl VariantEncoder for WireEncoder where - C: ?Sized + Context, + C: Context, W: Writer, { type Cx = C; type Ok = (); type EncodeTag<'this> - = WireEncoder<'a, W::Mut<'this>, OPT, C> + = WireEncoder, OPT, C> where Self: 'this; type EncodeData<'this> - = WireEncoder<'a, W::Mut<'this>, OPT, C> + = WireEncoder, OPT, C> where Self: 'this; @@ -526,13 +517,9 @@ where /// Encode a length prefix. #[inline] -fn encode_prefix( - cx: &C, - mut writer: W, - len: usize, -) -> Result<(), C::Error> +fn encode_prefix(cx: C, mut writer: W, len: usize) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, W: Writer, { let (tag, embedded) = Tag::with_len(Kind::Prefix, len); diff --git a/crates/musli/src/wire/int.rs b/crates/musli/src/wire/int.rs index a4b4553b0..ec0579b56 100644 --- a/crates/musli/src/wire/int.rs +++ b/crates/musli/src/wire/int.rs @@ -8,12 +8,12 @@ use super::tag::{Kind, Tag, DATA_MASK}; /// Governs how usize lengths are encoded into a [`Writer`]. #[inline] pub(crate) fn encode_length( - cx: &C, + cx: C, mut writer: W, value: usize, ) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, W: Writer, { match crate::options::length::() { @@ -48,11 +48,11 @@ where /// Governs how usize lengths are decoded from a [`Reader`]. #[inline] pub(crate) fn decode_length<'de, C, R, const OPT: Options>( - cx: &C, + cx: C, mut reader: R, ) -> Result where - C: ?Sized + Context, + C: Context, R: Reader<'de>, { match crate::options::length::() { @@ -101,12 +101,12 @@ where /// Governs how unsigned integers are encoded into a [`Writer`]. #[inline] pub(crate) fn encode_unsigned( - cx: &C, + cx: C, mut writer: W, value: T, ) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, W: Writer, T: UnsignedOps, { @@ -130,11 +130,11 @@ where /// Governs how unsigned integers are decoded from a [`Reader`]. #[inline] pub(crate) fn decode_unsigned<'de, C, R, T, const OPT: Options>( - cx: &C, + cx: C, mut reader: R, ) -> Result where - C: ?Sized + Context, + C: Context, R: Reader<'de>, T: UnsignedOps, { @@ -167,12 +167,12 @@ where /// Governs how signed integers are encoded into a [`Writer`]. #[inline] pub(crate) fn encode_signed( - cx: &C, + cx: C, writer: W, value: T, ) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, W: Writer, T: Signed, T::Unsigned: UnsignedOps, @@ -184,11 +184,11 @@ where /// Governs how signed integers are decoded from a [`Reader`]. #[inline] pub(crate) fn decode_signed<'de, C, R, T, const OPT: Options>( - cx: &C, + cx: C, reader: R, ) -> Result where - C: ?Sized + Context, + C: Context, R: Reader<'de>, T: Signed, T::Unsigned: UnsignedOps, diff --git a/crates/musli/src/wrap.rs b/crates/musli/src/wrap.rs index 564f2f5b8..593b5411f 100644 --- a/crates/musli/src/wrap.rs +++ b/crates/musli/src/wrap.rs @@ -54,9 +54,9 @@ where Self: 'this; #[inline] - fn finish(&mut self, _: &C) -> Result + fn finish(&mut self, _: C) -> Result where - C: ?Sized + Context, + C: Context, { Ok(()) } @@ -67,18 +67,18 @@ where } #[inline] - fn extend(&mut self, cx: &C, buffer: Vec) -> Result<(), C::Error> + fn extend(&mut self, cx: C, buffer: Vec) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { // SAFETY: the buffer never outlives this function call. self.write_bytes(cx, buffer.as_slice()) } #[inline] - fn write_bytes(&mut self, cx: &C, bytes: &[u8]) -> Result<(), C::Error> + fn write_bytes(&mut self, cx: C, bytes: &[u8]) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { self.inner.write_all(bytes).map_err(cx.map())?; cx.advance(bytes.len()); diff --git a/crates/musli/src/writer.rs b/crates/musli/src/writer.rs index 939bf64de..d8e5af526 100644 --- a/crates/musli/src/writer.rs +++ b/crates/musli/src/writer.rs @@ -54,28 +54,28 @@ pub trait Writer { Self: 'this; /// Finalize the writer and return the output. - fn finish(&mut self, cx: &C) -> Result + fn finish(&mut self, cx: C) -> Result where - C: ?Sized + Context; + C: Context; /// Reborrow the current type. fn borrow_mut(&mut self) -> Self::Mut<'_>; /// Write a buffer to the current writer. - fn extend(&mut self, cx: &C, buffer: Vec) -> Result<(), C::Error> + fn extend(&mut self, cx: C, buffer: Vec) -> Result<(), C::Error> where - C: ?Sized + Context; + C: Context; /// Write bytes to the current writer. - fn write_bytes(&mut self, cx: &C, bytes: &[u8]) -> Result<(), C::Error> + fn write_bytes(&mut self, cx: C, bytes: &[u8]) -> Result<(), C::Error> where - C: ?Sized + Context; + C: Context; /// Write a single byte. #[inline] - fn write_byte(&mut self, cx: &C, b: u8) -> Result<(), C::Error> + fn write_byte(&mut self, cx: C, b: u8) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { self.write_bytes(cx, &[b]) } @@ -105,9 +105,9 @@ where Self: 'this; #[inline] - fn finish(&mut self, cx: &C) -> Result + fn finish(&mut self, cx: C) -> Result where - C: ?Sized + Context, + C: Context, { (*self).finish(cx) } @@ -118,25 +118,25 @@ where } #[inline] - fn extend(&mut self, cx: &C, buffer: Vec) -> Result<(), C::Error> + fn extend(&mut self, cx: C, buffer: Vec) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { (*self).extend(cx, buffer) } #[inline] - fn write_bytes(&mut self, cx: &C, bytes: &[u8]) -> Result<(), C::Error> + fn write_bytes(&mut self, cx: C, bytes: &[u8]) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { (*self).write_bytes(cx, bytes) } #[inline] - fn write_byte(&mut self, cx: &C, b: u8) -> Result<(), C::Error> + fn write_byte(&mut self, cx: C, b: u8) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { (*self).write_byte(cx, b) } @@ -151,9 +151,9 @@ impl Writer for rust_alloc::vec::Vec { Self: 'this; #[inline] - fn finish(&mut self, _: &C) -> Result + fn finish(&mut self, _: C) -> Result where - C: ?Sized + Context, + C: Context, { Ok(()) } @@ -164,18 +164,18 @@ impl Writer for rust_alloc::vec::Vec { } #[inline] - fn extend(&mut self, cx: &C, buffer: Vec) -> Result<(), C::Error> + fn extend(&mut self, cx: C, buffer: Vec) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { // SAFETY: the buffer never outlives this function call. self.write_bytes(cx, buffer.as_slice()) } #[inline] - fn write_bytes(&mut self, cx: &C, bytes: &[u8]) -> Result<(), C::Error> + fn write_bytes(&mut self, cx: C, bytes: &[u8]) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { self.extend_from_slice(bytes); cx.advance(bytes.len()); @@ -183,9 +183,9 @@ impl Writer for rust_alloc::vec::Vec { } #[inline] - fn write_byte(&mut self, cx: &C, b: u8) -> Result<(), C::Error> + fn write_byte(&mut self, cx: C, b: u8) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { self.push(b); cx.advance(1); @@ -239,9 +239,9 @@ where Self: 'this; #[inline] - fn finish(&mut self, _: &C) -> Result + fn finish(&mut self, _: C) -> Result where - C: ?Sized + Context, + C: Context, { Ok(()) } @@ -252,9 +252,9 @@ where } #[inline] - fn extend(&mut self, cx: &C, buffer: Vec) -> Result<(), C::Error> + fn extend(&mut self, cx: C, buffer: Vec) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { if !self.buf.write(buffer.as_slice()) { return Err(cx.message("Buffer overflow")); @@ -264,9 +264,9 @@ where } #[inline] - fn write_bytes(&mut self, cx: &C, bytes: &[u8]) -> Result<(), C::Error> + fn write_bytes(&mut self, cx: C, bytes: &[u8]) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { if !self.buf.write(bytes) { return Err(cx.message("Buffer overflow")); diff --git a/crates/musli/src/writer/slice_mut_writer.rs b/crates/musli/src/writer/slice_mut_writer.rs index a435378b8..4834c5d5c 100644 --- a/crates/musli/src/writer/slice_mut_writer.rs +++ b/crates/musli/src/writer/slice_mut_writer.rs @@ -43,9 +43,9 @@ impl<'a> Writer for SliceMutWriter<'a> { Self: 'this; #[inline] - fn finish(&mut self, _: &C) -> Result + fn finish(&mut self, _: C) -> Result where - C: ?Sized + Context, + C: Context, { Ok(self.remaining()) } @@ -56,18 +56,18 @@ impl<'a> Writer for SliceMutWriter<'a> { } #[inline] - fn extend(&mut self, cx: &C, buffer: Vec) -> Result<(), C::Error> + fn extend(&mut self, cx: C, buffer: Vec) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { // SAFETY: the buffer never outlives this function call. self.write_bytes(cx, buffer.as_slice()) } #[inline] - fn write_bytes(&mut self, cx: &C, bytes: &[u8]) -> Result<(), C::Error> + fn write_bytes(&mut self, cx: C, bytes: &[u8]) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { let end = self.start.as_ptr().wrapping_add(bytes.len()); @@ -90,9 +90,9 @@ impl<'a> Writer for SliceMutWriter<'a> { } #[inline] - fn write_byte(&mut self, cx: &C, b: u8) -> Result<(), C::Error> + fn write_byte(&mut self, cx: C, b: u8) -> Result<(), C::Error> where - C: ?Sized + Context, + C: Context, { if self.start == self.end { return Err(cx.message(format_args!( diff --git a/crates/musli/tests/ui/visitor_attribute_ok.rs b/crates/musli/tests/ui/visitor_attribute_ok.rs index a315c5ad2..c9bc01b15 100644 --- a/crates/musli/tests/ui/visitor_attribute_ok.rs +++ b/crates/musli/tests/ui/visitor_attribute_ok.rs @@ -6,7 +6,7 @@ use musli::Context; struct AnyVisitor; #[musli::visitor] -impl<'de, C: ?Sized + Context> Visitor<'de, C> for AnyVisitor { +impl<'de, C> Visitor<'de, C> for AnyVisitor where C: Context { type Ok = (); #[inline] diff --git a/crates/musli/tests/visitors.rs b/crates/musli/tests/visitors.rs index 91c9dd206..d1f82009d 100644 --- a/crates/musli/tests/visitors.rs +++ b/crates/musli/tests/visitors.rs @@ -18,7 +18,7 @@ impl<'de, M> Decode<'de, M> for BytesReference<'de> { impl<'de, C> UnsizedVisitor<'de, C, [u8]> for Visitor where - C: ?Sized + Context, + C: Context, { type Ok = &'de [u8]; @@ -28,7 +28,7 @@ impl<'de, M> Decode<'de, M> for BytesReference<'de> { } #[inline] - fn visit_borrowed(self, _: &C, bytes: &'de [u8]) -> Result { + fn visit_borrowed(self, _: C, bytes: &'de [u8]) -> Result { Ok(bytes) } } @@ -75,7 +75,7 @@ impl<'de, M> Decode<'de, M> for StringReference<'de> { impl<'de, C> UnsizedVisitor<'de, C, str> for Visitor where - C: ?Sized + Context, + C: Context, { type Ok = &'de str; @@ -85,7 +85,7 @@ impl<'de, M> Decode<'de, M> for StringReference<'de> { } #[inline] - fn visit_borrowed(self, _: &C, bytes: &'de str) -> Result { + fn visit_borrowed(self, _: C, bytes: &'de str) -> Result { Ok(bytes) } } @@ -126,14 +126,12 @@ impl<'de, M> Decode<'de, M> for OwnedFn { where D: Decoder<'de>, { - decoder.cx(|cx, decoder| { - decoder.decode_unsized(|variant: &str| match variant { - "A" => Ok(OwnedFn::A), - "B" => Ok(OwnedFn::A), - other => { - Err(cx.message(format_args!("Expected either 'A' or 'B' but got {other}"))) - } - }) + let cx = decoder.cx(); + + decoder.decode_unsized(|variant: &str| match variant { + "A" => Ok(OwnedFn::A), + "B" => Ok(OwnedFn::A), + other => Err(cx.message(format_args!("Expected either 'A' or 'B' but got {other}"))), }) } } diff --git a/tests/benches/comparison.rs b/tests/benches/comparison.rs index 73abf9116..b791bf1ff 100644 --- a/tests/benches/comparison.rs +++ b/tests/benches/comparison.rs @@ -98,7 +98,9 @@ fn criterion_benchmark(c: &mut Criterion) { ); #[cfg(feature = "no-binary-equality")] - drop((actual, index)); + { + _ = (actual, index); + } } }}; }