diff --git a/README.md b/README.md index 0d7bf1312..a58f7d436 100644 --- a/README.md +++ b/README.md @@ -126,10 +126,9 @@ struct MyType { } impl<'de, M> Decode<'de, M> for MyType { - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de, Mode = M>, { let mut seq = decoder.decode_sequence(cx)?; let mut data = Vec::with_capacity(seq.size_hint(cx).or_default()); diff --git a/crates/musli-descriptive/src/de.rs b/crates/musli-descriptive/src/de.rs index 85d80fc0b..ca130daab 100644 --- a/crates/musli-descriptive/src/de.rs +++ b/crates/musli-descriptive/src/de.rs @@ -1,4 +1,5 @@ use core::fmt; +use core::marker::PhantomData; #[cfg(feature = "alloc")] use alloc::vec::Vec; @@ -21,38 +22,44 @@ use crate::tag::{Kind, Mark, Tag, F32, F64, I128, I16, I32, I64, I8, U128, U16, const BUFFER_OPTIONS: crate::options::Options = crate::options::new().build(); /// A very simple decoder. -pub struct SelfDecoder { +pub struct SelfDecoder { reader: R, + _marker: PhantomData, } -impl SelfDecoder { +impl SelfDecoder { /// Construct a new fixed width message encoder. #[inline] pub(crate) fn new(reader: R) -> Self { - Self { reader } + Self { + reader, + _marker: PhantomData, + } } } -pub struct SelfTupleDecoder { +pub struct SelfTupleDecoder { reader: R, + _marker: PhantomData, } -impl SelfTupleDecoder { +impl SelfTupleDecoder { #[inline] pub(crate) fn new(reader: R) -> Self { - Self { reader } + Self { + reader, + _marker: PhantomData, + } } } -impl<'de, R, const F: Options> SelfDecoder +impl<'de, R, const F: Options, C> SelfDecoder where R: Reader<'de>, + C: ?Sized + Context, { /// Skip over any sequences of values. - pub(crate) fn skip_any(&mut self, cx: &C) -> Result<(), C::Error> - where - C: ?Sized + Context, - { + pub(crate) fn skip_any(&mut self, cx: &C) -> Result<(), C::Error> { let tag = Tag::from_byte(self.reader.read_byte(cx)?); match tag.kind() { @@ -113,10 +120,7 @@ where // Standard function for decoding a pair sequence. #[inline] - fn shared_decode_map(mut self, cx: &C) -> Result, C::Error> - where - C: ?Sized + Context, - { + fn shared_decode_map(mut self, cx: &C) -> Result, C::Error> { let pos = cx.mark(); let len = self.decode_prefix(cx, Kind::Map, pos)?; Ok(RemainingSelfDecoder::new(len, self)) @@ -124,10 +128,7 @@ where // Standard function for decoding a pair sequence. #[inline] - fn shared_decode_sequence(mut self, cx: &C) -> Result, C::Error> - where - C: ?Sized + Context, - { + fn shared_decode_sequence(mut self, cx: &C) -> Result, C::Error> { let pos = cx.mark(); let len = self.decode_prefix(cx, Kind::Sequence, pos)?; Ok(RemainingSelfDecoder::new(len, self)) @@ -135,10 +136,7 @@ where /// Decode the length of a prefix. #[inline] - fn decode_prefix(&mut self, cx: &C, kind: Kind, mark: C::Mark) -> Result - where - C: ?Sized + Context, - { + fn decode_prefix(&mut self, cx: &C, kind: Kind, mark: C::Mark) -> Result { let tag = Tag::from_byte(self.reader.read_byte(cx)?); if tag.kind() != kind { @@ -160,10 +158,7 @@ where /// Decode the length of a prefix. #[inline] - fn decode_pack_length(&mut self, cx: &C, start: C::Mark) -> Result - where - C: ?Sized + Context, - { + fn decode_pack_length(&mut self, cx: &C, start: C::Mark) -> Result { let tag = Tag::from_byte(self.reader.read_byte(cx)?); match tag.kind() { @@ -189,26 +184,29 @@ where /// This simplifies implementing decoders that do not have any special handling /// for length-prefixed types. #[doc(hidden)] -pub struct RemainingSelfDecoder { +pub struct RemainingSelfDecoder { remaining: usize, - decoder: SelfDecoder, + decoder: SelfDecoder, } #[musli::decoder] -impl<'de, C, R, const F: Options> Decoder<'de, C> for SelfDecoder +impl<'de, R, const F: Options, C> Decoder<'de> for SelfDecoder where - C: ?Sized + Context, R: Reader<'de>, + C: ?Sized + Context, { - type WithContext = Self where U: Context; + type Cx = C; + type Error = C::Error; + type Mode = C::Mode; + type WithContext = SelfDecoder where U: Context; #[cfg(feature = "musli-value")] - type DecodeBuffer = musli_value::AsValueDecoder; - type DecodePack = SelfDecoder, F>; + type DecodeBuffer = musli_value::AsValueDecoder; + type DecodePack = SelfDecoder, F, C>; type DecodeSome = Self; - type DecodeSequence = RemainingSelfDecoder; - type DecodeTuple = SelfTupleDecoder; - type DecodeMap = RemainingSelfDecoder; - type DecodeStruct = RemainingSelfDecoder; + type DecodeSequence = RemainingSelfDecoder; + type DecodeTuple = SelfTupleDecoder; + type DecodeMap = RemainingSelfDecoder; + type DecodeStruct = RemainingSelfDecoder; type DecodeVariant = Self; #[inline] @@ -216,7 +214,7 @@ where where U: Context, { - Ok(self) + Ok(SelfDecoder::new(self.reader)) } #[inline] @@ -745,12 +743,13 @@ where } } -impl<'de, C, R, const F: Options> PackDecoder<'de, C> for SelfDecoder, F> +impl<'de, R, const F: Options, C> PackDecoder<'de> for SelfDecoder, F, C> where - C: ?Sized + Context, R: Reader<'de>, + C: ?Sized + Context, { - type DecodeNext<'this> = StorageDecoder< as Reader<'de>>::Mut<'this>, F> where Self: 'this; + type Cx = C; + type DecodeNext<'this> = StorageDecoder< as Reader<'de>>::Mut<'this>, F, C> where Self: 'this; #[inline] fn decode_next(&mut self, _: &C) -> Result, C::Error> { @@ -767,12 +766,13 @@ where } } -impl<'de, C, R, const F: Options> PackDecoder<'de, C> for SelfTupleDecoder +impl<'de, R, const F: Options, C> PackDecoder<'de> for SelfTupleDecoder where - C: ?Sized + Context, R: Reader<'de>, + C: ?Sized + Context, { - type DecodeNext<'this> = SelfDecoder, F> where Self: 'this; + type Cx = C; + type DecodeNext<'this> = SelfDecoder, F, C> where Self: 'this; #[inline] fn decode_next(&mut self, _: &C) -> Result, C::Error> { @@ -785,19 +785,20 @@ where } } -impl RemainingSelfDecoder { +impl RemainingSelfDecoder { #[inline] - fn new(remaining: usize, decoder: SelfDecoder) -> Self { + fn new(remaining: usize, decoder: SelfDecoder) -> Self { Self { remaining, decoder } } } -impl<'de, C, R, const F: Options> SequenceDecoder<'de, C> for RemainingSelfDecoder +impl<'de, R, const F: Options, C> SequenceDecoder<'de> for RemainingSelfDecoder where - C: ?Sized + Context, R: Reader<'de>, + C: ?Sized + Context, { - type DecodeNext<'this> = SelfDecoder, F> where Self: 'this; + type Cx = C; + type DecodeNext<'this> = SelfDecoder, F, C> where Self: 'this; #[inline] fn size_hint(&self, _: &C) -> SizeHint { @@ -816,12 +817,13 @@ where } #[musli::map_decoder] -impl<'de, C, R, const F: Options> MapDecoder<'de, C> for RemainingSelfDecoder +impl<'de, R, const F: Options, C> MapDecoder<'de> for RemainingSelfDecoder where - C: ?Sized + Context, R: Reader<'de>, + C: ?Sized + Context, { - type DecodeEntry<'this> = SelfDecoder, F> + type Cx = C; + type DecodeEntry<'this> = SelfDecoder, F, C> where Self: 'this; type IntoMapEntries = Self; @@ -847,15 +849,16 @@ where } } -impl<'de, C, R, const F: Options> MapEntriesDecoder<'de, C> for RemainingSelfDecoder +impl<'de, R, const F: Options, C> MapEntriesDecoder<'de> for RemainingSelfDecoder where - C: ?Sized + Context, R: Reader<'de>, + C: ?Sized + Context, { - type DecodeMapEntryKey<'this> = SelfDecoder, F> + type Cx = C; + type DecodeMapEntryKey<'this> = SelfDecoder, F, C> where Self: 'this; - type DecodeMapEntryValue<'this> = SelfDecoder, F> + type DecodeMapEntryValue<'this> = SelfDecoder, F, C> where Self: 'this; @@ -884,15 +887,16 @@ where } } -impl<'de, C, R, const F: Options> StructFieldsDecoder<'de, C> for RemainingSelfDecoder +impl<'de, R, const F: Options, C> StructFieldsDecoder<'de> for RemainingSelfDecoder where - C: ?Sized + Context, R: Reader<'de>, + C: ?Sized + Context, { - type DecodeStructFieldName<'this> = SelfDecoder, F> + type Cx = C; + type DecodeStructFieldName<'this> = SelfDecoder, F, C> where Self: 'this; - type DecodeStructFieldValue<'this> = SelfDecoder, F> + type DecodeStructFieldValue<'this> = SelfDecoder, F, C> where Self: 'this; @@ -928,12 +932,13 @@ where } } -impl<'de, C, R, const F: Options> MapEntryDecoder<'de, C> for SelfDecoder +impl<'de, R, const F: Options, C> MapEntryDecoder<'de> for SelfDecoder where - C: ?Sized + Context, R: Reader<'de>, + C: ?Sized + Context, { - type DecodeMapKey<'this> = SelfDecoder, F> where Self: 'this; + type Cx = C; + type DecodeMapKey<'this> = SelfDecoder, F, C> where Self: 'this; type DecodeMapValue = Self; #[inline] @@ -954,12 +959,13 @@ where } #[musli::struct_decoder] -impl<'de, C, R, const F: Options> StructDecoder<'de, C> for RemainingSelfDecoder +impl<'de, R, const F: Options, C> StructDecoder<'de> for RemainingSelfDecoder where - C: ?Sized + Context, R: Reader<'de>, + C: ?Sized + Context, { - type DecodeField<'this> = SelfDecoder, F> + type Cx = C; + type DecodeField<'this> = SelfDecoder, F, C> where Self: 'this; type IntoStructFields = Self; @@ -985,12 +991,13 @@ where } } -impl<'de, C, R, const F: Options> StructFieldDecoder<'de, C> for SelfDecoder +impl<'de, R, const F: Options, C> StructFieldDecoder<'de> for SelfDecoder where - C: ?Sized + Context, R: Reader<'de>, + C: ?Sized + Context, { - type DecodeFieldName<'this> = SelfDecoder, F> where Self: 'this; + type Cx = C; + type DecodeFieldName<'this> = SelfDecoder, F, C> where Self: 'this; type DecodeFieldValue = Self; #[inline] @@ -1009,13 +1016,14 @@ where } } -impl<'de, C, R, const F: Options> VariantDecoder<'de, C> for SelfDecoder +impl<'de, R, const F: Options, C> VariantDecoder<'de> for SelfDecoder where - C: ?Sized + Context, R: Reader<'de>, + C: ?Sized + Context, { - type DecodeTag<'this> = SelfDecoder, F> where Self: 'this; - type DecodeVariant<'this> = SelfDecoder, F> where Self: 'this; + type Cx = C; + type DecodeTag<'this> = SelfDecoder, F, C> where Self: 'this; + type DecodeVariant<'this> = SelfDecoder, F, C> where Self: 'this; #[inline] fn decode_tag(&mut self, _: &C) -> Result, C::Error> { @@ -1029,7 +1037,7 @@ where #[inline] fn skip_value(&mut self, cx: &C) -> Result { - SelfDecoder::<_, F>::new(self.reader.borrow_mut()).skip_any(cx)?; + self.skip_any(cx)?; Ok(true) } diff --git a/crates/musli-descriptive/src/en.rs b/crates/musli-descriptive/src/en.rs index 309c3aaba..75338bb16 100644 --- a/crates/musli-descriptive/src/en.rs +++ b/crates/musli-descriptive/src/en.rs @@ -1,4 +1,5 @@ use core::fmt; +use core::marker::PhantomData; use musli::en::{ Encoder, MapEncoder, MapEntriesEncoder, MapEntryEncoder, SequenceEncoder, StructEncoder, @@ -19,42 +20,52 @@ use crate::writer::{BufWriter, Writer}; const VARIANT: Tag = Tag::from_mark(Mark::Variant); /// A very simple encoder. -pub struct SelfEncoder { +pub struct SelfEncoder { writer: W, + _marker: PhantomData, } -impl SelfEncoder { +impl SelfEncoder { /// Construct a new fixed width message encoder. #[inline] pub(crate) fn new(writer: W) -> Self { - Self { writer } + Self { + writer, + _marker: PhantomData, + } } } -pub struct SelfPackEncoder { +pub struct SelfPackEncoder { writer: W, buffer: BufWriter, + _marker: PhantomData, } -impl SelfPackEncoder { +impl SelfPackEncoder { /// Construct a new fixed width message encoder. #[inline] pub(crate) fn new(writer: W, buffer: B) -> Self { Self { writer, buffer: BufWriter::new(buffer), + _marker: PhantomData, } } } #[musli::encoder] -impl Encoder for SelfEncoder +impl Encoder for SelfEncoder where W: Writer, + C: ?Sized + Context, { + type Cx = C; + type Error = C::Error; type Ok = (); - type WithContext = Self where U: Context; - type EncodePack<'this> = SelfPackEncoder, F> where C: 'this; + type Mode = C::Mode; + type WithContext = SelfEncoder where U: Context; + type EncodePack<'this> = SelfPackEncoder, F, C> where C: 'this; type EncodeSome = Self; type EncodeSequence = Self; type EncodeTuple = Self; @@ -70,7 +81,7 @@ where where U: Context, { - Ok(self) + Ok(SelfEncoder::new(self.writer)) } #[inline] @@ -296,7 +307,7 @@ where T: ?Sized + Encode, { self.writer.write_byte(cx, VARIANT.byte())?; - tag.encode(cx, SelfEncoder::<_, F>::new(self.writer.borrow_mut()))?; + tag.encode(cx, SelfEncoder::<_, F, _>::new(self.writer.borrow_mut()))?; self.encode_tuple(cx, len) } @@ -311,18 +322,20 @@ where T: ?Sized + Encode, { self.writer.write_byte(cx, VARIANT.byte())?; - tag.encode(cx, SelfEncoder::<_, F>::new(self.writer.borrow_mut()))?; + tag.encode(cx, SelfEncoder::<_, F, _>::new(self.writer.borrow_mut()))?; self.encode_struct(cx, len) } } -impl SequenceEncoder for SelfPackEncoder +impl SequenceEncoder for SelfPackEncoder where W: Writer, B: Buf, + C: ?Sized + Context, { + type Cx = C; type Ok = (); - type EncodeNext<'this> = StorageEncoder<&'this mut BufWriter, F> where Self: 'this; + type EncodeNext<'this> = StorageEncoder<&'this mut BufWriter, F, C> where Self: 'this; #[inline] fn encode_next(&mut self, _: &C) -> Result, C::Error> { @@ -366,12 +379,14 @@ where } } -impl SequenceEncoder for SelfEncoder +impl SequenceEncoder for SelfEncoder where W: Writer, + C: ?Sized + Context, { + type Cx = C; type Ok = (); - type EncodeNext<'this> = SelfEncoder, F> where Self: 'this; + type EncodeNext<'this> = SelfEncoder, F, C> where Self: 'this; #[inline] fn encode_next(&mut self, _: &C) -> Result, C::Error> { @@ -384,12 +399,14 @@ where } } -impl MapEncoder for SelfEncoder +impl MapEncoder for SelfEncoder where W: Writer, + C: ?Sized + Context, { + type Cx = C; type Ok = (); - type EncodeEntry<'this> = SelfEncoder, F> where Self: 'this; + type EncodeEntry<'this> = SelfEncoder, F, C> where Self: 'this; #[inline] fn encode_entry(&mut self, _: &C) -> Result, C::Error> { @@ -402,13 +419,15 @@ where } } -impl MapEntryEncoder for SelfEncoder +impl MapEntryEncoder for SelfEncoder where W: Writer, + C: ?Sized + Context, { + type Cx = C; type Ok = (); - type EncodeMapKey<'this> = SelfEncoder, F> where Self: 'this; - type EncodeMapValue<'this> = SelfEncoder, F> where Self: 'this; + type EncodeMapKey<'this> = SelfEncoder, F, C> where Self: 'this; + type EncodeMapValue<'this> = SelfEncoder, F, C> where Self: 'this; #[inline] fn encode_map_key(&mut self, _: &C) -> Result, C::Error> { @@ -426,13 +445,15 @@ where } } -impl MapEntriesEncoder for SelfEncoder +impl MapEntriesEncoder for SelfEncoder where W: Writer, + C: ?Sized + Context, { + type Cx = C; type Ok = (); - type EncodeMapEntryKey<'this> = SelfEncoder, F> where Self: 'this; - type EncodeMapEntryValue<'this> = SelfEncoder, F> where Self: 'this; + type EncodeMapEntryKey<'this> = SelfEncoder, F, C> where Self: 'this; + type EncodeMapEntryValue<'this> = SelfEncoder, F, C> where Self: 'this; #[inline] fn encode_map_entry_key(&mut self, _: &C) -> Result, C::Error> { @@ -450,12 +471,14 @@ where } } -impl StructEncoder for SelfEncoder +impl StructEncoder for SelfEncoder where W: Writer, + C: ?Sized + Context, { + type Cx = C; type Ok = (); - type EncodeField<'this> = SelfEncoder, F> where Self: 'this; + type EncodeField<'this> = SelfEncoder, F, C> where Self: 'this; #[inline] fn encode_field(&mut self, cx: &C) -> Result, C::Error> { @@ -468,13 +491,15 @@ where } } -impl StructFieldEncoder for SelfEncoder +impl StructFieldEncoder for SelfEncoder where W: Writer, + C: ?Sized + Context, { + type Cx = C; type Ok = (); - type EncodeFieldName<'this> = SelfEncoder, F> where Self: 'this; - type EncodeFieldValue<'this> = SelfEncoder, F> where Self: 'this; + type EncodeFieldName<'this> = SelfEncoder, F, C> where Self: 'this; + type EncodeFieldValue<'this> = SelfEncoder, F, C> where Self: 'this; #[inline] fn encode_field_name(&mut self, cx: &C) -> Result, C::Error> { @@ -492,13 +517,15 @@ where } } -impl VariantEncoder for SelfEncoder +impl VariantEncoder for SelfEncoder where W: Writer, + C: ?Sized + Context, { + type Cx = C; type Ok = (); - type EncodeTag<'this> = SelfEncoder, F> where Self: 'this; - type EncodeValue<'this> = SelfEncoder, F> where Self: 'this; + type EncodeTag<'this> = SelfEncoder, F, C> where Self: 'this; + type EncodeValue<'this> = SelfEncoder, F, C> where Self: 'this; #[inline] fn encode_tag(&mut self, _: &C) -> Result, C::Error> { @@ -518,13 +545,14 @@ where /// Encode a length prefix. #[inline] -fn encode_prefix( +fn encode_prefix( cx: &C, mut writer: W, kind: Kind, len: usize, ) -> Result<(), C::Error> where + C: ?Sized + Context, W: Writer, { let (tag, embedded) = Tag::with_len(kind, len); diff --git a/crates/musli-descriptive/src/encoding.rs b/crates/musli-descriptive/src/encoding.rs index c62b55a8f..bcc752eb6 100644 --- a/crates/musli-descriptive/src/encoding.rs +++ b/crates/musli-descriptive/src/encoding.rs @@ -148,7 +148,7 @@ impl Encoding { } } - musli_common::encoding_impls!(M, SelfEncoder::<_, F>::new, SelfDecoder::<_, F>::new); + musli_common::encoding_impls!(M, SelfEncoder::<_, F, _>::new, SelfDecoder::<_, F, _>::new); musli_common::encoding_from_slice_impls!(M, SelfDecoder::<_, F>::new); } diff --git a/crates/musli-descriptive/src/tag.rs b/crates/musli-descriptive/src/tag.rs index a833c3e38..350abcf69 100644 --- a/crates/musli-descriptive/src/tag.rs +++ b/crates/musli-descriptive/src/tag.rs @@ -5,7 +5,6 @@ use core::fmt; use core::mem; -use musli::Context; use musli::{Decode, Decoder}; /// Variant corresponding to marks. @@ -223,10 +222,9 @@ impl fmt::Debug for Tag { impl<'de, M> Decode<'de, M> for Tag { #[inline] - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de, Mode = M>, { Ok(Self::from_byte(decoder.decode_u8(cx)?)) } diff --git a/crates/musli-descriptive/src/test.rs b/crates/musli-descriptive/src/test.rs index 4396be16c..3384fed1b 100644 --- a/crates/musli-descriptive/src/test.rs +++ b/crates/musli-descriptive/src/test.rs @@ -30,10 +30,9 @@ impl<'de, M, T> Decode<'de, M> for Typed where T: Decode<'de, M>, { - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de, Mode = M>, { let mut unpack = decoder.decode_pack(cx)?; let tag = cx.decode(unpack.decode_next(cx)?)?; diff --git a/crates/musli-json/src/de/key_decoder.rs b/crates/musli-json/src/de/key_decoder.rs index b01fad57b..1fa83ea7f 100644 --- a/crates/musli-json/src/de/key_decoder.rs +++ b/crates/musli-json/src/de/key_decoder.rs @@ -1,4 +1,5 @@ use core::fmt; +use core::marker::PhantomData; use musli::de::{Decoder, NumberHint, SizeHint, TypeHint, ValueVisitor, Visitor}; use musli::Context; @@ -10,37 +11,39 @@ use super::{ }; /// A JSON object key decoder for Müsli. -pub(crate) struct JsonKeyDecoder

{ +pub(crate) struct JsonKeyDecoder { parser: P, + _marker: PhantomData, } -impl<'de, P> JsonKeyDecoder

+impl<'de, P, C> JsonKeyDecoder where P: Parser<'de>, + C: ?Sized + Context, { #[inline] - pub(super) fn skip_any(self, cx: &C) -> Result<(), C::Error> - where - C: ?Sized + Context, - { + pub(super) fn skip_any(self, cx: &C) -> Result<(), C::Error> { JsonDecoder::new(self.parser).skip_any(cx) } } -impl<'de, P> JsonKeyDecoder

+impl<'de, P, C> JsonKeyDecoder where P: Parser<'de>, + C: ?Sized + Context, { /// Construct a new fixed width message encoder. #[inline] pub(crate) fn new(parser: P) -> Self { - Self { parser } + Self { + parser, + _marker: PhantomData, + } } #[inline] - fn decode_escaped_bytes(mut self, cx: &C, visitor: V) -> Result + fn decode_escaped_bytes(mut self, cx: &C, visitor: V) -> Result where - C: ?Sized + Context, V: ValueVisitor<'de, C, [u8]>, { let Some(mut scratch) = cx.alloc() else { @@ -55,20 +58,23 @@ where } #[musli::decoder] -impl<'de, C, P> Decoder<'de, C> for JsonKeyDecoder

+impl<'de, P, C> Decoder<'de> for JsonKeyDecoder where - C: ?Sized + Context, P: Parser<'de>, + C: ?Sized + Context, { - type WithContext = Self where U: Context; - type DecodeStruct = JsonObjectDecoder

; + type Cx = C; + type Error = C::Error; + type Mode = C::Mode; + type WithContext = JsonKeyDecoder where U: Context; + type DecodeStruct = JsonObjectDecoder; #[inline] fn with_context(self, _: &C) -> Result, C::Error> where U: Context, { - Ok(self) + Ok(JsonKeyDecoder::new(self.parser)) } #[inline] diff --git a/crates/musli-json/src/de/mod.rs b/crates/musli-json/src/de/mod.rs index 48a70a385..0a1688448 100644 --- a/crates/musli-json/src/de/mod.rs +++ b/crates/musli-json/src/de/mod.rs @@ -20,6 +20,7 @@ mod variant_decoder; use self::variant_decoder::JsonVariantDecoder; use core::fmt; +use core::marker::PhantomData; use core::str; #[cfg(feature = "alloc")] @@ -45,25 +46,27 @@ use crate::parser::{integer, string, Parser, StringReference, Token}; const BUFFER_OPTIONS: crate::options::Options = crate::options::new().build(); /// A JSON decoder for Müsli. -pub(crate) struct JsonDecoder

{ +pub(crate) struct JsonDecoder { parser: P, + _marker: PhantomData, } -impl<'de, P> JsonDecoder

+impl<'de, P, C> JsonDecoder where P: Parser<'de>, + C: ?Sized + Context, { /// Construct a new fixed width message encoder. #[inline] pub(crate) fn new(parser: P) -> Self { - Self { parser } + Self { + parser, + _marker: PhantomData, + } } /// Skip over any values. - pub(crate) fn skip_any(mut self, cx: &C) -> Result<(), C::Error> - where - C: ?Sized + Context, - { + pub(crate) fn skip_any(mut self, cx: &C) -> Result<(), C::Error> { let start = cx.mark(); let actual = self.parser.peek(cx)?; @@ -101,53 +104,47 @@ where } #[inline] - fn parse_true(mut self, cx: &C) -> Result<(), C::Error> - where - C: ?Sized + Context, - { + fn parse_true(mut self, cx: &C) -> Result<(), C::Error> { self.parser.parse_exact(cx, "true") } #[inline] - fn parse_false(mut self, cx: &C) -> Result<(), C::Error> - where - C: ?Sized + Context, - { + fn parse_false(mut self, cx: &C) -> Result<(), C::Error> { self.parser.parse_exact(cx, "false") } #[inline] - fn parse_null(mut self, cx: &C) -> Result<(), C::Error> - where - C: ?Sized + Context, - { + fn parse_null(mut self, cx: &C) -> Result<(), C::Error> { self.parser.parse_exact(cx, "null") } } #[musli::decoder] -impl<'de, C, P> Decoder<'de, C> for JsonDecoder

+impl<'de, P, C> Decoder<'de> for JsonDecoder where - C: ?Sized + Context, P: Parser<'de>, + C: ?Sized + Context, { - type WithContext = Self where U: Context; + type Cx = C; + type Error = C::Error; + type Mode = C::Mode; + type WithContext = JsonDecoder where U: Context; #[cfg(feature = "musli-value")] - type DecodeBuffer = musli_value::AsValueDecoder; - type DecodePack = JsonSequenceDecoder

; - type DecodeSequence = JsonSequenceDecoder

; - type DecodeTuple = JsonSequenceDecoder

; - type DecodeMap = JsonObjectDecoder

; - type DecodeSome = JsonDecoder

; - type DecodeStruct = JsonObjectDecoder

; - type DecodeVariant = JsonVariantDecoder

; + type DecodeBuffer = musli_value::AsValueDecoder; + type DecodePack = JsonSequenceDecoder; + type DecodeSequence = JsonSequenceDecoder; + type DecodeTuple = JsonSequenceDecoder; + type DecodeMap = JsonObjectDecoder; + type DecodeSome = JsonDecoder; + type DecodeStruct = JsonObjectDecoder; + type DecodeVariant = JsonVariantDecoder; #[inline] fn with_context(self, _: &C) -> Result, C::Error> where U: Context, { - Ok(self) + Ok(JsonDecoder::new(self.parser)) } #[inline] diff --git a/crates/musli-json/src/de/object_decoder.rs b/crates/musli-json/src/de/object_decoder.rs index 28cedb9af..c65a601c1 100644 --- a/crates/musli-json/src/de/object_decoder.rs +++ b/crates/musli-json/src/de/object_decoder.rs @@ -1,3 +1,4 @@ +use core::marker::PhantomData; use core::mem; use musli::de::{MapDecoder, MapEntriesDecoder, SizeHint, StructDecoder, StructFieldsDecoder}; @@ -7,22 +8,21 @@ use crate::parser::{Parser, Token}; use super::{JsonDecoder, JsonKeyDecoder, JsonObjectPairDecoder}; -pub(crate) struct JsonObjectDecoder

{ +pub(crate) struct JsonObjectDecoder { first: bool, completed: bool, len: Option, parser: P, + _marker: PhantomData, } -impl<'de, P> JsonObjectDecoder

+impl<'de, P, C> JsonObjectDecoder where P: Parser<'de>, + C: ?Sized + Context, { #[inline] - pub(super) fn new(cx: &C, len: Option, mut parser: P) -> Result - where - C: ?Sized + Context, - { + pub(super) fn new(cx: &C, len: Option, mut parser: P) -> Result { parser.skip_whitespace(cx)?; let actual = parser.peek(cx)?; @@ -38,13 +38,11 @@ where completed: false, len, parser, + _marker: PhantomData, }) } - fn parse_map_key(&mut self, cx: &C) -> Result - where - C: ?Sized + Context, - { + fn parse_map_key(&mut self, cx: &C) -> Result { let first = mem::take(&mut self.first); loop { @@ -73,12 +71,13 @@ where } #[musli::map_decoder] -impl<'de, C, P> MapDecoder<'de, C> for JsonObjectDecoder

+impl<'de, P, C> MapDecoder<'de> for JsonObjectDecoder where - C: ?Sized + Context, P: Parser<'de>, + C: ?Sized + Context, { - type DecodeEntry<'this> = JsonObjectPairDecoder> + type Cx = C; + type DecodeEntry<'this> = JsonObjectPairDecoder, C> where Self: 'this; type IntoMapEntries = Self; @@ -108,15 +107,16 @@ where } } -impl<'de, C, P> MapEntriesDecoder<'de, C> for JsonObjectDecoder

+impl<'de, P, C> MapEntriesDecoder<'de> for JsonObjectDecoder where - C: ?Sized + Context, P: Parser<'de>, + C: ?Sized + Context, { - type DecodeMapEntryKey<'this> = JsonKeyDecoder> + type Cx = C; + type DecodeMapEntryKey<'this> = JsonKeyDecoder, C> where Self: 'this; - type DecodeMapEntryValue<'this> = JsonDecoder> where Self: 'this; + type DecodeMapEntryValue<'this> = JsonDecoder, C> where Self: 'this; #[inline] fn decode_map_entry_key( @@ -165,12 +165,13 @@ where } #[musli::struct_decoder] -impl<'de, C, P> StructDecoder<'de, C> for JsonObjectDecoder

+impl<'de, P, C> StructDecoder<'de> for JsonObjectDecoder where - C: ?Sized + Context, P: Parser<'de>, + C: ?Sized + Context, { - type DecodeField<'this> = JsonObjectPairDecoder> + type Cx = C; + type DecodeField<'this> = JsonObjectPairDecoder, C> where Self: 'this; type IntoStructFields = Self; @@ -196,15 +197,16 @@ where } } -impl<'de, C, P> StructFieldsDecoder<'de, C> for JsonObjectDecoder

+impl<'de, P, C> StructFieldsDecoder<'de> for JsonObjectDecoder where - C: ?Sized + Context, P: Parser<'de>, + C: ?Sized + Context, { - type DecodeStructFieldName<'this> = JsonKeyDecoder> + type Cx = C; + type DecodeStructFieldName<'this> = JsonKeyDecoder, C> where Self: 'this; - type DecodeStructFieldValue<'this> = JsonDecoder> where Self: 'this; + type DecodeStructFieldValue<'this> = JsonDecoder, C> where Self: 'this; #[inline] fn decode_struct_field_name( diff --git a/crates/musli-json/src/de/object_pair_decoder.rs b/crates/musli-json/src/de/object_pair_decoder.rs index bc94c0f2e..14163182c 100644 --- a/crates/musli-json/src/de/object_pair_decoder.rs +++ b/crates/musli-json/src/de/object_pair_decoder.rs @@ -1,3 +1,5 @@ +use core::marker::PhantomData; + use musli::de::{MapEntryDecoder, StructFieldDecoder}; use musli::Context; @@ -5,26 +7,31 @@ use crate::parser::{Parser, Token}; use super::{JsonDecoder, JsonKeyDecoder}; -pub(crate) struct JsonObjectPairDecoder

{ +pub(crate) struct JsonObjectPairDecoder { parser: P, + _marker: PhantomData, } -impl

JsonObjectPairDecoder

{ +impl JsonObjectPairDecoder { #[inline] pub(super) fn new(parser: P) -> Self { - Self { parser } + Self { + parser, + _marker: PhantomData, + } } } -impl<'de, C, P> MapEntryDecoder<'de, C> for JsonObjectPairDecoder

+impl<'de, P, C> MapEntryDecoder<'de> for JsonObjectPairDecoder where - C: ?Sized + Context, P: Parser<'de>, + C: ?Sized + Context, { - type DecodeMapKey<'this> = JsonKeyDecoder> + type Cx = C; + type DecodeMapKey<'this> = JsonKeyDecoder, C> where Self: 'this; - type DecodeMapValue = JsonDecoder

; + type DecodeMapValue = JsonDecoder; #[inline] fn decode_map_key(&mut self, _: &C) -> Result, C::Error> { @@ -57,15 +64,16 @@ where } } -impl<'de, C, P> StructFieldDecoder<'de, C> for JsonObjectPairDecoder

+impl<'de, P, C> StructFieldDecoder<'de> for JsonObjectPairDecoder where - C: ?Sized + Context, P: Parser<'de>, + C: ?Sized + Context, { - type DecodeFieldName<'this> = JsonKeyDecoder> + type Cx = C; + type DecodeFieldName<'this> = JsonKeyDecoder, C> where Self: 'this; - type DecodeFieldValue = JsonDecoder

; + type DecodeFieldValue = JsonDecoder; #[inline] fn decode_field_name(&mut self, cx: &C) -> Result, C::Error> { diff --git a/crates/musli-json/src/de/sequence_decoder.rs b/crates/musli-json/src/de/sequence_decoder.rs index a11d771a2..2f23a12cf 100644 --- a/crates/musli-json/src/de/sequence_decoder.rs +++ b/crates/musli-json/src/de/sequence_decoder.rs @@ -1,3 +1,4 @@ +use core::marker::PhantomData; use core::mem; use musli::de::{PackDecoder, SequenceDecoder, SizeHint}; @@ -7,22 +8,21 @@ use crate::parser::{Parser, Token}; use super::JsonDecoder; -pub(crate) struct JsonSequenceDecoder

{ +pub(crate) struct JsonSequenceDecoder { len: Option, first: bool, parser: P, terminated: bool, + _marker: PhantomData, } -impl<'de, P> JsonSequenceDecoder

+impl<'de, P, C> JsonSequenceDecoder where P: Parser<'de>, + C: ?Sized + Context, { #[inline] - pub(crate) fn new(cx: &C, len: Option, mut parser: P) -> Result - where - C: ?Sized + Context, - { + pub(crate) fn new(cx: &C, len: Option, mut parser: P) -> Result { let actual = parser.peek(cx)?; if !matches!(actual, Token::OpenBracket) { @@ -36,16 +36,18 @@ where first: true, parser, terminated: false, + _marker: PhantomData, }) } } -impl<'de, C, P> SequenceDecoder<'de, C> for JsonSequenceDecoder

+impl<'de, P, C> SequenceDecoder<'de> for JsonSequenceDecoder where - C: ?Sized + Context, P: Parser<'de>, + C: ?Sized + Context, { - type DecodeNext<'this> = JsonDecoder> + type Cx = C; + type DecodeNext<'this> = JsonDecoder, C> where Self: 'this; @@ -88,12 +90,13 @@ where } } -impl<'de, C, P> PackDecoder<'de, C> for JsonSequenceDecoder

+impl<'de, P, C> PackDecoder<'de> for JsonSequenceDecoder where - C: ?Sized + Context, P: Parser<'de>, + C: ?Sized + Context, { - type DecodeNext<'this> = JsonDecoder> + type Cx = C; + type DecodeNext<'this> = JsonDecoder, C> where Self: 'this; diff --git a/crates/musli-json/src/de/variant_decoder.rs b/crates/musli-json/src/de/variant_decoder.rs index aba378429..b54ab641a 100644 --- a/crates/musli-json/src/de/variant_decoder.rs +++ b/crates/musli-json/src/de/variant_decoder.rs @@ -1,3 +1,5 @@ +use core::marker::PhantomData; + use musli::de::VariantDecoder; use musli::Context; @@ -5,19 +7,18 @@ use crate::parser::{Parser, Token}; use super::{JsonDecoder, JsonKeyDecoder}; -pub(crate) struct JsonVariantDecoder

{ +pub(crate) struct JsonVariantDecoder { parser: P, + _marker: PhantomData, } -impl<'de, P> JsonVariantDecoder

+impl<'de, P, C> JsonVariantDecoder where P: Parser<'de>, + C: ?Sized + Context, { #[inline] - pub(crate) fn new(cx: &C, mut parser: P) -> Result - where - C: ?Sized + Context, - { + pub(crate) fn new(cx: &C, mut parser: P) -> Result { parser.skip_whitespace(cx)?; let actual = parser.peek(cx)?; @@ -27,19 +28,23 @@ where } parser.skip(cx, 1)?; - Ok(Self { parser }) + Ok(Self { + parser, + _marker: PhantomData, + }) } } -impl<'de, C, P> VariantDecoder<'de, C> for JsonVariantDecoder

+impl<'de, P, C> VariantDecoder<'de> for JsonVariantDecoder where - C: ?Sized + Context, P: Parser<'de>, + C: ?Sized + Context, { - type DecodeTag<'this> = JsonKeyDecoder> + type Cx = C; + type DecodeTag<'this> = JsonKeyDecoder, C> where Self: 'this; - type DecodeVariant<'this> = JsonDecoder> where Self: 'this; + type DecodeVariant<'this> = JsonDecoder, C> where Self: 'this; #[inline] fn decode_tag(&mut self, _: &C) -> Result, C::Error> { diff --git a/crates/musli-json/src/en/array_encoder.rs b/crates/musli-json/src/en/array_encoder.rs index 71bf85143..22896c250 100644 --- a/crates/musli-json/src/en/array_encoder.rs +++ b/crates/musli-json/src/en/array_encoder.rs @@ -1,3 +1,4 @@ +use core::marker::PhantomData; use core::mem::take; use musli::en::SequenceEncoder; @@ -8,45 +9,44 @@ use crate::writer::Writer; use super::JsonEncoder; /// Encoder for a JSON array. -pub(crate) struct JsonArrayEncoder { +pub(crate) struct JsonArrayEncoder { first: bool, end: &'static [u8], writer: W, + _marker: PhantomData, } -impl JsonArrayEncoder +impl JsonArrayEncoder where W: Writer, + C: ?Sized + Context, { #[inline] - pub(super) fn new(cx: &C, writer: W) -> Result - where - C: ?Sized + Context, - { + pub(super) fn new(cx: &C, writer: W) -> Result { Self::with_end(cx, writer, b"]") } #[inline] - pub(super) fn with_end(cx: &C, mut writer: W, end: &'static [u8]) -> Result - where - C: ?Sized + Context, - { + pub(super) fn with_end(cx: &C, mut writer: W, end: &'static [u8]) -> Result { writer.write_byte(cx, b'[')?; Ok(Self { first: true, end, writer, + _marker: PhantomData, }) } } -impl SequenceEncoder for JsonArrayEncoder +impl SequenceEncoder for JsonArrayEncoder where W: Writer, + C: ?Sized + Context, { + type Cx = C; type Ok = (); - type EncodeNext<'this> = JsonEncoder> + type EncodeNext<'this> = JsonEncoder, C> where Self: 'this; diff --git a/crates/musli-json/src/en/mod.rs b/crates/musli-json/src/en/mod.rs index 29c26f5d1..1abd8f213 100644 --- a/crates/musli-json/src/en/mod.rs +++ b/crates/musli-json/src/en/mod.rs @@ -14,6 +14,7 @@ mod variant_encoder; use self::variant_encoder::JsonVariantEncoder; use core::fmt; +use core::marker::PhantomData; use musli::en::{Encoder, SequenceEncoder}; use musli::{Context, Encode}; @@ -21,43 +22,50 @@ use musli::{Context, Encode}; use crate::writer::Writer; /// A JSON encoder for Müsli. -pub(crate) struct JsonEncoder { +pub(crate) struct JsonEncoder { writer: W, + _marker: PhantomData, } -impl JsonEncoder { +impl JsonEncoder { /// Construct a new fixed width message encoder. #[inline] pub(crate) fn new(writer: W) -> Self { - Self { writer } + Self { + writer, + _marker: PhantomData, + } } } #[musli::encoder] -impl Encoder for JsonEncoder +impl Encoder for JsonEncoder where W: Writer, C: ?Sized + Context, { + type Cx = C; + type Error = C::Error; type Ok = (); - type WithContext = Self where U: Context; - type EncodePack<'this> = JsonArrayEncoder where C: 'this; + type Mode = C::Mode; + type WithContext = JsonEncoder where U: Context; + type EncodePack<'this> = JsonArrayEncoder where C: 'this; type EncodeSome = Self; - type EncodeSequence = JsonArrayEncoder; - type EncodeTuple = JsonArrayEncoder; - type EncodeMap = JsonObjectEncoder; - type EncodeMapEntries = JsonObjectEncoder; - type EncodeStruct = JsonObjectEncoder; - type EncodeVariant = JsonVariantEncoder; - type EncodeTupleVariant = JsonArrayEncoder; - type EncodeStructVariant = JsonObjectEncoder; + type EncodeSequence = JsonArrayEncoder; + type EncodeTuple = JsonArrayEncoder; + type EncodeMap = JsonObjectEncoder; + type EncodeMapEntries = JsonObjectEncoder; + type EncodeStruct = JsonObjectEncoder; + type EncodeVariant = JsonVariantEncoder; + type EncodeTupleVariant = JsonArrayEncoder; + type EncodeStructVariant = JsonObjectEncoder; #[inline] fn with_context(self, _: &C) -> Result, C::Error> where U: Context, { - Ok(self) + Ok(JsonEncoder::new(self.writer)) } #[inline] @@ -201,7 +209,7 @@ where I: IntoIterator, I::Item: AsRef<[u8]>, { - let mut seq = JsonArrayEncoder::<_>::new(cx, self.writer)?; + let mut seq = JsonArrayEncoder::new(cx, self.writer)?; for bb in vectors { for &b in bb.as_ref() { diff --git a/crates/musli-json/src/en/object_encoder.rs b/crates/musli-json/src/en/object_encoder.rs index eed939b77..c0ed1eb42 100644 --- a/crates/musli-json/src/en/object_encoder.rs +++ b/crates/musli-json/src/en/object_encoder.rs @@ -1,3 +1,5 @@ +use core::marker::PhantomData; + use musli::en::{MapEncoder, MapEntriesEncoder, StructEncoder}; use musli::Context; @@ -5,45 +7,44 @@ use super::{JsonEncoder, JsonObjectKeyEncoder, JsonObjectPairEncoder}; use crate::writer::Writer; /// An object encoder for JSON. -pub(crate) struct JsonObjectEncoder { +pub(crate) struct JsonObjectEncoder { len: usize, end: &'static [u8], writer: W, + _marker: PhantomData, } -impl JsonObjectEncoder +impl JsonObjectEncoder where W: Writer, + C: ?Sized + Context, { #[inline] - pub(super) fn new(cx: &C, writer: W) -> Result - where - C: ?Sized + Context, - { + pub(super) fn new(cx: &C, writer: W) -> Result { Self::with_end(cx, writer, b"}") } #[inline] - pub(super) fn with_end(cx: &C, mut writer: W, end: &'static [u8]) -> Result - where - C: ?Sized + Context, - { + pub(super) fn with_end(cx: &C, mut writer: W, end: &'static [u8]) -> Result { writer.write_byte(cx, b'{')?; Ok(Self { len: 0, end, writer, + _marker: PhantomData, }) } } -impl MapEncoder for JsonObjectEncoder +impl MapEncoder for JsonObjectEncoder where W: Writer, + C: ?Sized + Context, { + type Cx = C; type Ok = (); - type EncodeEntry<'this> = JsonObjectPairEncoder> + type EncodeEntry<'this> = JsonObjectPairEncoder, C> where Self: 'this; @@ -63,15 +64,17 @@ where } } -impl MapEntriesEncoder for JsonObjectEncoder +impl MapEntriesEncoder for JsonObjectEncoder where W: Writer, + C: ?Sized + Context, { + type Cx = C; type Ok = (); - type EncodeMapEntryKey<'this> = JsonObjectKeyEncoder> + type EncodeMapEntryKey<'this> = JsonObjectKeyEncoder, C> where Self: 'this; - type EncodeMapEntryValue<'this> = JsonEncoder> where Self: 'this; + type EncodeMapEntryValue<'this> = JsonEncoder, C> where Self: 'this; #[inline] fn encode_map_entry_key(&mut self, cx: &C) -> Result, C::Error> { @@ -98,12 +101,14 @@ where } } -impl StructEncoder for JsonObjectEncoder +impl StructEncoder for JsonObjectEncoder where W: Writer, + C: ?Sized + Context, { + type Cx = C; type Ok = (); - type EncodeField<'this> = JsonObjectPairEncoder> + type EncodeField<'this> = JsonObjectPairEncoder, C> where Self: 'this; diff --git a/crates/musli-json/src/en/object_key_encoder.rs b/crates/musli-json/src/en/object_key_encoder.rs index 486ffeddf..d2867df03 100644 --- a/crates/musli-json/src/en/object_key_encoder.rs +++ b/crates/musli-json/src/en/object_key_encoder.rs @@ -1,18 +1,23 @@ use core::fmt; +use core::marker::PhantomData; use musli::en::Encoder; use musli::Context; use crate::writer::Writer; -pub(crate) struct JsonObjectKeyEncoder { +pub(crate) struct JsonObjectKeyEncoder { writer: W, + _marker: PhantomData, } -impl JsonObjectKeyEncoder { +impl JsonObjectKeyEncoder { #[inline] pub(super) fn new(writer: W) -> Self { - Self { writer } + Self { + writer, + _marker: PhantomData, + } } } @@ -28,19 +33,23 @@ macro_rules! format_integer { } #[musli::encoder] -impl Encoder for JsonObjectKeyEncoder +impl Encoder for JsonObjectKeyEncoder where W: Writer, + C: ?Sized + Context, { + type Cx = C; + type Error = C::Error; type Ok = (); - type WithContext = Self where U: Context; + type Mode = C::Mode; + type WithContext = JsonObjectKeyEncoder where U: Context; #[inline] fn with_context(self, _: &C) -> Result, C::Error> where U: Context, { - Ok(self) + Ok(JsonObjectKeyEncoder::new(self.writer)) } #[inline] diff --git a/crates/musli-json/src/en/object_pair_encoder.rs b/crates/musli-json/src/en/object_pair_encoder.rs index 0e3bd4f5e..c001a5a16 100644 --- a/crates/musli-json/src/en/object_pair_encoder.rs +++ b/crates/musli-json/src/en/object_pair_encoder.rs @@ -1,3 +1,5 @@ +use core::marker::PhantomData; + use musli::en::{MapEntryEncoder, StructFieldEncoder}; use musli::Context; @@ -6,27 +8,34 @@ use crate::writer::Writer; use super::{JsonEncoder, JsonObjectKeyEncoder}; /// Encoder for a JSON object pair. -pub(crate) struct JsonObjectPairEncoder { +pub(crate) struct JsonObjectPairEncoder { empty: bool, writer: W, + _marker: PhantomData, } -impl JsonObjectPairEncoder { +impl JsonObjectPairEncoder { #[inline] pub(super) const fn new(empty: bool, writer: W) -> Self { - Self { empty, writer } + Self { + empty, + writer, + _marker: PhantomData, + } } } -impl MapEntryEncoder for JsonObjectPairEncoder +impl MapEntryEncoder for JsonObjectPairEncoder where W: Writer, + C: ?Sized + Context, { + type Cx = C; type Ok = (); - type EncodeMapKey<'this> = JsonObjectKeyEncoder> + type EncodeMapKey<'this> = JsonObjectKeyEncoder, C> where Self: 'this; - type EncodeMapValue<'this> = JsonEncoder> where Self: 'this; + type EncodeMapValue<'this> = JsonEncoder, C> where Self: 'this; #[inline] fn encode_map_key(&mut self, cx: &C) -> Result, C::Error> { @@ -49,15 +58,17 @@ where } } -impl StructFieldEncoder for JsonObjectPairEncoder +impl StructFieldEncoder for JsonObjectPairEncoder where W: Writer, + C: ?Sized + Context, { + type Cx = C; type Ok = (); - type EncodeFieldName<'this> = JsonObjectKeyEncoder> + type EncodeFieldName<'this> = JsonObjectKeyEncoder, C> where Self: 'this; - type EncodeFieldValue<'this> = JsonEncoder> where Self: 'this; + type EncodeFieldValue<'this> = JsonEncoder, C> where Self: 'this; #[inline] fn encode_field_name(&mut self, cx: &C) -> Result, C::Error> { diff --git a/crates/musli-json/src/en/variant_encoder.rs b/crates/musli-json/src/en/variant_encoder.rs index 697579e8a..c7ca4012d 100644 --- a/crates/musli-json/src/en/variant_encoder.rs +++ b/crates/musli-json/src/en/variant_encoder.rs @@ -1,3 +1,5 @@ +use core::marker::PhantomData; + use musli::en::VariantEncoder; use musli::Context; @@ -6,33 +8,37 @@ use crate::writer::Writer; use super::{JsonEncoder, JsonObjectKeyEncoder}; /// A JSON variant encoder. -pub(crate) struct JsonVariantEncoder { +pub(crate) struct JsonVariantEncoder { writer: W, + _marker: PhantomData, } -impl JsonVariantEncoder +impl JsonVariantEncoder where W: Writer, + C: ?Sized + Context, { #[inline] - pub(super) fn new(cx: &C, mut writer: W) -> Result - where - C: ?Sized + Context, - { + pub(super) fn new(cx: &C, mut writer: W) -> Result { writer.write_byte(cx, b'{')?; - Ok(Self { writer }) + Ok(Self { + writer, + _marker: PhantomData, + }) } } -impl VariantEncoder for JsonVariantEncoder +impl VariantEncoder for JsonVariantEncoder where W: Writer, + C: ?Sized + Context, { + type Cx = C; type Ok = (); - type EncodeTag<'this> = JsonObjectKeyEncoder> + type EncodeTag<'this> = JsonObjectKeyEncoder, C> where Self: 'this; - type EncodeValue<'this> = JsonEncoder> + type EncodeValue<'this> = JsonEncoder, C> where Self: 'this; diff --git a/crates/musli-json/src/parser/integer.rs b/crates/musli-json/src/parser/integer.rs index a9e9e1d93..50dc3e261 100644 --- a/crates/musli-json/src/parser/integer.rs +++ b/crates/musli-json/src/parser/integer.rs @@ -216,10 +216,10 @@ where } /// Implementation to skip over a well-formed JSON number. -pub(crate) fn skip_number<'de, C, P>(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 - C: ?Sized + Context, P: Parser<'de>, + C: ?Sized + Context, { p.skip_whitespace(cx)?; @@ -269,8 +269,8 @@ where pub(crate) fn parse_unsigned_base<'de, T, C, P>(cx: &C, mut p: P) -> Result where T: Unsigned, - C: ?Sized + Context, P: Parser<'de>, + C: ?Sized + Context, { p.skip_whitespace(cx)?; @@ -284,8 +284,8 @@ where pub(crate) fn parse_unsigned_full<'de, T, C, P>(cx: &C, mut p: P) -> Result where T: Unsigned, - C: ?Sized + Context, P: Parser<'de>, + C: ?Sized + Context, { p.skip_whitespace(cx)?; @@ -368,8 +368,8 @@ where pub(crate) fn parse_signed_base<'de, T, C, P>(cx: &C, mut p: P) -> Result where T: Signed, - C: ?Sized + Context, P: Parser<'de>, + C: ?Sized + Context, { p.skip_whitespace(cx)?; @@ -387,8 +387,8 @@ where pub(crate) fn parse_signed_full<'de, T, C, P>(cx: &C, mut p: P) -> Result where T: Signed, - C: ?Sized + Context, P: Parser<'de>, + C: ?Sized + Context, { p.skip_whitespace(cx)?; @@ -406,8 +406,8 @@ where fn decode_unsigned_base<'de, T, C, P>(cx: &C, mut p: P, start: C::Mark) -> Result where T: Unsigned, - C: ?Sized + Context, P: Parser<'de>, + C: ?Sized + Context, { let base = match p.read_byte(cx)? { b'0' => T::ZERO, @@ -438,8 +438,8 @@ fn decode_unsigned_full<'de, T, C, P>( ) -> Result, C::Error> where T: Unsigned, - C: ?Sized + Context, P: Parser<'de>, + C: ?Sized + Context, { let base = decode_unsigned_base(cx, p.borrow_mut(), start)?; @@ -486,10 +486,10 @@ where /// Decode an exponent. #[inline(always)] -fn decode_exponent<'de, C, P>(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 - C: ?Sized + Context, P: Parser<'de>, + C: ?Sized + Context, { let mut is_negative = false; let mut e = 0u32; @@ -520,8 +520,8 @@ where fn digit<'de, T, C, P>(cx: &C, out: T, mut p: P, start: C::Mark) -> Result where T: Unsigned, - C: ?Sized + Context, P: Parser<'de>, + C: ?Sized + Context, { let Some(out) = out.checked_mul10() else { return Err(cx.marked_message(start, IntegerError::IntegerOverflow)); @@ -532,10 +532,10 @@ where /// Decode sequence of zeros. #[inline(always)] -fn decode_zeros<'de, C, P>(cx: &C, mut p: P) -> Result +fn decode_zeros<'de, P, C>(cx: &C, mut p: P) -> Result where - C: ?Sized + Context, P: Parser<'de>, + C: ?Sized + Context, { let mut count = 0i32; diff --git a/crates/musli-json/src/parser/string.rs b/crates/musli-json/src/parser/string.rs index 6bf12aa6e..222b56ece 100644 --- a/crates/musli-json/src/parser/string.rs +++ b/crates/musli-json/src/parser/string.rs @@ -286,10 +286,10 @@ pub(crate) fn decode_hex_val(val: u8) -> Option { } /// Specialized reader implementation from a slice. -pub(crate) fn skip_string<'de, C, P>(cx: &C, mut p: P, validate: bool) -> Result<(), C::Error> +pub(crate) fn skip_string<'de, P, C>(cx: &C, mut p: P, validate: bool) -> Result<(), C::Error> where - C: ?Sized + Context, P: Parser<'de>, + C: ?Sized + Context, { loop { while let Some(b) = p.peek_byte(cx)? { @@ -320,10 +320,10 @@ where /// Parses a JSON escape sequence and appends it into the scratch space. Assumes /// the previous byte read was a backslash. -fn skip_escape<'de, C, P>(cx: &C, mut p: P, validate: bool) -> Result<(), C::Error> +fn skip_escape<'de, P, C>(cx: &C, mut p: P, validate: bool) -> Result<(), C::Error> where - C: ?Sized + Context, P: Parser<'de>, + C: ?Sized + Context, { let start = cx.mark(); let b = p.read_byte(cx)?; diff --git a/crates/musli-macros/src/de.rs b/crates/musli-macros/src/de.rs index b1b5dde72..0cc98c76d 100644 --- a/crates/musli-macros/src/de.rs +++ b/crates/musli-macros/src/de.rs @@ -13,7 +13,6 @@ struct Ctxt<'a> { ctx_var: &'a Ident, decoder_var: &'a Ident, tag_var: &'a Ident, - c_param: &'a syn::Ident, trace: bool, trace_body: bool, } @@ -24,14 +23,12 @@ pub(crate) fn expand_decode_entry(e: Build<'_>) -> Result { let ctx_var = e.cx.ident("ctx"); let root_decoder_var = e.cx.ident("decoder"); let tag_var = e.cx.ident("tag"); - let c_param = e.cx.type_with_span("C", Span::call_site()); let d_param = e.cx.type_with_span("D", Span::call_site()); let cx = Ctxt { ctx_var: &ctx_var, decoder_var: &root_decoder_var, tag_var: &tag_var, - c_param: &c_param, trace: true, trace_body: true, }; @@ -90,10 +87,9 @@ pub(crate) fn expand_decode_entry(e: Build<'_>) -> Result { #[allow(clippy::let_unit_value)] impl #impl_generics #decode_t<#lt, #mode_ident> for #type_ident #type_generics #where_clause { #[inline] - fn decode<#c_param, #d_param>(#ctx_var: &#c_param, #root_decoder_var: #d_param) -> #core_result::Error> + fn decode<#d_param>(#ctx_var: &#d_param::Cx, #root_decoder_var: #d_param) -> #core_result::Error> where - #c_param: ?Sized + #context_t, - #d_param: #decoder_t<#lt, #c_param> + #d_param: #decoder_t<#lt, Mode = #mode_ident>, { #body } @@ -118,7 +114,6 @@ fn decode_enum(cx: &Ctxt<'_>, e: &Build<'_>, en: &Enum) -> Result { ctx_var, tag_var, decoder_var, - c_param, .. } = *cx; @@ -168,11 +163,11 @@ fn decode_enum(cx: &Ctxt<'_>, e: &Build<'_>, en: &Enum) -> Result { let mut fallback = match en.fallback { Some(ident) => { quote! {{ - if !#variant_decoder_t::<#c_param>::skip_value(&mut #variant_decoder_var, #ctx_var)? { + if !#variant_decoder_t::skip_value(&mut #variant_decoder_var, #ctx_var)? { return #result_err(#context_t::invalid_variant_tag(#ctx_var, #type_name, #variant_tag_var)); } - #variant_decoder_t::<#c_param>::end(#variant_decoder_var, #ctx_var)?; + #variant_decoder_t::end(#variant_decoder_var, #ctx_var)?; Self::#ident {} }} } @@ -207,7 +202,7 @@ fn decode_enum(cx: &Ctxt<'_>, e: &Build<'_>, en: &Enum) -> Result { }); decode_tag = quote! { - #decoder_t::<#c_param>::decode_string(#variant_decoder_var, #ctx_var, #visit_owned_fn("a string variant tag", |#ctx_var, string: &str| { + #decoder_t::decode_string(#variant_decoder_var, #ctx_var, #visit_owned_fn("a string variant tag", |#ctx_var, string: &str| { #result_ok(match string { #(#arms,)* string => { @@ -296,11 +291,11 @@ fn decode_enum(cx: &Ctxt<'_>, e: &Build<'_>, en: &Enum) -> Result { #enter let #output_var = { - let #body_decoder_var = #variant_decoder_t::<#c_param>::decode_value(&mut #variant_decoder_var, #ctx_var)?; + let #body_decoder_var = #variant_decoder_t::decode_value(&mut #variant_decoder_var, #ctx_var)?; #decode }; - #variant_decoder_t::<#c_param>::end(#variant_decoder_var, #ctx_var)?; + #variant_decoder_t::end(#variant_decoder_var, #ctx_var)?; #leave #output_var } @@ -323,12 +318,12 @@ fn decode_enum(cx: &Ctxt<'_>, e: &Build<'_>, en: &Enum) -> Result { #output_enum #enter - let mut #variant_decoder_var = #decoder_t::<#c_param>::decode_variant(#decoder_var, #ctx_var)?; + let mut #variant_decoder_var = #decoder_t::decode_variant(#decoder_var, #ctx_var)?; #variant_alloc let #variant_tag_var #name_type = { - let mut #variant_decoder_var = #variant_decoder_t::<#c_param>::decode_tag(&mut #variant_decoder_var, #ctx_var)?; + let mut #variant_decoder_var = #variant_decoder_t::decode_tag(&mut #variant_decoder_var, #ctx_var)?; #decode_tag }; @@ -367,7 +362,7 @@ fn decode_enum(cx: &Ctxt<'_>, e: &Build<'_>, en: &Enum) -> Result { Some(quote! { #tag_pattern => { #enter - let #buffer_decoder_var = #as_decoder_t::<#c_param>::as_decoder(&#buffer_var, #ctx_var)?; + let #buffer_decoder_var = #as_decoder_t::as_decoder(&#buffer_var, #ctx_var)?; let #variant_output_var = #decode; #leave #variant_output_var @@ -398,7 +393,7 @@ fn decode_enum(cx: &Ctxt<'_>, e: &Build<'_>, en: &Enum) -> Result { }); let decode_outcome = quote! { - #decoder_t::<#c_param>::decode_string(decoder, #ctx_var, #visit_owned_fn("a string field tag", |#ctx_var, string: &str| { + #decoder_t::decode_string(decoder, #ctx_var, #visit_owned_fn("a string field tag", |#ctx_var, string: &str| { #result_ok(match string { #field_tag => Outcome::Tag, string => { @@ -415,13 +410,13 @@ fn decode_enum(cx: &Ctxt<'_>, e: &Build<'_>, en: &Enum) -> Result { decode_match = quote! { match #decode_outcome { Outcome::Tag => { - break #struct_field_decoder_t::<#c_param>::decode_field_value(#entry_var, #ctx_var)?; + break #struct_field_decoder_t::decode_field_value(#entry_var, #ctx_var)?; } Outcome::AllocErr => { return #result_err(#context_t::alloc_failed(#ctx_var)); } Outcome::Err => { - if !#struct_field_decoder_t::<#c_param>::skip_field_value(#entry_var, #ctx_var)? { + if !#struct_field_decoder_t::skip_field_value(#entry_var, #ctx_var)? { return #result_err(#context_t::invalid_field_string_tag(#ctx_var, #type_name, #field_alloc_var)); } } @@ -434,10 +429,10 @@ fn decode_enum(cx: &Ctxt<'_>, e: &Build<'_>, en: &Enum) -> Result { decode_match = quote! { match #decode_t_decode(#ctx_var, decoder)? { #field_tag => { - break #struct_field_decoder_t::<#c_param>::decode_field_value(#entry_var, #ctx_var)?; + break #struct_field_decoder_t::decode_field_value(#entry_var, #ctx_var)?; } field => { - if !#struct_field_decoder_t::<#c_param>::skip_field_value(#entry_var, #ctx_var)? { + if !#struct_field_decoder_t::skip_field_value(#entry_var, #ctx_var)? { return #result_err(#context_t::invalid_field_tag(#ctx_var, #type_name, field)); } } @@ -463,17 +458,17 @@ fn decode_enum(cx: &Ctxt<'_>, e: &Build<'_>, en: &Enum) -> Result { #outcome_enum #enter - let #buffer_var = #decoder_t::<#c_param>::decode_buffer(#decoder_var, #ctx_var)?; - let st = #as_decoder_t::<#c_param>::as_decoder(&#buffer_var, #ctx_var)?; - let mut st = #decoder_t::<#c_param>::decode_struct(st, #ctx_var, None)?; + let #buffer_var = #decoder_t::decode_buffer(#decoder_var, #ctx_var)?; + let st = #as_decoder_t::as_decoder(&#buffer_var, #ctx_var)?; + let mut st = #decoder_t::decode_struct(st, #ctx_var, None)?; let #variant_tag_var #name_type = { let #variant_decoder_var = loop { - let #option_some(mut #entry_var) = #struct_decoder_t::<#c_param>::decode_field(&mut st, #ctx_var)? else { + let #option_some(mut #entry_var) = #struct_decoder_t::decode_field(&mut st, #ctx_var)? else { return #result_err(#context_t::missing_variant_field(#ctx_var, #type_name, #field_tag)); }; - let decoder = #struct_field_decoder_t::<#c_param>::decode_field_name(&mut #entry_var, #ctx_var)?; + let decoder = #struct_field_decoder_t::decode_field_name(&mut #entry_var, #ctx_var)?; #field_alloc #decode_match @@ -483,7 +478,7 @@ fn decode_enum(cx: &Ctxt<'_>, e: &Build<'_>, en: &Enum) -> Result { #decode_tag }; - #struct_decoder_t::<#c_param>::end(st, #ctx_var)?; + #struct_decoder_t::end(st, #ctx_var)?; let #output_var = match #variant_tag_var { #(#patterns,)* @@ -553,7 +548,7 @@ fn decode_enum(cx: &Ctxt<'_>, e: &Build<'_>, en: &Enum) -> Result { }); decode_match = quote! { - let outcome = #decoder_t::<#c_param>::decode_string(decoder, #ctx_var, #visit_owned_fn("a string field tag", |#ctx_var, string: &str| { + let outcome = #decoder_t::decode_string(decoder, #ctx_var, #visit_owned_fn("a string field tag", |#ctx_var, string: &str| { #result_ok(match string { #tag => Outcome::Tag, #content => Outcome::Content, @@ -569,7 +564,7 @@ fn decode_enum(cx: &Ctxt<'_>, e: &Build<'_>, en: &Enum) -> Result { match outcome { Outcome::Tag => { - let #variant_decoder_var = #struct_field_decoder_t::<#c_param>::decode_field_value(#entry_var, #ctx_var)?; + let #variant_decoder_var = #struct_field_decoder_t::decode_field_value(#entry_var, #ctx_var)?; #variant_alloc #tag_var = #option_some(#decode_tag); } @@ -578,7 +573,7 @@ fn decode_enum(cx: &Ctxt<'_>, e: &Build<'_>, en: &Enum) -> Result { return #result_err(#context_t::invalid_field_tag(#ctx_var, #type_name, #tag)); }; - let #body_decoder_var = #struct_field_decoder_t::<#c_param>::decode_field_value(#entry_var, #ctx_var)?; + let #body_decoder_var = #struct_field_decoder_t::decode_field_value(#entry_var, #ctx_var)?; break #result_ok(match #variant_tag_var { #(#patterns,)* @@ -589,7 +584,7 @@ fn decode_enum(cx: &Ctxt<'_>, e: &Build<'_>, en: &Enum) -> Result { return #result_err(#context_t::alloc_failed(#ctx_var)); } Outcome::Err => { - if !#struct_field_decoder_t::<#c_param>::skip_field_value(#entry_var, #ctx_var)? { + if !#struct_field_decoder_t::skip_field_value(#entry_var, #ctx_var)? { return #result_err(#context_t::invalid_field_string_tag(#ctx_var, #type_name, #field_alloc_var)); } } @@ -602,7 +597,7 @@ fn decode_enum(cx: &Ctxt<'_>, e: &Build<'_>, en: &Enum) -> Result { decode_match = quote! { match #decode_t_decode(#ctx_var, decoder)? { #tag => { - let #variant_decoder_var = #struct_field_decoder_t::<#c_param>::decode_field_value(#entry_var, #ctx_var)?; + let #variant_decoder_var = #struct_field_decoder_t::decode_field_value(#entry_var, #ctx_var)?; #variant_alloc #tag_var = #option_some(#decode_tag); } @@ -611,7 +606,7 @@ fn decode_enum(cx: &Ctxt<'_>, e: &Build<'_>, en: &Enum) -> Result { return #result_err(#context_t::invalid_field_tag(#ctx_var, #type_name, #tag)); }; - let #body_decoder_var = #struct_field_decoder_t::<#c_param>::decode_field_value(#entry_var, #ctx_var)?; + let #body_decoder_var = #struct_field_decoder_t::decode_field_value(#entry_var, #ctx_var)?; break #result_ok(match #variant_tag_var { #(#patterns,)* @@ -619,7 +614,7 @@ fn decode_enum(cx: &Ctxt<'_>, e: &Build<'_>, en: &Enum) -> Result { }); } field => { - if !#struct_field_decoder_t::<#c_param>::skip_field_value(#entry_var, #ctx_var)? { + if !#struct_field_decoder_t::skip_field_value(#entry_var, #ctx_var)? { return #result_err(#context_t::invalid_field_tag(#ctx_var, #type_name, field)); } } @@ -645,21 +640,21 @@ fn decode_enum(cx: &Ctxt<'_>, e: &Build<'_>, en: &Enum) -> Result { #outcome_enum #enter - let mut #struct_decoder_var = #decoder_t::<#c_param>::decode_struct(#decoder_var, #ctx_var, None)?; + let mut #struct_decoder_var = #decoder_t::decode_struct(#decoder_var, #ctx_var, None)?; let mut #tag_var = #option_none; let #output_var = loop { - let #option_some(mut #entry_var) = #struct_decoder_t::<#c_param>::decode_field(&mut #struct_decoder_var, #ctx_var)? else { + let #option_some(mut #entry_var) = #struct_decoder_t::decode_field(&mut #struct_decoder_var, #ctx_var)? else { return #result_err(#context_t::invalid_field_tag(#ctx_var, #type_name, "other")); }; - let decoder = #struct_field_decoder_t::<#c_param>::decode_field_name(&mut #entry_var, #ctx_var)?; + let decoder = #struct_field_decoder_t::decode_field_name(&mut #entry_var, #ctx_var)?; #field_alloc #decode_match }; - #struct_decoder_t::<#c_param>::end(#struct_decoder_var, #ctx_var)?; + #struct_decoder_t::end(#struct_decoder_var, #ctx_var)?; #leave #output_var }}) @@ -701,7 +696,6 @@ fn decode_tagged( ctx_var, decoder_var, tag_var, - c_param, .. } = *cx; @@ -820,7 +814,7 @@ fn decode_tagged( }); decode_tag = quote! { - #decoder_t::<#c_param>::decode_string(#struct_decoder_var, #ctx_var, #visit_owned_fn("a string variant tag", |#ctx_var, string: &str| { + #decoder_t::decode_string(#struct_decoder_var, #ctx_var, #visit_owned_fn("a string variant tag", |#ctx_var, string: &str| { #result_ok(match string { #(#patterns,)* string => { @@ -871,7 +865,7 @@ fn decode_tagged( } let skip_field = quote! { - #struct_field_decoder_t::<#c_param>::skip_field_value(#struct_decoder_var, #ctx_var)? + #struct_field_decoder_t::skip_field_value(#struct_decoder_var, #ctx_var)? }; let unsupported = match variant_tag { @@ -919,7 +913,7 @@ fn decode_tagged( quote! { #pattern_var => { #enter - let #struct_decoder_var = #struct_field_decoder_t::<#c_param>::decode_field_value(#struct_decoder_var, #ctx_var)?; + let #struct_decoder_var = #struct_field_decoder_t::decode_field_value(#struct_decoder_var, #ctx_var)?; #decode #leave } @@ -934,7 +928,7 @@ fn decode_tagged( tag_stmt.init = Some(syn::LocalInit { eq_token: ::default(), expr: Box::new(syn::Expr::Verbatim(quote! {{ - let #struct_decoder_var = #struct_field_decoder_t::<#c_param>::decode_field_name(&mut #struct_decoder_var, #ctx_var)?; + let #struct_decoder_var = #struct_field_decoder_t::decode_field_name(&mut #struct_decoder_var, #ctx_var)?; #decode_tag }})), diverge: None, @@ -965,15 +959,15 @@ fn decode_tagged( #(#decls)* #enter - let mut type_decoder = #decoder_t::<#c_param>::decode_struct(#decoder_var, #ctx_var, Some(#fields_len))?; + let mut type_decoder = #decoder_t::decode_struct(#decoder_var, #ctx_var, Some(#fields_len))?; - while let #option_some(mut #struct_decoder_var) = #struct_decoder_t::<#c_param>::decode_field(&mut type_decoder, #ctx_var)? { + while let #option_some(mut #struct_decoder_var) = #struct_decoder_t::decode_field(&mut type_decoder, #ctx_var)? { #field_alloc #tag_stmt #body } - #struct_decoder_t::<#c_param>::end(type_decoder, #ctx_var)?; + #struct_decoder_t::end(type_decoder, #ctx_var)?; #leave #path { #assigns } }}) @@ -1030,7 +1024,6 @@ fn decode_packed(cx: &Ctxt<'_>, e: &Build<'_>, st_: &Body<'_>) -> Result, e: &Build<'_>, st_: &Body<'_>) -> Result::decode_next(&mut unpack, #ctx_var)?; + let field_decoder = #pack_decoder_t::decode_next(&mut unpack, #ctx_var)?; #decode_path(#ctx_var, field_decoder)? } }); @@ -1082,9 +1075,9 @@ fn decode_packed(cx: &Ctxt<'_>, e: &Build<'_>, st_: &Body<'_>) -> Result::decode_pack(#decoder_var, #ctx_var)?; + let mut unpack = #decoder_t::decode_pack(#decoder_var, #ctx_var)?; let #output_var = #path { #(#assign),* }; - #pack_decoder_t::<#c_param>::end(unpack, #ctx_var)?; + #pack_decoder_t::end(unpack, #ctx_var)?; #leave #output_var }}) diff --git a/crates/musli-macros/src/en.rs b/crates/musli-macros/src/en.rs index add37ccc3..54f2ac2a3 100644 --- a/crates/musli-macros/src/en.rs +++ b/crates/musli-macros/src/en.rs @@ -11,7 +11,6 @@ use crate::internals::tokens::Tokens; struct Ctxt<'a> { ctx_var: &'a syn::Ident, encoder_var: &'a syn::Ident, - c_param: &'a syn::Ident, trace: bool, } @@ -22,18 +21,15 @@ pub(crate) fn expand_insert_entry(e: Build<'_>) -> Result { let encoder_var = e.cx.ident("encoder"); let ctx_var = e.cx.ident("ctx"); - let c_param = e.cx.type_with_span("C", Span::call_site()); let e_param = e.cx.type_with_span("E", Span::call_site()); let cx = Ctxt { ctx_var: &ctx_var, encoder_var: &encoder_var, - c_param: &c_param, trace: true, }; let Tokens { - context_t, core_result, encode_t, encoder_t, @@ -68,10 +64,9 @@ pub(crate) fn expand_insert_entry(e: Build<'_>) -> Result { #[automatically_derived] impl #impl_generics #encode_t<#mode_ident> for #type_ident #type_generics #where_clause { #[inline] - fn encode<#c_param, #e_param>(&self, #ctx_var: &#c_param, #encoder_var: #e_param) -> #core_result<<#e_param as #encoder_t<#c_param>>::Ok, <#c_param as #context_t>::Error> + fn encode<#e_param>(&self, #ctx_var: &#e_param::Cx, #encoder_var: #e_param) -> #core_result<<#e_param as #encoder_t>::Ok, <#e_param as #encoder_t>::Error> where - #c_param: ?Sized + #context_t, - #e_param: #encoder_t<#c_param>, + #e_param: #encoder_t, { #body } @@ -84,7 +79,6 @@ fn encode_struct(cx: &Ctxt<'_>, e: &Build<'_>, st: &Body<'_>) -> Result, e: &Build<'_>, st: &Body<'_>) -> Result::encode_struct_fn(#encoder_var, #ctx_var, #len, |#encoder_var| { + let #output_var = #encoder_t::encode_struct_fn(#encoder_var, #ctx_var, #len, |#encoder_var| { #(#encoders)* #result_ok(()) })?; @@ -151,7 +145,7 @@ fn encode_struct(cx: &Ctxt<'_>, e: &Build<'_>, st: &Body<'_>) -> Result::encode_pack_fn(#encoder_var, #ctx_var, |#pack_var| { + let #output_var = #encoder_t::encode_pack_fn(#encoder_var, #ctx_var, |#pack_var| { #(#decls)* #(#encoders)* #result_ok(()) @@ -179,7 +173,6 @@ fn insert_fields( let Ctxt { ctx_var, encoder_var, - c_param, .. } = *cx; @@ -239,10 +232,10 @@ fn insert_fields( encode = quote! { #enter - #struct_encoder_t::<#c_param>::encode_field_fn(#encoder_var, #ctx_var, |#pair_encoder_var| { - let #field_encoder_var = #struct_field_encoder_t::<#c_param>::encode_field_name(#pair_encoder_var, #ctx_var)?; + #struct_encoder_t::encode_field_fn(#encoder_var, #ctx_var, |#pair_encoder_var| { + let #field_encoder_var = #struct_field_encoder_t::encode_field_name(#pair_encoder_var, #ctx_var)?; #encode_t_encode(&#tag, #ctx_var, #field_encoder_var)?; - let #value_encoder_var = #struct_field_encoder_t::<#c_param>::encode_field_value(#pair_encoder_var, #ctx_var)?; + let #value_encoder_var = #struct_field_encoder_t::encode_field_value(#pair_encoder_var, #ctx_var)?; #encode_path(#access, #ctx_var, #value_encoder_var)?; #result_ok(()) })?; @@ -253,7 +246,7 @@ fn insert_fields( Packing::Packed => { encode = quote! { #enter - let #sequence_decoder_next_var = #sequence_encoder_t::<#c_param>::encode_next(#pack_var, #ctx_var)?; + let #sequence_decoder_next_var = #sequence_encoder_t::encode_next(#pack_var, #ctx_var)?; #encode_path(#access, #ctx_var, #sequence_decoder_next_var)?; #leave }; @@ -336,7 +329,6 @@ fn encode_variant( let Ctxt { ctx_var, encoder_var, - c_param, .. } = *cx; @@ -371,7 +363,7 @@ fn encode_variant( let decls = tests.iter().map(|t| &t.decl); encode = quote! {{ - #encoder_t::<#c_param>::encode_pack_fn(#encoder_var, #ctx_var, |#pack_var| { + #encoder_t::encode_pack_fn(#encoder_var, #ctx_var, |#pack_var| { #(#decls)* #(#encoders)* #result_ok(()) @@ -383,7 +375,7 @@ fn encode_variant( let len = length_test(v.st.fields.len(), &tests); encode = quote! {{ - #encoder_t::<#c_param>::encode_struct_fn(#encoder_var, #ctx_var, #len, |#encoder_var| { + #encoder_t::encode_struct_fn(#encoder_var, #ctx_var, #len, |#encoder_var| { #(#decls)* #(#encoders)* #result_ok(()) @@ -399,11 +391,11 @@ fn encode_variant( let tag_encoder = b.cx.ident("tag_encoder"); encode = quote! {{ - #encoder_t::<#c_param>::encode_variant_fn(#encoder_var, #ctx_var, |#variant_encoder| { - let #tag_encoder = #variant_encoder_t::<#c_param>::encode_tag(#variant_encoder, #ctx_var)?; + #encoder_t::encode_variant_fn(#encoder_var, #ctx_var, |#variant_encoder| { + let #tag_encoder = #variant_encoder_t::encode_tag(#variant_encoder, #ctx_var)?; #encode_t_encode(&#tag, #ctx_var, #tag_encoder)?; - let #encoder_var = #variant_encoder_t::<#c_param>::encode_value(#variant_encoder, #ctx_var)?; + let #encoder_var = #variant_encoder_t::encode_value(#variant_encoder, #ctx_var)?; #encode; #result_ok(()) })? @@ -420,8 +412,8 @@ fn encode_variant( let decls = tests.iter().map(|t| &t.decl); encode = quote! {{ - #encoder_t::<#c_param>::encode_struct_fn(#encoder_var, #ctx_var, 0, |#encoder_var| { - #struct_encoder_t::<#c_param>::insert_field(#encoder_var, #ctx_var, #field_tag, #tag)?; + #encoder_t::encode_struct_fn(#encoder_var, #ctx_var, 0, |#encoder_var| { + #struct_encoder_t::insert_field(#encoder_var, #ctx_var, #field_tag, #tag)?; #(#decls)* #(#encoders)* #result_ok(()) @@ -447,16 +439,16 @@ fn encode_variant( let content_tag = b.cx.ident("content_tag"); encode = quote! {{ - #encoder_t::<#c_param>::encode_struct_fn(#encoder_var, #ctx_var, 2, |#struct_encoder| { - #struct_encoder_t::<#c_param>::insert_field(#struct_encoder, #ctx_var, &#field_tag, #tag)?; + #encoder_t::encode_struct_fn(#encoder_var, #ctx_var, 2, |#struct_encoder| { + #struct_encoder_t::insert_field(#struct_encoder, #ctx_var, &#field_tag, #tag)?; - #struct_encoder_t::<#c_param>::encode_field_fn(#struct_encoder, #ctx_var, |#pair| { - let #content_tag = #struct_field_encoder_t::<#c_param>::encode_field_name(#pair, #ctx_var)?; + #struct_encoder_t::encode_field_fn(#struct_encoder, #ctx_var, |#pair| { + let #content_tag = #struct_field_encoder_t::encode_field_name(#pair, #ctx_var)?; #encode_t_encode(&#content, #ctx_var, #content_tag)?; - let #content_struct = #struct_field_encoder_t::<#c_param>::encode_field_value(#pair, #ctx_var)?; + let #content_struct = #struct_field_encoder_t::encode_field_value(#pair, #ctx_var)?; - #encoder_t::<#c_param>::encode_struct_fn(#content_struct, #ctx_var, #len, |#encoder_var| { + #encoder_t::encode_struct_fn(#content_struct, #ctx_var, #len, |#encoder_var| { #(#decls)* #(#encoders)* #result_ok(()) diff --git a/crates/musli-macros/src/lib.rs b/crates/musli-macros/src/lib.rs index 1607f0881..93be0f7a1 100644 --- a/crates/musli-macros/src/lib.rs +++ b/crates/musli-macros/src/lib.rs @@ -81,8 +81,9 @@ pub fn decoder(attr: TokenStream, input: TokenStream) -> TokenStream { match input.expand( "decoder", types::DECODER_TYPES, - &[], + None, "__UseMusliDecoderAttributeMacro", + types::Kind::SelfCx, ) { Ok(tokens) => tokens.into(), Err(err) => err.to_compile_error().into(), @@ -105,8 +106,9 @@ pub fn map_decoder(attr: TokenStream, input: TokenStream) -> TokenStream { match input.expand( "map_decoder", types::MAP_DECODER_TYPES, - &[], + None, "__UseMusliMapDecoderAttributeMacro", + types::Kind::SelfCx, ) { Ok(tokens) => tokens.into(), Err(err) => err.to_compile_error().into(), @@ -129,8 +131,9 @@ pub fn struct_decoder(attr: TokenStream, input: TokenStream) -> TokenStream { match input.expand( "struct_decoder", types::STRUCT_DECODER_TYPES, - &[], + None, "__UseMusliStructDecoderAttributeMacro", + types::Kind::SelfCx, ) { Ok(tokens) => tokens.into(), Err(err) => err.to_compile_error().into(), @@ -153,8 +156,9 @@ pub fn encoder(attr: TokenStream, input: TokenStream) -> TokenStream { match input.expand( "encoder", types::ENCODER_TYPES, - &["Ok"], + Some("Ok"), "__UseMusliEncoderAttributeMacro", + types::Kind::SelfCx, ) { Ok(tokens) => tokens.into(), Err(err) => err.to_compile_error().into(), @@ -177,8 +181,9 @@ pub fn visitor(attr: TokenStream, input: TokenStream) -> TokenStream { match input.expand( "visitor", types::VISITOR_TYPES, - &["Ok"], + Some("Ok"), "__UseMusliVisitorAttributeMacro", + types::Kind::GenericCx, ) { Ok(tokens) => tokens.into(), Err(err) => err.to_compile_error().into(), diff --git a/crates/musli-macros/src/types.rs b/crates/musli-macros/src/types.rs index 6fd828f05..8f4f5dea3 100644 --- a/crates/musli-macros/src/types.rs +++ b/crates/musli-macros/src/types.rs @@ -6,6 +6,8 @@ use syn::parse::Parse; use syn::punctuated::Punctuated; use syn::Token; +const U_PARAM: &str = "__U"; + #[derive(Debug, Clone, Copy)] pub(super) enum Ty { /// `str`. @@ -16,13 +18,20 @@ pub(super) enum Ty { #[derive(Debug, Clone, Copy)] pub(super) enum Extra { + /// `type Type = Never;` None, + /// `type Error = ::Error;` + Error, + /// `type Mode = ::Mode;` + Mode, Context, This, Visitor(Ty), } pub(super) const ENCODER_TYPES: &[(&str, Extra)] = &[ + ("Error", Extra::Error), + ("Mode", Extra::Mode), ("WithContext", Extra::Context), ("EncodeSome", Extra::None), ("EncodePack", Extra::This), @@ -37,6 +46,8 @@ pub(super) const ENCODER_TYPES: &[(&str, Extra)] = &[ ]; pub(super) const DECODER_TYPES: &[(&str, Extra)] = &[ + ("Error", Extra::Error), + ("Mode", Extra::Mode), ("WithContext", Extra::Context), ("DecodeBuffer", Extra::None), ("DecodeSome", Extra::None), @@ -58,6 +69,12 @@ pub(super) const VISITOR_TYPES: &[(&str, Extra)] = &[ ("Number", Extra::None), ]; +#[derive(Clone, Copy)] +pub(super) enum Kind { + SelfCx, + GenericCx, +} + pub(super) struct Types { item_impl: syn::ItemImpl, } @@ -76,8 +93,9 @@ impl Types { mut self, what: &str, types: &[(&str, Extra)], - arguments: &[&str], + argument: Option<&str>, hint: &str, + kind: Kind, ) -> syn::Result { let mut missing = types .iter() @@ -139,16 +157,29 @@ impl Types { impl_type.ty = syn::Type::Path(syn::TypePath { qself: None, - path: self.never_type(arguments, extra)?, + path: self.never_type(argument, extra, kind)?, }); self.item_impl.items.push(syn::ImplItem::Type(impl_type)); } for (ident, extra) in missing { - let generics = match extra { + let ty; + let generics; + + match extra { + Extra::Mode => { + ty = syn::parse_quote!(::Mode); + + generics = syn::Generics::default(); + } + Extra::Error => { + ty = syn::parse_quote!(::Error); + + generics = syn::Generics::default(); + } Extra::Context => { - let c_param = syn::Ident::new("__C", Span::call_site()); + let u_param = syn::Ident::new(U_PARAM, Span::call_site()); let mut where_clause = syn::WhereClause { where_token: ::default(), @@ -157,36 +188,32 @@ impl Types { where_clause .predicates - .push(syn::parse_quote!(#c_param: ::musli::context::Context)); + .push(syn::parse_quote!(#u_param: ::musli::context::Context)); let mut params = Punctuated::default(); params.push(syn::GenericParam::Type(syn::TypeParam { attrs: Vec::new(), - ident: c_param, + ident: u_param, colon_token: None, bounds: Punctuated::default(), eq_token: None, default: None, })); - syn::Generics { + ty = syn::Type::Path(syn::TypePath { + qself: None, + path: self.never_type(argument, extra, kind)?, + }); + + generics = syn::Generics { lt_token: Some(::default()), params, gt_token: Some(]>::default()), where_clause: Some(where_clause), - } + }; } Extra::This => { - let mut it = self.item_impl.generics.type_params(); - - let Some(syn::TypeParam { ident: c_param, .. }) = it.next() else { - return Err(syn::Error::new_spanned( - &self.item_impl.generics, - "Missing generic parameter in associated type (usually `C`)", - )); - }; - let this_lifetime = syn::Lifetime::new("'this", Span::call_site()); let mut where_clause = syn::WhereClause { @@ -196,7 +223,7 @@ impl Types { where_clause .predicates - .push(syn::parse_quote!(#c_param: #this_lifetime)); + .push(syn::parse_quote!(Self::Cx: #this_lifetime)); let mut params = Punctuated::default(); @@ -207,17 +234,27 @@ impl Types { bounds: Punctuated::default(), })); - syn::Generics { + ty = syn::Type::Path(syn::TypePath { + qself: None, + path: self.never_type(argument, extra, kind)?, + }); + + generics = syn::Generics { lt_token: Some(::default()), params, gt_token: Some(]>::default()), where_clause: Some(where_clause), - } + }; } - _ => syn::Generics::default(), - }; + _ => { + ty = syn::Type::Path(syn::TypePath { + qself: None, + path: self.never_type(argument, extra, kind)?, + }); - let never = self.never_type(arguments, extra)?; + generics = syn::Generics::default(); + } + }; let ty = syn::ImplItemType { attrs: Vec::new(), @@ -227,10 +264,7 @@ impl Types { ident, generics, eq_token: ::default(), - ty: syn::Type::Path(syn::TypePath { - qself: None, - path: never.clone(), - }), + ty, semi_token: ::default(), }; @@ -257,7 +291,12 @@ impl Types { Ok(self.item_impl.into_token_stream()) } - fn never_type(&self, arguments: &[&str], extra: Extra) -> syn::Result { + fn never_type( + &self, + argument: Option<&str>, + extra: Extra, + kind: Kind, + ) -> syn::Result { let mut never = syn::Path { leading_colon: None, segments: Punctuated::default(), @@ -277,15 +316,17 @@ impl Types { let mut args = Punctuated::default(); - for &arg in arguments { + if let Some(arg) = argument { args.push(syn::GenericArgument::Type(syn::Type::Path(syn::TypePath { qself: None, path: self_type(arg), }))); + } else { + args.push(syn::parse_quote!(())); } - if let Extra::Visitor(ty) = extra { - match ty { + match extra { + Extra::Visitor(ty) => match ty { Ty::Str => { args.push(syn::GenericArgument::Type(syn::Type::Path(syn::TypePath { qself: None, @@ -313,7 +354,18 @@ impl Types { }, ))); } + }, + Extra::Context => { + let u_param = syn::Ident::new(U_PARAM, Span::call_site()); + args.push(syn::parse_quote!(#u_param)); } + Extra::None | Extra::This => match kind { + Kind::SelfCx => { + args.push(syn::parse_quote!(Self::Cx)); + } + Kind::GenericCx => {} + }, + _ => {} } if !args.is_empty() { diff --git a/crates/musli-serde/src/deserializer.rs b/crates/musli-serde/src/deserializer.rs index dbb32e126..c292f159e 100644 --- a/crates/musli-serde/src/deserializer.rs +++ b/crates/musli-serde/src/deserializer.rs @@ -10,25 +10,30 @@ use serde::de; #[cfg(feature = "alloc")] use alloc::string::String; -pub struct Deserializer<'a, C: ?Sized, D> { - cx: &'a C, +pub struct Deserializer<'de, 'a, D> +where + D: Decoder<'de>, +{ + cx: &'a D::Cx, decoder: D, } -impl<'a, C: ?Sized, D> Deserializer<'a, C, D> { +impl<'de, 'a, D> Deserializer<'de, 'a, D> +where + D: Decoder<'de>, +{ /// Construct a new deserializer out of a decoder. - pub fn new(cx: &'a C, decoder: D) -> Self { + pub fn new(cx: &'a D::Cx, decoder: D) -> Self { Self { cx, decoder } } } -impl<'de, 'a, C, D> de::Deserializer<'de> for Deserializer<'a, C, D> +impl<'de, 'a, D> de::Deserializer<'de> for Deserializer<'de, 'a, D> where - C: ?Sized + Context, - C::Error: de::Error, - D: Decoder<'de, C>, + D: Decoder<'de>, + ::Error: de::Error, { - type Error = C::Error; + type Error = ::Error; #[inline] fn deserialize_any(self, visitor: V) -> Result @@ -342,14 +347,20 @@ where } } -struct TupleAccess<'a, C: ?Sized, D> { - cx: &'a C, +struct TupleAccess<'de, 'a, D> +where + D: PackDecoder<'de>, +{ + cx: &'a D::Cx, decoder: &'a mut D, remaining: usize, } -impl<'a, C: ?Sized, D> TupleAccess<'a, C, D> { - fn new(cx: &'a C, decoder: &'a mut D, len: usize) -> Self { +impl<'de, 'a, D> TupleAccess<'de, 'a, D> +where + D: PackDecoder<'de>, +{ + fn new(cx: &'a D::Cx, decoder: &'a mut D, len: usize) -> Self { TupleAccess { cx, decoder, @@ -358,13 +369,12 @@ impl<'a, C: ?Sized, D> TupleAccess<'a, C, D> { } } -impl<'de, 'a, C, D> de::SeqAccess<'de> for TupleAccess<'a, C, D> +impl<'de, 'a, D> de::SeqAccess<'de> for TupleAccess<'de, 'a, D> where - C: ?Sized + Context, - C::Error: de::Error, - D: PackDecoder<'de, C>, + D: PackDecoder<'de>, + ::Error: de::Error, { - type Error = C::Error; + type Error = ::Error; #[inline] fn next_element_seed(&mut self, seed: T) -> Result, Self::Error> @@ -387,15 +397,21 @@ where Some(self.remaining) } } -struct StructAccess<'a, C: ?Sized, D> { - cx: &'a C, +struct StructAccess<'de, 'a, D> +where + D: StructFieldsDecoder<'de>, +{ + cx: &'a D::Cx, decoder: &'a mut D, remaining: usize, } -impl<'a, C: ?Sized, D> StructAccess<'a, C, D> { +impl<'de, 'a, D> StructAccess<'de, 'a, D> +where + D: StructFieldsDecoder<'de>, +{ #[inline] - fn new(cx: &'a C, decoder: &'a mut D, fields: &'static [&'static str]) -> Self { + fn new(cx: &'a D::Cx, decoder: &'a mut D, fields: &'static [&'static str]) -> Self { StructAccess { cx, decoder, @@ -404,13 +420,12 @@ impl<'a, C: ?Sized, D> StructAccess<'a, C, D> { } } -impl<'de, 'a, C, D> de::MapAccess<'de> for StructAccess<'a, C, D> +impl<'de, 'a, D> de::MapAccess<'de> for StructAccess<'de, 'a, D> where - C: ?Sized + Context, - C::Error: de::Error, - D: StructFieldsDecoder<'de, C>, + D: StructFieldsDecoder<'de>, + ::Error: de::Error, { - type Error = C::Error; + type Error = ::Error; #[inline] fn next_key_seed(&mut self, seed: K) -> Result, Self::Error> @@ -484,24 +499,29 @@ where } } -struct SeqAccess<'a, C: ?Sized, D> { - cx: &'a C, +struct SeqAccess<'de, 'a, D> +where + D: SequenceDecoder<'de>, +{ + cx: &'a D::Cx, decoder: &'a mut D, } -impl<'a, C: ?Sized, D> SeqAccess<'a, C, D> { - fn new(cx: &'a C, decoder: &'a mut D) -> Self { +impl<'de, 'a, D> SeqAccess<'de, 'a, D> +where + D: SequenceDecoder<'de>, +{ + fn new(cx: &'a D::Cx, decoder: &'a mut D) -> Self { Self { cx, decoder } } } -impl<'de, 'a, C, D> de::SeqAccess<'de> for SeqAccess<'a, C, D> +impl<'de, 'a, D> de::SeqAccess<'de> for SeqAccess<'de, 'a, D> where - C: ?Sized + Context, - C::Error: de::Error, - D: SequenceDecoder<'de, C>, + D: SequenceDecoder<'de>, + ::Error: de::Error, { - type Error = C::Error; + type Error = ::Error; #[inline] fn next_element_seed(&mut self, seed: T) -> Result, Self::Error> @@ -525,24 +545,29 @@ where } } -struct MapAccess<'a, C: ?Sized, D: ?Sized> { - cx: &'a C, +struct MapAccess<'de, 'a, D: ?Sized> +where + D: MapEntriesDecoder<'de>, +{ + cx: &'a D::Cx, decoder: &'a mut D, } -impl<'a, C: ?Sized, D: ?Sized> MapAccess<'a, C, D> { - fn new(cx: &'a C, decoder: &'a mut D) -> Self { +impl<'de, 'a, D: ?Sized> MapAccess<'de, 'a, D> +where + D: MapEntriesDecoder<'de>, +{ + fn new(cx: &'a D::Cx, decoder: &'a mut D) -> Self { Self { cx, decoder } } } -impl<'de, 'a, C, D: ?Sized> de::MapAccess<'de> for MapAccess<'a, C, D> +impl<'de, 'a, D: ?Sized> de::MapAccess<'de> for MapAccess<'de, 'a, D> where - C: ?Sized + Context, - C::Error: de::Error, - D: MapEntriesDecoder<'de, C>, + D: MapEntriesDecoder<'de>, + ::Error: de::Error, { - type Error = C::Error; + type Error = ::Error; #[inline] fn next_key_seed(&mut self, seed: K) -> Result, Self::Error> @@ -632,27 +657,27 @@ where } #[inline] - fn visit_u8(self, _: &C, v: u8) -> Result::Error> { + fn visit_u8(self, _: &C, v: u8) -> Result { self.visitor.visit_u8(v) } #[inline] - fn visit_u16(self, _: &C, v: u16) -> Result::Error> { + fn visit_u16(self, _: &C, v: u16) -> Result { self.visitor.visit_u16(v) } #[inline] - fn visit_u32(self, _: &C, v: u32) -> Result::Error> { + fn visit_u32(self, _: &C, v: u32) -> Result { self.visitor.visit_u32(v) } #[inline] - fn visit_u64(self, _: &C, v: u64) -> Result::Error> { + fn visit_u64(self, _: &C, v: u64) -> Result { self.visitor.visit_u64(v) } #[inline] - fn visit_u128(self, _: &C, v: u128) -> Result::Error> { + 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) { @@ -663,27 +688,27 @@ where } #[inline] - fn visit_i8(self, _: &C, v: i8) -> Result::Error> { + fn visit_i8(self, _: &C, v: i8) -> Result { self.visitor.visit_i8(v) } #[inline] - fn visit_i16(self, _: &C, v: i16) -> Result::Error> { + fn visit_i16(self, _: &C, v: i16) -> Result { self.visitor.visit_i16(v) } #[inline] - fn visit_i32(self, _: &C, v: i32) -> Result::Error> { + fn visit_i32(self, _: &C, v: i32) -> Result { self.visitor.visit_i32(v) } #[inline] - fn visit_i64(self, _: &C, v: i64) -> Result::Error> { + fn visit_i64(self, _: &C, v: i64) -> Result { self.visitor.visit_i64(v) } #[inline] - fn visit_i128(self, _: &C, v: i128) -> Result::Error> { + 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) { @@ -694,12 +719,12 @@ where } #[inline] - fn visit_f32(self, _: &C, v: f32) -> Result::Error> { + fn visit_f32(self, _: &C, v: f32) -> Result { self.visitor.visit_f32(v) } #[inline] - fn visit_f64(self, _: &C, v: f64) -> Result::Error> { + fn visit_f64(self, _: &C, v: f64) -> Result { self.visitor.visit_f64(v) } @@ -722,29 +747,34 @@ where } #[inline] - fn visit_bytes(self, _: &C, v: &'de [u8]) -> Result::Error> { + fn visit_bytes(self, _: &C, v: &'de [u8]) -> Result { self.visitor.visit_bytes(v) } } -struct EnumAccess<'a, C: ?Sized, D> { - cx: &'a C, +struct EnumAccess<'de, 'a, D> +where + D: VariantDecoder<'de>, +{ + cx: &'a D::Cx, decoder: D, } -impl<'a, C: ?Sized, D> EnumAccess<'a, C, D> { - fn new(cx: &'a C, decoder: D) -> Self { +impl<'de, 'a, D> EnumAccess<'de, 'a, D> +where + D: VariantDecoder<'de>, +{ + fn new(cx: &'a D::Cx, decoder: D) -> Self { Self { cx, decoder } } } -impl<'a, 'de, C, D> de::VariantAccess<'de> for EnumAccess<'a, C, D> +impl<'de, 'a, D> de::VariantAccess<'de> for EnumAccess<'de, 'a, D> where - C: ?Sized + Context, - C::Error: de::Error, - D: VariantDecoder<'de, C>, + D: VariantDecoder<'de>, + ::Error: de::Error, { - type Error = C::Error; + type Error = ::Error; #[inline] fn unit_variant(mut self) -> Result<(), Self::Error> { @@ -797,13 +827,12 @@ where } } -impl<'a, 'de, C, D> de::EnumAccess<'de> for EnumAccess<'a, C, D> +impl<'de, 'a, D> de::EnumAccess<'de> for EnumAccess<'de, 'a, D> where - C: ?Sized + Context, - C::Error: de::Error, - D: VariantDecoder<'de, C>, + D: VariantDecoder<'de>, + ::Error: de::Error, { - type Error = C::Error; + type Error = ::Error; type Variant = Self; #[inline] @@ -953,7 +982,7 @@ where #[inline] fn visit_option(self, cx: &C, v: Option) -> Result where - D: Decoder<'de, C>, + D: Decoder<'de, Cx = C>, { match v { Some(v) => self.visitor.visit_some(Deserializer::new(cx, v)), @@ -964,7 +993,7 @@ where #[inline] fn visit_sequence(self, cx: &C, mut decoder: D) -> Result where - D: SequenceDecoder<'de, C>, + D: SequenceDecoder<'de, Cx = C>, { let value = self.visitor.visit_seq(SeqAccess::new(cx, &mut decoder))?; decoder.end(cx)?; @@ -974,7 +1003,7 @@ where #[inline] fn visit_map(self, cx: &C, decoder: D) -> Result where - D: MapDecoder<'de, C>, + D: MapDecoder<'de, Cx = C>, { let mut map_decoder = decoder.into_map_entries(cx)?; let value = self diff --git a/crates/musli-serde/src/lib.rs b/crates/musli-serde/src/lib.rs index 4a2ac657c..442c891f5 100644 --- a/crates/musli-serde/src/lib.rs +++ b/crates/musli-serde/src/lib.rs @@ -103,10 +103,9 @@ where /// Encode the given serde value `T` to the given [Encoder] using the serde /// compatibility layer. -pub fn encode(value: &T, cx: &C, encoder: E) -> Result +pub fn encode(value: &T, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, T: Serialize, { let encoder = encoder.with_context(cx)?; @@ -136,10 +135,9 @@ where /// Decode the given serde value `T` from the given [Decoder] using the serde /// compatibility layer. -pub fn decode<'de, C, D, T>(cx: &C, decoder: D) -> Result +pub fn decode<'de, D, T>(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de>, T: Deserialize<'de>, { let decoder = decoder.with_context(cx)?; diff --git a/crates/musli-serde/src/serializer.rs b/crates/musli-serde/src/serializer.rs index 57b890068..ee2f1846e 100644 --- a/crates/musli-serde/src/serializer.rs +++ b/crates/musli-serde/src/serializer.rs @@ -8,34 +8,39 @@ use musli::{Context, Encode, Encoder}; use serde::ser::{self, Serialize}; -pub struct Serializer<'a, C: ?Sized, E> { - cx: &'a C, +pub struct Serializer<'a, E> +where + E: Encoder, +{ + cx: &'a E::Cx, encoder: E, } -impl<'a, C: ?Sized, E> Serializer<'a, C, E> { +impl<'a, E> Serializer<'a, E> +where + E: Encoder, +{ /// Construct a new deserializer out of an encoder. - pub fn new(cx: &'a C, encoder: E) -> Self { + pub fn new(cx: &'a E::Cx, encoder: E) -> Self { Self { cx, encoder } } } -impl<'a, C, E> ser::Serializer for Serializer<'a, C, E> +impl<'a, E> ser::Serializer for Serializer<'a, E> where - C: ?Sized + Context, - C::Error: ser::Error, - E: Encoder, + E: Encoder, + ::Error: ser::Error, { type Ok = E::Ok; - type Error = C::Error; + type Error = ::Error; - type SerializeSeq = SerializeSeq<'a, C, E::EncodeSequence>; - type SerializeTuple = SerializeSeq<'a, C, E::EncodeTuple>; - type SerializeTupleStruct = SerializeTupleStruct<'a, C, E::EncodeStruct>; - type SerializeTupleVariant = SerializeSeq<'a, C, E::EncodeTupleVariant>; - type SerializeMap = SerializeMap<'a, C, E::EncodeMapEntries>; - type SerializeStruct = SerializeStruct<'a, C, E::EncodeStruct>; - type SerializeStructVariant = SerializeStructVariant<'a, C, E::EncodeStructVariant>; + type SerializeSeq = SerializeSeq<'a, E::EncodeSequence>; + type SerializeTuple = SerializeSeq<'a, E::EncodeTuple>; + type SerializeTupleStruct = SerializeTupleStruct<'a, E::EncodeStruct>; + type SerializeTupleVariant = SerializeSeq<'a, E::EncodeTupleVariant>; + type SerializeMap = SerializeMap<'a, E::EncodeMapEntries>; + type SerializeStruct = SerializeStruct<'a, E::EncodeStruct>; + type SerializeStructVariant = SerializeStructVariant<'a, E::EncodeStructVariant>; #[inline] fn serialize_bool(self, v: bool) -> Result { @@ -283,13 +288,19 @@ where } #[inline] -fn encode_variant(cx: &C, encoder: E, variant_tag: &T, f: F) -> Result +fn encode_variant( + cx: &E::Cx, + encoder: E, + variant_tag: &T, + f: F, +) -> Result::Error> where - C: ?Sized + Context, - C::Error: ser::Error, - E: Encoder, + ::Error: ser::Error, + E: Encoder, T: ?Sized + Serialize, - F: FnOnce(>::EncodeValue<'_>) -> Result, + F: FnOnce( + ::EncodeValue<'_>, + ) -> Result::Error>, { let mut variant = encoder.encode_variant(cx)?; variant_tag.serialize(Serializer::new(cx, variant.encode_tag(cx)?))?; @@ -298,25 +309,30 @@ where Ok(output) } -pub struct SerializeSeq<'a, C: ?Sized, E> { - cx: &'a C, +pub struct SerializeSeq<'a, E> +where + E: SequenceEncoder, +{ + cx: &'a E::Cx, encoder: E, } -impl<'a, C: ?Sized, E> SerializeSeq<'a, C, E> { - fn new(cx: &'a C, encoder: E) -> Self { +impl<'a, E> SerializeSeq<'a, E> +where + E: SequenceEncoder, +{ + fn new(cx: &'a E::Cx, encoder: E) -> Self { Self { cx, encoder } } } -impl<'a, C, E> ser::SerializeSeq for SerializeSeq<'a, C, E> +impl<'a, E> ser::SerializeSeq for SerializeSeq<'a, E> where - C: ?Sized + Context, - C::Error: ser::Error, - E: SequenceEncoder, + ::Error: ser::Error, + E: SequenceEncoder, { type Ok = E::Ok; - type Error = C::Error; + type Error = ::Error; #[inline] fn serialize_element(&mut self, value: &T) -> Result<(), Self::Error> @@ -334,14 +350,13 @@ where } } -impl<'a, C, E> ser::SerializeTuple for SerializeSeq<'a, C, E> +impl<'a, E> ser::SerializeTuple for SerializeSeq<'a, E> where - C: ?Sized + Context, - C::Error: ser::Error, - E: SequenceEncoder, + ::Error: ser::Error, + E: SequenceEncoder, { type Ok = E::Ok; - type Error = C::Error; + type Error = ::Error; #[inline] fn serialize_element(&mut self, value: &T) -> Result<(), Self::Error> @@ -357,14 +372,13 @@ where } } -impl<'a, C, E> ser::SerializeTupleVariant for SerializeSeq<'a, C, E> +impl<'a, E> ser::SerializeTupleVariant for SerializeSeq<'a, E> where - C: ?Sized + Context, - C::Error: ser::Error, - E: SequenceEncoder, + ::Error: ser::Error, + E: SequenceEncoder, { type Ok = E::Ok; - type Error = C::Error; + type Error = ::Error; #[inline] fn serialize_field(&mut self, value: &T) -> Result<(), Self::Error> @@ -380,14 +394,20 @@ where } } -pub struct SerializeTupleStruct<'a, C: ?Sized, E> { - cx: &'a C, +pub struct SerializeTupleStruct<'a, E> +where + E: StructEncoder, +{ + cx: &'a E::Cx, encoder: E, field: usize, } -impl<'a, C: ?Sized, E> SerializeTupleStruct<'a, C, E> { - fn new(cx: &'a C, encoder: E) -> Self { +impl<'a, E> SerializeTupleStruct<'a, E> +where + E: StructEncoder, +{ + fn new(cx: &'a E::Cx, encoder: E) -> Self { Self { cx, encoder, @@ -396,14 +416,13 @@ impl<'a, C: ?Sized, E> SerializeTupleStruct<'a, C, E> { } } -impl<'a, C, E> ser::SerializeTupleStruct for SerializeTupleStruct<'a, C, E> +impl<'a, E> ser::SerializeTupleStruct for SerializeTupleStruct<'a, E> where - C: ?Sized + Context, - C::Error: ser::Error, - E: StructEncoder, + ::Error: ser::Error, + E: StructEncoder, { type Ok = E::Ok; - type Error = C::Error; + type Error = ::Error; #[inline] fn serialize_field(&mut self, value: &T) -> Result<(), Self::Error> @@ -428,25 +447,30 @@ where } } -pub struct SerializeMap<'a, C: ?Sized, E> { - cx: &'a C, +pub struct SerializeMap<'a, E> +where + E: MapEntriesEncoder, +{ + cx: &'a E::Cx, encoder: E, } -impl<'a, C: ?Sized, E> SerializeMap<'a, C, E> { - fn new(cx: &'a C, encoder: E) -> Self { +impl<'a, E> SerializeMap<'a, E> +where + E: MapEntriesEncoder, +{ + fn new(cx: &'a E::Cx, encoder: E) -> Self { Self { cx, encoder } } } -impl<'a, C, E> ser::SerializeMap for SerializeMap<'a, C, E> +impl<'a, E> ser::SerializeMap for SerializeMap<'a, E> where - C: ?Sized + Context, - C::Error: ser::Error, - E: MapEntriesEncoder, + ::Error: ser::Error, + E: MapEntriesEncoder, { type Ok = E::Ok; - type Error = C::Error; + type Error = ::Error; #[inline] fn serialize_key(&mut self, key: &T) -> Result<(), Self::Error> @@ -474,25 +498,30 @@ where } } -pub struct SerializeStruct<'a, C: ?Sized, E> { - cx: &'a C, +pub struct SerializeStruct<'a, E> +where + E: StructEncoder, +{ + cx: &'a E::Cx, encoder: E, } -impl<'a, C: ?Sized, E> SerializeStruct<'a, C, E> { - fn new(cx: &'a C, encoder: E) -> Self { +impl<'a, E> SerializeStruct<'a, E> +where + E: StructEncoder, +{ + fn new(cx: &'a E::Cx, encoder: E) -> Self { Self { cx, encoder } } } -impl<'a, C, E> ser::SerializeStruct for SerializeStruct<'a, C, E> +impl<'a, E> ser::SerializeStruct for SerializeStruct<'a, E> where - C: ?Sized + Context, - C::Error: ser::Error, - E: StructEncoder, + ::Error: ser::Error, + E: StructEncoder, { type Ok = E::Ok; - type Error = C::Error; + type Error = ::Error; #[inline] fn serialize_field( @@ -516,25 +545,30 @@ where } } -pub struct SerializeStructVariant<'a, C: ?Sized, E> { - cx: &'a C, +pub struct SerializeStructVariant<'a, E> +where + E: StructEncoder, +{ + cx: &'a E::Cx, encoder: E, } -impl<'a, C: ?Sized, E> SerializeStructVariant<'a, C, E> { - fn new(cx: &'a C, encoder: E) -> Self { +impl<'a, E> SerializeStructVariant<'a, E> +where + E: StructEncoder, +{ + fn new(cx: &'a E::Cx, encoder: E) -> Self { Self { cx, encoder } } } -impl<'a, C, E> ser::SerializeStructVariant for SerializeStructVariant<'a, C, E> +impl<'a, E> ser::SerializeStructVariant for SerializeStructVariant<'a, E> where - C: ?Sized + Context, - C::Error: ser::Error, - E: StructEncoder, + ::Error: ser::Error, + E: StructEncoder, { type Ok = E::Ok; - type Error = C::Error; + type Error = ::Error; #[inline] fn serialize_field( diff --git a/crates/musli-storage/src/de.rs b/crates/musli-storage/src/de.rs index d07a6f82f..a47b20564 100644 --- a/crates/musli-storage/src/de.rs +++ b/crates/musli-storage/src/de.rs @@ -1,4 +1,5 @@ use core::fmt; +use core::marker::PhantomData; #[cfg(feature = "alloc")] use alloc::vec::Vec; @@ -13,15 +14,19 @@ use crate::options::Options; use crate::reader::Reader; /// A very simple decoder suitable for storage decoding. -pub struct StorageDecoder { +pub struct StorageDecoder { reader: R, + _marker: PhantomData, } -impl StorageDecoder { +impl StorageDecoder { /// Construct a new fixed width message encoder. #[inline] pub fn new(reader: R) -> Self { - Self { reader } + Self { + reader, + _marker: PhantomData, + } } } @@ -30,23 +35,26 @@ impl StorageDecoder { /// This simplifies implementing decoders that do not have any special handling /// for length-prefixed types. #[doc(hidden)] -pub struct LimitedStorageDecoder { +pub struct LimitedStorageDecoder { remaining: usize, - decoder: StorageDecoder, + decoder: StorageDecoder, } #[musli::decoder] -impl<'de, R, const F: Options, C: ?Sized + Context> Decoder<'de, C> for StorageDecoder +impl<'de, R, const F: Options, C: ?Sized + Context> Decoder<'de> for StorageDecoder where R: Reader<'de>, { - type WithContext = Self where U: Context; + type Cx = C; + type Error = C::Error; + type Mode = C::Mode; + type WithContext = StorageDecoder where U: Context; type DecodePack = Self; type DecodeSome = Self; - type DecodeSequence = LimitedStorageDecoder; + type DecodeSequence = LimitedStorageDecoder; type DecodeTuple = Self; - type DecodeMap = LimitedStorageDecoder; - type DecodeStruct = LimitedStorageDecoder; + type DecodeMap = LimitedStorageDecoder; + type DecodeStruct = LimitedStorageDecoder; type DecodeVariant = Self; #[inline] @@ -54,7 +62,7 @@ where where U: Context, { - Ok(self) + Ok(StorageDecoder::new(self.reader)) } #[inline] @@ -266,11 +274,12 @@ where } } -impl<'de, R, const F: Options, C: ?Sized + Context> PackDecoder<'de, C> for StorageDecoder +impl<'de, R, const F: Options, C: ?Sized + Context> PackDecoder<'de> for StorageDecoder where R: Reader<'de>, { - type DecodeNext<'this> = StorageDecoder, F> where Self: 'this; + type Cx = C; + type DecodeNext<'this> = StorageDecoder, F, C> where Self: 'this; #[inline] fn decode_next(&mut self, _: &C) -> Result, C::Error> { @@ -283,26 +292,25 @@ where } } -impl<'de, R, const F: Options> LimitedStorageDecoder +impl<'de, R, const F: Options, C> LimitedStorageDecoder where + C: ?Sized + Context, R: Reader<'de>, { #[inline] - fn new(cx: &C, mut decoder: StorageDecoder) -> Result - where - C: ?Sized + Context, - { + fn new(cx: &C, mut decoder: StorageDecoder) -> Result { let remaining = musli_common::int::decode_usize::<_, _, F>(cx, &mut decoder.reader)?; Ok(Self { remaining, decoder }) } } -impl<'de, R, const F: Options, C: ?Sized + Context> SequenceDecoder<'de, C> - for LimitedStorageDecoder +impl<'de, R, const F: Options, C: ?Sized + Context> SequenceDecoder<'de> + for LimitedStorageDecoder where R: Reader<'de>, { - type DecodeNext<'this> = StorageDecoder, F> where Self: 'this; + type Cx = C; + type DecodeNext<'this> = StorageDecoder, F, C> where Self: 'this; #[inline] fn size_hint(&self, _: &C) -> SizeHint { @@ -321,12 +329,13 @@ where } #[musli::map_decoder] -impl<'de, R, const F: Options, C: ?Sized + Context> MapDecoder<'de, C> - for LimitedStorageDecoder +impl<'de, R, const F: Options, C: ?Sized + Context> MapDecoder<'de> + for LimitedStorageDecoder where R: Reader<'de>, { - type DecodeEntry<'this> = StorageDecoder, F> + type Cx = C; + type DecodeEntry<'this> = StorageDecoder, F, C> where Self: 'this; type IntoMapEntries = Self; @@ -352,11 +361,12 @@ where } } -impl<'de, R, const F: Options, C: ?Sized + Context> MapEntryDecoder<'de, C> for StorageDecoder +impl<'de, R, const F: Options, C: ?Sized + Context> MapEntryDecoder<'de> for StorageDecoder where R: Reader<'de>, { - type DecodeMapKey<'this> = StorageDecoder, F> where Self: 'this; + type Cx = C; + type DecodeMapKey<'this> = StorageDecoder, F, C> where Self: 'this; type DecodeMapValue = Self; #[inline] @@ -376,12 +386,13 @@ where } #[musli::struct_decoder] -impl<'de, R, const F: Options, C: ?Sized + Context> StructDecoder<'de, C> - for LimitedStorageDecoder +impl<'de, R, const F: Options, C: ?Sized + Context> StructDecoder<'de> + for LimitedStorageDecoder where R: Reader<'de>, { - type DecodeField<'this> = StorageDecoder, F> + type Cx = C; + type DecodeField<'this> = StorageDecoder, F, C> where Self: 'this; @@ -408,12 +419,13 @@ where } } -impl<'de, R, const F: Options, C: ?Sized + Context> StructFieldDecoder<'de, C> - for StorageDecoder +impl<'de, R, const F: Options, C: ?Sized + Context> StructFieldDecoder<'de> + for StorageDecoder where R: Reader<'de>, { - type DecodeFieldName<'this> = StorageDecoder, F> where Self: 'this; + type Cx = C; + type DecodeFieldName<'this> = StorageDecoder, F, C> where Self: 'this; type DecodeFieldValue = Self; #[inline] @@ -432,13 +444,14 @@ where } } -impl<'de, R, const F: Options, C: ?Sized + Context> MapEntriesDecoder<'de, C> - for LimitedStorageDecoder +impl<'de, R, const F: Options, C: ?Sized + Context> MapEntriesDecoder<'de> + for LimitedStorageDecoder where R: Reader<'de>, { - type DecodeMapEntryKey<'this> = StorageDecoder, F> where Self: 'this; - type DecodeMapEntryValue<'this> = StorageDecoder, F> where Self: 'this; + type Cx = C; + type DecodeMapEntryKey<'this> = StorageDecoder, F, C> where Self: 'this; + type DecodeMapEntryValue<'this> = StorageDecoder, F, C> where Self: 'this; #[inline] fn decode_map_entry_key( @@ -464,13 +477,14 @@ where } } -impl<'de, R, const F: Options, C: ?Sized + Context> StructFieldsDecoder<'de, C> - for LimitedStorageDecoder +impl<'de, R, const F: Options, C: ?Sized + Context> StructFieldsDecoder<'de> + for LimitedStorageDecoder where R: Reader<'de>, { - type DecodeStructFieldName<'this> = StorageDecoder, F> where Self: 'this; - type DecodeStructFieldValue<'this> = StorageDecoder, F> where Self: 'this; + type Cx = C; + type DecodeStructFieldName<'this> = StorageDecoder, F, C> where Self: 'this; + type DecodeStructFieldValue<'this> = StorageDecoder, F, C> where Self: 'this; #[inline] fn decode_struct_field_name( @@ -504,12 +518,13 @@ where } } -impl<'de, R, const F: Options, C: ?Sized + Context> VariantDecoder<'de, C> for StorageDecoder +impl<'de, R, const F: Options, C: ?Sized + Context> VariantDecoder<'de> for StorageDecoder where R: Reader<'de>, { - type DecodeTag<'this> = StorageDecoder, F> where Self: 'this; - type DecodeVariant<'this> = StorageDecoder, F> where Self: 'this; + type Cx = C; + type DecodeTag<'this> = StorageDecoder, F, C> where Self: 'this; + type DecodeVariant<'this> = StorageDecoder, F, C> where Self: 'this; #[inline] fn decode_tag(&mut self, _: &C) -> Result, C::Error> { diff --git a/crates/musli-storage/src/en.rs b/crates/musli-storage/src/en.rs index 678a9b18a..31db2757d 100644 --- a/crates/musli-storage/src/en.rs +++ b/crates/musli-storage/src/en.rs @@ -1,4 +1,5 @@ use core::fmt; +use core::marker::PhantomData; use musli::en::{ Encode, Encoder, MapEncoder, MapEntriesEncoder, MapEntryEncoder, SequenceEncoder, @@ -12,30 +13,37 @@ use crate::writer::Writer; const DEFAULT_OPTIONS: options::Options = options::new().build(); /// The alias for a [StorageEncoder] that is used for packs. -pub type PackEncoder = StorageEncoder; +pub type PackEncoder = StorageEncoder; /// A vaery simple encoder suitable for storage encoding. -pub struct StorageEncoder { +pub struct StorageEncoder { writer: W, + _marker: PhantomData, } -impl StorageEncoder { +impl StorageEncoder { /// Construct a new fixed width message encoder. #[inline] pub fn new(writer: W) -> Self { - Self { writer } + Self { + writer, + _marker: PhantomData, + } } } #[musli::encoder] -impl Encoder for StorageEncoder +impl Encoder for StorageEncoder where C: ?Sized + Context, W: Writer, { + type Cx = C; + type Error = C::Error; type Ok = (); - type WithContext = Self where U: Context; - type EncodePack<'this> = Self where C: 'this; + type Mode = C::Mode; + type WithContext = StorageEncoder where U: Context; + type EncodePack<'this> = StorageEncoder where Self::Cx: 'this; type EncodeSome = Self; type EncodeSequence = Self; type EncodeTuple = Self; @@ -51,7 +59,7 @@ where where U: Context, { - Ok(self) + Ok(StorageEncoder::new(self.writer)) } #[inline] @@ -253,7 +261,7 @@ where where T: ?Sized + Encode, { - let encoder = StorageEncoder::<_, F>::new(self.writer.borrow_mut()); + let encoder = StorageEncoder::<_, F, _>::new(self.writer.borrow_mut()); tag.encode(cx, encoder)?; Ok(self) } @@ -268,20 +276,21 @@ where where T: ?Sized + Encode, { - let encoder = StorageEncoder::<_, F>::new(self.writer.borrow_mut()); + let encoder = StorageEncoder::<_, F, _>::new(self.writer.borrow_mut()); tag.encode(cx, encoder)?; musli_common::int::encode_usize::<_, _, F>(cx, self.writer.borrow_mut(), len)?; Ok(self) } } -impl SequenceEncoder for StorageEncoder +impl SequenceEncoder for StorageEncoder where C: ?Sized + Context, W: Writer, { + type Cx = C; type Ok = (); - type EncodeNext<'this> = StorageEncoder, F> where Self: 'this; + type EncodeNext<'this> = StorageEncoder, F, C> where Self: 'this; #[inline] fn encode_next(&mut self, _: &C) -> Result, C::Error> { @@ -294,13 +303,14 @@ where } } -impl MapEncoder for StorageEncoder +impl MapEncoder for StorageEncoder where C: ?Sized + Context, W: Writer, { + type Cx = C; type Ok = (); - type EncodeEntry<'this> = StorageEncoder, F> where Self: 'this; + type EncodeEntry<'this> = StorageEncoder, F, C> where Self: 'this; #[inline] fn encode_entry(&mut self, _: &C) -> Result, C::Error> { @@ -313,14 +323,15 @@ where } } -impl MapEntryEncoder for StorageEncoder +impl MapEntryEncoder for StorageEncoder where C: ?Sized + Context, W: Writer, { + type Cx = C; type Ok = (); - type EncodeMapKey<'this> = StorageEncoder, F> where Self: 'this; - type EncodeMapValue<'this> = StorageEncoder, F> where Self: 'this; + type EncodeMapKey<'this> = StorageEncoder, F, C> where Self: 'this; + type EncodeMapValue<'this> = StorageEncoder, F, C> where Self: 'this; #[inline] fn encode_map_key(&mut self, _: &C) -> Result, C::Error> { @@ -338,14 +349,15 @@ where } } -impl MapEntriesEncoder for StorageEncoder +impl MapEntriesEncoder for StorageEncoder where C: ?Sized + Context, W: Writer, { + type Cx = C; type Ok = (); - type EncodeMapEntryKey<'this> = StorageEncoder, F> where Self: 'this; - type EncodeMapEntryValue<'this> = StorageEncoder, F> where Self: 'this; + type EncodeMapEntryKey<'this> = StorageEncoder, F, C> where Self: 'this; + type EncodeMapEntryValue<'this> = StorageEncoder, F, C> where Self: 'this; #[inline] fn encode_map_entry_key(&mut self, _: &C) -> Result, C::Error> { @@ -363,13 +375,14 @@ where } } -impl StructEncoder for StorageEncoder +impl StructEncoder for StorageEncoder where C: ?Sized + Context, W: Writer, { + type Cx = C; type Ok = (); - type EncodeField<'this> = StorageEncoder, F> where Self: 'this; + type EncodeField<'this> = StorageEncoder, F, C> where Self: 'this; #[inline] fn encode_field(&mut self, cx: &C) -> Result, C::Error> { @@ -382,14 +395,15 @@ where } } -impl StructFieldEncoder for StorageEncoder +impl StructFieldEncoder for StorageEncoder where C: ?Sized + Context, W: Writer, { + type Cx = C; type Ok = (); - type EncodeFieldName<'this> = StorageEncoder, F> where Self: 'this; - type EncodeFieldValue<'this> = StorageEncoder, F> where Self: 'this; + type EncodeFieldName<'this> = StorageEncoder, F, C> where Self: 'this; + type EncodeFieldValue<'this> = StorageEncoder, F, C> where Self: 'this; #[inline] fn encode_field_name(&mut self, cx: &C) -> Result, C::Error> { @@ -407,14 +421,15 @@ where } } -impl VariantEncoder for StorageEncoder +impl VariantEncoder for StorageEncoder where C: ?Sized + Context, W: Writer, { + type Cx = C; type Ok = (); - type EncodeTag<'this> = StorageEncoder, F> where Self: 'this; - type EncodeValue<'this> = StorageEncoder, F> where Self: 'this; + type EncodeTag<'this> = StorageEncoder, F, C> where Self: 'this; + type EncodeValue<'this> = StorageEncoder, F, C> where Self: 'this; #[inline] fn encode_tag(&mut self, _: &C) -> Result, C::Error> { diff --git a/crates/musli-storage/src/encoding.rs b/crates/musli-storage/src/encoding.rs index 968de9c99..de9990cb8 100644 --- a/crates/musli-storage/src/encoding.rs +++ b/crates/musli-storage/src/encoding.rs @@ -170,7 +170,11 @@ impl Encoding { } } - musli_common::encoding_impls!(M, StorageEncoder::<_, F>::new, StorageDecoder::<_, F>::new); + musli_common::encoding_impls!( + M, + StorageEncoder::<_, F, _>::new, + StorageDecoder::<_, F, _>::new + ); musli_common::encoding_from_slice_impls!(M, StorageDecoder::<_, F>::new); } diff --git a/crates/musli-value/src/de.rs b/crates/musli-value/src/de.rs index 22a4e0d85..57b500071 100644 --- a/crates/musli-value/src/de.rs +++ b/crates/musli-value/src/de.rs @@ -1,4 +1,5 @@ use core::fmt; +use core::marker::PhantomData; use core::slice; #[cfg(feature = "alloc")] @@ -18,14 +19,18 @@ use crate::value::{Number, Value}; use crate::AsValueDecoder; /// Encoder for a single value. -pub struct ValueDecoder<'de, const F: Options> { +pub struct ValueDecoder<'de, const F: Options, C: ?Sized> { value: &'de Value, + _marker: PhantomData, } -impl<'de, const F: Options> ValueDecoder<'de, F> { +impl<'de, const F: Options, C: ?Sized> ValueDecoder<'de, F, C> { #[inline] pub(crate) const fn new(value: &'de Value) -> Self { - Self { value } + Self { + value, + _marker: PhantomData, + } } } @@ -42,23 +47,26 @@ macro_rules! ensure { } #[musli::decoder] -impl<'de, C: ?Sized + Context, const F: Options> Decoder<'de, C> for ValueDecoder<'de, F> { - type WithContext = Self where U: Context; - type DecodeBuffer = AsValueDecoder; +impl<'de, C: ?Sized + Context, const F: Options> Decoder<'de> for ValueDecoder<'de, F, C> { + type Cx = C; + type Error = C::Error; + type Mode = C::Mode; + type WithContext = ValueDecoder<'de, F, U> where U: Context; + type DecodeBuffer = AsValueDecoder; type DecodeSome = Self; - type DecodePack = StorageDecoder, F>; - type DecodeSequence = IterValueDecoder<'de, F>; - type DecodeTuple = IterValueDecoder<'de, F>; - type DecodeMap = IterValuePairsDecoder<'de, F>; - type DecodeStruct = IterValuePairsDecoder<'de, F>; - type DecodeVariant = IterValueVariantDecoder<'de, F>; + type DecodePack = StorageDecoder, F, C>; + type DecodeSequence = IterValueDecoder<'de, F, C>; + type DecodeTuple = IterValueDecoder<'de, F, C>; + type DecodeMap = IterValuePairsDecoder<'de, F, C>; + type DecodeStruct = IterValuePairsDecoder<'de, F, C>; + type DecodeVariant = IterValueVariantDecoder<'de, F, C>; #[inline] fn with_context(self, _: &C) -> Result, C::Error> where U: Context, { - Ok(self) + Ok(ValueDecoder::new(self.value)) } #[inline] @@ -274,7 +282,7 @@ impl<'de, C: ?Sized + Context, const F: Options> Decoder<'de, C> for ValueDecode #[inline] fn decode_any(self, cx: &C, visitor: V) -> Result where - V: Visitor<'de, C>, + V: Visitor<'de, Self::Cx>, { match self.value { Value::Unit => visitor.visit_unit(cx), @@ -308,25 +316,28 @@ impl<'de, C: ?Sized + Context, const F: Options> Decoder<'de, C> for ValueDecode } #[cfg(feature = "alloc")] Value::Sequence(values) => { - visitor.visit_sequence(cx, IterValueDecoder::::new(values)) + visitor.visit_sequence(cx, IterValueDecoder::::new(values)) } #[cfg(feature = "alloc")] - Value::Map(values) => visitor.visit_map(cx, IterValuePairsDecoder::::new(values)), + Value::Map(values) => visitor.visit_map(cx, IterValuePairsDecoder::::new(values)), #[cfg(feature = "alloc")] Value::Variant(variant) => { - visitor.visit_variant(cx, IterValueVariantDecoder::::new(variant)) + visitor.visit_variant(cx, IterValueVariantDecoder::::new(variant)) } #[cfg(feature = "alloc")] Value::Option(option) => visitor.visit_option( cx, - option.as_ref().map(|value| ValueDecoder::::new(value)), + option + .as_ref() + .map(|value| ValueDecoder::::new(value)), ), } } } -impl<'a, C: ?Sized + Context, const F: Options> AsDecoder for ValueDecoder<'a, F> { - type Decoder<'this> = ValueDecoder<'this, F> where Self: 'this; +impl<'a, C: ?Sized + Context, const F: Options> AsDecoder for ValueDecoder<'a, F, C> { + type Cx = C; + type Decoder<'this> = ValueDecoder<'this, F, C> where Self: 'this; #[inline] fn as_decoder(&self, _: &C) -> Result, C::Error> { @@ -336,22 +347,25 @@ impl<'a, C: ?Sized + Context, const F: Options> AsDecoder for ValueDecoder<'a /// A decoder over a simple value iterator. -pub struct IterValueDecoder<'de, const F: Options> { +pub struct IterValueDecoder<'de, const F: Options, C: ?Sized> { iter: slice::Iter<'de, Value>, + _marker: PhantomData, } #[cfg(feature = "alloc")] -impl<'de, const F: Options> IterValueDecoder<'de, F> { +impl<'de, const F: Options, C: ?Sized> IterValueDecoder<'de, F, C> { #[inline] fn new(values: &'de [Value]) -> Self { Self { iter: values.iter(), + _marker: PhantomData, } } } -impl<'de, C: ?Sized + Context, const F: Options> PackDecoder<'de, C> for IterValueDecoder<'de, F> { - type DecodeNext<'this> = ValueDecoder<'de, F> +impl<'de, C: ?Sized + Context, const F: Options> PackDecoder<'de> for IterValueDecoder<'de, F, C> { + type Cx = C; + type DecodeNext<'this> = ValueDecoder<'de, F, C> where Self: 'this; @@ -369,10 +383,11 @@ impl<'de, C: ?Sized + Context, const F: Options> PackDecoder<'de, C> for IterVal } } -impl<'de, C: ?Sized + Context, const F: Options> SequenceDecoder<'de, C> - for IterValueDecoder<'de, F> +impl<'de, C: ?Sized + Context, const F: Options> SequenceDecoder<'de> + for IterValueDecoder<'de, F, C> { - type DecodeNext<'this> = ValueDecoder<'de, F> + type Cx = C; + type DecodeNext<'this> = ValueDecoder<'de, F, C> where Self: 'this; @@ -396,25 +411,28 @@ impl<'de, C: ?Sized + Context, const F: Options> SequenceDecoder<'de, C> } /// A decoder over a simple value pair iterator. -pub struct IterValuePairsDecoder<'de, const F: Options> { +pub struct IterValuePairsDecoder<'de, const F: Options, C: ?Sized> { iter: slice::Iter<'de, (Value, Value)>, + _marker: PhantomData, } #[cfg(feature = "alloc")] -impl<'de, const F: Options> IterValuePairsDecoder<'de, F> { +impl<'de, const F: Options, C: ?Sized> IterValuePairsDecoder<'de, F, C> { #[inline] fn new(values: &'de [(Value, Value)]) -> Self { Self { iter: values.iter(), + _marker: PhantomData, } } } #[musli::map_decoder] -impl<'de, C: ?Sized + Context, const F: Options> MapDecoder<'de, C> - for IterValuePairsDecoder<'de, F> +impl<'de, C: ?Sized + Context, const F: Options> MapDecoder<'de> + for IterValuePairsDecoder<'de, F, C> { - type DecodeEntry<'this> = IterValuePairDecoder<'de, F> + type Cx = C; + type DecodeEntry<'this> = IterValuePairDecoder<'de, F, C> where Self: 'this; type IntoMapEntries = Self; @@ -440,14 +458,14 @@ impl<'de, C: ?Sized + Context, const F: Options> MapDecoder<'de, C> } } -impl<'de, C: ?Sized + Context, const F: Options> MapEntriesDecoder<'de, C> - for IterValuePairsDecoder<'de, F> +impl<'de, C: ?Sized + Context, const F: Options> MapEntriesDecoder<'de> + for IterValuePairsDecoder<'de, F, C> { - type DecodeMapEntryKey<'this> = ValueDecoder<'de, F> + type Cx = C; + type DecodeMapEntryKey<'this> = ValueDecoder<'de, F, C> where Self: 'this; - - type DecodeMapEntryValue<'this> = ValueDecoder<'de, F> where Self: 'this; + type DecodeMapEntryValue<'this> = ValueDecoder<'de, F, C> where Self: 'this; #[inline] fn decode_map_entry_key( @@ -484,14 +502,14 @@ impl<'de, C: ?Sized + Context, const F: Options> MapEntriesDecoder<'de, C> } } -impl<'de, C: ?Sized + Context, const F: Options> MapEntryDecoder<'de, C> - for IterValuePairDecoder<'de, F> +impl<'de, C: ?Sized + Context, const F: Options> MapEntryDecoder<'de> + for IterValuePairDecoder<'de, F, C> { - type DecodeMapKey<'this> = ValueDecoder<'de, F> + type Cx = C; + type DecodeMapKey<'this> = ValueDecoder<'de, F, C> where Self: 'this; - - type DecodeMapValue = ValueDecoder<'de, F>; + type DecodeMapValue = ValueDecoder<'de, F, C>; #[inline] fn decode_map_key(&mut self, _: &C) -> Result, C::Error> { @@ -510,10 +528,11 @@ impl<'de, C: ?Sized + Context, const F: Options> MapEntryDecoder<'de, C> } #[musli::struct_decoder] -impl<'de, C: ?Sized + Context, const F: Options> StructDecoder<'de, C> - for IterValuePairsDecoder<'de, F> +impl<'de, C: ?Sized + Context, const F: Options> StructDecoder<'de> + for IterValuePairsDecoder<'de, F, C> { - type DecodeField<'this> = IterValuePairDecoder<'de, F> + type Cx = C; + type DecodeField<'this> = IterValuePairDecoder<'de, F, C> where Self: 'this; type IntoStructFields = Self; @@ -539,14 +558,14 @@ impl<'de, C: ?Sized + Context, const F: Options> StructDecoder<'de, C> } } -impl<'de, C: ?Sized + Context, const F: Options> StructFieldsDecoder<'de, C> - for IterValuePairsDecoder<'de, F> +impl<'de, C: ?Sized + Context, const F: Options> StructFieldsDecoder<'de> + for IterValuePairsDecoder<'de, F, C> { - type DecodeStructFieldName<'this> = ValueDecoder<'de, F> + type Cx = C; + type DecodeStructFieldName<'this> = ValueDecoder<'de, F, C> where Self: 'this; - - type DecodeStructFieldValue<'this> = ValueDecoder<'de, F> where Self: 'this; + type DecodeStructFieldValue<'this> = ValueDecoder<'de, F, C> where Self: 'this; #[inline] fn decode_struct_field_name( @@ -583,14 +602,15 @@ impl<'de, C: ?Sized + Context, const F: Options> StructFieldsDecoder<'de, C> } } -impl<'de, C: ?Sized + Context, const F: Options> StructFieldDecoder<'de, C> - for IterValuePairDecoder<'de, F> +impl<'de, C: ?Sized + Context, const F: Options> StructFieldDecoder<'de> + for IterValuePairDecoder<'de, F, C> { - type DecodeFieldName<'this> = ValueDecoder<'de, F> + type Cx = C; + type DecodeFieldName<'this> = ValueDecoder<'de, F, C> where Self: 'this; - type DecodeFieldValue = ValueDecoder<'de, F>; + type DecodeFieldValue = ValueDecoder<'de, F, C>; #[inline] fn decode_field_name(&mut self, cx: &C) -> Result, C::Error> { @@ -609,38 +629,46 @@ impl<'de, C: ?Sized + Context, const F: Options> StructFieldDecoder<'de, C> } /// A decoder over a simple value pair iterator. -pub struct IterValuePairDecoder<'de, const F: Options> { +pub struct IterValuePairDecoder<'de, const F: Options, C: ?Sized> { pair: &'de (Value, Value), + _marker: PhantomData, } -impl<'de, const F: Options> IterValuePairDecoder<'de, F> { +impl<'de, const F: Options, C: ?Sized> IterValuePairDecoder<'de, F, C> { #[inline] const fn new(pair: &'de (Value, Value)) -> Self { - Self { pair } + Self { + pair, + _marker: PhantomData, + } } } /// A decoder over a simple value pair as a variant. -pub struct IterValueVariantDecoder<'de, const F: Options> { +pub struct IterValueVariantDecoder<'de, const F: Options, C: ?Sized> { pair: &'de (Value, Value), + _marker: PhantomData, } #[cfg(feature = "alloc")] -impl<'de, const F: Options> IterValueVariantDecoder<'de, F> { +impl<'de, const F: Options, C: ?Sized> IterValueVariantDecoder<'de, F, C> { #[inline] const fn new(pair: &'de (Value, Value)) -> Self { - Self { pair } + Self { + pair, + _marker: PhantomData, + } } } -impl<'de, C: ?Sized + Context, const F: Options> VariantDecoder<'de, C> - for IterValueVariantDecoder<'de, F> +impl<'de, C: ?Sized + Context, const F: Options> VariantDecoder<'de> + for IterValueVariantDecoder<'de, F, C> { - type DecodeTag<'this> = ValueDecoder<'de, F> + type Cx = C; + type DecodeTag<'this> = ValueDecoder<'de, F, C> where Self: 'this; - - type DecodeVariant<'this> = ValueDecoder<'de, F> + type DecodeVariant<'this> = ValueDecoder<'de, F, C> where Self: 'this; diff --git a/crates/musli-value/src/en.rs b/crates/musli-value/src/en.rs index 0199dbc8e..a671b4392 100644 --- a/crates/musli-value/src/en.rs +++ b/crates/musli-value/src/en.rs @@ -1,3 +1,5 @@ +use core::marker::PhantomData; + #[cfg(feature = "alloc")] use alloc::boxed::Box; #[cfg(feature = "alloc")] @@ -50,52 +52,59 @@ where } /// Encoder for a single value. -pub struct ValueEncoder { +pub struct ValueEncoder { output: O, + _marker: PhantomData, } -impl ValueEncoder { +impl ValueEncoder { #[inline] pub(crate) fn new(output: O) -> Self { - Self { output } + Self { + output, + _marker: PhantomData, + } } } #[musli::encoder] -impl Encoder for ValueEncoder +impl Encoder for ValueEncoder where - C: ?Sized + Context, O: ValueOutput, + C: ?Sized + Context, { + type Cx = C; + type Error = C::Error; type Ok = (); - type WithContext = Self where U: Context; + type Mode = C::Mode; + type WithContext = ValueEncoder where U: Context; #[cfg(feature = "alloc")] - type EncodeSome = ValueEncoder>; + type EncodeSome = ValueEncoder, C>; #[cfg(feature = "alloc")] - type EncodePack<'this> = SequenceValueEncoder where C: 'this; + type EncodePack<'this> = SequenceValueEncoder where C: 'this; #[cfg(feature = "alloc")] - type EncodeSequence = SequenceValueEncoder; + type EncodeSequence = SequenceValueEncoder; #[cfg(feature = "alloc")] - type EncodeTuple = SequenceValueEncoder; + type EncodeTuple = SequenceValueEncoder; #[cfg(feature = "alloc")] - type EncodeMap = MapValueEncoder; + type EncodeMap = MapValueEncoder; #[cfg(feature = "alloc")] - type EncodeMapEntries = MapValueEncoder; + type EncodeMapEntries = MapValueEncoder; #[cfg(feature = "alloc")] - type EncodeStruct = MapValueEncoder; + type EncodeStruct = MapValueEncoder; #[cfg(feature = "alloc")] - type EncodeVariant = VariantValueEncoder; + type EncodeVariant = VariantValueEncoder; #[cfg(feature = "alloc")] - type EncodeTupleVariant = VariantSequenceEncoder; + type EncodeTupleVariant = VariantSequenceEncoder; #[cfg(feature = "alloc")] - type EncodeStructVariant = VariantStructEncoder; + type EncodeStructVariant = VariantStructEncoder; #[inline] fn with_context(self, _: &C) -> Result, C::Error> where U: Context, { - Ok(self) + Ok(ValueEncoder::new(self.output)) } #[inline] @@ -347,31 +356,34 @@ where /// A pack encoder. #[cfg(feature = "alloc")] -pub struct SequenceValueEncoder { +pub struct SequenceValueEncoder { output: O, values: Vec, + _marker: PhantomData, } #[cfg(feature = "alloc")] -impl SequenceValueEncoder { +impl SequenceValueEncoder { #[inline] fn new(output: O) -> Self { Self { output, values: Vec::new(), + _marker: PhantomData, } } } #[cfg(feature = "alloc")] -impl SequenceEncoder for SequenceValueEncoder +impl SequenceEncoder for SequenceValueEncoder where - C: ?Sized + Context, O: ValueOutput, + C: ?Sized + Context, { + type Cx = C; type Ok = (); - type EncodeNext<'this> = ValueEncoder<&'this mut Vec> + type EncodeNext<'this> = ValueEncoder<&'this mut Vec, C> where Self: 'this; @@ -389,30 +401,33 @@ where /// A pairs encoder. #[cfg(feature = "alloc")] -pub struct MapValueEncoder { +pub struct MapValueEncoder { output: O, values: Vec<(Value, Value)>, + _marker: PhantomData, } #[cfg(feature = "alloc")] -impl MapValueEncoder { +impl MapValueEncoder { #[inline] fn new(output: O) -> Self { Self { output, values: Vec::new(), + _marker: PhantomData, } } } #[cfg(feature = "alloc")] -impl MapEncoder for MapValueEncoder +impl MapEncoder for MapValueEncoder where - C: ?Sized + Context, O: ValueOutput, + C: ?Sized + Context, { + type Cx = C; type Ok = (); - type EncodeEntry<'this> = PairValueEncoder<'this> + type EncodeEntry<'this> = PairValueEncoder<'this, C> where Self: 'this; @@ -429,16 +444,17 @@ where } #[cfg(feature = "alloc")] -impl MapEntriesEncoder for MapValueEncoder +impl MapEntriesEncoder for MapValueEncoder where - C: ?Sized + Context, O: ValueOutput, + C: ?Sized + Context, { + type Cx = C; type Ok = (); - type EncodeMapEntryKey<'this> = ValueEncoder<&'this mut Value> + type EncodeMapEntryKey<'this> = ValueEncoder<&'this mut Value, C> where Self: 'this; - type EncodeMapEntryValue<'this> = ValueEncoder<&'this mut Value> + type EncodeMapEntryValue<'this> = ValueEncoder<&'this mut Value, C> where Self: 'this; @@ -473,14 +489,15 @@ where } #[cfg(feature = "alloc")] -impl StructEncoder for MapValueEncoder +impl StructEncoder for MapValueEncoder where - C: ?Sized + Context, O: ValueOutput, + C: ?Sized + Context, { + type Cx = C; type Ok = (); - type EncodeField<'this> = PairValueEncoder<'this> + type EncodeField<'this> = PairValueEncoder<'this, C> where Self: 'this; @@ -498,32 +515,35 @@ where /// A pairs encoder. #[cfg(feature = "alloc")] -pub struct PairValueEncoder<'a> { +pub struct PairValueEncoder<'a, C: ?Sized> { output: &'a mut Vec<(Value, Value)>, pair: (Value, Value), + _marker: PhantomData, } #[cfg(feature = "alloc")] -impl<'a> PairValueEncoder<'a> { +impl<'a, C: ?Sized> PairValueEncoder<'a, C> { #[inline] fn new(output: &'a mut Vec<(Value, Value)>) -> Self { Self { output, pair: (Value::Unit, Value::Unit), + _marker: PhantomData, } } } #[cfg(feature = "alloc")] -impl<'a, C> MapEntryEncoder for PairValueEncoder<'a> +impl<'a, C> MapEntryEncoder for PairValueEncoder<'a, C> where C: ?Sized + Context, { + type Cx = C; type Ok = (); - type EncodeMapKey<'this> = ValueEncoder<&'this mut Value> + type EncodeMapKey<'this> = ValueEncoder<&'this mut Value, C> where Self: 'this; - type EncodeMapValue<'this> = ValueEncoder<&'this mut Value> where Self: 'this; + type EncodeMapValue<'this> = ValueEncoder<&'this mut Value, C> where Self: 'this; #[inline] fn encode_map_key(&mut self, _: &C) -> Result, C::Error> { @@ -543,15 +563,16 @@ where } #[cfg(feature = "alloc")] -impl<'a, C> StructFieldEncoder for PairValueEncoder<'a> +impl<'a, C> StructFieldEncoder for PairValueEncoder<'a, C> where C: ?Sized + Context, { + type Cx = C; type Ok = (); - type EncodeFieldName<'this> = ValueEncoder<&'this mut Value> + type EncodeFieldName<'this> = ValueEncoder<&'this mut Value, C> where Self: 'this; - type EncodeFieldValue<'this> = ValueEncoder<&'this mut Value> where Self: 'this; + type EncodeFieldValue<'this> = ValueEncoder<&'this mut Value, C> where Self: 'this; #[inline] fn encode_field_name(&mut self, _: &C) -> Result, C::Error> { @@ -572,33 +593,36 @@ where /// A pairs encoder. #[cfg(feature = "alloc")] -pub struct VariantValueEncoder { +pub struct VariantValueEncoder { output: O, pair: (Value, Value), + _marker: PhantomData, } #[cfg(feature = "alloc")] -impl VariantValueEncoder { +impl VariantValueEncoder { #[inline] fn new(output: O) -> Self { Self { output, pair: (Value::Unit, Value::Unit), + _marker: PhantomData, } } } #[cfg(feature = "alloc")] -impl VariantEncoder for VariantValueEncoder +impl VariantEncoder for VariantValueEncoder where - C: ?Sized + Context, O: ValueOutput, + C: ?Sized + Context, { + type Cx = C; type Ok = (); - type EncodeTag<'this> = ValueEncoder<&'this mut Value> + type EncodeTag<'this> = ValueEncoder<&'this mut Value, C> where Self: 'this; - type EncodeValue<'this> = ValueEncoder<&'this mut Value> + type EncodeValue<'this> = ValueEncoder<&'this mut Value, C> where Self: 'this; @@ -621,33 +645,36 @@ where /// A variant sequence encoder. #[cfg(feature = "alloc")] -pub struct VariantSequenceEncoder { +pub struct VariantSequenceEncoder { output: O, variant: Value, values: Vec, + _marker: PhantomData, } #[cfg(feature = "alloc")] -impl VariantSequenceEncoder { +impl VariantSequenceEncoder { #[inline] fn new(output: O, variant: Value, len: usize) -> Self { Self { output, variant, values: Vec::with_capacity(len), + _marker: PhantomData, } } } #[cfg(feature = "alloc")] -impl SequenceEncoder for VariantSequenceEncoder +impl SequenceEncoder for VariantSequenceEncoder where - C: ?Sized + Context, O: ValueOutput, + C: ?Sized + Context, { + type Cx = C; type Ok = (); - type EncodeNext<'this> = ValueEncoder<&'this mut Vec> + type EncodeNext<'this> = ValueEncoder<&'this mut Vec, C> where Self: 'this; @@ -668,33 +695,36 @@ where /// A variant struct encoder. #[cfg(feature = "alloc")] -pub struct VariantStructEncoder { +pub struct VariantStructEncoder { output: O, variant: Value, fields: Vec<(Value, Value)>, + _marker: PhantomData, } #[cfg(feature = "alloc")] -impl VariantStructEncoder { +impl VariantStructEncoder { #[inline] fn new(output: O, variant: Value, len: usize) -> Self { Self { output, variant, fields: Vec::with_capacity(len), + _marker: PhantomData, } } } #[cfg(feature = "alloc")] -impl StructEncoder for VariantStructEncoder +impl StructEncoder for VariantStructEncoder where - C: ?Sized + Context, O: ValueOutput, + C: ?Sized + Context, { + type Cx = C; type Ok = (); - type EncodeField<'this> = PairValueEncoder<'this> + type EncodeField<'this> = PairValueEncoder<'this, C> where Self: 'this; diff --git a/crates/musli-value/src/lib.rs b/crates/musli-value/src/lib.rs index 9c84f05b8..59254a1ab 100644 --- a/crates/musli-value/src/lib.rs +++ b/crates/musli-value/src/lib.rs @@ -59,5 +59,5 @@ where let mut buf = musli_common::exports::allocator::buffer(); let alloc = musli_common::exports::allocator::new(&mut buf); let cx = musli_common::exports::context::Same::<_, DefaultMode, Error>::new(&alloc); - T::decode(&cx, value.decoder::()) + T::decode(&cx, value.decoder::()) } diff --git a/crates/musli-value/src/value.rs b/crates/musli-value/src/value.rs index 3703b49cb..78be40528 100644 --- a/crates/musli-value/src/value.rs +++ b/crates/musli-value/src/value.rs @@ -1,3 +1,5 @@ +use core::marker::PhantomData; + #[cfg(feature = "alloc")] use alloc::borrow::ToOwned; #[cfg(feature = "alloc")] @@ -82,13 +84,13 @@ impl Value { /// Construct a [AsValueDecoder] implementation out of this value which /// emits the specified error `E`. #[inline] - pub fn into_value_decoder(self) -> AsValueDecoder { + pub fn into_value_decoder(self) -> AsValueDecoder { AsValueDecoder::new(self) } /// Get a decoder associated with a value. #[inline] - pub(crate) fn decoder(&self) -> ValueDecoder<'_, F> { + pub(crate) fn decoder(&self) -> ValueDecoder<'_, F, C> { ValueDecoder::new(self) } } @@ -152,10 +154,9 @@ from!(f32, F32); from!(f64, F64); impl Encode for Number { - fn encode(&self, cx: &C, encoder: E) -> Result + fn encode(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { match self { Number::U8(n) => encoder.encode_u8(cx, *n), @@ -304,7 +305,7 @@ impl<'de, C: ?Sized + Context> Visitor<'de, C> for AnyVisitor { #[inline] fn visit_option(self, cx: &C, decoder: Option) -> Result where - D: Decoder<'de, C>, + D: Decoder<'de, Cx = C>, { match decoder { Some(decoder) => Ok(Value::Option(Some(Box::new(Value::decode(cx, decoder)?)))), @@ -316,7 +317,7 @@ impl<'de, C: ?Sized + Context> Visitor<'de, C> for AnyVisitor { #[inline] fn visit_sequence(self, cx: &C, mut seq: D) -> Result where - D: SequenceDecoder<'de, C>, + D: SequenceDecoder<'de, Cx = C>, { let mut out = Vec::with_capacity(seq.size_hint(cx).or_default()); @@ -332,7 +333,7 @@ impl<'de, C: ?Sized + Context> Visitor<'de, C> for AnyVisitor { #[inline] fn visit_map(self, cx: &C, mut map: D) -> Result where - D: MapDecoder<'de, C>, + D: MapDecoder<'de, Cx = C>, { let mut out = Vec::with_capacity(map.size_hint(cx).or_default()); @@ -368,7 +369,7 @@ impl<'de, C: ?Sized + Context> Visitor<'de, C> for AnyVisitor { #[inline] fn visit_variant(self, cx: &C, mut variant: D) -> Result where - D: VariantDecoder<'de, C>, + D: VariantDecoder<'de, Cx = C>, { let first = cx.decode(variant.decode_tag(cx)?)?; let second = cx.decode(variant.decode_value(cx)?)?; @@ -378,10 +379,10 @@ impl<'de, C: ?Sized + Context> Visitor<'de, C> for AnyVisitor { } impl<'de, M> Decode<'de, M> for Value { - fn decode(cx: &C, decoder: D) -> Result + #[inline] + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de, Mode = M>, { decoder.decode_any(cx, AnyVisitor) } @@ -516,10 +517,9 @@ impl<'de, C: ?Sized + Context> NumberVisitor<'de, C> for ValueNumberVisitor { } impl Encode for Value { - fn encode(&self, cx: &C, encoder: E) -> Result + fn encode(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { match self { Value::Unit => encoder.encode_unit(cx), @@ -570,20 +570,25 @@ impl Encode for Value { } /// Value's [AsDecoder] implementation. -pub struct AsValueDecoder { +pub struct AsValueDecoder { value: Value, + _marker: PhantomData, } -impl AsValueDecoder { +impl AsValueDecoder { /// Construct a new buffered value decoder. #[inline] pub fn new(value: Value) -> Self { - Self { value } + Self { + value, + _marker: PhantomData, + } } } -impl AsDecoder for AsValueDecoder { - type Decoder<'this> = ValueDecoder<'this, F> where Self: 'this; +impl AsDecoder for AsValueDecoder { + type Cx = C; + type Decoder<'this> = ValueDecoder<'this, F, C> where Self: 'this; #[inline] fn as_decoder(&self, _: &C) -> Result, C::Error> { diff --git a/crates/musli-wire/src/de.rs b/crates/musli-wire/src/de.rs index 73d634441..ad43c3ce5 100644 --- a/crates/musli-wire/src/de.rs +++ b/crates/musli-wire/src/de.rs @@ -1,4 +1,5 @@ use core::fmt; +use core::marker::PhantomData; #[cfg(feature = "alloc")] use alloc::vec::Vec; @@ -17,27 +18,29 @@ use crate::tag::Kind; use crate::tag::Tag; /// A very simple decoder. -pub struct WireDecoder { +pub struct WireDecoder { reader: R, + _marker: PhantomData, } -impl WireDecoder { +impl WireDecoder { /// Construct a new fixed width message encoder. #[inline] pub(crate) fn new(reader: R) -> Self { - Self { reader } + Self { + reader, + _marker: PhantomData, + } } } -impl<'de, R, const F: Options> WireDecoder +impl<'de, R, const F: Options, C> WireDecoder where R: Reader<'de>, + C: ?Sized + Context, { /// Skip over any sequences of values. - pub(crate) fn skip_any(&mut self, cx: &C) -> Result<(), C::Error> - where - C: ?Sized + Context, - { + pub(crate) fn skip_any(&mut self, cx: &C) -> Result<(), C::Error> { let tag = Tag::from_byte(self.reader.read_byte(cx)?); match tag.kind() { @@ -76,10 +79,7 @@ where } #[inline] - fn decode_sequence_len(&mut self, cx: &C) -> Result - where - C: ?Sized + Context, - { + fn decode_sequence_len(&mut self, cx: &C) -> Result { let tag = Tag::from_byte(self.reader.read_byte(cx)?); match tag.kind() { @@ -97,33 +97,24 @@ where // Standard function for decoding a pair sequence. #[inline] - fn shared_decode_pair_sequence( + fn shared_decode_pair_sequence( mut self, cx: &C, - ) -> Result, C::Error> - where - C: ?Sized + Context, - { + ) -> Result, C::Error> { let len = self.decode_sequence_len(cx)?; Ok(RemainingWireDecoder::new(len / 2, self)) } // Standard function for decoding a pair sequence. #[inline] - fn shared_decode_sequence(mut self, cx: &C) -> Result, C::Error> - where - C: ?Sized + Context, - { + fn shared_decode_sequence(mut self, cx: &C) -> Result, C::Error> { let len = self.decode_sequence_len(cx)?; Ok(RemainingWireDecoder::new(len, self)) } /// Decode the length of a prefix. #[inline] - fn decode_len(&mut self, cx: &C, start: C::Mark) -> Result - where - C: ?Sized + Context, - { + fn decode_len(&mut self, cx: &C, start: C::Mark) -> Result { let tag = Tag::from_byte(self.reader.read_byte(cx)?); match tag.kind() { @@ -149,24 +140,27 @@ where /// This simplifies implementing decoders that do not have any special handling /// for length-prefixed types. #[doc(hidden)] -pub struct RemainingWireDecoder { +pub struct RemainingWireDecoder { remaining: usize, - decoder: WireDecoder, + decoder: WireDecoder, } #[musli::decoder] -impl<'de, C, R, const F: Options> Decoder<'de, C> for WireDecoder +impl<'de, R, const F: Options, C> Decoder<'de> for WireDecoder where C: ?Sized + Context, R: Reader<'de>, { - type WithContext = Self where U: Context; - type DecodePack = WireDecoder, F>; + type Cx = C; + type Error = C::Error; + type Mode = C::Mode; + type WithContext = WireDecoder where U: Context; + type DecodePack = WireDecoder, F, C>; type DecodeSome = Self; - type DecodeSequence = RemainingWireDecoder; - type DecodeTuple = TupleWireDecoder; - type DecodeMap = RemainingWireDecoder; - type DecodeStruct = RemainingWireDecoder; + type DecodeSequence = RemainingWireDecoder; + type DecodeTuple = TupleWireDecoder; + type DecodeMap = RemainingWireDecoder; + type DecodeStruct = RemainingWireDecoder; type DecodeVariant = Self; #[inline] @@ -174,7 +168,7 @@ where where U: Context, { - Ok(self) + Ok(WireDecoder::new(self.reader)) } #[inline] @@ -428,12 +422,13 @@ where } } -impl<'de, C, R, const F: Options> PackDecoder<'de, C> for WireDecoder, F> +impl<'de, R, const F: Options, C> PackDecoder<'de> for WireDecoder, F, C> where C: ?Sized + Context, R: Reader<'de>, { - type DecodeNext<'this> = StorageDecoder< as Reader<'de>>::Mut<'this>, F> where Self: 'this; + type Cx = C; + type DecodeNext<'this> = StorageDecoder< as Reader<'de>>::Mut<'this>, F, C> where Self: 'this; #[inline] fn decode_next(&mut self, _: &C) -> Result, C::Error> { @@ -450,19 +445,20 @@ where } } -impl RemainingWireDecoder { +impl RemainingWireDecoder { #[inline] - fn new(remaining: usize, decoder: WireDecoder) -> Self { + fn new(remaining: usize, decoder: WireDecoder) -> Self { Self { remaining, decoder } } } -impl<'de, C, R, const F: Options> SequenceDecoder<'de, C> for RemainingWireDecoder +impl<'de, R, const F: Options, C> SequenceDecoder<'de> for RemainingWireDecoder where C: ?Sized + Context, R: Reader<'de>, { - type DecodeNext<'this> = WireDecoder, F> where Self: 'this; + type Cx = C; + type DecodeNext<'this> = WireDecoder, F, C> where Self: 'this; #[inline] fn size_hint(&self, _: &C) -> SizeHint { @@ -480,13 +476,14 @@ where } } -impl<'de, C, R, const F: Options> VariantDecoder<'de, C> for WireDecoder +impl<'de, R, const F: Options, C> VariantDecoder<'de> for WireDecoder where C: ?Sized + Context, R: Reader<'de>, { - type DecodeTag<'this> = WireDecoder, F> where Self: 'this; - type DecodeVariant<'this> = WireDecoder, F> where Self: 'this; + type Cx = C; + type DecodeTag<'this> = WireDecoder, F, C> where Self: 'this; + type DecodeVariant<'this> = WireDecoder, F, C> where Self: 'this; #[inline] fn decode_tag(&mut self, _: &C) -> Result, C::Error> { @@ -511,12 +508,13 @@ where } #[musli::map_decoder] -impl<'de, C, R, const F: Options> MapDecoder<'de, C> for RemainingWireDecoder +impl<'de, R, const F: Options, C> MapDecoder<'de> for RemainingWireDecoder where C: ?Sized + Context, R: Reader<'de>, { - type DecodeEntry<'this> = WireDecoder, F> + type Cx = C; + type DecodeEntry<'this> = WireDecoder, F, C> where Self: 'this; type IntoMapEntries = Self; @@ -542,12 +540,13 @@ where } } -impl<'de, C, R, const F: Options> MapEntryDecoder<'de, C> for WireDecoder +impl<'de, R, const F: Options, C> MapEntryDecoder<'de> for WireDecoder where C: ?Sized + Context, R: Reader<'de>, { - type DecodeMapKey<'this> = WireDecoder, F> where Self: 'this; + type Cx = C; + type DecodeMapKey<'this> = WireDecoder, F, C> where Self: 'this; type DecodeMapValue = Self; #[inline] @@ -568,12 +567,13 @@ where } #[musli::struct_decoder] -impl<'de, C, R, const F: Options> StructDecoder<'de, C> for RemainingWireDecoder +impl<'de, R, const F: Options, C> StructDecoder<'de> for RemainingWireDecoder where C: ?Sized + Context, R: Reader<'de>, { - type DecodeField<'this> = WireDecoder, F> + type Cx = C; + type DecodeField<'this> = WireDecoder, F, C> where Self: 'this; type IntoStructFields = Self; @@ -599,12 +599,13 @@ where } } -impl<'de, C, R, const F: Options> StructFieldDecoder<'de, C> for WireDecoder +impl<'de, R, const F: Options, C> StructFieldDecoder<'de> for WireDecoder where C: ?Sized + Context, R: Reader<'de>, { - type DecodeFieldName<'this> = WireDecoder, F> where Self: 'this; + type Cx = C; + type DecodeFieldName<'this> = WireDecoder, F, C> where Self: 'this; type DecodeFieldValue = Self; #[inline] @@ -623,15 +624,16 @@ where } } -impl<'de, C, R, const F: Options> MapEntriesDecoder<'de, C> for RemainingWireDecoder +impl<'de, R, const F: Options, C> MapEntriesDecoder<'de> for RemainingWireDecoder where C: ?Sized + Context, R: Reader<'de>, { - type DecodeMapEntryKey<'this> = WireDecoder, F> + type Cx = C; + type DecodeMapEntryKey<'this> = WireDecoder, F, C> where Self: 'this; - type DecodeMapEntryValue<'this> = WireDecoder, F> + type DecodeMapEntryValue<'this> = WireDecoder, F, C> where Self: 'this; @@ -660,15 +662,16 @@ where } } -impl<'de, C, R, const F: Options> StructFieldsDecoder<'de, C> for RemainingWireDecoder +impl<'de, R, const F: Options, C> StructFieldsDecoder<'de> for RemainingWireDecoder where C: ?Sized + Context, R: Reader<'de>, { - type DecodeStructFieldName<'this> = WireDecoder, F> + type Cx = C; + type DecodeStructFieldName<'this> = WireDecoder, F, C> where Self: 'this; - type DecodeStructFieldValue<'this> = WireDecoder, F> + type DecodeStructFieldValue<'this> = WireDecoder, F, C> where Self: 'this; @@ -769,25 +772,31 @@ impl fmt::Display for BadLength { } /// A tuple wire decoder. -pub struct TupleWireDecoder { +pub struct TupleWireDecoder { reader: R, remaining: usize, + _marker: PhantomData, } -impl TupleWireDecoder { +impl TupleWireDecoder { /// Construct a new fixed width message encoder. #[inline] pub(crate) fn new(reader: R, remaining: usize) -> Self { - Self { reader, remaining } + Self { + reader, + remaining, + _marker: PhantomData, + } } } -impl<'de, C, R, const F: Options> PackDecoder<'de, C> for TupleWireDecoder +impl<'de, R, const F: Options, C> PackDecoder<'de> for TupleWireDecoder where C: ?Sized + Context, R: Reader<'de>, { - type DecodeNext<'this> = WireDecoder, F> where Self: 'this; + type Cx = C; + type DecodeNext<'this> = WireDecoder, F, C> where Self: 'this; #[inline] fn decode_next(&mut self, cx: &C) -> Result, C::Error> { @@ -802,7 +811,7 @@ where #[inline] fn end(mut self, cx: &C) -> Result<(), C::Error> { while self.remaining > 0 { - WireDecoder::<_, F>::new(self.reader.borrow_mut()).skip_any(cx)?; + WireDecoder::<_, F, _>::new(self.reader.borrow_mut()).skip_any(cx)?; self.remaining -= 1; } diff --git a/crates/musli-wire/src/en.rs b/crates/musli-wire/src/en.rs index 5dca65f4c..3fc323960 100644 --- a/crates/musli-wire/src/en.rs +++ b/crates/musli-wire/src/en.rs @@ -1,4 +1,5 @@ use core::fmt; +use core::marker::PhantomData; use musli::en::{ Encode, Encoder, MapEncoder, MapEntriesEncoder, MapEntryEncoder, SequenceEncoder, @@ -12,25 +13,27 @@ use crate::tag::{Kind, Tag, MAX_INLINE_LEN}; use crate::writer::{BufWriter, Writer}; /// A very simple encoder. -pub struct WireEncoder { +pub struct WireEncoder { writer: W, + _marker: PhantomData, } -impl WireEncoder +impl WireEncoder where W: Writer, + C: ?Sized + Context, { /// Construct a new fixed width message encoder. #[inline] pub(crate) fn new(writer: W) -> Self { - Self { writer } + Self { + writer, + _marker: PhantomData, + } } #[inline] - fn encode_map_len(&mut self, cx: &C, len: usize) -> Result<(), C::Error> - where - C: ?Sized + Context, - { + fn encode_map_len(&mut self, cx: &C, len: usize) -> Result<(), C::Error> { let Some(len) = len.checked_mul(2) else { return Err(cx.message("Map length overflow")); }; @@ -46,10 +49,7 @@ where } #[inline] - fn encode_tuple_len(&mut self, cx: &C, len: usize) -> Result<(), C::Error> - where - C: ?Sized + Context, - { + fn encode_tuple_len(&mut self, cx: &C, len: usize) -> Result<(), C::Error> { let (tag, embedded) = Tag::with_len(Kind::Sequence, len); self.writer.write_byte(cx, tag.byte())?; @@ -61,31 +61,36 @@ where } } -pub struct WirePackEncoder { +pub struct WirePackEncoder { writer: W, buffer: BufWriter, + _marker: PhantomData, } -impl WirePackEncoder { +impl WirePackEncoder { /// Construct a new fixed width message encoder. #[inline] pub(crate) fn new(writer: W, buffer: B) -> Self { Self { writer, buffer: BufWriter::new(buffer), + _marker: PhantomData, } } } #[musli::encoder] -impl Encoder for WireEncoder +impl Encoder for WireEncoder where C: ?Sized + Context, W: Writer, { + type Cx = C; + type Error = C::Error; type Ok = (); - type WithContext = Self where U: Context; - type EncodePack<'this> = WirePackEncoder, F> where C: 'this; + type Mode = C::Mode; + type WithContext = WireEncoder where U: Context; + type EncodePack<'this> = WirePackEncoder, F, C> where C: 'this; type EncodeSome = Self; type EncodeSequence = Self; type EncodeTuple = Self; @@ -101,7 +106,7 @@ where where U: Context, { - Ok(self) + Ok(WireEncoder::new(self.writer)) } #[inline] @@ -323,7 +328,7 @@ where { self.writer .write_byte(cx, Tag::new(Kind::Sequence, 2).byte())?; - tag.encode(cx, WireEncoder::<_, F>::new(self.writer.borrow_mut()))?; + tag.encode(cx, WireEncoder::<_, F, _>::new(self.writer.borrow_mut()))?; self.encode_tuple(cx, len) } @@ -339,19 +344,20 @@ where { self.writer .write_byte(cx, Tag::new(Kind::Sequence, 2).byte())?; - tag.encode(cx, WireEncoder::<_, F>::new(self.writer.borrow_mut()))?; + tag.encode(cx, WireEncoder::<_, F, _>::new(self.writer.borrow_mut()))?; self.encode_struct(cx, len) } } -impl SequenceEncoder for WirePackEncoder +impl SequenceEncoder for WirePackEncoder where C: ?Sized + Context, W: Writer, B: Buf, { + type Cx = C; type Ok = (); - type EncodeNext<'this> = StorageEncoder<&'this mut BufWriter, F> where Self: 'this, B: Buf; + type EncodeNext<'this> = StorageEncoder<&'this mut BufWriter, F, C> where Self: 'this, B: Buf; #[inline] fn encode_next(&mut self, _: &C) -> Result, C::Error> { @@ -395,13 +401,14 @@ where } } -impl SequenceEncoder for WireEncoder +impl SequenceEncoder for WireEncoder where C: ?Sized + Context, W: Writer, { + type Cx = C; type Ok = (); - type EncodeNext<'this> = WireEncoder, F> where Self: 'this; + type EncodeNext<'this> = WireEncoder, F, C> where Self: 'this; #[inline] fn encode_next(&mut self, _: &C) -> Result, C::Error> { @@ -414,13 +421,14 @@ where } } -impl MapEncoder for WireEncoder +impl MapEncoder for WireEncoder where C: ?Sized + Context, W: Writer, { + type Cx = C; type Ok = (); - type EncodeEntry<'this> = WireEncoder, F> where Self: 'this; + type EncodeEntry<'this> = WireEncoder, F, C> where Self: 'this; #[inline] fn encode_entry(&mut self, _: &C) -> Result, C::Error> { @@ -433,14 +441,15 @@ where } } -impl MapEntriesEncoder for WireEncoder +impl MapEntriesEncoder for WireEncoder where C: ?Sized + Context, W: Writer, { + type Cx = C; type Ok = (); - type EncodeMapEntryKey<'this> = WireEncoder, F> where Self: 'this; - type EncodeMapEntryValue<'this> = WireEncoder, F> where Self: 'this; + type EncodeMapEntryKey<'this> = WireEncoder, F, C> where Self: 'this; + type EncodeMapEntryValue<'this> = WireEncoder, F, C> where Self: 'this; #[inline] fn encode_map_entry_key(&mut self, _: &C) -> Result, C::Error> { @@ -458,14 +467,15 @@ where } } -impl MapEntryEncoder for WireEncoder +impl MapEntryEncoder for WireEncoder where C: ?Sized + Context, W: Writer, { + type Cx = C; type Ok = (); - type EncodeMapKey<'this> = WireEncoder, F> where Self: 'this; - type EncodeMapValue<'this> = WireEncoder, F> where Self: 'this; + type EncodeMapKey<'this> = WireEncoder, F, C> where Self: 'this; + type EncodeMapValue<'this> = WireEncoder, F, C> where Self: 'this; #[inline] fn encode_map_key(&mut self, _: &C) -> Result, C::Error> { @@ -483,13 +493,14 @@ where } } -impl StructEncoder for WireEncoder +impl StructEncoder for WireEncoder where C: ?Sized + Context, W: Writer, { + type Cx = C; type Ok = (); - type EncodeField<'this> = WireEncoder, F> where Self: 'this; + type EncodeField<'this> = WireEncoder, F, C> where Self: 'this; #[inline] fn encode_field(&mut self, cx: &C) -> Result, C::Error> { @@ -502,14 +513,15 @@ where } } -impl StructFieldEncoder for WireEncoder +impl StructFieldEncoder for WireEncoder where C: ?Sized + Context, W: Writer, { + type Cx = C; type Ok = (); - type EncodeFieldName<'this> = WireEncoder, F> where Self: 'this; - type EncodeFieldValue<'this> = WireEncoder, F> where Self: 'this; + type EncodeFieldName<'this> = WireEncoder, F, C> where Self: 'this; + type EncodeFieldValue<'this> = WireEncoder, F, C> where Self: 'this; #[inline] fn encode_field_name(&mut self, cx: &C) -> Result, C::Error> { @@ -527,14 +539,15 @@ where } } -impl VariantEncoder for WireEncoder +impl VariantEncoder for WireEncoder where C: ?Sized + Context, W: Writer, { + type Cx = C; type Ok = (); - type EncodeTag<'this> = WireEncoder, F> where Self: 'this; - type EncodeValue<'this> = WireEncoder, F> where Self: 'this; + type EncodeTag<'this> = WireEncoder, F, C> where Self: 'this; + type EncodeValue<'this> = WireEncoder, F, C> where Self: 'this; #[inline] fn encode_tag(&mut self, _: &C) -> Result, C::Error> { diff --git a/crates/musli-wire/src/encoding.rs b/crates/musli-wire/src/encoding.rs index 44bf8edb3..a64845b19 100644 --- a/crates/musli-wire/src/encoding.rs +++ b/crates/musli-wire/src/encoding.rs @@ -172,7 +172,7 @@ impl Encoding { } } - musli_common::encoding_impls!(M, WireEncoder::<_, F>::new, WireDecoder::<_, F>::new); + musli_common::encoding_impls!(M, WireEncoder::<_, F, _>::new, WireDecoder::<_, F, _>::new); musli_common::encoding_from_slice_impls!(M, WireDecoder::<_, F>::new); } diff --git a/crates/musli-wire/src/tag.rs b/crates/musli-wire/src/tag.rs index 4a438a563..0be39618f 100644 --- a/crates/musli-wire/src/tag.rs +++ b/crates/musli-wire/src/tag.rs @@ -5,7 +5,6 @@ use core::fmt; use core::mem; -use musli::Context; use musli::{Decode, Decoder}; /// Data masked into the data type. @@ -146,10 +145,9 @@ impl fmt::Debug for Tag { impl<'de, M> Decode<'de, M> for Tag { #[inline] - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de, Mode = M>, { Ok(Self::from_byte(decoder.decode_u8(cx)?)) } diff --git a/crates/musli-wire/src/test.rs b/crates/musli-wire/src/test.rs index 6466a3c2e..f79999244 100644 --- a/crates/musli-wire/src/test.rs +++ b/crates/musli-wire/src/test.rs @@ -30,10 +30,9 @@ impl<'de, M, T> Decode<'de, M> for Typed where T: Decode<'de, M>, { - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de, Mode = M>, { let mut unpack = decoder.decode_pack(cx)?; let tag = cx.decode(unpack.decode_next(cx)?)?; diff --git a/crates/musli/README.md b/crates/musli/README.md index 0d7bf1312..a58f7d436 100644 --- a/crates/musli/README.md +++ b/crates/musli/README.md @@ -126,10 +126,9 @@ struct MyType { } impl<'de, M> Decode<'de, M> for MyType { - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de, Mode = M>, { let mut seq = decoder.decode_sequence(cx)?; let mut data = Vec::with_capacity(seq.size_hint(cx).or_default()); diff --git a/crates/musli/src/compat.rs b/crates/musli/src/compat.rs index 90566f824..a014fa64d 100644 --- a/crates/musli/src/compat.rs +++ b/crates/musli/src/compat.rs @@ -3,7 +3,6 @@ use crate::de::{Decode, DecodeBytes, Decoder, SequenceDecoder}; use crate::en::{Encode, EncodeBytes, Encoder, SequenceEncoder}; -use crate::Context; /// Ensures that the given value `T` is encoded as a sequence. /// @@ -22,20 +21,18 @@ impl Sequence { impl Encode for Sequence<()> { #[inline] - fn encode(&self, cx: &C, encoder: E) -> Result + fn encode(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { encoder.encode_sequence(cx, 0)?.end(cx) } } impl<'de, M> Decode<'de, M> for Sequence<()> { - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de>, { let seq = decoder.decode_sequence(cx)?; seq.end(cx)?; @@ -54,7 +51,7 @@ impl<'de, M> Decode<'de, M> for Sequence<()> { /// # Examples /// /// ``` -/// use musli::{Context, Decode, Decoder}; +/// use musli::{Decode, Decoder}; /// use musli::compat::Bytes; /// /// struct Struct { @@ -62,10 +59,9 @@ impl<'de, M> Decode<'de, M> for Sequence<()> { /// } /// /// impl<'de, M> Decode<'de, M> for Struct { -/// fn decode(cx: &C, decoder: D) -> Result +/// fn decode(cx: &D::Cx, decoder: D) -> Result /// where -/// C: ?Sized + Context, -/// D: Decoder<'de, C>, +/// D: Decoder<'de, Mode = M>, /// { /// let Bytes(field) = Decode::decode(cx, decoder)?; /// @@ -107,7 +103,7 @@ where /// # Examples /// /// ``` -/// use musli::{Context, Decode, Decoder}; +/// use musli::{Decode, Decoder}; /// use musli::compat::Packed; /// /// struct Struct { @@ -116,10 +112,9 @@ where /// } /// /// impl<'de, M> Decode<'de, M> for Struct { -/// fn decode(cx: &C, decoder: D) -> Result +/// fn decode(cx: &D::Cx, decoder: D) -> Result /// where -/// C: ?Sized + Context, -/// D: Decoder<'de, C>, +/// D: Decoder<'de, Mode = M>, /// { /// let Packed((field, field2)) = Decode::decode(cx, decoder)?; /// diff --git a/crates/musli/src/context.rs b/crates/musli/src/context.rs index d6ca2acdb..b55c8082c 100644 --- a/crates/musli/src/context.rs +++ b/crates/musli/src/context.rs @@ -34,7 +34,7 @@ pub trait Context { fn decode<'de, T, D>(&self, decoder: D) -> Result where T: Decode<'de, Self::Mode>, - D: Decoder<'de, Self>, + D: Decoder<'de, Cx = Self, Mode = Self::Mode>, { T::decode(self, decoder) } @@ -43,7 +43,7 @@ pub trait Context { fn decode_bytes<'de, T, D>(&self, decoder: D) -> Result where T: DecodeBytes<'de, Self::Mode>, - D: Decoder<'de, Self>, + D: Decoder<'de, Cx = Self, Mode = Self::Mode>, { T::decode_bytes(self, decoder) } diff --git a/crates/musli/src/de/as_decoder.rs b/crates/musli/src/de/as_decoder.rs index a7e4006b2..495ff008b 100644 --- a/crates/musli/src/de/as_decoder.rs +++ b/crates/musli/src/de/as_decoder.rs @@ -3,12 +3,19 @@ use crate::Context; use super::Decoder; /// Trait that allows a type to be repeatedly coerced into a decoder. -pub trait AsDecoder { +pub trait AsDecoder { + /// Context associated with the decoder. + type Cx: ?Sized + Context; /// The decoder we reborrow as. - type Decoder<'this>: Decoder<'this, C> + type Decoder<'this>: Decoder< + 'this, + Cx = Self::Cx, + Error = ::Error, + Mode = ::Mode, + > where Self: 'this; /// Borrow self as a new decoder. - fn as_decoder(&self, cx: &C) -> Result, C::Error>; + fn as_decoder(&self, cx: &Self::Cx) -> Result, ::Error>; } diff --git a/crates/musli/src/de/decode.rs b/crates/musli/src/de/decode.rs index 959d0e8e5..9f513f1d8 100644 --- a/crates/musli/src/de/decode.rs +++ b/crates/musli/src/de/decode.rs @@ -26,17 +26,16 @@ pub use musli_macros::Decode; /// Implementing manually: /// /// ``` -/// use musli::{Context, Decode, Decoder}; +/// use musli::{Decode, Decoder}; /// /// struct MyType { /// data: [u8; 128], /// } /// /// impl<'de, M> Decode<'de, M> for MyType { -/// fn decode(cx: &C, decoder: D) -> Result +/// fn decode(cx: &D::Cx, decoder: D) -> Result /// where -/// C: ?Sized + Context, -/// D: Decoder<'de, C>, +/// D: Decoder<'de>, /// { /// Ok(Self { /// data: decoder.decode_array(cx)?, @@ -46,10 +45,9 @@ pub use musli_macros::Decode; /// ``` pub trait Decode<'de, M = DefaultMode>: Sized { /// Decode the given input. - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result::Error> where - C: ?Sized + Context, - D: Decoder<'de, C>; + D: Decoder<'de, Mode = M>; } /// Trait governing how types are decoded specifically for tracing. @@ -62,8 +60,7 @@ pub trait Decode<'de, M = DefaultMode>: Sized { /// [`fmt::Display`]: std::fmt::Display pub trait TraceDecode<'de, M = DefaultMode>: Sized { /// Decode the given input. - fn trace_decode(cx: &C, decoder: D) -> Result + fn trace_decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>; + D: Decoder<'de, Mode = M>; } diff --git a/crates/musli/src/de/decode_bytes.rs b/crates/musli/src/de/decode_bytes.rs index f99164389..a828f8061 100644 --- a/crates/musli/src/de/decode_bytes.rs +++ b/crates/musli/src/de/decode_bytes.rs @@ -24,7 +24,7 @@ use crate::Context; /// Implementing manually: /// /// ``` -/// use musli::{Context, Decode, Decoder}; +/// use musli::{Decode, Decoder}; /// use musli::de::DecodeBytes; /// /// struct MyType { @@ -32,10 +32,9 @@ use crate::Context; /// } /// /// impl<'de, M> Decode<'de, M> for MyType { -/// fn decode(cx: &C, decoder: D) -> Result +/// fn decode(cx: &D::Cx, decoder: D) -> Result /// where -/// C: ?Sized + Context, -/// D: Decoder<'de, C>, +/// D: Decoder<'de>, /// { /// Ok(Self { /// data: DecodeBytes::decode_bytes(cx, decoder)?, @@ -45,8 +44,7 @@ use crate::Context; /// ``` pub trait DecodeBytes<'de, M = DefaultMode>: Sized { /// Decode the given input as bytes. - fn decode_bytes(cx: &C, decoder: D) -> Result + fn decode_bytes(cx: &D::Cx, decoder: D) -> Result::Error> where - C: ?Sized + Context, - D: Decoder<'de, C>; + D: Decoder<'de, Mode = M>; } diff --git a/crates/musli/src/de/decoder.rs b/crates/musli/src/de/decoder.rs index 15a91fdf4..524ad10d5 100644 --- a/crates/musli/src/de/decoder.rs +++ b/crates/musli/src/de/decoder.rs @@ -1,7 +1,6 @@ #![allow(unused_variables)] use core::fmt; -use core::marker::PhantomData; use crate::de::{NumberVisitor, StructDecoder, TypeHint, ValueVisitor, Visitor}; use crate::expecting::{self, Expecting}; @@ -10,28 +9,34 @@ use crate::Context; use super::{AsDecoder, MapDecoder, PackDecoder, SequenceDecoder, VariantDecoder}; /// Trait governing the implementation of a decoder. -pub trait Decoder<'de, C: ?Sized + Context>: Sized { +pub trait Decoder<'de>: Sized { + /// Context associated with the decoder. + type Cx: ?Sized + Context; + /// Error associated with decoding. + type Error; + /// Mode associated with decoding. + type Mode; /// [`Decoder`] with a different context returned by /// [`Decoder::with_context`] - type WithContext: Decoder<'de, U> + type WithContext: Decoder<'de, Cx = U, Error = U::Error, Mode = U::Mode> where U: Context; /// Decoder returned by [`Decoder::decode_buffer`]. - type DecodeBuffer: AsDecoder; + type DecodeBuffer: AsDecoder; /// Decoder returned by [`Decoder::decode_option`]. - type DecodeSome: Decoder<'de, C>; + type DecodeSome: Decoder<'de, Cx = Self::Cx, Error = Self::Error, Mode = Self::Mode>; /// Decoder returned by [`Decoder::decode_pack`]. - type DecodePack: PackDecoder<'de, C>; + type DecodePack: PackDecoder<'de, Cx = Self::Cx>; /// Decoder returned by [`Decoder::decode_sequence`]. - type DecodeSequence: SequenceDecoder<'de, C>; + type DecodeSequence: SequenceDecoder<'de, Cx = Self::Cx>; /// Decoder returned by [`Decoder::decode_tuple`]. - type DecodeTuple: PackDecoder<'de, C>; + type DecodeTuple: PackDecoder<'de, Cx = Self::Cx>; /// Decoder returned by [`Decoder::decode_map`]. - type DecodeMap: MapDecoder<'de, C>; + type DecodeMap: MapDecoder<'de, Cx = Self::Cx>; /// Decoder returned by [`Decoder::decode_struct`]. - type DecodeStruct: StructDecoder<'de, C>; + type DecodeStruct: StructDecoder<'de, Cx = Self::Cx>; /// Decoder returned by [`Decoder::decode_variant`]. - type DecodeVariant: VariantDecoder<'de, C>; + type DecodeVariant: VariantDecoder<'de, Cx = Self::Cx>; /// This is a type argument used to hint to any future implementor that they /// should be using the [`#[musli::decoder]`][crate::decoder] attribute @@ -40,7 +45,10 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { type __UseMusliDecoderAttributeMacro; /// Construct an decoder with a different context. - fn with_context(self, cx: &C) -> Result, C::Error> + fn with_context( + self, + cx: &Self::Cx, + ) -> Result, ::Error> where U: Context, { @@ -56,21 +64,26 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// # Examples /// /// ``` - /// use core::fmt; - /// use core::convert::Infallible; + /// use std::fmt; + /// use std::convert::Infallible; + /// use std::marker::PhantomData; /// /// use musli::Context; /// use musli::de::{self, Decoder}; /// - /// struct MyDecoder; + /// struct MyDecoder { + /// _marker: PhantomData, + /// } /// /// #[musli::decoder] - /// impl Decoder<'_, C> for MyDecoder { + /// impl Decoder<'_> for MyDecoder { + /// type Cx = C; + /// /// fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { /// write!(f, "32-bit unsigned integers") /// } /// - /// fn decode_u32(self, _: &C) -> Result { + /// fn decode_u32(self, _: &C) -> Result::Error> { /// Ok(42) /// } /// } @@ -78,7 +91,7 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result; /// Skip over the current next value. - fn skip(self, cx: &C) -> Result<(), C::Error> { + fn skip(self, cx: &Self::Cx) -> Result<(), ::Error> { Err(cx.message(format_args!( "Skipping is not supported, expected {}", ExpectingWrapper::new(&self).format() @@ -92,7 +105,7 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// detailed (`a 32-bit unsigned integer`) to vague (`a number`). /// /// This is used to construct dynamic containers of types. - fn type_hint(&mut self, cx: &C) -> Result { + fn type_hint(&mut self, cx: &Self::Cx) -> Result::Error> { Ok(TypeHint::Any) } @@ -124,10 +137,9 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// } /// /// impl<'de, M> Decode<'de, M> for MyVariantType { - /// fn decode(cx: &C, decoder: D) -> Result + /// fn decode(cx: &D::Cx, decoder: D) -> Result /// where - /// C: ?Sized + Context, - /// D: Decoder<'de, C>, + /// D: Decoder<'de>, /// { /// let mut buffer = decoder.decode_buffer(cx)?; /// @@ -138,7 +150,7 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// return Err(cx.missing_variant_tag("MyVariantType")); /// }; /// - /// let found = e.decode_map_key(cx)?.decode_string(cx, musli::utils::visit_owned_fn("a string that is 'type'", |cx: &C, string: &str| { + /// let found = e.decode_map_key(cx)?.decode_string(cx, musli::utils::visit_owned_fn("a string that is 'type'", |cx: &D::Cx, string: &str| { /// Ok(string == "type") /// }))?; /// @@ -158,7 +170,10 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// } /// ``` #[inline] - fn decode_buffer(self, cx: &C) -> Result { + fn decode_buffer( + self, + cx: &Self::Cx, + ) -> Result::Error> { Err(cx.message(format_args!( "Decode buffering not supported, expected {}", ExpectingWrapper::new(&self).format() @@ -181,14 +196,13 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Decode, Decoder}; + /// use musli::{Decode, Decoder}; /// # struct UnitType; /// /// impl<'de, M> Decode<'de, M> for UnitType { - /// fn decode(cx: &C, decoder: D) -> Result + /// fn decode(cx: &D::Cx, decoder: D) -> Result /// where - /// C: ?Sized + Context, - /// D: Decoder<'de, C>, + /// D: Decoder<'de>, /// { /// decoder.decode_unit(cx)?; /// Ok(UnitType) @@ -196,7 +210,7 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// } /// ``` #[inline] - fn decode_unit(self, cx: &C) -> Result<(), C::Error> { + fn decode_unit(self, cx: &Self::Cx) -> Result<(), ::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Unit, ExpectingWrapper::new(&self), @@ -222,14 +236,13 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Decode, Decoder}; + /// use musli::{Decode, Decoder}; /// # struct BooleanField { field: bool } /// /// impl<'de, M> Decode<'de, M> for BooleanField { - /// fn decode(cx: &C, decoder: D) -> Result + /// fn decode(cx: &D::Cx, decoder: D) -> Result /// where - /// C: ?Sized + Context, - /// D: Decoder<'de, C>, + /// D: Decoder<'de>, /// { /// Ok(Self { /// field: decoder.decode_bool(cx)?, @@ -238,7 +251,7 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// } /// ``` #[inline] - fn decode_bool(self, cx: &C) -> Result { + fn decode_bool(self, cx: &Self::Cx) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Bool, ExpectingWrapper::new(&self), @@ -264,14 +277,13 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Decode, Decoder}; + /// use musli::{Decode, Decoder}; /// # struct MyType { data: char } /// /// impl<'de, M> Decode<'de, M> for MyType { - /// fn decode(cx: &C, decoder: D) -> Result + /// fn decode(cx: &D::Cx, decoder: D) -> Result /// where - /// C: ?Sized + Context, - /// D: Decoder<'de, C>, + /// D: Decoder<'de>, /// { /// Ok(Self { /// data: decoder.decode_char(cx)?, @@ -280,7 +292,7 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// } /// ``` #[inline] - fn decode_char(self, cx: &C) -> Result { + fn decode_char(self, cx: &Self::Cx) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Char, ExpectingWrapper::new(&self), @@ -306,14 +318,13 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Decode, Decoder}; + /// use musli::{Decode, Decoder}; /// # struct MyType { data: u8 } /// /// impl<'de, M> Decode<'de, M> for MyType { - /// fn decode(cx: &C, decoder: D) -> Result + /// fn decode(cx: &D::Cx, decoder: D) -> Result /// where - /// C: ?Sized + Context, - /// D: Decoder<'de, C>, + /// D: Decoder<'de>, /// { /// Ok(Self { /// data: decoder.decode_u8(cx)?, @@ -322,7 +333,7 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// } /// ``` #[inline] - fn decode_u8(self, cx: &C) -> Result { + fn decode_u8(self, cx: &Self::Cx) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Unsigned8, ExpectingWrapper::new(&self), @@ -348,14 +359,13 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Decode, Decoder}; + /// use musli::{Decode, Decoder}; /// # struct MyType { data: u16 } /// /// impl<'de, M> Decode<'de, M> for MyType { - /// fn decode(cx: &C, decoder: D) -> Result + /// fn decode(cx: &D::Cx, decoder: D) -> Result /// where - /// C: ?Sized + Context, - /// D: Decoder<'de, C>, + /// D: Decoder<'de>, /// { /// Ok(Self { /// data: decoder.decode_u16(cx)?, @@ -364,7 +374,7 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// } /// ``` #[inline] - fn decode_u16(self, cx: &C) -> Result { + fn decode_u16(self, cx: &Self::Cx) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Unsigned16, ExpectingWrapper::new(&self), @@ -390,14 +400,13 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Decode, Decoder}; + /// use musli::{Decode, Decoder}; /// # struct MyType { data: u32 } /// /// impl<'de, M> Decode<'de, M> for MyType { - /// fn decode(cx: &C, decoder: D) -> Result + /// fn decode(cx: &D::Cx, decoder: D) -> Result /// where - /// C: ?Sized + Context, - /// D: Decoder<'de, C>, + /// D: Decoder<'de>, /// { /// Ok(Self { /// data: decoder.decode_u32(cx)?, @@ -406,7 +415,7 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// } /// ``` #[inline] - fn decode_u32(self, cx: &C) -> Result { + fn decode_u32(self, cx: &Self::Cx) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Unsigned32, ExpectingWrapper::new(&self), @@ -432,14 +441,13 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Decode, Decoder}; + /// use musli::{Decode, Decoder}; /// # struct MyType { data: u64 } /// /// impl<'de, M> Decode<'de, M> for MyType { - /// fn decode(cx: &C, decoder: D) -> Result + /// fn decode(cx: &D::Cx, decoder: D) -> Result /// where - /// C: ?Sized + Context, - /// D: Decoder<'de, C>, + /// D: Decoder<'de>, /// { /// Ok(Self { /// data: decoder.decode_u64(cx)?, @@ -448,7 +456,7 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// } /// ``` #[inline] - fn decode_u64(self, cx: &C) -> Result { + fn decode_u64(self, cx: &Self::Cx) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Unsigned64, ExpectingWrapper::new(&self), @@ -474,14 +482,13 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Decode, Decoder}; + /// use musli::{Decode, Decoder}; /// # struct MyType { data: u128 } /// /// impl<'de, M> Decode<'de, M> for MyType { - /// fn decode(cx: &C, decoder: D) -> Result + /// fn decode(cx: &D::Cx, decoder: D) -> Result /// where - /// C: ?Sized + Context, - /// D: Decoder<'de, C>, + /// D: Decoder<'de>, /// { /// Ok(Self { /// data: decoder.decode_u128(cx)?, @@ -490,7 +497,7 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// } /// ``` #[inline] - fn decode_u128(self, cx: &C) -> Result { + fn decode_u128(self, cx: &Self::Cx) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Unsigned128, ExpectingWrapper::new(&self), @@ -516,14 +523,13 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Decode, Decoder}; + /// use musli::{Decode, Decoder}; /// # struct MyType { data: i8 } /// /// impl<'de, M> Decode<'de, M> for MyType { - /// fn decode(cx: &C, decoder: D) -> Result + /// fn decode(cx: &D::Cx, decoder: D) -> Result /// where - /// C: ?Sized + Context, - /// D: Decoder<'de, C>, + /// D: Decoder<'de>, /// { /// Ok(Self { /// data: decoder.decode_i8(cx)?, @@ -532,7 +538,7 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// } /// ``` #[inline] - fn decode_i8(self, cx: &C) -> Result { + fn decode_i8(self, cx: &Self::Cx) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Signed8, ExpectingWrapper::new(&self), @@ -558,14 +564,13 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Decode, Decoder}; + /// use musli::{Decode, Decoder}; /// # struct MyType { data: i16 } /// /// impl<'de, M> Decode<'de, M> for MyType { - /// fn decode(cx: &C, decoder: D) -> Result + /// fn decode(cx: &D::Cx, decoder: D) -> Result /// where - /// C: ?Sized + Context, - /// D: Decoder<'de, C>, + /// D: Decoder<'de>, /// { /// Ok(Self { /// data: decoder.decode_i16(cx)?, @@ -574,7 +579,7 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// } /// ``` #[inline] - fn decode_i16(self, cx: &C) -> Result { + fn decode_i16(self, cx: &Self::Cx) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Signed16, ExpectingWrapper::new(&self), @@ -600,14 +605,13 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Decode, Decoder}; + /// use musli::{Decode, Decoder}; /// # struct MyType { data: i32 } /// /// impl<'de, M> Decode<'de, M> for MyType { - /// fn decode(cx: &C, decoder: D) -> Result + /// fn decode(cx: &D::Cx, decoder: D) -> Result /// where - /// C: ?Sized + Context, - /// D: Decoder<'de, C>, + /// D: Decoder<'de>, /// { /// Ok(Self { /// data: decoder.decode_i32(cx)?, @@ -616,7 +620,7 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// } /// ``` #[inline] - fn decode_i32(self, cx: &C) -> Result { + fn decode_i32(self, cx: &Self::Cx) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Signed32, ExpectingWrapper::new(&self), @@ -642,14 +646,13 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Decode, Decoder}; + /// use musli::{Decode, Decoder}; /// # struct MyType { data: i64 } /// /// impl<'de, M> Decode<'de, M> for MyType { - /// fn decode(cx: &C, decoder: D) -> Result + /// fn decode(cx: &D::Cx, decoder: D) -> Result /// where - /// C: ?Sized + Context, - /// D: Decoder<'de, C>, + /// D: Decoder<'de>, /// { /// Ok(Self { /// data: decoder.decode_i64(cx)?, @@ -658,7 +661,7 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// } /// ``` #[inline] - fn decode_i64(self, cx: &C) -> Result { + fn decode_i64(self, cx: &Self::Cx) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Signed64, ExpectingWrapper::new(&self), @@ -684,14 +687,13 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Decode, Decoder}; + /// use musli::{Decode, Decoder}; /// # struct MyType { data: i128 } /// /// impl<'de, M> Decode<'de, M> for MyType { - /// fn decode(cx: &C, decoder: D) -> Result + /// fn decode(cx: &D::Cx, decoder: D) -> Result /// where - /// C: ?Sized + Context, - /// D: Decoder<'de, C>, + /// D: Decoder<'de>, /// { /// Ok(Self { /// data: decoder.decode_i128(cx)?, @@ -700,7 +702,7 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// } /// ``` #[inline] - fn decode_i128(self, cx: &C) -> Result { + fn decode_i128(self, cx: &Self::Cx) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Signed128, ExpectingWrapper::new(&self), @@ -726,14 +728,13 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Decode, Decoder}; + /// use musli::{Decode, Decoder}; /// # struct MyType { data: usize } /// /// impl<'de, M> Decode<'de, M> for MyType { - /// fn decode(cx: &C, decoder: D) -> Result + /// fn decode(cx: &D::Cx, decoder: D) -> Result /// where - /// C: ?Sized + Context, - /// D: Decoder<'de, C>, + /// D: Decoder<'de>, /// { /// Ok(Self { /// data: decoder.decode_usize(cx)?, @@ -742,7 +743,7 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// } /// ``` #[inline] - fn decode_usize(self, cx: &C) -> Result { + fn decode_usize(self, cx: &Self::Cx) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Usize, ExpectingWrapper::new(&self), @@ -768,14 +769,13 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Decode, Decoder}; + /// use musli::{Decode, Decoder}; /// # struct MyType { data: isize } /// /// impl<'de, M> Decode<'de, M> for MyType { - /// fn decode(cx: &C, decoder: D) -> Result + /// fn decode(cx: &D::Cx, decoder: D) -> Result /// where - /// C: ?Sized + Context, - /// D: Decoder<'de, C>, + /// D: Decoder<'de>, /// { /// Ok(Self { /// data: decoder.decode_isize(cx)?, @@ -784,7 +784,7 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// } /// ``` #[inline] - fn decode_isize(self, cx: &C) -> Result { + fn decode_isize(self, cx: &Self::Cx) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Isize, ExpectingWrapper::new(&self), @@ -810,14 +810,13 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Decode, Decoder}; + /// use musli::{Decode, Decoder}; /// # struct MyType { data: f32 } /// /// impl<'de, M> Decode<'de, M> for MyType { - /// fn decode(cx: &C, decoder: D) -> Result + /// fn decode(cx: &D::Cx, decoder: D) -> Result /// where - /// C: ?Sized + Context, - /// D: Decoder<'de, C>, + /// D: Decoder<'de>, /// { /// Ok(Self { /// data: decoder.decode_f32(cx)?, @@ -826,7 +825,7 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// } /// ``` #[inline] - fn decode_f32(self, cx: &C) -> Result { + fn decode_f32(self, cx: &Self::Cx) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Float32, ExpectingWrapper::new(&self), @@ -852,14 +851,13 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Decode, Decoder}; + /// use musli::{Decode, Decoder}; /// # struct MyType { data: f64 } /// /// impl<'de, M> Decode<'de, M> for MyType { - /// fn decode(cx: &C, decoder: D) -> Result + /// fn decode(cx: &D::Cx, decoder: D) -> Result /// where - /// C: ?Sized + Context, - /// D: Decoder<'de, C>, + /// D: Decoder<'de>, /// { /// Ok(Self { /// data: decoder.decode_f64(cx)?, @@ -868,7 +866,7 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// } /// ``` #[inline] - fn decode_f64(self, cx: &C) -> Result { + fn decode_f64(self, cx: &Self::Cx) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Float64, ExpectingWrapper::new(&self), @@ -878,9 +876,13 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// Decode an unknown number using a visitor that can handle arbitrary /// precision numbers. #[inline] - fn decode_number(self, cx: &C, visitor: V) -> Result + fn decode_number( + self, + cx: &Self::Cx, + visitor: V, + ) -> Result::Error> where - V: NumberVisitor<'de, C>, + V: NumberVisitor<'de, Self::Cx>, { Err(cx.message(expecting::unsupported_type( &expecting::Number, @@ -907,14 +909,13 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Decode, Decoder}; + /// use musli::{Decode, Decoder}; /// # struct MyType { data: [u8; 128] } /// /// impl<'de, M> Decode<'de, M> for MyType { - /// fn decode(cx: &C, decoder: D) -> Result + /// fn decode(cx: &D::Cx, decoder: D) -> Result /// where - /// C: ?Sized + Context, - /// D: Decoder<'de, C>, + /// D: Decoder<'de>, /// { /// Ok(Self { /// data: decoder.decode_array(cx)?, @@ -923,7 +924,10 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// } /// ``` #[inline] - fn decode_array(self, cx: &C) -> Result<[u8; N], C::Error> { + fn decode_array( + self, + cx: &Self::Cx, + ) -> Result<[u8; N], ::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Array, ExpectingWrapper::new(&self), @@ -957,10 +961,9 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// /// impl<'de, M> Decode<'de, M> for BytesReference<'de> { /// #[inline] - /// fn decode(cx: &C, decoder: D) -> Result + /// fn decode(cx: &D::Cx, decoder: D) -> Result /// where - /// C: ?Sized + Context, - /// D: Decoder<'de, C>, + /// D: Decoder<'de>, /// { /// struct Visitor; /// @@ -988,9 +991,13 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// } /// ``` #[inline] - fn decode_bytes(self, cx: &C, visitor: V) -> Result + fn decode_bytes( + self, + cx: &Self::Cx, + visitor: V, + ) -> Result::Error> where - V: ValueVisitor<'de, C, [u8]>, + V: ValueVisitor<'de, Self::Cx, [u8]>, { Err(cx.message(expecting::unsupported_type( &expecting::Bytes, @@ -1025,10 +1032,9 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// /// impl<'de, M> Decode<'de, M> for StringReference<'de> { /// #[inline] - /// fn decode(cx: &C, decoder: D) -> Result + /// fn decode(cx: &D::Cx, decoder: D) -> Result /// where - /// C: ?Sized + Context, - /// D: Decoder<'de, C>, + /// D: Decoder<'de>, /// { /// struct Visitor; /// @@ -1056,9 +1062,13 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// } /// ``` #[inline] - fn decode_string(self, cx: &C, visitor: V) -> Result + fn decode_string( + self, + cx: &Self::Cx, + visitor: V, + ) -> Result::Error> where - V: ValueVisitor<'de, C, str>, + V: ValueVisitor<'de, Self::Cx, str>, { Err(cx.message(expecting::unsupported_type( &expecting::String, @@ -1073,7 +1083,7 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// Deriving an implementation: /// /// ``` - /// use musli::Decode; + /// use musli::{Context, Decode}; /// /// #[derive(Decode)] /// struct OptionalField { @@ -1088,10 +1098,9 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// # struct OptionalField { data: Option} /// /// impl<'de, M> Decode<'de, M> for OptionalField { - /// fn decode(cx: &C, decoder: D) -> Result + /// fn decode(cx: &D::Cx, decoder: D) -> Result /// where - /// C: ?Sized + Context, - /// D: Decoder<'de, C>, + /// D: Decoder<'de>, /// { /// let data = if let Some(decoder) = decoder.decode_option(cx)? { /// Some(cx.decode(decoder)?) @@ -1105,7 +1114,10 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// ``` #[inline] #[must_use = "Decoders must be consumed"] - fn decode_option(self, cx: &C) -> Result, C::Error> { + fn decode_option( + self, + cx: &Self::Cx, + ) -> Result, ::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Option, ExpectingWrapper::new(&self), @@ -1143,10 +1155,9 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// /// impl<'de, M> Decode<'de, M> for PackedStruct { /// #[inline] - /// fn decode(cx: &C, decoder: D) -> Result + /// fn decode(cx: &D::Cx, decoder: D) -> Result /// where - /// C: ?Sized + Context, - /// D: Decoder<'de, C>, + /// D: Decoder<'de>, /// { /// let mut pack = decoder.decode_pack(cx)?; /// let field = pack.next(cx)?; @@ -1158,7 +1169,7 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// } /// ``` #[inline] - fn decode_pack(self, cx: &C) -> Result { + fn decode_pack(self, cx: &Self::Cx) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Pack, ExpectingWrapper::new(&self), @@ -1196,10 +1207,9 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// /// impl<'de, M> Decode<'de, M> for PackedStruct { /// #[inline] - /// fn decode(cx: &C, decoder: D) -> Result + /// fn decode(cx: &D::Cx, decoder: D) -> Result /// where - /// C: ?Sized + Context, - /// D: Decoder<'de, C>, + /// D: Decoder<'de>, /// { /// decoder.decode_pack_fn(cx, |pack| Ok(Self { /// field: pack.next(cx)?, @@ -1209,9 +1219,9 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// } /// ``` #[inline] - fn decode_pack_fn(self, cx: &C, f: F) -> Result + fn decode_pack_fn(self, cx: &Self::Cx, f: F) -> Result::Error> where - F: FnOnce(&mut Self::DecodePack) -> Result, + F: FnOnce(&mut Self::DecodePack) -> Result::Error>, { let mut pack = self.decode_pack(cx)?; let result = f(&mut pack)?; @@ -1242,10 +1252,9 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// # struct VectorField { data: Vec } /// /// impl<'de, M> Decode<'de, M> for VectorField { - /// fn decode(cx: &C, decoder: D) -> Result + /// fn decode(cx: &D::Cx, decoder: D) -> Result /// where - /// C: ?Sized + Context, - /// D: Decoder<'de, C>, + /// D: Decoder<'de>, /// { /// let mut seq = decoder.decode_sequence(cx)?; /// let mut data = Vec::new(); @@ -1261,7 +1270,10 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// } /// ``` #[inline] - fn decode_sequence(self, cx: &C) -> Result { + fn decode_sequence( + self, + cx: &Self::Cx, + ) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Sequence, ExpectingWrapper::new(&self), @@ -1294,10 +1306,9 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// } /// /// impl<'de, M> Decode<'de, M> for VectorField { - /// fn decode(cx: &C, decoder: D) -> Result + /// fn decode(cx: &D::Cx, decoder: D) -> Result /// where - /// C: ?Sized + Context, - /// D: Decoder<'de, C>, + /// D: Decoder<'de>, /// { /// decoder.decode_sequence_fn(cx, |seq| { /// let mut data = Vec::new(); @@ -1312,9 +1323,13 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// } /// ``` #[inline] - fn decode_sequence_fn(self, cx: &C, f: F) -> Result + fn decode_sequence_fn( + self, + cx: &Self::Cx, + f: F, + ) -> Result::Error> where - F: FnOnce(&mut Self::DecodeSequence) -> Result, + F: FnOnce(&mut Self::DecodeSequence) -> Result::Error>, { let mut sequence = self.decode_sequence(cx)?; let result = f(&mut sequence)?; @@ -1343,10 +1358,9 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// # struct TupleStruct(String, u32); /// /// impl<'de, M> Decode<'de, M> for TupleStruct { - /// fn decode(cx: &C, decoder: D) -> Result + /// fn decode(cx: &D::Cx, decoder: D) -> Result /// where - /// C: ?Sized + Context, - /// D: Decoder<'de, C>, + /// D: Decoder<'de>, /// { /// let mut tuple = decoder.decode_tuple(cx, 2)?; /// let string = cx.decode(tuple.decode_next(cx)?)?; @@ -1359,9 +1373,9 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { #[inline] fn decode_tuple( self, - cx: &C, + cx: &Self::Cx, #[allow(unused)] len: usize, - ) -> Result { + ) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Tuple, ExpectingWrapper::new(&self), @@ -1389,10 +1403,9 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// # struct TupleStruct(String, u32); /// /// impl<'de, M> Decode<'de, M> for TupleStruct { - /// fn decode(cx: &C, decoder: D) -> Result + /// fn decode(cx: &D::Cx, decoder: D) -> Result /// where - /// C: ?Sized + Context, - /// D: Decoder<'de, C>, + /// D: Decoder<'de>, /// { /// decoder.decode_tuple_fn(cx, 2, |tuple| { /// Ok(Self(tuple.next(cx)?, tuple.next(cx)?)) @@ -1401,9 +1414,14 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// } /// ``` #[inline] - fn decode_tuple_fn(self, cx: &C, len: usize, f: F) -> Result + fn decode_tuple_fn( + self, + cx: &Self::Cx, + len: usize, + f: F, + ) -> Result::Error> where - F: FnOnce(&mut Self::DecodeTuple) -> Result, + F: FnOnce(&mut Self::DecodeTuple) -> Result::Error>, { let mut tuple = self.decode_tuple(cx, len)?; let result = f(&mut tuple)?; @@ -1422,7 +1440,8 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// /// ``` /// use std::collections::HashMap; - /// use musli::Decode; + /// + /// use musli::{Context, Decode}; /// /// #[derive(Decode)] /// struct MapStruct { @@ -1439,10 +1458,9 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// # struct MapStruct { data: HashMap } /// /// impl<'de, M> Decode<'de, M> for MapStruct { - /// fn decode(cx: &C, decoder: D) -> Result + /// fn decode(cx: &D::Cx, decoder: D) -> Result /// where - /// C: ?Sized + Context, - /// D: Decoder<'de, C>, + /// D: Decoder<'de>, /// { /// let mut map = decoder.decode_map(cx)?; /// let mut data = HashMap::with_capacity(map.size_hint(cx).or_default()); @@ -1462,7 +1480,7 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// } /// ``` #[inline] - fn decode_map(self, cx: &C) -> Result { + fn decode_map(self, cx: &Self::Cx) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Map, ExpectingWrapper::new(&self), @@ -1495,15 +1513,14 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// ``` /// use std::collections::HashMap; /// - /// use musli::{Context, Decode, Decoder}; + /// use musli::{Decode, Decoder}; /// use musli::de::{MapDecoder, MapEntryDecoder}; /// # struct MapStruct { data: HashMap } /// /// impl<'de, M> Decode<'de, M> for MapStruct { - /// fn decode(cx: &C, decoder: D) -> Result + /// fn decode(cx: &D::Cx, decoder: D) -> Result /// where - /// C: ?Sized + Context, - /// D: Decoder<'de, C>, + /// D: Decoder<'de>, /// { /// decoder.decode_map_fn(cx, |map| { /// let mut data = HashMap::with_capacity(map.size_hint(cx).or_default()); @@ -1518,9 +1535,9 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// } /// ``` #[inline] - fn decode_map_fn(self, cx: &C, f: F) -> Result + fn decode_map_fn(self, cx: &Self::Cx, f: F) -> Result::Error> where - F: FnOnce(&mut Self::DecodeMap) -> Result, + F: FnOnce(&mut Self::DecodeMap) -> Result::Error>, { let mut map = self.decode_map(cx)?; let result = f(&mut map)?; @@ -1558,10 +1575,9 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// # struct Struct { string: String, integer: u32 } /// /// impl<'de, M> Decode<'de, M> for Struct { - /// fn decode(cx: &C, decoder: D) -> Result + /// fn decode(cx: &D::Cx, decoder: D) -> Result /// where - /// C: ?Sized + Context, - /// D: Decoder<'de, C>, + /// D: Decoder<'de>, /// { /// let mut st = decoder.decode_struct(cx, None)?; /// let mut string = None; @@ -1594,7 +1610,11 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// } /// ``` #[inline] - fn decode_struct(self, cx: &C, fields: Option) -> Result { + fn decode_struct( + self, + cx: &Self::Cx, + fields: Option, + ) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Struct, ExpectingWrapper::new(&self), @@ -1615,10 +1635,9 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// } /// /// impl<'de, M> Decode<'de, M> for Enum { - /// fn decode(cx: &C, decoder: D) -> Result + /// fn decode(cx: &D::Cx, decoder: D) -> Result /// where - /// C: ?Sized + Context, - /// D: Decoder<'de, C>, + /// D: Decoder<'de>, /// { /// let mut variant = decoder.decode_variant(cx)?; /// let tag = cx.decode(variant.decode_tag(cx)?)?; @@ -1641,7 +1660,10 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// } /// ``` #[inline] - fn decode_variant(self, cx: &C) -> Result { + fn decode_variant( + self, + cx: &Self::Cx, + ) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Variant, ExpectingWrapper::new(&self), @@ -1653,9 +1675,9 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { /// If the current encoding does not support dynamic decoding, /// [`Visitor::visit_any`] will be called with the current decoder. #[inline] - fn decode_any(self, cx: &C, visitor: V) -> Result + fn decode_any(self, cx: &Self::Cx, visitor: V) -> Result::Error> where - V: Visitor<'de, C>, + V: Visitor<'de, Self::Cx>, { Err(cx.message(format_args!( "Any type not supported, expected {}", @@ -1665,22 +1687,20 @@ pub trait Decoder<'de, C: ?Sized + Context>: Sized { } #[repr(transparent)] -struct ExpectingWrapper<'a, T, C: ?Sized> { +struct ExpectingWrapper { inner: T, - _marker: PhantomData<&'a C>, } -impl<'a, T, C: ?Sized> ExpectingWrapper<'a, 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<'a, 'de, T, C> Expecting for ExpectingWrapper<'a, T, C> +impl<'de, T> Expecting for ExpectingWrapper where - T: Decoder<'de, C>, - C: ?Sized + Context, + T: Decoder<'de>, { #[inline] fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { diff --git a/crates/musli/src/de/map_decoder.rs b/crates/musli/src/de/map_decoder.rs index 9df834494..2ec0cf32d 100644 --- a/crates/musli/src/de/map_decoder.rs +++ b/crates/musli/src/de/map_decoder.rs @@ -3,13 +3,15 @@ use crate::Context; use super::{Decode, Decoder, MapEntriesDecoder, MapEntryDecoder, SizeHint}; /// Trait governing how to decode a sequence of pairs. -pub trait MapDecoder<'de, C: ?Sized + Context>: Sized { +pub trait MapDecoder<'de>: Sized { + /// Context associated with the decoder. + type Cx: ?Sized + Context; /// The decoder to use for a key. - type DecodeEntry<'this>: MapEntryDecoder<'de, C> + type DecodeEntry<'this>: MapEntryDecoder<'de, Cx = Self::Cx> where Self: 'this; /// Decoder for a sequence of map pairs. - type IntoMapEntries: MapEntriesDecoder<'de, C>; + type IntoMapEntries: MapEntriesDecoder<'de, Cx = Self::Cx>; /// This is a type argument used to hint to any future implementor that they /// should be using the [`#[musli::map_decoder]`][crate::map_decoder] @@ -18,19 +20,22 @@ pub trait MapDecoder<'de, C: ?Sized + Context>: Sized { type __UseMusliMapDecoderAttributeMacro; /// Get a size hint of known remaining elements. - fn size_hint(&self, cx: &C) -> SizeHint; + fn size_hint(&self, cx: &Self::Cx) -> SizeHint; /// Decode the next key. This returns `Ok(None)` where there are no more /// elements to decode. #[must_use = "Decoders must be consumed"] - fn decode_entry(&mut self, cx: &C) -> Result>, C::Error>; + fn decode_entry( + &mut self, + cx: &Self::Cx, + ) -> Result>, ::Error>; /// End the pair decoder. /// /// If there are any remaining elements in the sequence of pairs, this /// indicates that they should be flushed. #[inline] - fn end(mut self, cx: &C) -> Result<(), C::Error> { + fn end(mut self, cx: &Self::Cx) -> Result<(), ::Error> { // Skip remaining elements. while let Some(mut item) = self.decode_entry(cx)? { item.decode_map_key(cx)?.skip(cx)?; @@ -41,10 +46,10 @@ pub trait MapDecoder<'de, C: ?Sized + Context>: Sized { } /// Decode the next map entry as a tuple. - fn entry(&mut self, cx: &C) -> Result, C::Error> + fn entry(&mut self, cx: &Self::Cx) -> Result, ::Error> where - K: Decode<'de, C::Mode>, - V: Decode<'de, C::Mode>, + K: Decode<'de, ::Mode>, + V: Decode<'de, ::Mode>, { match self.decode_entry(cx)? { Some(mut entry) => { @@ -61,7 +66,10 @@ pub trait MapDecoder<'de, C: ?Sized + Context>: Sized { /// The length of the map must somehow be determined from the underlying /// format. #[inline] - fn into_map_entries(self, cx: &C) -> Result + fn into_map_entries( + self, + cx: &Self::Cx, + ) -> Result::Error> where Self: Sized, { diff --git a/crates/musli/src/de/map_entries_decoder.rs b/crates/musli/src/de/map_entries_decoder.rs index b5dc3b772..9717c6996 100644 --- a/crates/musli/src/de/map_entries_decoder.rs +++ b/crates/musli/src/de/map_entries_decoder.rs @@ -9,14 +9,25 @@ use super::Decoder; /// /// If you do not intend to implement this, then serde compatibility for your /// format might be degraded. -pub trait MapEntriesDecoder<'de, C: ?Sized + Context>: Sized { +pub trait MapEntriesDecoder<'de>: Sized { + /// Context associated with the decoder. + type Cx: ?Sized + Context; /// The decoder to use for a tuple field index. - type DecodeMapEntryKey<'this>: Decoder<'de, C> + type DecodeMapEntryKey<'this>: Decoder< + 'de, + Cx = Self::Cx, + Error = ::Error, + Mode = ::Mode, + > where Self: 'this; - /// The decoder to use for a tuple field value. - type DecodeMapEntryValue<'this>: Decoder<'de, C> + type DecodeMapEntryValue<'this>: Decoder< + 'de, + Cx = Self::Cx, + Error = ::Error, + Mode = ::Mode, + > where Self: 'this; @@ -27,22 +38,25 @@ pub trait MapEntriesDecoder<'de, C: ?Sized + Context>: Sized { #[must_use = "Decoders must be consumed"] fn decode_map_entry_key( &mut self, - cx: &C, - ) -> Result>, C::Error>; + cx: &Self::Cx, + ) -> Result>, ::Error>; /// Decode the value in the map. #[must_use = "Decoders must be consumed"] - fn decode_map_entry_value(&mut self, cx: &C) - -> Result, C::Error>; + fn decode_map_entry_value( + &mut self, + cx: &Self::Cx, + ) -> Result, ::Error>; /// Indicate that the value should be skipped. /// /// The boolean returned indicates if the value was skipped or not. - fn skip_map_entry_value(&mut self, cx: &C) -> Result; + fn skip_map_entry_value(&mut self, cx: &Self::Cx) + -> Result::Error>; /// End pair decoding. #[inline] - fn end(mut self, cx: &C) -> Result<(), C::Error> { + fn end(mut self, cx: &Self::Cx) -> Result<(), ::Error> { loop { let Some(item) = self.decode_map_entry_key(cx)? else { break; diff --git a/crates/musli/src/de/map_entry_decoder.rs b/crates/musli/src/de/map_entry_decoder.rs index ef4614a23..de0eae0ca 100644 --- a/crates/musli/src/de/map_entry_decoder.rs +++ b/crates/musli/src/de/map_entry_decoder.rs @@ -3,27 +3,45 @@ use crate::Context; use super::Decoder; /// Trait governing how to decode a map entry. -pub trait MapEntryDecoder<'de, C: ?Sized + Context> { +pub trait MapEntryDecoder<'de> { + /// Context associated with the decoder. + type Cx: ?Sized + Context; /// The decoder to use for a tuple field index. - type DecodeMapKey<'this>: Decoder<'de, C> + type DecodeMapKey<'this>: Decoder< + 'de, + Cx = Self::Cx, + Error = ::Error, + Mode = ::Mode, + > where Self: 'this; /// The decoder to use for a tuple field value. - type DecodeMapValue: Decoder<'de, C>; + type DecodeMapValue: Decoder< + 'de, + Cx = Self::Cx, + Error = ::Error, + Mode = ::Mode, + >; /// Return the decoder for the first value in the pair. /// /// If this is a map the first value would be the key of the map, if this is /// a struct the first value would be the field of the struct. #[must_use = "Decoders must be consumed"] - fn decode_map_key(&mut self, cx: &C) -> Result, C::Error>; + fn decode_map_key( + &mut self, + cx: &Self::Cx, + ) -> Result, ::Error>; /// Decode the second value in the pair.. #[must_use = "Decoders must be consumed"] - fn decode_map_value(self, cx: &C) -> Result; + fn decode_map_value( + self, + cx: &Self::Cx, + ) -> Result::Error>; /// Indicate that the second value should be skipped. /// /// The boolean returned indicates if the value was skipped or not. - fn skip_map_value(self, cx: &C) -> Result; + fn skip_map_value(self, cx: &Self::Cx) -> Result::Error>; } diff --git a/crates/musli/src/de/number_visitor.rs b/crates/musli/src/de/number_visitor.rs index 13e588bd9..075ff0445 100644 --- a/crates/musli/src/de/number_visitor.rs +++ b/crates/musli/src/de/number_visitor.rs @@ -155,7 +155,7 @@ pub trait NumberVisitor<'de, C: ?Sized + Context>: Sized { #[inline] fn visit_any(self, cx: &C, _: D, hint: TypeHint) -> Result where - D: Decoder<'de, C>, + D: Decoder<'de, Cx = C>, { Err(cx.message(expecting::unsupported_type( &hint, diff --git a/crates/musli/src/de/pack_decoder.rs b/crates/musli/src/de/pack_decoder.rs index aff9df51b..4b46c0e89 100644 --- a/crates/musli/src/de/pack_decoder.rs +++ b/crates/musli/src/de/pack_decoder.rs @@ -3,27 +3,37 @@ use crate::Context; use super::{Decode, Decoder}; /// A pack that can construct decoders. -pub trait PackDecoder<'de, C: ?Sized + Context> { +pub trait PackDecoder<'de> { + /// Context associated with the decoder. + type Cx: ?Sized + Context; /// The encoder to use for the pack. - type DecodeNext<'this>: Decoder<'de, C> + type DecodeNext<'this>: Decoder< + 'de, + Cx = Self::Cx, + Error = ::Error, + Mode = ::Mode, + > where Self: 'this; /// Return decoder to unpack the next element. #[must_use = "Decoders must be consumed"] - fn decode_next(&mut self, cx: &C) -> Result, C::Error>; + fn decode_next( + &mut self, + cx: &Self::Cx, + ) -> Result, ::Error>; /// Stop decoding the current pack. /// /// This is required to call after a pack has finished decoding. - fn end(self, cx: &C) -> Result<(), C::Error>; + fn end(self, cx: &Self::Cx) -> Result<(), ::Error>; /// Unpack a value of the given type. #[inline] - fn next(&mut self, cx: &C) -> Result + fn next(&mut self, cx: &Self::Cx) -> Result::Error> where Self: Sized, - T: Decode<'de, C::Mode>, + T: Decode<'de, ::Mode>, { T::decode(cx, self.decode_next(cx)?) } diff --git a/crates/musli/src/de/sequence_decoder.rs b/crates/musli/src/de/sequence_decoder.rs index 32dde321c..403ae2fe0 100644 --- a/crates/musli/src/de/sequence_decoder.rs +++ b/crates/musli/src/de/sequence_decoder.rs @@ -3,24 +3,34 @@ use crate::Context; use super::{Decode, Decoder, SizeHint}; /// Trait governing how to decode a sequence. -pub trait SequenceDecoder<'de, C: ?Sized + Context>: Sized { +pub trait SequenceDecoder<'de>: Sized { + /// Context associated with the decoder. + type Cx: ?Sized + Context; /// The decoder for individual items. - type DecodeNext<'this>: Decoder<'de, C> + type DecodeNext<'this>: Decoder< + 'de, + Cx = Self::Cx, + Error = ::Error, + Mode = ::Mode, + > where Self: 'this; /// Get a size hint of known remaining elements. - fn size_hint(&self, cx: &C) -> SizeHint; + fn size_hint(&self, cx: &Self::Cx) -> SizeHint; /// Decode the next element. #[must_use = "Decoders must be consumed"] - fn decode_next(&mut self, cx: &C) -> Result>, C::Error>; + fn decode_next( + &mut self, + cx: &Self::Cx, + ) -> Result>, ::Error>; /// Stop decoding the current sequence. /// /// This is required to call after a sequence has finished decoding. #[inline] - fn end(mut self, cx: &C) -> Result<(), C::Error> { + fn end(mut self, cx: &Self::Cx) -> Result<(), ::Error> { while let Some(item) = self.decode_next(cx)? { item.skip(cx)?; } @@ -30,10 +40,10 @@ pub trait SequenceDecoder<'de, C: ?Sized + Context>: Sized { /// Decode the next element of the given type. #[inline] - fn next(&mut self, cx: &C) -> Result, C::Error> + fn next(&mut self, cx: &Self::Cx) -> Result, ::Error> where Self: Sized, - T: Decode<'de, C::Mode>, + T: Decode<'de, ::Mode>, { let Some(decoder) = self.decode_next(cx)? else { return Ok(None); diff --git a/crates/musli/src/de/struct_decoder.rs b/crates/musli/src/de/struct_decoder.rs index 4f049a031..63e181f1d 100644 --- a/crates/musli/src/de/struct_decoder.rs +++ b/crates/musli/src/de/struct_decoder.rs @@ -3,13 +3,15 @@ use crate::Context; use super::{Decoder, SizeHint, StructFieldDecoder, StructFieldsDecoder}; /// Trait governing how to decode fields in a struct. -pub trait StructDecoder<'de, C: ?Sized + Context>: Sized { +pub trait StructDecoder<'de>: Sized { + /// Context associated with the decoder. + type Cx: ?Sized + Context; /// The decoder to use for a key. - type DecodeField<'this>: StructFieldDecoder<'de, C> + type DecodeField<'this>: StructFieldDecoder<'de, Cx = Self::Cx> where Self: 'this; /// Decoder for a sequence of struct pairs. - type IntoStructFields: StructFieldsDecoder<'de, C>; + type IntoStructFields: StructFieldsDecoder<'de, Cx = Self::Cx>; /// This is a type argument used to hint to any future implementor that they /// should be using the [`#[musli::struct_decoder]`][crate::struct_decoder] @@ -18,17 +20,20 @@ pub trait StructDecoder<'de, C: ?Sized + Context>: Sized { type __UseMusliStructDecoderAttributeMacro; /// Get a size hint of known remaining fields. - fn size_hint(&self, cx: &C) -> SizeHint; + fn size_hint(&self, cx: &Self::Cx) -> SizeHint; /// Decode the next field. #[must_use = "Decoders must be consumed"] - fn decode_field(&mut self, cx: &C) -> Result>, C::Error>; + fn decode_field( + &mut self, + cx: &Self::Cx, + ) -> Result>, ::Error>; /// End the struct decoder. /// /// If there are any remaining elements in the sequence of pairs, this /// indicates that they should be flushed. - fn end(mut self, cx: &C) -> Result<(), C::Error> { + fn end(mut self, cx: &Self::Cx) -> Result<(), ::Error> { while let Some(mut item) = self.decode_field(cx)? { item.decode_field_name(cx)?.skip(cx)?; item.skip_field_value(cx)?; @@ -46,7 +51,10 @@ pub trait StructDecoder<'de, C: ?Sized + Context>: Sized { /// /// The size of a struct might therefore change from one session to another. #[inline] - fn into_struct_fields(self, cx: &C) -> Result + fn into_struct_fields( + self, + cx: &Self::Cx, + ) -> Result::Error> where Self: Sized, { diff --git a/crates/musli/src/de/struct_field_decoder.rs b/crates/musli/src/de/struct_field_decoder.rs index a13ed89d3..5315f5f41 100644 --- a/crates/musli/src/de/struct_field_decoder.rs +++ b/crates/musli/src/de/struct_field_decoder.rs @@ -3,24 +3,42 @@ use crate::Context; use super::Decoder; /// Trait governing how to decode a struct field. -pub trait StructFieldDecoder<'de, C: ?Sized + Context> { +pub trait StructFieldDecoder<'de> { + /// Context associated with the decoder. + type Cx: ?Sized + Context; /// The decoder to use for a tuple field index. - type DecodeFieldName<'this>: Decoder<'de, C> + type DecodeFieldName<'this>: Decoder< + 'de, + Cx = Self::Cx, + Error = ::Error, + Mode = ::Mode, + > where Self: 'this; /// The decoder to use for a tuple field value. - type DecodeFieldValue: Decoder<'de, C>; + type DecodeFieldValue: Decoder< + 'de, + Cx = Self::Cx, + Error = ::Error, + Mode = ::Mode, + >; /// Return the decoder for the field name. #[must_use = "Decoders must be consumed"] - fn decode_field_name(&mut self, cx: &C) -> Result, C::Error>; + fn decode_field_name( + &mut self, + cx: &Self::Cx, + ) -> Result, ::Error>; /// Decode the field value. #[must_use = "Decoders must be consumed"] - fn decode_field_value(self, cx: &C) -> Result; + fn decode_field_value( + self, + cx: &Self::Cx, + ) -> Result::Error>; /// Indicate that the field value should be skipped. /// /// The boolean returned indicates if the value was skipped or not. - fn skip_field_value(self, cx: &C) -> Result; + fn skip_field_value(self, cx: &Self::Cx) -> Result::Error>; } diff --git a/crates/musli/src/de/struct_fields_decoder.rs b/crates/musli/src/de/struct_fields_decoder.rs index 860129bae..b05d4be40 100644 --- a/crates/musli/src/de/struct_fields_decoder.rs +++ b/crates/musli/src/de/struct_fields_decoder.rs @@ -9,13 +9,25 @@ use super::Decoder; /// /// If you do not intend to implement this, then serde compatibility for your /// format might be degraded. -pub trait StructFieldsDecoder<'de, C: ?Sized + Context> { +pub trait StructFieldsDecoder<'de> { + /// Context associated with the decoder. + type Cx: ?Sized + Context; /// The decoder to use for a tuple field index. - type DecodeStructFieldName<'this>: Decoder<'de, C> + type DecodeStructFieldName<'this>: Decoder< + 'de, + Cx = Self::Cx, + Error = ::Error, + Mode = ::Mode, + > where Self: 'this; /// The decoder to use for a tuple field value. - type DecodeStructFieldValue<'this>: Decoder<'de, C> + type DecodeStructFieldValue<'this>: Decoder< + 'de, + Cx = Self::Cx, + Error = ::Error, + Mode = ::Mode, + > where Self: 'this; @@ -26,21 +38,24 @@ pub trait StructFieldsDecoder<'de, C: ?Sized + Context> { #[must_use = "Decoders must be consumed"] fn decode_struct_field_name( &mut self, - cx: &C, - ) -> Result, C::Error>; + cx: &Self::Cx, + ) -> Result, ::Error>; /// Decode the second value in the pair.. #[must_use = "Decoders must be consumed"] fn decode_struct_field_value( &mut self, - cx: &C, - ) -> Result, C::Error>; + cx: &Self::Cx, + ) -> Result, ::Error>; /// Indicate that the second value should be skipped. /// /// The boolean returned indicates if the value was skipped or not. - fn skip_struct_field_value(&mut self, cx: &C) -> Result; + fn skip_struct_field_value( + &mut self, + cx: &Self::Cx, + ) -> Result::Error>; /// End pair decoding. - fn end(self, cx: &C) -> Result<(), C::Error>; + fn end(self, cx: &Self::Cx) -> Result<(), ::Error>; } diff --git a/crates/musli/src/de/value_visitor.rs b/crates/musli/src/de/value_visitor.rs index c2c737f59..34d06b210 100644 --- a/crates/musli/src/de/value_visitor.rs +++ b/crates/musli/src/de/value_visitor.rs @@ -62,7 +62,7 @@ where #[inline] fn visit_any(self, cx: &C, _: D, hint: TypeHint) -> Result where - D: Decoder<'de, C>, + D: Decoder<'de, Cx = C>, { Err(cx.message(expecting::unsupported_type( &hint, diff --git a/crates/musli/src/de/variant_decoder.rs b/crates/musli/src/de/variant_decoder.rs index 39ca96882..93879f9c1 100644 --- a/crates/musli/src/de/variant_decoder.rs +++ b/crates/musli/src/de/variant_decoder.rs @@ -3,13 +3,25 @@ use crate::Context; use super::Decoder; /// Trait governing how to decode a variant. -pub trait VariantDecoder<'de, C: ?Sized + Context> { +pub trait VariantDecoder<'de> { + /// Context associated with the decoder. + type Cx: ?Sized + Context; /// The decoder to use for the variant tag. - type DecodeTag<'this>: Decoder<'de, C> + type DecodeTag<'this>: Decoder< + 'de, + Cx = Self::Cx, + Error = ::Error, + Mode = ::Mode, + > where Self: 'this; /// The decoder to use for the variant value. - type DecodeVariant<'this>: Decoder<'de, C> + type DecodeVariant<'this>: Decoder< + 'de, + Cx = Self::Cx, + Error = ::Error, + Mode = ::Mode, + > where Self: 'this; @@ -18,17 +30,23 @@ pub trait VariantDecoder<'de, C: ?Sized + Context> { /// If this is a map the first value would be the key of the map, if this is /// a struct the first value would be the field of the struct. #[must_use = "Decoders must be consumed"] - fn decode_tag(&mut self, cx: &C) -> Result, C::Error>; + fn decode_tag( + &mut self, + cx: &Self::Cx, + ) -> Result, ::Error>; /// Decode the second value in the pair.. #[must_use = "Decoders must be consumed"] - fn decode_value(&mut self, cx: &C) -> Result, C::Error>; + fn decode_value( + &mut self, + cx: &Self::Cx, + ) -> Result, ::Error>; /// Indicate that the second value should be skipped. /// /// The boolean returned indicates if the value was skipped or not. - fn skip_value(&mut self, cx: &C) -> Result; + fn skip_value(&mut self, cx: &Self::Cx) -> Result::Error>; /// End the pair decoder. - fn end(self, cx: &C) -> Result<(), C::Error>; + fn end(self, cx: &Self::Cx) -> Result<(), ::Error>; } diff --git a/crates/musli/src/de/visitor.rs b/crates/musli/src/de/visitor.rs index ae9e4f447..29241ff23 100644 --- a/crates/musli/src/de/visitor.rs +++ b/crates/musli/src/de/visitor.rs @@ -39,7 +39,7 @@ pub trait Visitor<'de, C: ?Sized + Context>: Sized { /// or the underlying format doesn't know which type to decode. fn visit_any(self, cx: &C, _: D, hint: TypeHint) -> Result where - D: Decoder<'de, C>, + D: Decoder<'de, Cx = C>, { Err(cx.message(expecting::unsupported_type( &hint, @@ -204,7 +204,7 @@ pub trait Visitor<'de, C: ?Sized + Context>: Sized { #[inline] fn visit_option(self, cx: &C, _: Option) -> Result where - D: Decoder<'de, C>, + D: Decoder<'de, Cx = C>, { Err(cx.message(expecting::unsupported_type( &expecting::Option, @@ -216,7 +216,7 @@ pub trait Visitor<'de, C: ?Sized + Context>: Sized { #[inline] fn visit_sequence(self, cx: &C, decoder: D) -> Result where - D: SequenceDecoder<'de, C>, + D: SequenceDecoder<'de, Cx = C>, { Err(cx.message(expecting::unsupported_type( &expecting::SequenceWith(decoder.size_hint(cx)), @@ -226,9 +226,9 @@ pub trait Visitor<'de, C: ?Sized + Context>: Sized { /// Indicates that the visited type is a map. #[inline] - fn visit_map(self, cx: &C, decoder: D) -> Result + fn visit_map(self, cx: &C, decoder: D) -> Result::Error> where - D: MapDecoder<'de, C>, + D: MapDecoder<'de, Cx = C>, { Err(cx.message(expecting::unsupported_type( &expecting::MapWith(decoder.size_hint(cx)), @@ -267,7 +267,7 @@ pub trait Visitor<'de, C: ?Sized + Context>: Sized { #[inline] fn visit_variant(self, cx: &C, _: D) -> Result where - D: VariantDecoder<'de, C>, + D: VariantDecoder<'de, Cx = C>, { Err(cx.message(expecting::unsupported_type( &expecting::Variant, diff --git a/crates/musli/src/derives.rs b/crates/musli/src/derives.rs index 5becc93ad..682750af5 100644 --- a/crates/musli/src/derives.rs +++ b/crates/musli/src/derives.rs @@ -613,20 +613,18 @@ //! } //! //! mod module { -//! use musli::{Context, Decoder, Encoder}; +//! use musli::{Decoder, Encoder}; //! //! use super::Field; //! -//! pub fn encode(field: &Field, cx: &C, encoder: E) -> Result +//! pub fn encode(field: &Field, cx: &E::Cx, encoder: E) -> Result //! where -//! C: ?Sized + Context, -//! E: Encoder, +//! E: Encoder, //! # { todo!() } //! -//! pub fn decode<'de, C, D>(cx: &C, decoder: D) -> Result +//! pub fn decode<'de, D>(cx: &D::Cx, decoder: D) -> Result //! where -//! C: ?Sized + Context, -//! D: Decoder<'de, C>, +//! D: Decoder<'de>, //! # { todo!() } //! } //! # } @@ -650,20 +648,18 @@ //! } //! //! mod module { -//! use musli::{Context, Decoder, Encoder}; +//! use musli::{Decoder, Encoder}; //! //! use super::Field; //! -//! pub fn encode(field: &Field, cx: &C, encoder: E) -> Result +//! pub fn encode(field: &Field, cx: &E::Cx, encoder: E) -> Result //! where -//! C: ?Sized + Context, -//! E: Encoder, +//! E: Encoder, //! # { todo!() } //! -//! pub fn decode<'de, C, D, T>(cx: &C, decoder: D) -> Result, C::Error> +//! pub fn decode<'de, D, T>(cx: &D::Cx, decoder: D) -> Result, D::Error> //! where -//! C: ?Sized + Context, -//! D: Decoder<'de, C>, +//! D: Decoder<'de>, //! # { todo!() } //! } //! # } @@ -691,18 +687,16 @@ //! //! use super::CustomUuid; //! -//! pub fn encode(uuid: &CustomUuid, cx: &C, encoder: E) -> Result +//! pub fn encode(uuid: &CustomUuid, cx: &E::Cx, encoder: E) -> Result //! where -//! C: ?Sized + Context, -//! E: Encoder, +//! E: Encoder, //! { //! uuid.0.encode(cx, encoder) //! } //! -//! pub fn decode<'de, C, D>(cx: &C, decoder: D) -> Result +//! pub fn decode<'de, D>(cx: &D::Cx, decoder: D) -> Result //! where -//! C: ?Sized + Context, -//! D: Decoder<'de, C>, +//! D: Decoder<'de>, //! { //! Ok(CustomUuid(cx.decode(decoder)?)) //! } @@ -714,20 +708,18 @@ //! //! use musli::{Context, Decode, Decoder, Encode, Encoder}; //! -//! pub fn encode(set: &HashSet, cx: &C, encoder: E) -> Result +//! pub fn encode(set: &HashSet, cx: &E::Cx, encoder: E) -> Result //! where -//! C: ?Sized + Context, -//! E: Encoder, -//! T: Encode + Eq + Hash, +//! E: Encoder, +//! T: Encode + Eq + Hash, //! { //! HashSet::::encode(set, cx, encoder) //! } //! -//! pub fn decode<'de, C, D, T>(cx: &C, decoder: D) -> Result, C::Error> +//! pub fn decode<'de, D, T>(cx: &D::Cx, decoder: D) -> Result, D::Error> //! where -//! C: ?Sized + Context, -//! D: Decoder<'de, C>, -//! T: Decode<'de, C::Mode> + Eq + Hash, +//! D: Decoder<'de>, +//! T: Decode<'de, D::Mode> + Eq + Hash, //! { //! cx.decode(decoder) //! } diff --git a/crates/musli/src/en/encode.rs b/crates/musli/src/en/encode.rs index c66ccb0fa..ac3ca35e0 100644 --- a/crates/musli/src/en/encode.rs +++ b/crates/musli/src/en/encode.rs @@ -24,17 +24,16 @@ pub use musli_macros::Encode; /// Implementing manually: /// /// ``` -/// use musli::{Context, Encode, Encoder}; +/// use musli::{Encode, Encoder}; /// /// struct MyType { /// data: [u8; 128], /// } /// /// impl Encode for MyType { -/// fn encode(&self, cx: &C, encoder: E) -> Result +/// fn encode(&self, cx: &E::Cx, encoder: E) -> Result /// where -/// C: ?Sized + Context, -/// E: Encoder, +/// E: Encoder, /// { /// encoder.encode_array(cx, &self.data) /// } @@ -42,10 +41,9 @@ pub use musli_macros::Encode; /// ``` pub trait Encode { /// Encode the given output. - fn encode(&self, cx: &C, encoder: E) -> Result + fn encode(&self, cx: &E::Cx, encoder: E) -> Result::Error> where - C: ?Sized + Context, - E: Encoder; + E: Encoder; } /// Trait governing how types are encoded specifically for tracing. @@ -58,10 +56,9 @@ pub trait Encode { /// [`fmt::Display`]: std::fmt::Display pub trait TraceEncode { /// Encode the given output. - fn trace_encode(&self, cx: &C, encoder: E) -> Result + fn trace_encode(&self, cx: &E::Cx, encoder: E) -> Result::Error> where - C: ?Sized + Context, - E: Encoder; + E: Encoder; } impl Encode for &T @@ -69,10 +66,9 @@ where T: ?Sized + Encode, { #[inline] - fn encode(&self, cx: &C, encoder: E) -> Result + fn encode(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { (**self).encode(cx, encoder) } @@ -83,10 +79,9 @@ where T: ?Sized + Encode, { #[inline] - fn encode(&self, cx: &C, encoder: E) -> Result + fn encode(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { (**self).encode(cx, encoder) } diff --git a/crates/musli/src/en/encode_bytes.rs b/crates/musli/src/en/encode_bytes.rs index abf9bf2c5..131e36c1f 100644 --- a/crates/musli/src/en/encode_bytes.rs +++ b/crates/musli/src/en/encode_bytes.rs @@ -1,6 +1,5 @@ use crate::en::Encoder; use crate::mode::DefaultMode; -use crate::Context; /// Trait governing how a type is encoded as bytes. /// @@ -24,7 +23,7 @@ use crate::Context; /// Implementing manually: /// /// ``` -/// use musli::{Context, Encode, Encoder}; +/// use musli::{Encode, Encoder}; /// use musli::en::EncodeBytes; /// /// struct MyType { @@ -32,10 +31,9 @@ use crate::Context; /// } /// /// impl Encode for MyType { -/// fn encode(&self, cx: &C, encoder: E) -> Result +/// fn encode(&self, cx: &E::Cx, encoder: E) -> Result /// where -/// C: ?Sized + Context, -/// E: Encoder, +/// E: Encoder, /// { /// self.data.encode_bytes(cx, encoder) /// } @@ -43,10 +41,9 @@ use crate::Context; /// ``` pub trait EncodeBytes { /// Encode the given output as bytes. - fn encode_bytes(&self, cx: &C, encoder: E) -> Result + fn encode_bytes(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder; + E: Encoder; } impl EncodeBytes for &T @@ -54,10 +51,9 @@ where T: ?Sized + EncodeBytes, { #[inline] - fn encode_bytes(&self, cx: &C, encoder: E) -> Result + fn encode_bytes(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { (**self).encode_bytes(cx, encoder) } @@ -68,10 +64,9 @@ where T: ?Sized + EncodeBytes, { #[inline] - fn encode_bytes(&self, cx: &C, encoder: E) -> Result + fn encode_bytes(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { (**self).encode_bytes(cx, encoder) } diff --git a/crates/musli/src/en/encoder.rs b/crates/musli/src/en/encoder.rs index 05db84c4a..5d8a80951 100644 --- a/crates/musli/src/en/encoder.rs +++ b/crates/musli/src/en/encoder.rs @@ -1,7 +1,6 @@ #![allow(unused_variables)] use core::fmt; -use core::marker::PhantomData; use crate::expecting::{self, Expecting}; use crate::Context; @@ -11,37 +10,43 @@ use super::{ }; /// Trait governing how the encoder works. -pub trait Encoder: Sized { +pub trait Encoder: Sized { + /// Context associated with the encoder. + type Cx: ?Sized + Context; /// The type returned by the encoder. For [Encode] implementations ensures /// that they are used correctly, since only functions returned by the /// [Encoder] is capable of returning this value. type Ok; + /// Error associated with encoding. + type Error; + /// Mode associated with encoding. + type Mode; /// Constructed [`Encoder`] with a different context. - type WithContext: Encoder + type WithContext: Encoder where U: Context; /// A simple pack that packs a sequence of elements. - type EncodePack<'this>: SequenceEncoder + type EncodePack<'this>: SequenceEncoder where - C: 'this; + Self::Cx: 'this; /// Encoder returned when encoding an optional value which is present. - type EncodeSome: Encoder; + type EncodeSome: Encoder; /// The type of a sequence encoder. - type EncodeSequence: SequenceEncoder; + type EncodeSequence: SequenceEncoder; /// The type of a tuple encoder. - type EncodeTuple: SequenceEncoder; + type EncodeTuple: SequenceEncoder; /// The type of a map encoder. - type EncodeMap: MapEncoder; + type EncodeMap: MapEncoder; /// Streaming encoder for map pairs. - type EncodeMapEntries: MapEntriesEncoder; + type EncodeMapEntries: MapEntriesEncoder; /// Encoder that can encode a struct. - type EncodeStruct: StructEncoder; + type EncodeStruct: StructEncoder; /// Encoder for a struct variant. - type EncodeVariant: VariantEncoder; + type EncodeVariant: VariantEncoder; /// Specialized encoder for a tuple variant. - type EncodeTupleVariant: SequenceEncoder; + type EncodeTupleVariant: SequenceEncoder; /// Specialized encoder for a struct variant. - type EncodeStructVariant: StructEncoder; + type EncodeStructVariant: StructEncoder; /// This is a type argument used to hint to any future implementor that they /// should be using the [`#[musli::encoder]`][crate::encoder] attribute @@ -50,7 +55,10 @@ pub trait Encoder: Sized { type __UseMusliEncoderAttributeMacro; /// Construct an encoder with a different context. - fn with_context(self, cx: &C) -> Result, C::Error> + fn with_context( + self, + cx: &Self::Cx, + ) -> Result, ::Error> where U: Context, { @@ -80,21 +88,20 @@ pub trait Encoder: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Encode, Encoder}; + /// use musli::{Encode, Encoder}; /// # struct UnitStruct; /// /// impl Encode for UnitStruct { - /// fn encode(&self, cx: &C, encoder: E) -> Result + /// fn encode(&self, cx: &E::Cx, encoder: E) -> Result /// where - /// C: ?Sized + Context, - /// E: Encoder, + /// E: Encoder, /// { /// encoder.encode_unit(cx) /// } /// } /// ``` #[inline] - fn encode_unit(self, cx: &C) -> Result { + fn encode_unit(self, cx: &Self::Cx) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Unit, ExpectingWrapper::new(&self), @@ -120,21 +127,20 @@ pub trait Encoder: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Encode, Encoder}; + /// use musli::{Encode, Encoder}; /// # struct MyType { data: bool } /// /// impl Encode for MyType { - /// fn encode(&self, cx: &C, encoder: E) -> Result + /// fn encode(&self, cx: &E::Cx, encoder: E) -> Result /// where - /// C: ?Sized + Context, - /// E: Encoder, + /// E: Encoder, /// { /// encoder.encode_bool(cx, self.data) /// } /// } /// ``` #[inline] - fn encode_bool(self, cx: &C, v: bool) -> Result { + fn encode_bool(self, cx: &Self::Cx, v: bool) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Bool, ExpectingWrapper::new(&self), @@ -160,21 +166,20 @@ pub trait Encoder: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Encode, Encoder}; + /// use musli::{Encode, Encoder}; /// # struct MyType { data: char } /// /// impl Encode for MyType { - /// fn encode(&self, cx: &C, encoder: E) -> Result + /// fn encode(&self, cx: &E::Cx, encoder: E) -> Result /// where - /// C: ?Sized + Context, - /// E: Encoder, + /// E: Encoder, /// { /// encoder.encode_char(cx, self.data) /// } /// } /// ``` #[inline] - fn encode_char(self, cx: &C, v: char) -> Result { + fn encode_char(self, cx: &Self::Cx, v: char) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Char, ExpectingWrapper::new(&self), @@ -200,21 +205,20 @@ pub trait Encoder: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Encode, Encoder}; + /// use musli::{Encode, Encoder}; /// # struct MyType { data: u8 } /// /// impl Encode for MyType { - /// fn encode(&self, cx: &C, encoder: E) -> Result + /// fn encode(&self, cx: &E::Cx, encoder: E) -> Result /// where - /// C: ?Sized + Context, - /// E: Encoder, + /// E: Encoder, /// { /// encoder.encode_u8(cx, self.data) /// } /// } /// ``` #[inline] - fn encode_u8(self, cx: &C, v: u8) -> Result { + fn encode_u8(self, cx: &Self::Cx, v: u8) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Unsigned8, ExpectingWrapper::new(&self), @@ -240,21 +244,20 @@ pub trait Encoder: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Encode, Encoder}; + /// use musli::{Encode, Encoder}; /// # struct MyType { data: u16 } /// /// impl Encode for MyType { - /// fn encode(&self, cx: &C, encoder: E) -> Result + /// fn encode(&self, cx: &E::Cx, encoder: E) -> Result /// where - /// C: ?Sized + Context, - /// E: Encoder, + /// E: Encoder, /// { /// encoder.encode_u16(cx, self.data) /// } /// } /// ``` #[inline] - fn encode_u16(self, cx: &C, v: u16) -> Result { + fn encode_u16(self, cx: &Self::Cx, v: u16) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Unsigned16, ExpectingWrapper::new(&self), @@ -280,21 +283,20 @@ pub trait Encoder: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Encode, Encoder}; + /// use musli::{Encode, Encoder}; /// # struct MyType { data: u32 } /// /// impl Encode for MyType { - /// fn encode(&self, cx: &C, encoder: E) -> Result + /// fn encode(&self, cx: &E::Cx, encoder: E) -> Result /// where - /// C: ?Sized + Context, - /// E: Encoder, + /// E: Encoder, /// { /// encoder.encode_u32(cx, self.data) /// } /// } /// ``` #[inline] - fn encode_u32(self, cx: &C, v: u32) -> Result { + fn encode_u32(self, cx: &Self::Cx, v: u32) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Unsigned32, ExpectingWrapper::new(&self), @@ -320,21 +322,20 @@ pub trait Encoder: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Encode, Encoder}; + /// use musli::{Encode, Encoder}; /// # struct MyType { data: u64 } /// /// impl Encode for MyType { - /// fn encode(&self, cx: &C, encoder: E) -> Result + /// fn encode(&self, cx: &E::Cx, encoder: E) -> Result /// where - /// C: ?Sized + Context, - /// E: Encoder, + /// E: Encoder, /// { /// encoder.encode_u64(cx, self.data) /// } /// } /// ``` #[inline] - fn encode_u64(self, cx: &C, v: u64) -> Result { + fn encode_u64(self, cx: &Self::Cx, v: u64) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Unsigned64, ExpectingWrapper::new(&self), @@ -360,21 +361,20 @@ pub trait Encoder: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Encode, Encoder}; + /// use musli::{Encode, Encoder}; /// # struct MyType { data: u128 } /// /// impl Encode for MyType { - /// fn encode(&self, cx: &C, encoder: E) -> Result + /// fn encode(&self, cx: &E::Cx, encoder: E) -> Result /// where - /// C: ?Sized + Context, - /// E: Encoder, + /// E: Encoder, /// { /// encoder.encode_u128(cx, self.data) /// } /// } /// ``` #[inline] - fn encode_u128(self, cx: &C, v: u128) -> Result { + fn encode_u128(self, cx: &Self::Cx, v: u128) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Unsigned128, ExpectingWrapper::new(&self), @@ -400,21 +400,20 @@ pub trait Encoder: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Encode, Encoder}; + /// use musli::{Encode, Encoder}; /// # struct MyType { data: i8 } /// /// impl Encode for MyType { - /// fn encode(&self, cx: &C, encoder: E) -> Result + /// fn encode(&self, cx: &E::Cx, encoder: E) -> Result /// where - /// C: ?Sized + Context, - /// E: Encoder, + /// E: Encoder, /// { /// encoder.encode_i8(cx, self.data) /// } /// } /// ``` #[inline] - fn encode_i8(self, cx: &C, v: i8) -> Result { + fn encode_i8(self, cx: &Self::Cx, v: i8) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Signed8, ExpectingWrapper::new(&self), @@ -440,21 +439,20 @@ pub trait Encoder: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Encode, Encoder}; + /// use musli::{Encode, Encoder}; /// # struct MyType { data: i16 } /// /// impl Encode for MyType { - /// fn encode(&self, cx: &C, encoder: E) -> Result + /// fn encode(&self, cx: &E::Cx, encoder: E) -> Result /// where - /// C: ?Sized + Context, - /// E: Encoder, + /// E: Encoder, /// { /// encoder.encode_i16(cx, self.data) /// } /// } /// ``` #[inline] - fn encode_i16(self, cx: &C, v: i16) -> Result { + fn encode_i16(self, cx: &Self::Cx, v: i16) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Signed16, ExpectingWrapper::new(&self), @@ -480,21 +478,20 @@ pub trait Encoder: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Encode, Encoder}; + /// use musli::{Encode, Encoder}; /// # struct MyType { data: i32 } /// /// impl Encode for MyType { - /// fn encode(&self, cx: &C, encoder: E) -> Result + /// fn encode(&self, cx: &E::Cx, encoder: E) -> Result /// where - /// C: ?Sized + Context, - /// E: Encoder, + /// E: Encoder, /// { /// encoder.encode_i32(cx, self.data) /// } /// } /// ``` #[inline] - fn encode_i32(self, cx: &C, v: i32) -> Result { + fn encode_i32(self, cx: &Self::Cx, v: i32) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Signed32, ExpectingWrapper::new(&self), @@ -520,21 +517,20 @@ pub trait Encoder: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Encode, Encoder}; + /// use musli::{Encode, Encoder}; /// # struct MyType { data: i64 } /// /// impl Encode for MyType { - /// fn encode(&self, cx: &C, encoder: E) -> Result + /// fn encode(&self, cx: &E::Cx, encoder: E) -> Result /// where - /// C: ?Sized + Context, - /// E: Encoder, + /// E: Encoder, /// { /// encoder.encode_i64(cx, self.data) /// } /// } /// ``` #[inline] - fn encode_i64(self, cx: &C, v: i64) -> Result { + fn encode_i64(self, cx: &Self::Cx, v: i64) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Signed64, ExpectingWrapper::new(&self), @@ -560,21 +556,20 @@ pub trait Encoder: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Encode, Encoder}; + /// use musli::{Encode, Encoder}; /// # struct MyType { data: i128 } /// /// impl Encode for MyType { - /// fn encode(&self, cx: &C, encoder: E) -> Result + /// fn encode(&self, cx: &E::Cx, encoder: E) -> Result /// where - /// C: ?Sized + Context, - /// E: Encoder, + /// E: Encoder, /// { /// encoder.encode_i128(cx, self.data) /// } /// } /// ``` #[inline] - fn encode_i128(self, cx: &C, v: i128) -> Result { + fn encode_i128(self, cx: &Self::Cx, v: i128) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Signed128, ExpectingWrapper::new(&self), @@ -600,21 +595,24 @@ pub trait Encoder: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Encode, Encoder}; + /// use musli::{Encode, Encoder}; /// # struct MyType { data: usize } /// /// impl Encode for MyType { - /// fn encode(&self, cx: &C, encoder: E) -> Result + /// fn encode(&self, cx: &E::Cx, encoder: E) -> Result /// where - /// C: ?Sized + Context, - /// E: Encoder, + /// E: Encoder, /// { /// encoder.encode_usize(cx, self.data) /// } /// } /// ``` #[inline] - fn encode_usize(self, cx: &C, v: usize) -> Result { + fn encode_usize( + self, + cx: &Self::Cx, + v: usize, + ) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Usize, ExpectingWrapper::new(&self), @@ -640,21 +638,24 @@ pub trait Encoder: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Encode, Encoder}; + /// use musli::{Encode, Encoder}; /// # struct MyType { data: isize } /// /// impl Encode for MyType { - /// fn encode(&self, cx: &C, encoder: E) -> Result + /// fn encode(&self, cx: &E::Cx, encoder: E) -> Result /// where - /// C: ?Sized + Context, - /// E: Encoder, + /// E: Encoder, /// { /// encoder.encode_isize(cx, self.data) /// } /// } /// ``` #[inline] - fn encode_isize(self, cx: &C, v: isize) -> Result { + fn encode_isize( + self, + cx: &Self::Cx, + v: isize, + ) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Isize, ExpectingWrapper::new(&self), @@ -680,21 +681,20 @@ pub trait Encoder: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Encode, Encoder}; + /// use musli::{Encode, Encoder}; /// # struct MyType { data: f32 } /// /// impl Encode for MyType { - /// fn encode(&self, cx: &C, encoder: E) -> Result + /// fn encode(&self, cx: &E::Cx, encoder: E) -> Result /// where - /// C: ?Sized + Context, - /// E: Encoder, + /// E: Encoder, /// { /// encoder.encode_f32(cx, self.data) /// } /// } /// ``` #[inline] - fn encode_f32(self, cx: &C, v: f32) -> Result { + fn encode_f32(self, cx: &Self::Cx, v: f32) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Float32, ExpectingWrapper::new(&self), @@ -720,21 +720,20 @@ pub trait Encoder: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Encode, Encoder}; + /// use musli::{Encode, Encoder}; /// # struct MyType { data: f64 } /// /// impl Encode for MyType { - /// fn encode(&self, cx: &C, encoder: E) -> Result + /// fn encode(&self, cx: &E::Cx, encoder: E) -> Result /// where - /// C: ?Sized + Context, - /// E: Encoder, + /// E: Encoder, /// { /// encoder.encode_f64(cx, self.data) /// } /// } /// ``` #[inline] - fn encode_f64(self, cx: &C, v: f64) -> Result { + fn encode_f64(self, cx: &Self::Cx, v: f64) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Float64, ExpectingWrapper::new(&self), @@ -760,21 +759,24 @@ pub trait Encoder: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Encode, Encoder}; + /// use musli::{Encode, Encoder}; /// # struct MyType { data: [u8; 128] } /// /// impl Encode for MyType { - /// fn encode(&self, cx: &C, encoder: E) -> Result + /// fn encode(&self, cx: &E::Cx, encoder: E) -> Result /// where - /// C: ?Sized + Context, - /// E: Encoder, + /// E: Encoder, /// { /// encoder.encode_array(cx, &self.data) /// } /// } /// ``` #[inline] - fn encode_array(self, cx: &C, array: &[u8; N]) -> Result { + fn encode_array( + self, + cx: &Self::Cx, + array: &[u8; N], + ) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Array, ExpectingWrapper::new(&self), @@ -801,21 +803,24 @@ pub trait Encoder: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Encode, Encoder}; + /// use musli::{Encode, Encoder}; /// # struct MyType { data: Vec } /// /// impl Encode for MyType { - /// fn encode(&self, cx: &C, encoder: E) -> Result + /// fn encode(&self, cx: &E::Cx, encoder: E) -> Result /// where - /// C: ?Sized + Context, - /// E: Encoder, + /// E: Encoder, /// { /// encoder.encode_bytes(cx, self.data.as_slice()) /// } /// } /// ``` #[inline] - fn encode_bytes(self, cx: &C, bytes: &[u8]) -> Result { + fn encode_bytes( + self, + cx: &Self::Cx, + bytes: &[u8], + ) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Bytes, ExpectingWrapper::new(&self), @@ -851,15 +856,14 @@ pub trait Encoder: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Encode, Encoder}; + /// use musli::{Encode, Encoder}; /// # use std::collections::VecDeque; /// # struct MyType { data: VecDeque } /// /// impl Encode for MyType { - /// fn encode(&self, cx: &C, encoder: E) -> Result + /// fn encode(&self, cx: &E::Cx, encoder: E) -> Result /// where - /// C: ?Sized + Context, - /// E: Encoder, + /// E: Encoder, /// { /// let (first, second) = self.data.as_slices(); /// encoder.encode_bytes_vectored(cx, self.data.len(), [first, second]) @@ -867,7 +871,12 @@ pub trait Encoder: Sized { /// } /// ``` #[inline] - fn encode_bytes_vectored(self, cx: &C, len: usize, vectors: I) -> Result + fn encode_bytes_vectored( + self, + cx: &Self::Cx, + len: usize, + vectors: I, + ) -> Result::Error> where I: IntoIterator, I::Item: AsRef<[u8]>, @@ -897,21 +906,24 @@ pub trait Encoder: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Encode, Encoder}; + /// use musli::{Encode, Encoder}; /// # struct MyType { data: String } /// /// impl Encode for MyType { - /// fn encode(&self, cx: &C, encoder: E) -> Result + /// fn encode(&self, cx: &E::Cx, encoder: E) -> Result /// where - /// C: ?Sized + Context, - /// E: Encoder, + /// E: Encoder, /// { /// encoder.encode_string(cx, self.data.as_str()) /// } /// } /// ``` #[inline] - fn encode_string(self, cx: &C, string: &str) -> Result { + fn encode_string( + self, + cx: &Self::Cx, + string: &str, + ) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::String, ExpectingWrapper::new(&self), @@ -937,14 +949,13 @@ pub trait Encoder: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Encode, Encoder}; + /// use musli::{Encode, Encoder}; /// # struct MyType { data: Option } /// /// impl Encode for MyType { - /// fn encode(&self, cx: &C, encoder: E) -> Result + /// fn encode(&self, cx: &E::Cx, encoder: E) -> Result /// where - /// C: ?Sized + Context, - /// E: Encoder, + /// E: Encoder, /// { /// match &self.data { /// Some(data) => { @@ -958,7 +969,7 @@ pub trait Encoder: Sized { /// } /// ``` #[inline] - fn encode_some(self, cx: &C) -> Result { + fn encode_some(self, cx: &Self::Cx) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Option, ExpectingWrapper::new(&self), @@ -984,14 +995,13 @@ pub trait Encoder: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Encode, Encoder}; + /// use musli::{Encode, Encoder}; /// # struct MyType { data: Option } /// /// impl Encode for MyType { - /// fn encode(&self, cx: &C, encoder: E) -> Result + /// fn encode(&self, cx: &E::Cx, encoder: E) -> Result /// where - /// C: ?Sized + Context, - /// E: Encoder, + /// E: Encoder, /// { /// match &self.data { /// Some(data) => { @@ -1005,7 +1015,7 @@ pub trait Encoder: Sized { /// } /// ``` #[inline] - fn encode_none(self, cx: &C) -> Result { + fn encode_none(self, cx: &Self::Cx) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Option, ExpectingWrapper::new(&self), @@ -1022,7 +1032,7 @@ pub trait Encoder: Sized { /// # Examples /// /// ``` - /// use musli::{Context, Encode, Encoder}; + /// use musli::{Encode, Encoder}; /// use musli::en::{SequenceEncoder}; /// /// struct PackedStruct { @@ -1031,10 +1041,9 @@ pub trait Encoder: Sized { /// } /// /// impl Encode for PackedStruct { - /// fn encode(&self, cx: &C, encoder: E) -> Result + /// fn encode(&self, cx: &E::Cx, encoder: E) -> Result /// where - /// C: ?Sized + Context, - /// E: Encoder, + /// E: Encoder, /// { /// let mut pack = encoder.encode_pack(cx)?; /// self.field.encode(cx, pack.encode_next(cx)?)?; @@ -1044,7 +1053,10 @@ pub trait Encoder: Sized { /// } /// ``` #[inline] - fn encode_pack(self, cx: &C) -> Result, C::Error> { + fn encode_pack( + self, + cx: &Self::Cx, + ) -> Result, ::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Pack, ExpectingWrapper::new(&self), @@ -1056,7 +1068,7 @@ pub trait Encoder: Sized { /// # Examples /// /// ``` - /// use musli::{Context, Encode, Encoder}; + /// use musli::{Encode, Encoder}; /// use musli::en::{SequenceEncoder}; /// /// struct PackedStruct { @@ -1065,10 +1077,9 @@ pub trait Encoder: Sized { /// } /// /// impl Encode for PackedStruct { - /// fn encode(&self, cx: &C, encoder: E) -> Result + /// fn encode(&self, cx: &E::Cx, encoder: E) -> Result /// where - /// C: ?Sized + Context, - /// E: Encoder, + /// E: Encoder, /// { /// encoder.encode_pack_fn(cx, |pack| { /// self.field.encode(cx, pack.encode_next(cx)?)?; @@ -1079,9 +1090,13 @@ pub trait Encoder: Sized { /// } /// ``` #[inline] - fn encode_pack_fn(self, cx: &C, f: F) -> Result + fn encode_pack_fn( + self, + cx: &Self::Cx, + f: F, + ) -> Result::Error> where - F: FnOnce(&mut Self::EncodePack<'_>) -> Result<(), C::Error>, + F: FnOnce(&mut Self::EncodePack<'_>) -> Result<(), ::Error>, { let mut pack = self.encode_pack(cx)?; f(&mut pack)?; @@ -1113,15 +1128,14 @@ pub trait Encoder: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Encode, Encoder}; + /// use musli::{Encode, Encoder}; /// use musli::en::{SequenceEncoder}; /// # struct MyType { data: Vec } /// /// impl Encode for MyType { - /// fn encode(&self, cx: &C, encoder: E) -> Result + /// fn encode(&self, cx: &E::Cx, encoder: E) -> Result /// where - /// C: ?Sized + Context, - /// E: Encoder, + /// E: Encoder, /// { /// let mut seq = encoder.encode_sequence(cx, self.data.len())?; /// @@ -1134,7 +1148,11 @@ pub trait Encoder: Sized { /// } /// ``` #[inline] - fn encode_sequence(self, cx: &C, len: usize) -> Result { + fn encode_sequence( + self, + cx: &Self::Cx, + len: usize, + ) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Sequence, ExpectingWrapper::new(&self), @@ -1146,15 +1164,14 @@ pub trait Encoder: Sized { /// # Examples /// /// ``` - /// use musli::{Context, Encode, Encoder}; + /// use musli::{Encode, Encoder}; /// use musli::en::{SequenceEncoder}; /// # struct MyType { data: Vec } /// /// impl Encode for MyType { - /// fn encode(&self, cx: &C, encoder: E) -> Result + /// fn encode(&self, cx: &E::Cx, encoder: E) -> Result /// where - /// C: ?Sized + Context, - /// E: Encoder, + /// E: Encoder, /// { /// encoder.encode_sequence_fn(cx, self.data.len(), |seq| { /// for element in &self.data { @@ -1167,9 +1184,14 @@ pub trait Encoder: Sized { /// } /// ``` #[inline] - fn encode_sequence_fn(self, cx: &C, len: usize, f: F) -> Result + fn encode_sequence_fn( + self, + cx: &Self::Cx, + len: usize, + f: F, + ) -> Result::Error> where - F: FnOnce(&mut Self::EncodeSequence) -> Result<(), C::Error>, + F: FnOnce(&mut Self::EncodeSequence) -> Result<(), ::Error>, { let mut seq = self.encode_sequence(cx, len)?; f(&mut seq)?; @@ -1189,16 +1211,15 @@ pub trait Encoder: Sized { /// # Examples /// /// ``` - /// use musli::{Context, Encode, Encoder}; + /// use musli::{Encode, Encoder}; /// use musli::en::{SequenceEncoder}; /// /// struct PackedTuple(u32, [u8; 128]); /// /// impl Encode for PackedTuple { - /// fn encode(&self, cx: &C, encoder: E) -> Result + /// fn encode(&self, cx: &E::Cx, encoder: E) -> Result /// where - /// C: ?Sized + Context, - /// E: Encoder, + /// E: Encoder, /// { /// let mut tuple = encoder.encode_tuple(cx, 2)?; /// self.0.encode(cx, tuple.encode_next(cx)?)?; @@ -1208,7 +1229,11 @@ pub trait Encoder: Sized { /// } /// ``` #[inline] - fn encode_tuple(self, cx: &C, len: usize) -> Result { + fn encode_tuple( + self, + cx: &Self::Cx, + len: usize, + ) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Tuple, ExpectingWrapper::new(&self), @@ -1234,15 +1259,14 @@ pub trait Encoder: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Encode, Encoder}; + /// use musli::{Encode, Encoder}; /// use musli::en::{MapEncoder}; /// # struct Struct { name: String, age: u32 } /// /// impl Encode for Struct { - /// fn encode(&self, cx: &C, encoder: E) -> Result + /// fn encode(&self, cx: &E::Cx, encoder: E) -> Result /// where - /// C: ?Sized + Context, - /// E: Encoder, + /// E: Encoder, /// { /// let mut map = encoder.encode_map(cx, 2)?; /// map.insert_entry(cx, "name", &self.name)?; @@ -1252,7 +1276,11 @@ pub trait Encoder: Sized { /// } /// ``` #[inline] - fn encode_map(self, cx: &C, len: usize) -> Result { + fn encode_map( + self, + cx: &Self::Cx, + len: usize, + ) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Map, ExpectingWrapper::new(&self), @@ -1264,7 +1292,7 @@ pub trait Encoder: Sized { /// # Examples /// /// ``` - /// use musli::{Context, Encode, Encoder}; + /// use musli::{Encode, Encoder}; /// use musli::en::{MapEncoder}; /// /// struct Struct { @@ -1273,10 +1301,9 @@ pub trait Encoder: Sized { /// } /// /// impl Encode for Struct { - /// fn encode(&self, cx: &C, encoder: E) -> Result + /// fn encode(&self, cx: &E::Cx, encoder: E) -> Result /// where - /// C: ?Sized + Context, - /// E: Encoder, + /// E: Encoder, /// { /// encoder.encode_map_fn(cx, 2, |map| { /// map.insert_entry(cx, "name", &self.name)?; @@ -1287,9 +1314,14 @@ pub trait Encoder: Sized { /// } /// ``` #[inline] - fn encode_map_fn(self, cx: &C, len: usize, f: F) -> Result + fn encode_map_fn( + self, + cx: &Self::Cx, + len: usize, + f: F, + ) -> Result::Error> where - F: FnOnce(&mut Self::EncodeMap) -> Result<(), C::Error>, + F: FnOnce(&mut Self::EncodeMap) -> Result<(), ::Error>, { let mut map = self.encode_map(cx, len)?; f(&mut map)?; @@ -1301,7 +1333,7 @@ pub trait Encoder: Sized { /// # Examples /// /// ``` - /// use musli::{Context, Encode, Encoder}; + /// use musli::{Encode, Encoder}; /// use musli::en::{MapEntriesEncoder}; /// /// struct Struct { @@ -1310,10 +1342,9 @@ pub trait Encoder: Sized { /// } /// /// impl Encode for Struct { - /// fn encode(&self, cx: &C, encoder: E) -> Result + /// fn encode(&self, cx: &E::Cx, encoder: E) -> Result /// where - /// C: ?Sized + Context, - /// E: Encoder, + /// E: Encoder, /// { /// let mut m = encoder.encode_map_entries(cx, 2)?; /// @@ -1328,7 +1359,11 @@ pub trait Encoder: Sized { /// } /// ``` #[inline] - fn encode_map_entries(self, cx: &C, len: usize) -> Result { + fn encode_map_entries( + self, + cx: &Self::Cx, + len: usize, + ) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::MapPairs, ExpectingWrapper::new(&self), @@ -1340,7 +1375,7 @@ pub trait Encoder: Sized { /// # Examples /// /// ``` - /// use musli::{Context, Encode, Encoder}; + /// use musli::{Encode, Encoder}; /// use musli::en::{StructEncoder}; /// /// struct Struct { @@ -1349,10 +1384,9 @@ pub trait Encoder: Sized { /// } /// /// impl Encode for Struct { - /// fn encode(&self, cx: &C, encoder: E) -> Result + /// fn encode(&self, cx: &E::Cx, encoder: E) -> Result /// where - /// C: ?Sized + Context, - /// E: Encoder, + /// E: Encoder, /// { /// let mut st = encoder.encode_struct(cx, 2)?; /// st.insert_field(cx, "name", &self.name)?; @@ -1362,7 +1396,11 @@ pub trait Encoder: Sized { /// } /// ``` #[inline] - fn encode_struct(self, cx: &C, fields: usize) -> Result { + fn encode_struct( + self, + cx: &Self::Cx, + fields: usize, + ) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Struct, ExpectingWrapper::new(&self), @@ -1374,7 +1412,7 @@ pub trait Encoder: Sized { /// # Examples /// /// ``` - /// use musli::{Context, Encode, Encoder}; + /// use musli::{Encode, Encoder}; /// use musli::en::{StructEncoder}; /// /// struct Struct { @@ -1383,10 +1421,9 @@ pub trait Encoder: Sized { /// } /// /// impl Encode for Struct { - /// fn encode(&self, cx: &C, encoder: E) -> Result + /// fn encode(&self, cx: &E::Cx, encoder: E) -> Result /// where - /// C: ?Sized + Context, - /// E: Encoder, + /// E: Encoder, /// { /// encoder.encode_struct_fn(cx, 2, |st| { /// st.insert_field(cx, "name", &self.name)?; @@ -1397,9 +1434,14 @@ pub trait Encoder: Sized { /// } /// ``` #[inline] - fn encode_struct_fn(self, cx: &C, fields: usize, f: F) -> Result + fn encode_struct_fn( + self, + cx: &Self::Cx, + fields: usize, + f: F, + ) -> Result::Error> where - F: FnOnce(&mut Self::EncodeStruct) -> Result<(), C::Error>, + F: FnOnce(&mut Self::EncodeStruct) -> Result<(), ::Error>, { let mut st = self.encode_struct(cx, fields)?; f(&mut st)?; @@ -1427,15 +1469,14 @@ pub trait Encoder: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Encode, Encoder}; + /// use musli::{Encode, Encoder}; /// use musli::en::{VariantEncoder, StructEncoder, SequenceEncoder}; /// # enum Enum { UnitVariant, TupleVariant(String), StructVariant { data: String, age: u32 } } /// /// impl Encode for Enum { - /// fn encode(&self, cx: &C, encoder: E) -> Result + /// fn encode(&self, cx: &E::Cx, encoder: E) -> Result /// where - /// C: ?Sized + Context, - /// E: Encoder, + /// E: Encoder, /// { /// let mut variant = encoder.encode_variant(cx)?; /// @@ -1461,7 +1502,10 @@ pub trait Encoder: Sized { /// } /// ``` #[inline] - fn encode_variant(self, cx: &C) -> Result { + fn encode_variant( + self, + cx: &Self::Cx, + ) -> Result::Error> { Err(cx.message(expecting::unsupported_type( &expecting::Variant, ExpectingWrapper::new(&self), @@ -1473,7 +1517,7 @@ pub trait Encoder: Sized { /// # Examples /// /// ``` - /// use musli::{Context, Encode, Encoder}; + /// use musli::{Encode, Encoder}; /// use musli::en::{VariantEncoder, StructEncoder, SequenceEncoder}; /// /// enum Enum { @@ -1486,10 +1530,9 @@ pub trait Encoder: Sized { /// } /// /// impl Encode for Enum { - /// fn encode(&self, cx: &C, encoder: E) -> Result + /// fn encode(&self, cx: &E::Cx, encoder: E) -> Result /// where - /// C: ?Sized + Context, - /// E: Encoder, + /// E: Encoder, /// { /// match self { /// Enum::UnitVariant => { @@ -1516,9 +1559,13 @@ pub trait Encoder: Sized { /// } /// ``` #[inline] - fn encode_variant_fn(self, cx: &C, f: F) -> Result + fn encode_variant_fn( + self, + cx: &Self::Cx, + f: F, + ) -> Result::Error> where - F: FnOnce(&mut Self::EncodeVariant) -> Result<(), C::Error>, + F: FnOnce(&mut Self::EncodeVariant) -> Result<(), ::Error>, { let mut variant = self.encode_variant(cx)?; f(&mut variant)?; @@ -1543,15 +1590,14 @@ pub trait Encoder: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Encode, Encoder}; + /// use musli::{Encode, Encoder}; /// use musli::en::{VariantEncoder, StructEncoder, SequenceEncoder}; /// # enum Enum { UnitVariant } /// /// impl Encode for Enum { - /// fn encode(&self, cx: &C, encoder: E) -> Result + /// fn encode(&self, cx: &E::Cx, encoder: E) -> Result /// where - /// C: ?Sized + Context, - /// E: Encoder, + /// E: Encoder, /// { /// match self { /// Enum::UnitVariant => { @@ -1562,9 +1608,13 @@ pub trait Encoder: Sized { /// } /// ``` #[inline] - fn encode_unit_variant(self, cx: &C, tag: &T) -> Result + fn encode_unit_variant( + self, + cx: &Self::Cx, + tag: &T, + ) -> Result::Error> where - T: ?Sized + Encode, + T: ?Sized + Encode<::Mode>, { let mut variant = self.encode_variant(cx)?; let t = variant.encode_tag(cx)?; @@ -1592,15 +1642,14 @@ pub trait Encoder: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Encode, Encoder}; + /// use musli::{Encode, Encoder}; /// use musli::en::{VariantEncoder, StructEncoder, SequenceEncoder}; /// # enum Enum { TupleVariant(String) } /// /// impl Encode for Enum { - /// fn encode(&self, cx: &C, encoder: E) -> Result + /// fn encode(&self, cx: &E::Cx, encoder: E) -> Result /// where - /// C: ?Sized + Context, - /// E: Encoder, + /// E: Encoder, /// { /// match self { /// Enum::TupleVariant(data) => { @@ -1615,12 +1664,12 @@ pub trait Encoder: Sized { #[inline] fn encode_tuple_variant( self, - cx: &C, + cx: &Self::Cx, tag: &T, fields: usize, - ) -> Result + ) -> Result::Error> where - T: ?Sized + Encode, + T: ?Sized + Encode<::Mode>, { Err(cx.message(expecting::unsupported_type( &expecting::TupleVariant, @@ -1649,15 +1698,14 @@ pub trait Encoder: Sized { /// Implementing manually: /// /// ``` - /// use musli::{Context, Encode, Encoder}; + /// use musli::{Encode, Encoder}; /// use musli::en::{VariantEncoder, StructEncoder, SequenceEncoder}; /// # enum Enum { StructVariant { data: String, age: u32 } } /// /// impl Encode for Enum { - /// fn encode(&self, cx: &C, encoder: E) -> Result + /// fn encode(&self, cx: &E::Cx, encoder: E) -> Result /// where - /// C: ?Sized + Context, - /// E: Encoder, + /// E: Encoder, /// { /// match self { /// Enum::StructVariant { data, age } => { @@ -1673,12 +1721,12 @@ pub trait Encoder: Sized { #[inline] fn encode_struct_variant( self, - cx: &C, + cx: &Self::Cx, tag: &T, fields: usize, - ) -> Result + ) -> Result::Error> where - T: ?Sized + Encode, + T: ?Sized + Encode<::Mode>, { Err(cx.message(expecting::unsupported_type( &expecting::StructVariant, @@ -1688,12 +1736,11 @@ pub trait Encoder: Sized { } #[repr(transparent)] -struct ExpectingWrapper<'a, T, C: ?Sized> { +struct ExpectingWrapper { inner: T, - _marker: PhantomData<&'a C>, } -impl<'a, T, C: ?Sized> ExpectingWrapper<'a, T, C> { +impl ExpectingWrapper { #[inline] const fn new(value: &T) -> &Self { // SAFETY: `ExpectingWrapper` is repr(transparent) over `T`. @@ -1701,10 +1748,9 @@ impl<'a, T, C: ?Sized> ExpectingWrapper<'a, T, C> { } } -impl<'a, T, C> Expecting for ExpectingWrapper<'a, T, C> +impl Expecting for ExpectingWrapper where - T: Encoder, - C: ?Sized + Context, + T: Encoder, { #[inline] fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { diff --git a/crates/musli/src/en/map_encoder.rs b/crates/musli/src/en/map_encoder.rs index 3f3305d51..a16b7f76e 100644 --- a/crates/musli/src/en/map_encoder.rs +++ b/crates/musli/src/en/map_encoder.rs @@ -3,27 +3,37 @@ use crate::Context; use super::{Encode, MapEntryEncoder}; /// Encoder for a map. -pub trait MapEncoder { +pub trait MapEncoder { + /// Context associated with the encoder. + type Cx: ?Sized + Context; /// Result type of the encoder. type Ok; /// Encode the next pair. - type EncodeEntry<'this>: MapEntryEncoder + type EncodeEntry<'this>: MapEntryEncoder where Self: 'this; /// Encode the next pair. - fn encode_entry(&mut self, cx: &C) -> Result, C::Error>; + fn encode_entry( + &mut self, + cx: &Self::Cx, + ) -> Result, ::Error>; /// Finish encoding pairs. - fn end(self, cx: &C) -> Result; + fn end(self, cx: &Self::Cx) -> Result::Error>; /// Insert a pair immediately. #[inline] - fn insert_entry(&mut self, cx: &C, key: F, value: S) -> Result<(), C::Error> + fn insert_entry( + &mut self, + cx: &Self::Cx, + key: F, + value: S, + ) -> Result<(), ::Error> where Self: Sized, - F: Encode, - S: Encode, + F: Encode<::Mode>, + S: Encode<::Mode>, { self.encode_entry(cx)?.insert_entry(cx, key, value)?; Ok(()) diff --git a/crates/musli/src/en/map_entries_encoder.rs b/crates/musli/src/en/map_entries_encoder.rs index 68d8a2b69..aca4654ea 100644 --- a/crates/musli/src/en/map_entries_encoder.rs +++ b/crates/musli/src/en/map_entries_encoder.rs @@ -9,36 +9,58 @@ use super::{Encode, Encoder}; /// /// If you do not intend to implement this, then serde compatibility for your /// format might be degraded. -pub trait MapEntriesEncoder { +pub trait MapEntriesEncoder { + /// Context associated with the encoder. + type Cx: ?Sized + Context; /// Result type of the encoder. type Ok; /// The encoder returned when advancing the map encoder to encode the key. - type EncodeMapEntryKey<'this>: Encoder + type EncodeMapEntryKey<'this>: Encoder< + Cx = Self::Cx, + Ok = Self::Ok, + Error = ::Error, + Mode = ::Mode, + > where Self: 'this; /// The encoder returned when advancing the map encoder to encode the value. - type EncodeMapEntryValue<'this>: Encoder + type EncodeMapEntryValue<'this>: Encoder< + Cx = Self::Cx, + Ok = Self::Ok, + Error = ::Error, + Mode = ::Mode, + > where Self: 'this; /// Return the encoder for the key in the entry. #[must_use = "Encoders must be consumed"] - fn encode_map_entry_key(&mut self, cx: &C) -> Result, C::Error>; + fn encode_map_entry_key( + &mut self, + cx: &Self::Cx, + ) -> Result, ::Error>; /// Return encoder for value in the entry. #[must_use = "Encoders must be consumed"] - fn encode_map_entry_value(&mut self, cx: &C) - -> Result, C::Error>; + fn encode_map_entry_value( + &mut self, + cx: &Self::Cx, + ) -> Result, ::Error>; /// Stop encoding this pair. - fn end(self, cx: &C) -> Result; + fn end(self, cx: &Self::Cx) -> Result::Error>; /// Insert the pair immediately. #[inline] - fn insert_entry(&mut self, cx: &C, key: K, value: V) -> Result<(), C::Error> + fn insert_entry( + &mut self, + cx: &Self::Cx, + key: K, + value: V, + ) -> Result<(), ::Error> where - K: Encode, - V: Encode, + K: Encode<::Mode>, + V: Encode<::Mode>, { key.encode(cx, self.encode_map_entry_key(cx)?)?; value.encode(cx, self.encode_map_entry_value(cx)?)?; diff --git a/crates/musli/src/en/map_entry_encoder.rs b/crates/musli/src/en/map_entry_encoder.rs index 0cf8f9295..4d141a7e7 100644 --- a/crates/musli/src/en/map_entry_encoder.rs +++ b/crates/musli/src/en/map_entry_encoder.rs @@ -3,36 +3,59 @@ use crate::Context; use super::{Encode, Encoder}; /// Trait governing how to encode a map entry. -pub trait MapEntryEncoder { +pub trait MapEntryEncoder { + /// Context associated with the encoder. + type Cx: ?Sized + Context; /// Result type of the encoder. type Ok; /// The encoder returned when advancing the map encoder to encode the key. - type EncodeMapKey<'this>: Encoder + type EncodeMapKey<'this>: Encoder< + Cx = Self::Cx, + Ok = Self::Ok, + Error = ::Error, + Mode = ::Mode, + > where Self: 'this; /// The encoder returned when advancing the map encoder to encode the value. - type EncodeMapValue<'this>: Encoder + type EncodeMapValue<'this>: Encoder< + Cx = Self::Cx, + Ok = Self::Ok, + Error = ::Error, + Mode = ::Mode, + > where Self: 'this; /// Return the encoder for the key in the entry. #[must_use = "Encoders must be consumed"] - fn encode_map_key(&mut self, cx: &C) -> Result, C::Error>; + fn encode_map_key( + &mut self, + cx: &Self::Cx, + ) -> Result, ::Error>; /// Return encoder for value in the entry. #[must_use = "Encoders must be consumed"] - fn encode_map_value(&mut self, cx: &C) -> Result, C::Error>; + fn encode_map_value( + &mut self, + cx: &Self::Cx, + ) -> Result, ::Error>; /// Stop encoding this pair. - fn end(self, cx: &C) -> Result; + fn end(self, cx: &Self::Cx) -> Result::Error>; /// Insert the pair immediately. #[inline] - fn insert_entry(mut self, cx: &C, key: K, value: V) -> Result + fn insert_entry( + mut self, + cx: &Self::Cx, + key: K, + value: V, + ) -> Result::Error> where Self: Sized, - K: Encode, - V: Encode, + K: Encode<::Mode>, + V: Encode<::Mode>, { key.encode(cx, self.encode_map_key(cx)?)?; value.encode(cx, self.encode_map_value(cx)?)?; diff --git a/crates/musli/src/en/sequence_encoder.rs b/crates/musli/src/en/sequence_encoder.rs index 2999368e5..eb860e57b 100644 --- a/crates/musli/src/en/sequence_encoder.rs +++ b/crates/musli/src/en/sequence_encoder.rs @@ -3,26 +3,36 @@ use crate::Context; use super::{Encode, Encoder}; /// Trait governing how to encode a sequence. -pub trait SequenceEncoder { +pub trait SequenceEncoder { + /// Context associated with the encoder. + type Cx: ?Sized + Context; /// Result type of the encoder. type Ok; /// The encoder returned when advancing the sequence encoder. - type EncodeNext<'this>: Encoder + type EncodeNext<'this>: Encoder< + Cx = Self::Cx, + Ok = Self::Ok, + Error = ::Error, + Mode = ::Mode, + > where Self: 'this; /// Return encoder for the next element. #[must_use = "Encoder must be consumed"] - fn encode_next(&mut self, cx: &C) -> Result, C::Error>; + fn encode_next( + &mut self, + cx: &Self::Cx, + ) -> Result, ::Error>; /// Finish encoding the sequence. - fn end(self, cx: &C) -> Result; + fn end(self, cx: &Self::Cx) -> Result::Error>; /// Push an element into the sequence. #[inline] - fn push(&mut self, cx: &C, value: T) -> Result<(), C::Error> + fn push(&mut self, cx: &Self::Cx, value: T) -> Result<(), ::Error> where - T: Encode, + T: Encode<::Mode>, { let encoder = self.encode_next(cx)?; value.encode(cx, encoder)?; diff --git a/crates/musli/src/en/struct_encoder.rs b/crates/musli/src/en/struct_encoder.rs index ee2f4145c..ea8488209 100644 --- a/crates/musli/src/en/struct_encoder.rs +++ b/crates/musli/src/en/struct_encoder.rs @@ -3,25 +3,34 @@ use crate::Context; use super::{Encode, StructFieldEncoder}; /// Encoder for a struct. -pub trait StructEncoder { +pub trait StructEncoder { + /// Context associated with the encoder. + type Cx: ?Sized + Context; /// Result type of the encoder. type Ok; /// Encoder for the next struct field. - type EncodeField<'this>: StructFieldEncoder + type EncodeField<'this>: StructFieldEncoder where Self: 'this; /// Encode the next field. - fn encode_field(&mut self, cx: &C) -> Result, C::Error>; + fn encode_field( + &mut self, + cx: &Self::Cx, + ) -> Result, ::Error>; /// Finish encoding the struct. - fn end(self, cx: &C) -> Result; + fn end(self, cx: &Self::Cx) -> Result::Error>; /// Encode the next field using a closure. #[inline] - fn encode_field_fn(&mut self, cx: &C, f: F) -> Result + fn encode_field_fn( + &mut self, + cx: &Self::Cx, + f: F, + ) -> Result::Error> where - F: FnOnce(&mut Self::EncodeField<'_>) -> Result, + F: FnOnce(&mut Self::EncodeField<'_>) -> Result::Error>, { let mut encoder = self.encode_field(cx)?; let output = f(&mut encoder)?; @@ -31,10 +40,15 @@ pub trait StructEncoder { /// Insert a field immediately. #[inline] - fn insert_field(&mut self, cx: &C, field: F, value: V) -> Result<(), C::Error> + fn insert_field( + &mut self, + cx: &Self::Cx, + field: F, + value: V, + ) -> Result<(), ::Error> where - F: Encode, - V: Encode, + F: Encode<::Mode>, + V: Encode<::Mode>, { self.encode_field(cx)?.insert_field(cx, field, value)?; Ok(()) diff --git a/crates/musli/src/en/struct_field_encoder.rs b/crates/musli/src/en/struct_field_encoder.rs index b4c3def4c..f23c12e02 100644 --- a/crates/musli/src/en/struct_field_encoder.rs +++ b/crates/musli/src/en/struct_field_encoder.rs @@ -3,36 +3,59 @@ use crate::Context; use super::{Encode, Encoder}; /// Trait governing how to encode a sequence of pairs. -pub trait StructFieldEncoder { +pub trait StructFieldEncoder { + /// Context associated with the encoder. + type Cx: ?Sized + Context; /// Result type of the encoder. type Ok; /// The encoder returned when advancing the map encoder to encode the key. - type EncodeFieldName<'this>: Encoder + type EncodeFieldName<'this>: Encoder< + Cx = Self::Cx, + Ok = Self::Ok, + Error = ::Error, + Mode = ::Mode, + > where Self: 'this; /// The encoder returned when advancing the map encoder to encode the value. - type EncodeFieldValue<'this>: Encoder + type EncodeFieldValue<'this>: Encoder< + Cx = Self::Cx, + Ok = Self::Ok, + Error = ::Error, + Mode = ::Mode, + > where Self: 'this; /// Return the encoder for the field in the struct. #[must_use = "Encoders must be consumed"] - fn encode_field_name(&mut self, cx: &C) -> Result, C::Error>; + fn encode_field_name( + &mut self, + cx: &Self::Cx, + ) -> Result, ::Error>; /// Return encoder for the field value in the struct. #[must_use = "Encoders must be consumed"] - fn encode_field_value(&mut self, cx: &C) -> Result, C::Error>; + fn encode_field_value( + &mut self, + cx: &Self::Cx, + ) -> Result, ::Error>; /// Stop encoding this field. - fn end(self, cx: &C) -> Result; + fn end(self, cx: &Self::Cx) -> Result::Error>; /// Insert the pair immediately. #[inline] - fn insert_field(mut self, cx: &C, name: N, value: V) -> Result + fn insert_field( + mut self, + cx: &Self::Cx, + name: N, + value: V, + ) -> Result::Error> where Self: Sized, - N: Encode, - V: Encode, + N: Encode<::Mode>, + V: Encode<::Mode>, { name.encode(cx, self.encode_field_name(cx)?)?; value.encode(cx, self.encode_field_value(cx)?)?; diff --git a/crates/musli/src/en/variant_encoder.rs b/crates/musli/src/en/variant_encoder.rs index ef7f20489..8abf68286 100644 --- a/crates/musli/src/en/variant_encoder.rs +++ b/crates/musli/src/en/variant_encoder.rs @@ -3,36 +3,59 @@ use crate::Context; use super::{Encode, Encoder}; /// Trait governing how to encode a variant. -pub trait VariantEncoder { +pub trait VariantEncoder { + /// Context associated with the encoder. + type Cx: ?Sized + Context; /// Result type of the encoder. type Ok; /// The encoder returned when advancing the map encoder to encode the key. - type EncodeTag<'this>: Encoder + type EncodeTag<'this>: Encoder< + Cx = Self::Cx, + Ok = Self::Ok, + Error = ::Error, + Mode = ::Mode, + > where Self: 'this; /// The encoder returned when advancing the map encoder to encode the value. - type EncodeValue<'this>: Encoder + type EncodeValue<'this>: Encoder< + Cx = Self::Cx, + Ok = Self::Ok, + Error = ::Error, + Mode = ::Mode, + > where Self: 'this; /// Return the encoder for the first element in the variant. #[must_use = "Encoders must be consumed"] - fn encode_tag(&mut self, cx: &C) -> Result, C::Error>; + fn encode_tag( + &mut self, + cx: &Self::Cx, + ) -> Result, ::Error>; /// Return encoder for the second element in the variant. #[must_use = "Encoders must be consumed"] - fn encode_value(&mut self, cx: &C) -> Result, C::Error>; + fn encode_value( + &mut self, + cx: &Self::Cx, + ) -> Result, ::Error>; /// End the variant encoder. - fn end(self, cx: &C) -> Result; + fn end(self, cx: &Self::Cx) -> Result::Error>; /// Insert the variant immediately. #[inline] - fn insert_variant(mut self, cx: &C, tag: T, value: V) -> Result + fn insert_variant( + mut self, + cx: &Self::Cx, + tag: T, + value: V, + ) -> Result::Error> where Self: Sized, - T: Encode, - V: Encode, + T: Encode<::Mode>, + V: Encode<::Mode>, { tag.encode(cx, self.encode_tag(cx)?)?; value.encode(cx, self.encode_value(cx)?)?; diff --git a/crates/musli/src/impls/alloc.rs b/crates/musli/src/impls/alloc.rs index 188d92b70..cc3184555 100644 --- a/crates/musli/src/impls/alloc.rs +++ b/crates/musli/src/impls/alloc.rs @@ -31,10 +31,9 @@ use crate::Context; impl Encode for String { #[inline] - fn encode(&self, cx: &C, encoder: E) -> Result + fn encode(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { self.as_str().encode(cx, encoder) } @@ -42,10 +41,9 @@ impl Encode for String { impl<'de, M> Decode<'de, M> for String { #[inline] - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de, Mode = M>, { struct Visitor; @@ -82,10 +80,9 @@ impl<'de, M> Decode<'de, M> for String { impl<'de, M> Decode<'de, M> for Box { #[inline] - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de, Mode = M>, { let string: String = cx.decode(decoder)?; Ok(string.into()) @@ -97,10 +94,9 @@ where T: Decode<'de, M>, { #[inline] - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de, Mode = M>, { let vec: Vec = cx.decode(decoder)?; Ok(Box::from(vec)) @@ -119,10 +115,9 @@ macro_rules! cow { ) => { impl $encode for Cow<'_, $ty> { #[inline] - fn $encode_fn(&self, cx: &C, encoder: E) -> Result + fn $encode_fn(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { self.as_ref().$encode_fn(cx, encoder) } @@ -130,10 +125,9 @@ macro_rules! cow { impl<'de, M> $decode<'de, M> for Cow<'de, $ty> { #[inline] - fn $decode_fn(cx: &C, decoder: D) -> Result + fn $decode_fn(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de, Mode = M>, { struct Visitor; @@ -223,10 +217,9 @@ macro_rules! sequence { $($extra: $extra_bound0 $(+ $extra_bound)*),* { #[inline] - fn encode(&self, $cx: &C, encoder: E) -> Result + fn encode(&self, $cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { let mut seq = encoder.encode_sequence($cx, self.len())?; @@ -250,10 +243,9 @@ macro_rules! sequence { $($extra: $extra_bound0 $(+ $extra_bound)*),* { #[inline] - fn decode($cx: &C, decoder: D) -> Result + fn decode($cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de, Mode = M>, { let mut $access = decoder.decode_sequence($cx)?; let mut out = $factory; @@ -318,10 +310,9 @@ macro_rules! map { $($extra: $extra_bound0 $(+ $extra_bound)*),* { #[inline] - fn encode(&self, $cx: &C, encoder: E) -> Result + fn encode(&self, $cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { let mut map = encoder.encode_map($cx, self.len())?; @@ -343,10 +334,9 @@ macro_rules! map { $($extra: $extra_bound0 $(+ $extra_bound)*),* { #[inline] - fn trace_encode(&self, $cx: &C, encoder: E) -> Result + fn trace_encode(&self, $cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { let mut map = encoder.encode_map($cx, self.len())?; @@ -370,10 +360,9 @@ macro_rules! map { $($extra: $extra_bound0 $(+ $extra_bound)*),* { #[inline] - fn decode($cx: &C, decoder: D) -> Result + fn decode($cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de, Mode = M>, { decoder.decode_map_fn($cx, |$access| { let mut out = $with_capacity; @@ -394,10 +383,9 @@ macro_rules! map { $($extra: $extra_bound0 $(+ $extra_bound)*),* { #[inline] - fn trace_decode($cx: &C, decoder: D) -> Result + fn trace_decode($cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de, Mode = M>, { decoder.decode_map_fn($cx, |$access| { let mut out = $with_capacity; @@ -430,10 +418,9 @@ map!( impl Encode for CString { #[inline] - fn encode(&self, cx: &C, encoder: E) -> Result + fn encode(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { encoder.encode_bytes(cx, self.to_bytes_with_nul()) } @@ -441,10 +428,9 @@ impl Encode for CString { impl<'de, M> Decode<'de, M> for CString { #[inline] - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de>, { struct Visitor; @@ -489,10 +475,9 @@ macro_rules! smart_pointer { T: ?Sized + Encode, { #[inline] - fn encode(&self, cx: &C, encoder: E) -> Result + fn encode(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { self.as_ref().encode(cx, encoder) } @@ -503,10 +488,9 @@ macro_rules! smart_pointer { T: Decode<'de, M>, { #[inline] - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de, Mode = M>, { Ok($ty::new(cx.decode(decoder)?)) } @@ -514,10 +498,9 @@ macro_rules! smart_pointer { impl<'de, M> DecodeBytes<'de, M> for $ty<[u8]> { #[inline] - fn decode_bytes(cx: &C, decoder: D) -> Result + fn decode_bytes(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de, Mode = M>, { Ok($ty::from(>::decode_bytes(cx, decoder)?)) } @@ -525,10 +508,9 @@ macro_rules! smart_pointer { impl<'de, M> Decode<'de, M> for $ty { #[inline] - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de, Mode = M>, { Ok($ty::from(CString::decode(cx, decoder)?)) } @@ -538,10 +520,9 @@ macro_rules! smart_pointer { #[cfg_attr(doc_cfg, doc(cfg(all(feature = "std", any(unix, windows)))))] impl<'de, M> Decode<'de, M> for $ty { #[inline] - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de, Mode = M>, { Ok($ty::from(PathBuf::decode(cx, decoder)?)) } @@ -551,10 +532,9 @@ macro_rules! smart_pointer { #[cfg_attr(doc_cfg, doc(cfg(all(feature = "std", any(unix, windows)))))] impl<'de, M> Decode<'de, M> for $ty { #[inline] - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de, Mode = M>, { Ok($ty::from(OsString::decode(cx, decoder)?)) } @@ -578,10 +558,9 @@ enum PlatformTag { impl Encode for OsStr { #[cfg(unix)] #[inline] - fn encode(&self, cx: &C, encoder: E) -> Result + fn encode(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { use std::os::unix::ffi::OsStrExt; @@ -596,10 +575,9 @@ impl Encode for OsStr { #[cfg(windows)] #[inline] - fn encode(&self, cx: &C, encoder: E) -> Result + fn encode(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { use crate::en::VariantEncoder; use crate::Buf; @@ -629,10 +607,9 @@ impl Encode for OsStr { #[cfg_attr(doc_cfg, doc(cfg(all(feature = "std", any(unix, windows)))))] impl Encode for OsString { #[inline] - fn encode(&self, cx: &C, encoder: E) -> Result + fn encode(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { self.as_os_str().encode(cx, encoder) } @@ -642,10 +619,9 @@ impl Encode for OsString { #[cfg_attr(doc_cfg, doc(cfg(all(feature = "std", any(unix, windows)))))] impl<'de, M> Decode<'de, M> for OsString { #[inline] - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de>, { use crate::de::VariantDecoder; @@ -713,10 +689,9 @@ impl<'de, M> Decode<'de, M> for OsString { #[cfg_attr(doc_cfg, doc(cfg(all(feature = "std", any(unix, windows)))))] impl Encode for Path { #[inline] - fn encode(&self, cx: &C, encoder: E) -> Result + fn encode(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { self.as_os_str().encode(cx, encoder) } @@ -726,10 +701,9 @@ impl Encode for Path { #[cfg_attr(doc_cfg, doc(cfg(all(feature = "std", any(unix, windows)))))] impl Encode for PathBuf { #[inline] - fn encode(&self, cx: &C, encoder: E) -> Result + fn encode(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { self.as_path().encode(cx, encoder) } @@ -739,10 +713,9 @@ impl Encode for PathBuf { #[cfg_attr(doc_cfg, doc(cfg(all(feature = "std", any(unix, windows)))))] impl<'de, M> Decode<'de, M> for PathBuf { #[inline] - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de>, { let string: OsString = cx.decode(decoder)?; Ok(PathBuf::from(string)) @@ -751,10 +724,9 @@ impl<'de, M> Decode<'de, M> for PathBuf { impl EncodeBytes for Vec { #[inline] - fn encode_bytes(&self, cx: &C, encoder: E) -> Result + fn encode_bytes(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { encoder.encode_bytes(cx, self.as_slice()) } @@ -762,10 +734,9 @@ impl EncodeBytes for Vec { impl<'de, M> DecodeBytes<'de, M> for Vec { #[inline] - fn decode_bytes(cx: &C, decoder: D) -> Result + fn decode_bytes(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de, Mode = M>, { struct Visitor; @@ -797,10 +768,9 @@ impl<'de, M> DecodeBytes<'de, M> for Vec { impl EncodeBytes for VecDeque { #[inline] - fn encode_bytes(&self, cx: &C, encoder: E) -> Result + fn encode_bytes(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { let (first, second) = self.as_slices(); encoder.encode_bytes_vectored(cx, self.len(), &[first, second]) @@ -809,10 +779,9 @@ impl EncodeBytes for VecDeque { impl<'de, M> DecodeBytes<'de, M> for VecDeque { #[inline] - fn decode_bytes(cx: &C, decoder: D) -> Result + fn decode_bytes(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de, Mode = M>, { Ok(VecDeque::from(>::decode_bytes(cx, decoder)?)) } diff --git a/crates/musli/src/impls/mod.rs b/crates/musli/src/impls/mod.rs index 567aa8ee4..9790c3b21 100644 --- a/crates/musli/src/impls/mod.rs +++ b/crates/musli/src/impls/mod.rs @@ -20,10 +20,9 @@ use crate::Context; impl Encode for () { #[inline] - fn encode(&self, cx: &C, encoder: E) -> Result + fn encode(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { encoder.encode_unit(cx) } @@ -31,10 +30,9 @@ impl Encode for () { impl<'de, M> Decode<'de, M> for () { #[inline] - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de>, { decoder.decode_unit(cx) } @@ -42,20 +40,18 @@ impl<'de, M> Decode<'de, M> for () { impl Encode for marker::PhantomData { #[inline] - fn encode(&self, cx: &C, encoder: E) -> Result + fn encode(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { encoder.encode_unit(cx) } } impl<'de, M, T> Decode<'de, M> for marker::PhantomData { - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de>, { decoder.decode_unit(cx)?; Ok(marker::PhantomData) @@ -67,10 +63,9 @@ macro_rules! atomic_impl { $( #[cfg(target_has_atomic = $size)] impl<'de, M> Decode<'de, M> for core::sync::atomic::$ty { - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de>, { cx.decode(decoder).map(Self::new) } @@ -89,20 +84,18 @@ macro_rules! non_zero { ($ty:ty) => { impl Encode for $ty { #[inline] - fn encode(&self, cx: &C, encoder: E) -> Result + fn encode(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { self.get().encode(cx, encoder) } } impl<'de, M> Decode<'de, M> for $ty { - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de>, { let value = cx.decode(decoder)?; @@ -154,10 +147,9 @@ where T: Encode, { #[inline] - fn encode(&self, cx: &C, encoder: E) -> Result + fn encode(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { let mut seq = encoder.encode_sequence(cx, N)?; @@ -171,10 +163,9 @@ where impl<'de, M, const N: usize> Decode<'de, M> for [u8; N] { #[inline] - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de>, { decoder.decode_array(cx) } @@ -184,10 +175,9 @@ macro_rules! impl_number { ($ty:ty, $read:ident, $write:ident) => { impl Encode for $ty { #[inline] - fn encode(&self, cx: &C, encoder: E) -> Result + fn encode(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { encoder.$write(cx, *self) } @@ -195,10 +185,9 @@ macro_rules! impl_number { impl<'de, M> Decode<'de, M> for $ty { #[inline] - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de>, { decoder.$read(cx) } @@ -208,10 +197,9 @@ macro_rules! impl_number { impl Encode for bool { #[inline] - fn encode(&self, cx: &C, encoder: E) -> Result + fn encode(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { encoder.encode_bool(cx, *self) } @@ -219,10 +207,9 @@ impl Encode for bool { impl<'de, M> Decode<'de, M> for bool { #[inline] - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de>, { decoder.decode_bool(cx) } @@ -230,10 +217,9 @@ impl<'de, M> Decode<'de, M> for bool { impl Encode for char { #[inline] - fn encode(&self, cx: &C, encoder: E) -> Result + fn encode(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { encoder.encode_char(cx, *self) } @@ -241,10 +227,9 @@ impl Encode for char { impl<'de, M> Decode<'de, M> for char { #[inline] - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de>, { decoder.decode_char(cx) } @@ -267,10 +252,9 @@ impl_number!(f64, decode_f64, encode_f64); impl Encode for str { #[inline] - fn encode(&self, cx: &C, encoder: E) -> Result + fn encode(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { encoder.encode_string(cx, self) } @@ -278,10 +262,9 @@ impl Encode for str { impl<'de, M> Decode<'de, M> for &'de str { #[inline] - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de>, { struct Visitor; @@ -310,10 +293,9 @@ impl Encode for [T] where T: Encode, { - fn encode(&self, cx: &C, encoder: E) -> Result + fn encode(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { let mut seq = encoder.encode_sequence(cx, self.len())?; @@ -333,10 +315,9 @@ where impl<'de, M> Decode<'de, M> for &'de [u8] { #[inline] - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de>, { struct Visitor; @@ -366,10 +347,9 @@ where T: Encode, { #[inline] - fn encode(&self, cx: &C, encoder: E) -> Result + fn encode(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { match self { Some(value) => value.encode(cx, encoder.encode_some(cx)?), @@ -383,10 +363,9 @@ where T: Decode<'de, M>, { #[inline] - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de, Mode = M>, { if let Some(decoder) = decoder.decode_option(cx)? { Ok(Some(cx.decode(decoder)?)) @@ -409,10 +388,9 @@ where U: Encode, { #[inline] - fn encode(&self, cx: &C, encoder: E) -> Result + fn encode(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { let variant = encoder.encode_variant(cx)?; @@ -429,10 +407,9 @@ where U: Decode<'de, M>, { #[inline] - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de, Mode = M>, { let mut variant = decoder.decode_variant(cx)?; @@ -453,10 +430,9 @@ where T: Encode, { #[inline] - fn encode(&self, cx: &C, encoder: E) -> Result + fn encode(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { self.0.encode(cx, encoder) } @@ -467,10 +443,9 @@ where T: Decode<'de, M>, { #[inline] - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de, Mode = M>, { Ok(Wrapping(cx.decode(decoder)?)) } @@ -478,10 +453,9 @@ where impl Encode for CStr { #[inline] - fn encode(&self, cx: &C, encoder: E) -> Result + fn encode(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { encoder.encode_bytes(cx, self.to_bytes_with_nul()) } @@ -489,10 +463,9 @@ impl Encode for CStr { impl<'de, M> Decode<'de, M> for &'de CStr { #[inline] - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de>, { let bytes = cx.decode(decoder)?; CStr::from_bytes_with_nul(bytes).map_err(cx.map()) @@ -501,10 +474,9 @@ impl<'de, M> Decode<'de, M> for &'de CStr { impl EncodeBytes for [u8] { #[inline] - fn encode_bytes(&self, cx: &C, encoder: E) -> Result + fn encode_bytes(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { encoder.encode_bytes(cx, self) } @@ -512,10 +484,9 @@ impl EncodeBytes for [u8] { impl EncodeBytes for [u8; N] { #[inline] - fn encode_bytes(&self, cx: &C, encoder: E) -> Result + fn encode_bytes(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { encoder.encode_array(cx, self) } @@ -523,10 +494,9 @@ impl EncodeBytes for [u8; N] { impl<'de, M> DecodeBytes<'de, M> for &'de [u8] { #[inline] - fn decode_bytes(cx: &C, decoder: D) -> Result + fn decode_bytes(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de>, { Decode::decode(cx, decoder) } @@ -534,10 +504,9 @@ impl<'de, M> DecodeBytes<'de, M> for &'de [u8] { impl<'de, M, const N: usize> DecodeBytes<'de, M> for [u8; N] { #[inline] - fn decode_bytes(cx: &C, decoder: D) -> Result + fn decode_bytes(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de>, { decoder.decode_array(cx) } diff --git a/crates/musli/src/impls/net.rs b/crates/musli/src/impls/net.rs index 6b83ac62f..048ad4da2 100644 --- a/crates/musli/src/impls/net.rs +++ b/crates/musli/src/impls/net.rs @@ -6,10 +6,9 @@ use crate::Context; impl Encode for Ipv4Addr { #[inline] - fn encode(&self, cx: &C, encoder: E) -> Result + fn encode(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { encoder.encode_array(cx, &self.octets()) } @@ -17,10 +16,9 @@ impl Encode for Ipv4Addr { impl<'de, M> Decode<'de, M> for Ipv4Addr { #[inline] - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de>, { decoder.decode_array::<4>(cx).map(Ipv4Addr::from) } @@ -28,10 +26,9 @@ impl<'de, M> Decode<'de, M> for Ipv4Addr { impl Encode for Ipv6Addr { #[inline] - fn encode(&self, cx: &C, encoder: E) -> Result + fn encode(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { encoder.encode_array(cx, &self.octets()) } @@ -39,10 +36,9 @@ impl Encode for Ipv6Addr { impl<'de, M> Decode<'de, M> for Ipv6Addr { #[inline] - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de>, { decoder.decode_array::<16>(cx).map(Ipv6Addr::from) } @@ -57,10 +53,9 @@ enum IpAddrTag { impl Encode for IpAddr { #[inline] - fn encode(&self, cx: &C, encoder: E) -> Result + fn encode(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { let variant = encoder.encode_variant(cx)?; @@ -73,10 +68,9 @@ impl Encode for IpAddr { impl<'de, M> Decode<'de, M> for IpAddr { #[inline] - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de>, { let mut variant = decoder.decode_variant(cx)?; @@ -94,10 +88,9 @@ impl<'de, M> Decode<'de, M> for IpAddr { impl Encode for SocketAddrV4 { #[inline] - fn encode(&self, cx: &C, encoder: E) -> Result + fn encode(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { let mut pack = encoder.encode_pack(cx)?; pack.push(cx, self.ip())?; @@ -108,10 +101,9 @@ impl Encode for SocketAddrV4 { impl<'de, M> Decode<'de, M> for SocketAddrV4 { #[inline] - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de>, { decoder.decode_pack_fn(cx, |pack| { Ok(SocketAddrV4::new(pack.next(cx)?, pack.next(cx)?)) @@ -121,10 +113,9 @@ impl<'de, M> Decode<'de, M> for SocketAddrV4 { impl Encode for SocketAddrV6 { #[inline] - fn encode(&self, cx: &C, encoder: E) -> Result + fn encode(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { let mut pack = encoder.encode_pack(cx)?; pack.push(cx, self.ip())?; @@ -137,10 +128,9 @@ impl Encode for SocketAddrV6 { impl<'de, M> Decode<'de, M> for SocketAddrV6 { #[inline] - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de>, { decoder.decode_pack_fn(cx, |pack| { Ok(Self::new( @@ -162,10 +152,9 @@ enum SocketAddrTag { impl Encode for SocketAddr { #[inline] - fn encode(&self, cx: &C, encoder: E) -> Result + fn encode(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { let variant = encoder.encode_variant(cx)?; @@ -178,10 +167,9 @@ impl Encode for SocketAddr { impl<'de, M> Decode<'de, M> for SocketAddr { #[inline] - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de>, { let mut variant = decoder.decode_variant(cx)?; diff --git a/crates/musli/src/impls/range.rs b/crates/musli/src/impls/range.rs index 6f77cf1a6..71f290a61 100644 --- a/crates/musli/src/impls/range.rs +++ b/crates/musli/src/impls/range.rs @@ -11,10 +11,9 @@ macro_rules! implement { { #[inline] #[allow(unused_mut)] - fn encode(&self, cx: &C, encoder: E) -> Result + fn encode(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { let mut tuple = encoder.encode_tuple(cx, $count)?; $( @@ -29,10 +28,9 @@ macro_rules! implement { $($type: Decode<'de, M>,)* { #[inline] - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de, Mode = M>, { let ($($field,)*) = cx.decode(decoder)?; Ok($ty { $($field,)* }) @@ -48,10 +46,9 @@ macro_rules! implement_new { T: Encode, { #[inline] - fn encode(&self, cx: &C, encoder: E) -> Result + fn encode(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { let mut tuple = encoder.encode_tuple(cx, $count)?; $(self.$field().encode(cx, tuple.encode_next(cx)?)?;)* @@ -64,10 +61,9 @@ macro_rules! implement_new { T: Decode<'de, M>, { #[inline] - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de, Mode = M>, { let ($($field,)*) = Decode::decode(cx, decoder)?; Ok($ty::new($($field,)*)) diff --git a/crates/musli/src/impls/tuples.rs b/crates/musli/src/impls/tuples.rs index fb5fdeb3c..e31fc90a8 100644 --- a/crates/musli/src/impls/tuples.rs +++ b/crates/musli/src/impls/tuples.rs @@ -43,10 +43,9 @@ macro_rules! declare { (($ty0:ident, $ident0:ident) $(, ($ty:ident, $ident:ident))* $(,)?) => { impl Encode for ($ty0, $($ty),*) where $ty0: Encode, $($ty: Encode),* { #[inline] - fn encode(&self, cx: &C, encoder: E) -> Result + fn encode(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { let mut pack = encoder.encode_tuple(cx, count!($ident0 $($ident)*))?; let ($ident0, $($ident),*) = self; @@ -58,10 +57,9 @@ macro_rules! declare { impl<'de, M, $ty0, $($ty,)*> Decode<'de, M> for ($ty0, $($ty),*) where $ty0: Decode<'de, M>, $($ty: Decode<'de, M>),* { #[inline] - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C> + D: Decoder<'de, Mode = M>, { let mut tuple = decoder.decode_tuple(cx, count!($ident0 $($ident)*))?; let $ident0 = cx.decode(tuple.decode_next(cx)?)?; @@ -73,10 +71,9 @@ macro_rules! declare { impl Encode for Packed<($ty0, $($ty),*)> where $ty0: Encode, $($ty: Encode),* { #[inline] - fn encode(&self, cx: &C, encoder: E) -> Result + fn encode(&self, cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { let Packed(($ident0, $($ident),*)) = self; let mut pack = encoder.encode_pack(cx)?; @@ -88,10 +85,9 @@ macro_rules! declare { impl<'de, M, $ty0, $($ty,)*> Decode<'de, M> for Packed<($ty0, $($ty),*)> where $ty0: Decode<'de, M>, $($ty: Decode<'de, M>),* { #[inline] - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C> + D: Decoder<'de, Mode = M>, { decoder.decode_pack_fn(cx, |pack| { let $ident0 = pack.next(cx)?; diff --git a/crates/musli/src/lib.rs b/crates/musli/src/lib.rs index 71a3ecdbe..53dc36104 100644 --- a/crates/musli/src/lib.rs +++ b/crates/musli/src/lib.rs @@ -128,10 +128,9 @@ //! } //! //! impl<'de, M> Decode<'de, M> for MyType { -//! fn decode(cx: &C, decoder: D) -> Result +//! fn decode(cx: &D::Cx, decoder: D) -> Result //! where -//! C: ?Sized + Context, -//! D: Decoder<'de, C>, +//! D: Decoder<'de, Mode = M>, //! { //! let mut seq = decoder.decode_sequence(cx)?; //! let mut data = Vec::with_capacity(seq.size_hint(cx).or_default()); @@ -442,23 +441,26 @@ pub use self::en::{Encode, Encoder}; /// /// ``` /// use std::fmt; +/// use std::marker::PhantomData; /// /// use musli::Context; /// use musli::en::Encoder; /// -/// struct MyEncoder<'a> { +/// struct MyEncoder<'a, C: ?Sized> { /// value: &'a mut Option, +/// _marker: PhantomData, /// } /// /// #[musli::encoder] -/// impl Encoder for MyEncoder<'_> { +/// impl Encoder for MyEncoder<'_, C> { +/// type Cx = C; /// type Ok = (); /// /// fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { /// write!(f, "32-bit unsigned integers") /// } /// -/// fn encode_u32(self, cx: &C, value: u32) -> Result<(), C::Error> { +/// fn encode_u32(self, cx: &C, value: u32) -> Result<(), Self::Error> { /// *self.value = Some(value); /// Ok(()) /// } @@ -481,19 +483,24 @@ pub use musli_macros::encoder; /// /// ``` /// use std::fmt; +/// use std::marker::PhantomData; /// /// use musli::Context; /// use musli::de::Decoder; /// -/// struct MyDecoder; +/// struct MyDecoder { +/// _marker: PhantomData, +/// } /// /// #[musli::decoder] -/// impl Decoder<'_, C> for MyDecoder { +/// impl Decoder<'_> for MyDecoder { +/// type Cx = C; +/// /// fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { /// write!(f, "32-bit unsigned integers") /// } /// -/// fn decode_u32(self, _: &C) -> Result { +/// fn decode_u32(self, _: &C) -> Result { /// Ok(42) /// } /// } @@ -543,7 +550,8 @@ pub use musli_macros::struct_decoder; /// # Examples /// /// ``` -/// use core::fmt; +/// use std::fmt; +/// use std::marker::PhantomData; /// /// use musli::Context; /// use musli::de::Visitor; diff --git a/crates/musli/src/never.rs b/crates/musli/src/never.rs index ff41e0abe..6bd7a3fe3 100644 --- a/crates/musli/src/never.rs +++ b/crates/musli/src/never.rs @@ -53,14 +53,17 @@ pub enum NeverMarker {} /// /// ``` /// use std::fmt; +/// use std::marker::PhantomData; /// /// use musli::Context; /// use musli::de::Decoder; /// -/// struct MyDecoder(u32); +/// struct MyDecoder(u32, PhantomData); /// /// #[musli::decoder] -/// impl Decoder<'_, C> for MyDecoder where { +/// impl Decoder<'_> for MyDecoder where { +/// type Cx = C; +/// /// fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { /// write!(f, "32-bit unsigned integers") /// } @@ -80,8 +83,11 @@ pub struct Never { _marker: marker::PhantomData<(A, B)>, } -impl<'de, C: ?Sized + Context> Decoder<'de, C> for Never { - type WithContext = Self +impl<'de, C: ?Sized + Context> Decoder<'de> for Never<(), C> { + type Cx = C; + type Error = C::Error; + type Mode = C::Mode; + type WithContext = Never<(), U> where U: Context; type DecodeBuffer = Self; @@ -108,8 +114,9 @@ impl<'de, C: ?Sized + Context> Decoder<'de, C> for Never { } } -impl AsDecoder for Never { - type Decoder<'this> = Self; +impl AsDecoder for Never<(), C> { + type Cx = C; + type Decoder<'this> = Self where Self: 'this; #[inline] fn as_decoder(&self, _: &C) -> Result, C::Error> { @@ -117,8 +124,9 @@ impl AsDecoder for Never { } } -impl<'de, C: ?Sized + Context> StructFieldDecoder<'de, C> for Never { - type DecodeFieldName<'this> = Self; +impl<'de, C: ?Sized + Context> StructFieldDecoder<'de> for Never<(), C> { + type Cx = C; + type DecodeFieldName<'this> = Self where Self: 'this; type DecodeFieldValue = Self; #[inline] @@ -137,9 +145,10 @@ impl<'de, C: ?Sized + Context> StructFieldDecoder<'de, C> for Never { } } -impl<'de, C: ?Sized + Context> MapEntriesDecoder<'de, C> for Never { - type DecodeMapEntryKey<'this> = Self; - type DecodeMapEntryValue<'this> = Self; +impl<'de, C: ?Sized + Context> MapEntriesDecoder<'de> for Never<(), C> { + type Cx = C; + type DecodeMapEntryKey<'this> = Self where Self: 'this; + type DecodeMapEntryValue<'this> = Self where Self: 'this; #[inline] fn decode_map_entry_key( @@ -165,8 +174,9 @@ impl<'de, C: ?Sized + Context> MapEntriesDecoder<'de, C> for Never { } } -impl<'de, C: ?Sized + Context> StructDecoder<'de, C> for Never { - type DecodeField<'this> = Self; +impl<'de, C: ?Sized + Context> StructDecoder<'de> for Never<(), C> { + type Cx = C; + type DecodeField<'this> = Self where Self: 'this; type IntoStructFields = Self; type __UseMusliStructDecoderAttributeMacro = (); @@ -192,9 +202,10 @@ impl<'de, C: ?Sized + Context> StructDecoder<'de, C> for Never { } } -impl<'de, C: ?Sized + Context> StructFieldsDecoder<'de, C> for Never { - type DecodeStructFieldName<'this> = Self; - type DecodeStructFieldValue<'this> = Self; +impl<'de, C: ?Sized + Context> StructFieldsDecoder<'de> for Never<(), C> { + type Cx = C; + type DecodeStructFieldName<'this> = Self where Self: 'this; + type DecodeStructFieldValue<'this> = Self where Self: 'this; #[inline] fn decode_struct_field_name( @@ -223,9 +234,10 @@ impl<'de, C: ?Sized + Context> StructFieldsDecoder<'de, C> for Never { } } -impl<'de, C: ?Sized + Context> VariantDecoder<'de, C> for Never { - type DecodeTag<'this> = Self; - type DecodeVariant<'this> = Self; +impl<'de, C: ?Sized + Context> VariantDecoder<'de> for Never<(), C> { + type Cx = C; + type DecodeTag<'this> = Self where Self: 'this; + type DecodeVariant<'this> = Self where Self: 'this; #[inline] fn decode_tag(&mut self, _: &C) -> Result, C::Error> { @@ -248,8 +260,9 @@ impl<'de, C: ?Sized + Context> VariantDecoder<'de, C> for Never { } } -impl<'de, C: ?Sized + Context> MapDecoder<'de, C> for Never { - type DecodeEntry<'this> = Self; +impl<'de, C: ?Sized + Context> MapDecoder<'de> for Never<(), C> { + type Cx = C; + type DecodeEntry<'this> = Self where Self: 'this; type IntoMapEntries = Self; type __UseMusliMapDecoderAttributeMacro = (); @@ -275,8 +288,9 @@ impl<'de, C: ?Sized + Context> MapDecoder<'de, C> for Never { } } -impl<'de, C: ?Sized + Context> MapEntryDecoder<'de, C> for Never { - type DecodeMapKey<'this> = Self; +impl<'de, C: ?Sized + Context> MapEntryDecoder<'de> for Never<(), C> { + type Cx = C; + type DecodeMapKey<'this> = Self where Self: 'this; type DecodeMapValue = Self; #[inline] @@ -295,8 +309,9 @@ impl<'de, C: ?Sized + Context> MapEntryDecoder<'de, C> for Never { } } -impl<'de, C: ?Sized + Context> SequenceDecoder<'de, C> for Never { - type DecodeNext<'this> = Self; +impl<'de, C: ?Sized + Context> SequenceDecoder<'de> for Never<(), C> { + type Cx = C; + type DecodeNext<'this> = Self where Self: 'this; #[inline] fn size_hint(&self, _: &C) -> SizeHint { @@ -314,8 +329,9 @@ impl<'de, C: ?Sized + Context> SequenceDecoder<'de, C> for Never { } } -impl<'de, C: ?Sized + Context> PackDecoder<'de, C> for Never { - type DecodeNext<'this> = Self; +impl<'de, C: ?Sized + Context> PackDecoder<'de> for Never<(), C> { + type Cx = C; + type DecodeNext<'this> = Self where Self: 'this; #[inline] fn decode_next(&mut self, _: &C) -> Result, C::Error> { @@ -328,10 +344,13 @@ impl<'de, C: ?Sized + Context> PackDecoder<'de, C> for Never { } } -impl Encoder for Never { +impl Encoder for Never { + type Cx = C; + type Error = C::Error; type Ok = O; - type WithContext = Self where U: Context; - type EncodePack<'this> = Self where C: 'this; + type Mode = C::Mode; + type WithContext = Never where U: Context; + type EncodePack<'this> = Self where Self::Cx: 'this; type EncodeSome = Self; type EncodeSequence = Self; type EncodeTuple = Self; @@ -377,9 +396,10 @@ where } } -impl SequenceEncoder for Never { +impl SequenceEncoder for Never { + type Cx = C; type Ok = O; - type EncodeNext<'this> = Self; + type EncodeNext<'this> = Self where Self: 'this; #[inline] fn encode_next(&mut self, _: &C) -> Result, C::Error> { @@ -392,9 +412,10 @@ impl SequenceEncoder for Never { } } -impl MapEncoder for Never { +impl MapEncoder for Never { + type Cx = C; type Ok = O; - type EncodeEntry<'this> = Self; + type EncodeEntry<'this> = Self where Self: 'this; #[inline] fn encode_entry(&mut self, _: &C) -> Result, C::Error> { @@ -406,10 +427,11 @@ impl MapEncoder for Never { } } -impl MapEntryEncoder for Never { +impl MapEntryEncoder for Never { + type Cx = C; type Ok = O; - type EncodeMapKey<'this> = Self; - type EncodeMapValue<'this> = Self; + type EncodeMapKey<'this> = Self where Self: 'this; + type EncodeMapValue<'this> = Self where Self: 'this; #[inline] fn encode_map_key(&mut self, _: &C) -> Result, C::Error> { @@ -427,10 +449,11 @@ impl MapEntryEncoder for Never { } } -impl MapEntriesEncoder for Never { +impl MapEntriesEncoder for Never { + type Cx = C; type Ok = O; - type EncodeMapEntryKey<'this> = Self; - type EncodeMapEntryValue<'this> = Self; + type EncodeMapEntryKey<'this> = Self where Self: 'this; + type EncodeMapEntryValue<'this> = Self where Self: 'this; #[inline] fn encode_map_entry_key(&mut self, _: &C) -> Result, C::Error> { @@ -448,9 +471,10 @@ impl MapEntriesEncoder for Never { } } -impl StructEncoder for Never { +impl StructEncoder for Never { + type Cx = C; type Ok = O; - type EncodeField<'this> = Self; + type EncodeField<'this> = Self where Self: 'this; #[inline] fn encode_field(&mut self, _: &C) -> Result, C::Error> { @@ -462,10 +486,11 @@ impl StructEncoder for Never { } } -impl StructFieldEncoder for Never { +impl StructFieldEncoder for Never { + type Cx = C; type Ok = O; - type EncodeFieldName<'this> = Self; - type EncodeFieldValue<'this> = Self; + type EncodeFieldName<'this> = Self where Self: 'this; + type EncodeFieldValue<'this> = Self where Self: 'this; #[inline] fn encode_field_name(&mut self, _: &C) -> Result, C::Error> { @@ -483,10 +508,11 @@ impl StructFieldEncoder for Never { } } -impl VariantEncoder for Never { +impl VariantEncoder for Never { + type Cx = C; type Ok = O; - type EncodeTag<'this> = Self; - type EncodeValue<'this> = Self; + type EncodeTag<'this> = Self where Self: 'this; + type EncodeValue<'this> = Self where Self: 'this; #[inline] fn encode_tag(&mut self, _: &C) -> Result, C::Error> { diff --git a/crates/musli/src/utils/visit_owned_fn.rs b/crates/musli/src/utils/visit_owned_fn.rs index 1dd743ce1..085ebffd2 100644 --- a/crates/musli/src/utils/visit_owned_fn.rs +++ b/crates/musli/src/utils/visit_owned_fn.rs @@ -21,12 +21,11 @@ use crate::Context; /// } /// /// impl<'de, M> Decode<'de, M> for Enum { -/// fn decode(cx: &C, decoder: D) -> Result +/// fn decode(cx: &D::Cx, decoder: D) -> Result /// where -/// C: ?Sized + Context, -/// D: Decoder<'de, C>, +/// D: Decoder<'de>, /// { -/// decoder.decode_string(cx, musli::utils::visit_owned_fn("A string variant for Enum", |cx: &C, variant: &str| { +/// decoder.decode_string(cx, musli::utils::visit_owned_fn("A string variant for Enum", |cx: &D::Cx, variant: &str| { /// match variant { /// "A" => Ok(Enum::A), /// "B" => Ok(Enum::A), diff --git a/crates/tests/tests/ui/custom_decoder_error.rs b/crates/tests/tests/ui/custom_decoder_error.rs index ae8afe2c3..a1b376e04 100644 --- a/crates/tests/tests/ui/custom_decoder_error.rs +++ b/crates/tests/tests/ui/custom_decoder_error.rs @@ -8,20 +8,17 @@ pub struct Container<'a> { mod bytes { use musli::{Decoder, Encoder}; - use musli::Context; - pub(crate) fn encode(this: &[u8], cx: &C, mut encoder: E) -> Result<(), C::Error> + pub(crate) fn encode(this: &[u8], cx: &E::Cx, mut encoder: E) -> Result<(), E::Error> where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { todo!() } - pub(crate) fn decode<'de, C, D>(cx: &C, mut decoder: D) -> Result, C::Error> + pub(crate) fn decode<'de, D>(cx: &D::Cx, mut decoder: D) -> Result, D::Error> where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de>, { todo!() } diff --git a/crates/tests/tests/ui/private_fn_error.rs b/crates/tests/tests/ui/private_fn_error.rs index d13758449..00f319921 100644 --- a/crates/tests/tests/ui/private_fn_error.rs +++ b/crates/tests/tests/ui/private_fn_error.rs @@ -7,22 +7,20 @@ struct Struct { } mod array { - use musli::{Context, Encoder, Decoder}; + use musli::{Encoder, Decoder}; #[inline] - fn encode(this: &[T; N], cx: &C, encoder: E) -> Result + fn encode(this: &[T; N], cx: &E::Cx, encoder: E) -> Result where - C: ?Sized + Context, - E: Encoder, + E: Encoder, { todo!() } #[inline] - fn decode<'de, C, D, T, const N: usize>(cx: &C, decoder: D) -> Result<[T; N], C::Error> + fn decode<'de, D, T, const N: usize>(cx: &D::Cx, decoder: D) -> Result<[T; N], D::Error> where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de>, { todo!() } diff --git a/crates/tests/tests/ui/private_fn_error.stderr b/crates/tests/tests/ui/private_fn_error.stderr index f4437425e..7abea2621 100644 --- a/crates/tests/tests/ui/private_fn_error.stderr +++ b/crates/tests/tests/ui/private_fn_error.stderr @@ -5,13 +5,12 @@ error[E0603]: function `decode` is private | ^^^^^ private function | note: the function `decode` is defined here - --> tests/ui/private_fn_error.rs:22:5 + --> tests/ui/private_fn_error.rs:21:5 | -22 | / fn decode<'de, C, D, T, const N: usize>(cx: &C, decoder: D) -> Result<[T; N], C::Error> -23 | | where -24 | | C: ?Sized + Context, -25 | | D: Decoder<'de, C>, -26 | | { -27 | | todo!() -28 | | } +21 | / fn decode<'de, D, T, const N: usize>(cx: &D::Cx, decoder: D) -> Result<[T; N], D::Error> +22 | | where +23 | | D: Decoder<'de>, +24 | | { +25 | | todo!() +26 | | } | |_____^ diff --git a/crates/tests/tests/visitors.rs b/crates/tests/tests/visitors.rs index fe522d957..14c8a40c6 100644 --- a/crates/tests/tests/visitors.rs +++ b/crates/tests/tests/visitors.rs @@ -10,10 +10,9 @@ pub struct BytesReference<'de> { impl<'de, M> Decode<'de, M> for BytesReference<'de> { #[inline] - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de>, { struct Visitor; @@ -67,10 +66,9 @@ pub struct StringReference<'de> { impl<'de, M> Decode<'de, M> for StringReference<'de> { #[inline] - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de>, { struct Visitor; @@ -122,22 +120,22 @@ pub enum OwnedFn { } impl<'de, M> Decode<'de, M> for OwnedFn { - fn decode(cx: &C, decoder: D) -> Result + fn decode(cx: &D::Cx, decoder: D) -> Result where - C: ?Sized + Context, - D: Decoder<'de, C>, + D: Decoder<'de>, { decoder.decode_string( cx, - musli::utils::visit_owned_fn("A string variant for Enum", |cx: &C, variant: &str| { - match variant { + musli::utils::visit_owned_fn( + "A string variant for Enum", + |cx: &D::Cx, 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}"))) } - } - }), + }, + ), ) } }