Skip to content

Commit

Permalink
Add context accessor to all encoders and decoders
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Jan 1, 2025
1 parent c5ea51f commit 9486150
Show file tree
Hide file tree
Showing 33 changed files with 355 additions and 42 deletions.
2 changes: 1 addition & 1 deletion crates/musli-core/src/de/as_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::Decoder;
/// Trait that allows a type to be repeatedly coerced into a decoder.
pub trait AsDecoder {
/// Context associated with the decoder.
type Cx: ?Sized + Context;
type Cx: Context;
/// The decoder we reborrow as.
type Decoder<'this>: Decoder<
'this,
Expand Down
5 changes: 3 additions & 2 deletions crates/musli-core/src/de/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub enum TryFastDecode<T, D> {
#[must_use = "Decoders must be consumed through one of its decode_* methods"]
pub trait Decoder<'de>: Sized {
/// Context associated with the decoder.
type Cx: ?Sized + Context<Error = Self::Error, Mode = Self::Mode>;
type Cx: Context<Error = Self::Error, Mode = Self::Mode>;
/// Error associated with decoding.
type Error;
/// Mode associated with decoding.
Expand Down Expand Up @@ -55,10 +55,11 @@ pub trait Decoder<'de>: Sized {
#[doc(hidden)]
type __UseMusliDecoderAttributeMacro;

/// Perform an operation while accessing the context.
/// Access the context associated with the decoder.
fn cx(&self) -> Self::Cx;

/// Construct an decoder with a different context.
#[inline]
fn with_context<U>(self, cx: U) -> Result<Self::WithContext<U>, <Self::Cx as Context>::Error>
where
U: Context,
Expand Down
5 changes: 4 additions & 1 deletion crates/musli-core/src/de/entries_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use super::{Decoder, SizeHint};
#[must_use = "Must call end_entries to complete decoding"]
pub trait EntriesDecoder<'de> {
/// Context associated with the decoder.
type Cx: ?Sized + Context;
type Cx: Context;
/// The decoder to use for a tuple field index.
type DecodeEntryKey<'this>: Decoder<
'de,
Expand All @@ -32,6 +32,9 @@ pub trait EntriesDecoder<'de> {
where
Self: 'this;

/// Access the context associated with the decoder.
fn cx(&self) -> Self::Cx;

/// Get a size hint for the size of the map being decoded.
#[inline]
fn size_hint(&self) -> SizeHint {
Expand Down
5 changes: 4 additions & 1 deletion crates/musli-core/src/de/entry_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::{Decoder, SizeHint};
/// Trait governing how to decode a map entry.
pub trait EntryDecoder<'de> {
/// Context associated with the decoder.
type Cx: ?Sized + Context;
type Cx: Context;
/// The decoder to use for a tuple field index.
type DecodeKey<'this>: Decoder<
'de,
Expand All @@ -23,6 +23,9 @@ pub trait EntryDecoder<'de> {
Mode = <Self::Cx as Context>::Mode,
>;

/// Access the context associated with the decoder.
fn cx(&self) -> Self::Cx;

/// Get a size hint for the size of the map being decoded.
#[inline]
fn size_hint(&self) -> SizeHint {
Expand Down
5 changes: 4 additions & 1 deletion crates/musli-core/src/de/map_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::{Decode, Decoder, EntriesDecoder, EntryDecoder, SizeHint};
/// Trait governing how to decode a sequence of pairs.
pub trait MapDecoder<'de> {
/// Context associated with the decoder.
type Cx: ?Sized + Context;
type Cx: Context;
/// The decoder to use for a key.
type DecodeEntry<'this>: EntryDecoder<'de, Cx = Self::Cx>
where
Expand All @@ -15,6 +15,9 @@ pub trait MapDecoder<'de> {
where
Self: 'this;

/// Access the context associated with the decoder.
fn cx(&self) -> Self::Cx;

/// Get a size hint of known remaining elements.
#[inline]
fn size_hint(&self) -> SizeHint {
Expand Down
5 changes: 4 additions & 1 deletion crates/musli-core/src/de/sequence_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::{Decode, Decoder, SizeHint};
/// Trait governing how to decode a sequence.
pub trait SequenceDecoder<'de> {
/// Context associated with the decoder.
type Cx: ?Sized + Context;
type Cx: Context;
/// The decoder for individual items.
type DecodeNext<'this>: Decoder<
'de,
Expand All @@ -16,6 +16,9 @@ pub trait SequenceDecoder<'de> {
where
Self: 'this;

/// Access the context associated with the decoder.
fn cx(&self) -> Self::Cx;

/// Get a size hint of known remaining elements.
#[inline]
fn size_hint(&self) -> SizeHint {
Expand Down
5 changes: 4 additions & 1 deletion crates/musli-core/src/de/variant_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::Decoder;
/// Trait governing how to decode a variant.
pub trait VariantDecoder<'de> {
/// Context associated with the decoder.
type Cx: ?Sized + Context;
type Cx: Context;
/// The decoder to use for the variant tag.
type DecodeTag<'this>: Decoder<
'de,
Expand All @@ -25,6 +25,9 @@ pub trait VariantDecoder<'de> {
where
Self: 'this;

/// Access the context associated with the decoder.
fn cx(&self) -> Self::Cx;

/// 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
Expand Down
12 changes: 6 additions & 6 deletions crates/musli-core/src/de/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,23 +200,23 @@ where

/// Indicates that the visited type is a sequence.
#[inline]
fn visit_sequence<D>(self, cx: C, decoder: &mut D) -> Result<Self::Ok, C::Error>
fn visit_sequence<D>(self, decoder: &mut D) -> Result<Self::Ok, C::Error>
where
D: ?Sized + SequenceDecoder<'de, Cx = C>,
{
Err(cx.message(expecting::unsupported_type(
Err(decoder.cx().message(expecting::unsupported_type(
&expecting::SequenceWith(decoder.size_hint()),
ExpectingWrapper::new(&self),
)))
}

/// Indicates that the visited type is a map.
#[inline]
fn visit_map<D>(self, cx: C, decoder: &mut D) -> Result<Self::Ok, <D::Cx as Context>::Error>
fn visit_map<D>(self, decoder: &mut D) -> Result<Self::Ok, <D::Cx as Context>::Error>
where
D: ?Sized + MapDecoder<'de, Cx = C>,
{
Err(cx.message(expecting::unsupported_type(
Err(decoder.cx().message(expecting::unsupported_type(
&expecting::MapWith(decoder.size_hint()),
ExpectingWrapper::new(&self),
)))
Expand All @@ -242,11 +242,11 @@ where

/// Indicates that the visited type is a variant.
#[inline]
fn visit_variant<D>(self, cx: C, _: &mut D) -> Result<Self::Ok, C::Error>
fn visit_variant<D>(self, decoder: &mut D) -> Result<Self::Ok, C::Error>
where
D: VariantDecoder<'de, Cx = C>,
{
Err(cx.message(expecting::unsupported_type(
Err(decoder.cx().message(expecting::unsupported_type(
&expecting::Variant,
ExpectingWrapper::new(&self),
)))
Expand Down
4 changes: 2 additions & 2 deletions crates/musli-core/src/en/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ where
#[must_use = "Encoders must be consumed through one of its encode_* methods"]
pub trait Encoder: Sized {
/// Context associated with the encoder.
type Cx: ?Sized + Context<Error = Self::Error, Mode = Self::Mode>;
type Cx: Context<Error = Self::Error, Mode = Self::Mode>;
/// 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.
Expand Down Expand Up @@ -60,7 +60,7 @@ pub trait Encoder: Sized {
#[doc(hidden)]
type __UseMusliEncoderAttributeMacro;

/// Perform an operation while accessing the context.
/// Access the context associated with the encoder.
fn cx(&self) -> Self::Cx;

/// Construct an encoder with a different context.
Expand Down
5 changes: 4 additions & 1 deletion crates/musli-core/src/en/entries_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use super::{Encode, Encoder};
/// format might be degraded.
pub trait EntriesEncoder {
/// Context associated with the encoder.
type Cx: ?Sized + Context;
type Cx: Context;
/// Result type of the encoder.
type Ok;
/// The encoder returned when advancing the map encoder to encode the key.
Expand All @@ -33,6 +33,9 @@ pub trait EntriesEncoder {
where
Self: 'this;

/// Access the context associated with the encoder.
fn cx(&self) -> Self::Cx;

/// Return the encoder for the key in the entry.
#[must_use = "Encoders must be consumed"]
fn encode_entry_key(
Expand Down
5 changes: 4 additions & 1 deletion crates/musli-core/src/en/entry_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::{Encode, Encoder};
/// Trait governing how to encode a map entry.
pub trait EntryEncoder {
/// Context associated with the encoder.
type Cx: ?Sized + Context;
type Cx: Context;
/// Result type of the encoder.
type Ok;
/// The encoder returned when advancing the map encoder to encode the key.
Expand All @@ -27,6 +27,9 @@ pub trait EntryEncoder {
where
Self: 'this;

/// Access the context associated with the encoder.
fn cx(&self) -> Self::Cx;

/// Return the encoder for the key in the entry.
#[must_use = "Encoders must be consumed"]
fn encode_key(&mut self) -> Result<Self::EncodeKey<'_>, <Self::Cx as Context>::Error>;
Expand Down
5 changes: 4 additions & 1 deletion crates/musli-core/src/en/map_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ use super::{Encode, EntryEncoder};
/// Encoder for a map.
pub trait MapEncoder {
/// Context associated with the encoder.
type Cx: ?Sized + Context;
type Cx: Context;
/// Result type of the encoder.
type Ok;
/// Encode the next pair.
type EncodeEntry<'this>: EntryEncoder<Cx = Self::Cx, Ok = Self::Ok>
where
Self: 'this;

/// Access the context associated with the encoder.
fn cx(&self) -> Self::Cx;

/// Encode the next pair.
#[must_use = "Encoders must be consumed"]
fn encode_entry(&mut self) -> Result<Self::EncodeEntry<'_>, <Self::Cx as Context>::Error>;
Expand Down
5 changes: 4 additions & 1 deletion crates/musli-core/src/en/variant_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::{Encode, Encoder};
#[must_use = "Must call end_variant to finish encoding"]
pub trait VariantEncoder {
/// Context associated with the encoder.
type Cx: ?Sized + Context;
type Cx: Context;
/// Result type of the encoder.
type Ok;
/// The encoder returned when advancing the map encoder to encode the key.
Expand All @@ -28,6 +28,9 @@ pub trait VariantEncoder {
where
Self: 'this;

/// Access the context associated with the encoder.
fn cx(&self) -> Self::Cx;

/// Return the encoder for the first element in the variant.
#[must_use = "Encoders must be consumed"]
fn encode_tag(&mut self) -> Result<Self::EncodeTag<'_>, <Self::Cx as Context>::Error>;
Expand Down
2 changes: 1 addition & 1 deletion crates/musli-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ pub use musli_macros::decoder;
/// #[musli_core::visitor(crate = musli_core)]
/// impl<'de, C> Visitor<'de, C> for AnyVisitor
/// where
/// C: ?Sized + Context,
/// C: Context,
/// {
/// type Ok = ();
///
Expand Down
45 changes: 45 additions & 0 deletions crates/musli-core/src/never.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ where
where
Self: 'this;

#[inline]
fn cx(&self) -> Self::Cx {
match self._never {}
}

#[inline]
fn decode_entry_key(&mut self) -> Result<Option<Self::DecodeEntryKey<'_>>, C::Error> {
match self._never {}
Expand Down Expand Up @@ -222,6 +227,11 @@ where
where
Self: 'this;

#[inline]
fn cx(&self) -> Self::Cx {
match self._never {}
}

#[inline]
fn decode_tag(&mut self) -> Result<Self::DecodeTag<'_>, C::Error> {
match self._never {}
Expand All @@ -247,6 +257,11 @@ where
where
Self: 'this;

#[inline]
fn cx(&self) -> Self::Cx {
match self._never {}
}

#[inline]
fn size_hint(&self) -> SizeHint {
match self._never {}
Expand Down Expand Up @@ -276,6 +291,11 @@ where
Self: 'this;
type DecodeValue = Self;

#[inline]
fn cx(&self) -> Self::Cx {
match self._never {}
}

#[inline]
fn decode_key(&mut self) -> Result<Self::DecodeKey<'_>, C::Error> {
match self._never {}
Expand All @@ -297,6 +317,11 @@ where
where
Self: 'this;

#[inline]
fn cx(&self) -> Self::Cx {
match self._never {}
}

#[inline]
fn decode_next(&mut self) -> Result<Self::DecodeNext<'_>, C::Error> {
match self._never {}
Expand Down Expand Up @@ -410,6 +435,11 @@ where
where
Self: 'this;

#[inline]
fn cx(&self) -> Self::Cx {
match self._never {}
}

#[inline]
fn encode_entry(&mut self) -> Result<Self::EncodeEntry<'_>, C::Error> {
match self._never {}
Expand All @@ -436,6 +466,11 @@ where
where
Self: 'this;

#[inline]
fn cx(&self) -> Self::Cx {
match self._never {}
}

#[inline]
fn encode_key(&mut self) -> Result<Self::EncodeKey<'_>, C::Error> {
match self._never {}
Expand Down Expand Up @@ -468,6 +503,11 @@ where
where
Self: 'this;

#[inline]
fn cx(&self) -> Self::Cx {
match self._never {}
}

#[inline]
fn encode_entry_key(&mut self) -> Result<Self::EncodeEntryKey<'_>, C::Error> {
match self._never {}
Expand Down Expand Up @@ -500,6 +540,11 @@ where
where
Self: 'this;

#[inline]
fn cx(&self) -> Self::Cx {
match self._never {}
}

#[inline]
fn encode_tag(&mut self) -> Result<Self::EncodeTag<'_>, C::Error> {
match self._never {}
Expand Down
Loading

0 comments on commit 9486150

Please sign in to comment.