Skip to content

Commit

Permalink
Remove specialized hint associated types
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed May 29, 2024
1 parent a0f679f commit f83c0c7
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 100 deletions.
24 changes: 7 additions & 17 deletions crates/musli-core/src/de/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,8 @@ pub trait Decoder<'de>: Sized {
type DecodePack: SequenceDecoder<'de, Cx = Self::Cx>;
/// Decoder returned by [`Decoder::decode_sequence`].
type DecodeSequence: SequenceDecoder<'de, Cx = Self::Cx>;
/// Decoder returned by [`Decoder::decode_sequence_hint`].
type DecodeSequenceHint: SequenceDecoder<'de, Cx = Self::Cx>;
/// Decoder returned by [`Decoder::decode_map`].
type DecodeMap: MapDecoder<'de, Cx = Self::Cx>;
/// Decoder used by [`Decoder::decode_map_hint`].
type DecodeMapHint: MapDecoder<'de, Cx = Self::Cx>;
/// Decoder returned by [`Decoder::decode_map_entries`].
type DecodeMapEntries: EntriesDecoder<'de, Cx = Self::Cx>;
/// Decoder used by [`Decoder::decode_variant`].
Expand Down Expand Up @@ -1285,12 +1281,9 @@ pub trait Decoder<'de>: Sized {
f: F,
) -> Result<O, <Self::Cx as Context>::Error>
where
F: FnOnce(&mut Self::DecodeSequenceHint) -> Result<O, <Self::Cx as Context>::Error>,
F: FnOnce(&mut Self::DecodeSequence) -> Result<O, <Self::Cx as Context>::Error>,
{
Err(self.cx().message(expecting::unsupported_type(
&expecting::SequenceWith(hint.size_hint()),
ExpectingWrapper::new(&self),
)))
self.decode_sequence(f)
}

/// Decode a map who's size is not known at compile time.
Expand Down Expand Up @@ -1401,24 +1394,21 @@ pub trait Decoder<'de>: Sized {
/// }
/// ```
#[inline]
fn decode_map_hint<F, O>(self, hint: &MapHint, f: F) -> Result<O, <Self::Cx as Context>::Error>
fn decode_map_hint<F, O>(self, _: &MapHint, f: F) -> Result<O, <Self::Cx as Context>::Error>
where
F: FnOnce(&mut Self::DecodeMapHint) -> Result<O, <Self::Cx as Context>::Error>,
F: FnOnce(&mut Self::DecodeMap) -> Result<O, <Self::Cx as Context>::Error>,
{
Err(self.cx().message(expecting::unsupported_type(
&expecting::Map,
ExpectingWrapper::new(&self),
)))
self.decode_map(f)
}

/// Simplified decoding a map of unknown length.
///
/// The length of the map must somehow be determined from the underlying
/// format.
#[inline]
fn decode_map_entries(self) -> Result<Self::DecodeMapEntries, <Self::Cx as Context>::Error>
fn decode_map_entries<F, O>(self, f: F) -> Result<O, <Self::Cx as Context>::Error>
where
Self: Sized,
F: FnOnce(&mut Self::DecodeMapEntries) -> Result<O, <Self::Cx as Context>::Error>,
{
Err(self.cx().message(expecting::unsupported_type(
&expecting::MapEntries,
Expand Down
2 changes: 0 additions & 2 deletions crates/musli-core/src/never.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ impl<'de, C: ?Sized + Context> Decoder<'de> for Never<(), C> {
type DecodeBuffer = Self;
type DecodePack = Self;
type DecodeSequence = Self;
type DecodeSequenceHint = Self;
type DecodeMapHint = Self;
type DecodeMapEntries = Self;
type DecodeSome = Self;
type DecodeMap = Self;
Expand Down
2 changes: 0 additions & 2 deletions crates/musli-macros/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ pub(super) const DECODER_TYPES: &[(&str, Extra)] = &[
("DecodeSome", Extra::None),
("DecodePack", Extra::None),
("DecodeSequence", Extra::None),
("DecodeSequenceHint", Extra::None),
("DecodeMap", Extra::None),
("DecodeMapHint", Extra::None),
("DecodeMapEntries", Extra::None),
("DecodeVariant", Extra::None),
];
Expand Down
25 changes: 6 additions & 19 deletions crates/musli/src/descriptive/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::de::{
Decode, DecodeUnsized, Decoder, EntriesDecoder, EntryDecoder, MapDecoder, SequenceDecoder,
SizeHint, Skip, UnsizedVisitor, VariantDecoder, Visitor,
};
use crate::hint::{MapHint, SequenceHint};
use crate::int::continuation as c;
#[cfg(feature = "value")]
use crate::options;
Expand Down Expand Up @@ -229,9 +228,7 @@ where
type DecodePack = SelfDecoder<'a, Limit<R>, OPT, C>;
type DecodeSome = Self;
type DecodeSequence = RemainingSelfDecoder<'a, R, OPT, C>;
type DecodeSequenceHint = RemainingSelfDecoder<'a, R, OPT, C>;
type DecodeMap = RemainingSelfDecoder<'a, R, OPT, C>;
type DecodeMapHint = RemainingSelfDecoder<'a, R, OPT, C>;
type DecodeMapEntries = RemainingSelfDecoder<'a, R, OPT, C>;
type DecodeVariant = Self;

Expand Down Expand Up @@ -591,14 +588,6 @@ where
Ok(output)
}

#[inline]
fn decode_sequence_hint<F, O>(self, _: &SequenceHint, f: F) -> Result<O, C::Error>
where
F: FnOnce(&mut Self::DecodeSequenceHint) -> Result<O, C::Error>,
{
self.decode_sequence(f)
}

#[inline]
fn decode_map<F, O>(self, f: F) -> Result<O, C::Error>
where
Expand All @@ -611,16 +600,14 @@ where
}

#[inline]
fn decode_map_hint<F, O>(self, _: &MapHint, f: F) -> Result<O, C::Error>
fn decode_map_entries<F, O>(self, f: F) -> Result<O, C::Error>
where
F: FnOnce(&mut Self::DecodeMapHint) -> Result<O, C::Error>,
F: FnOnce(&mut Self::DecodeMapEntries) -> Result<O, C::Error>,
{
self.decode_map(f)
}

#[inline]
fn decode_map_entries(self) -> Result<Self::DecodeMapEntries, C::Error> {
self.shared_decode_map()
let mut decoder = self.shared_decode_map()?;
let output = f(&mut decoder)?;
decoder.skip_map_remaining()?;
Ok(output)
}

#[inline]
Expand Down
13 changes: 7 additions & 6 deletions crates/musli/src/json/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,7 @@ where
type DecodeBuffer = crate::value::AsValueDecoder<'a, BUFFER_OPTIONS, C>;
type DecodePack = JsonSequenceDecoder<'a, P, C>;
type DecodeSequence = JsonSequenceDecoder<'a, P, C>;
type DecodeSequenceHint = JsonSequenceDecoder<'a, P, C>;
type DecodeMap = JsonObjectDecoder<'a, P, C>;
type DecodeMapHint = JsonObjectDecoder<'a, P, C>;
type DecodeMapEntries = JsonObjectDecoder<'a, P, C>;
type DecodeSome = JsonDecoder<'a, P, C>;
type DecodeVariant = JsonVariantDecoder<'a, P, C>;
Expand Down Expand Up @@ -396,7 +394,7 @@ where
#[inline]
fn decode_sequence_hint<F, O>(self, hint: &SequenceHint, f: F) -> Result<O, C::Error>
where
F: FnOnce(&mut Self::DecodeSequenceHint) -> Result<O, C::Error>,
F: FnOnce(&mut Self::DecodeSequence) -> Result<O, C::Error>,
{
let mut decoder = JsonSequenceDecoder::new(self.cx, Some(hint.size), self.parser)?;
let output = f(&mut decoder)?;
Expand All @@ -418,7 +416,7 @@ where
#[inline]
fn decode_map_hint<F, O>(self, hint: &MapHint, f: F) -> Result<O, C::Error>
where
F: FnOnce(&mut Self::DecodeMapHint) -> Result<O, C::Error>,
F: FnOnce(&mut Self::DecodeMap) -> Result<O, C::Error>,
{
let mut decoder = JsonObjectDecoder::new(self.cx, Some(hint.size), self.parser)?;
let output = f(&mut decoder)?;
Expand All @@ -427,8 +425,11 @@ where
}

#[inline]
fn decode_map_entries(self) -> Result<Self::DecodeMapEntries, C::Error> {
JsonObjectDecoder::new(self.cx, None, self.parser)
fn decode_map_entries<F, O>(self, f: F) -> Result<O, C::Error>
where
F: FnOnce(&mut Self::DecodeMapEntries) -> Result<O, C::Error>,
{
self.decode_map(f)
}

#[inline]
Expand Down
20 changes: 7 additions & 13 deletions crates/musli/src/serde/deserializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,8 @@ where
where
V: de::Visitor<'de>,
{
let mut decoder = self.decoder.decode_map_entries()?;
let output = visitor.visit_map(MapAccess::new(self.cx, &mut decoder))?;
decoder.end_entries()?;
Ok(output)
self.decoder
.decode_map_entries(|decoder| visitor.visit_map(MapAccess::new(self.cx, decoder)))
}

#[inline]
Expand All @@ -302,10 +300,8 @@ where
where
V: de::Visitor<'de>,
{
let mut decoder = self.decoder.decode_map_entries()?;
let output = visitor.visit_map(StructAccess::new(self.cx, &mut decoder))?;
decoder.end_entries()?;
Ok(output)
self.decoder
.decode_map_entries(|decoder| visitor.visit_map(StructAccess::new(self.cx, decoder)))
}

#[inline]
Expand Down Expand Up @@ -668,11 +664,9 @@ where
where
V: de::Visitor<'de>,
{
let decoder = self.decoder.decode_value()?;
let mut st = decoder.decode_map_entries()?;
let value = visitor.visit_map(StructAccess::new(self.cx, &mut st))?;
st.end_entries()?;
Ok(value)
self.decoder
.decode_value()?
.decode_map_entries(|decoder| visitor.visit_map(StructAccess::new(self.cx, decoder)))
}
}

Expand Down
20 changes: 2 additions & 18 deletions crates/musli/src/storage/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::de::{
DecodeUnsized, Decoder, EntriesDecoder, EntryDecoder, MapDecoder, SequenceDecoder, SizeHint,
UnsizedVisitor, VariantDecoder,
};
use crate::hint::{MapHint, SequenceHint};
use crate::{Context, Decode, Options, Reader};

/// A very simple decoder suitable for storage decoding.
Expand Down Expand Up @@ -48,8 +47,6 @@ where
type DecodePack = Self;
type DecodeSome = Self;
type DecodeSequence = LimitedStorageDecoder<'a, R, OPT, C>;
type DecodeSequenceHint = LimitedStorageDecoder<'a, R, OPT, C>;
type DecodeMapHint = LimitedStorageDecoder<'a, R, OPT, C>;
type DecodeMap = LimitedStorageDecoder<'a, R, OPT, C>;
type DecodeMapEntries = LimitedStorageDecoder<'a, R, OPT, C>;
type DecodeVariant = Self;
Expand Down Expand Up @@ -287,14 +284,6 @@ where
Ok(output)
}

#[inline]
fn decode_sequence_hint<F, O>(self, _: &SequenceHint, f: F) -> Result<O, C::Error>
where
F: FnOnce(&mut Self::DecodeSequenceHint) -> Result<O, C::Error>,
{
self.decode_sequence(f)
}

#[inline]
fn decode_map<F, O>(self, f: F) -> Result<O, C::Error>
where
Expand All @@ -312,18 +301,13 @@ where
}

#[inline]
fn decode_map_hint<F, O>(self, _: &MapHint, f: F) -> Result<O, C::Error>
fn decode_map_entries<F, O>(self, f: F) -> Result<O, C::Error>
where
F: FnOnce(&mut Self::DecodeMapHint) -> Result<O, C::Error>,
F: FnOnce(&mut Self::DecodeMapEntries) -> Result<O, C::Error>,
{
self.decode_map(f)
}

#[inline]
fn decode_map_entries(self) -> Result<Self::DecodeMapEntries, C::Error> {
LimitedStorageDecoder::new(self.cx, self.reader)
}

#[inline]
fn decode_variant<F, O>(mut self, f: F) -> Result<O, C::Error>
where
Expand Down
22 changes: 5 additions & 17 deletions crates/musli/src/value/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::de::{
SequenceDecoder, SizeHint, Skip, VariantDecoder, Visitor,
};
#[cfg(feature = "alloc")]
use crate::hint::{MapHint, SequenceHint};
use crate::hint::SequenceHint;
use crate::reader::SliceReader;
use crate::storage::de::StorageDecoder;
use crate::{Context, Options};
Expand Down Expand Up @@ -91,9 +91,7 @@ impl<'a, 'de, C: ?Sized + Context, const OPT: Options> Decoder<'de>
type DecodeSome = Self;
type DecodePack = StorageDecoder<'a, SliceReader<'de>, OPT, C>;
type DecodeSequence = IterValueDecoder<'a, 'de, OPT, C>;
type DecodeSequenceHint = IterValueDecoder<'a, 'de, OPT, C>;
type DecodeMap = IterValuePairsDecoder<'a, 'de, OPT, C>;
type DecodeMapHint = IterValuePairsDecoder<'a, 'de, OPT, C>;
type DecodeMapEntries = IterValuePairsDecoder<'a, 'de, OPT, C>;
type DecodeVariant = IterValueVariantDecoder<'a, 'de, OPT, C>;

Expand Down Expand Up @@ -296,7 +294,7 @@ impl<'a, 'de, C: ?Sized + Context, const OPT: Options> Decoder<'de>
#[inline]
fn decode_sequence_hint<F, O>(self, _: &SequenceHint, f: F) -> Result<O, C::Error>
where
F: FnOnce(&mut Self::DecodeSequenceHint) -> Result<O, C::Error>,
F: FnOnce(&mut Self::DecodeSequence) -> Result<O, C::Error>,
{
ensure!(self, hint, ExpectedSequence(hint), Value::Sequence(sequence) => {
f(&mut IterValueDecoder::new(self.cx, sequence))
Expand All @@ -316,21 +314,11 @@ impl<'a, 'de, C: ?Sized + Context, const OPT: Options> Decoder<'de>

#[cfg(feature = "alloc")]
#[inline]
fn decode_map_hint<F, O>(self, _: &MapHint, f: F) -> Result<O, C::Error>
fn decode_map_entries<F, O>(self, f: F) -> Result<O, C::Error>
where
F: FnOnce(&mut Self::DecodeMapHint) -> Result<O, C::Error>,
F: FnOnce(&mut Self::DecodeMapEntries) -> Result<O, C::Error>,
{
ensure!(self, hint, ExpectedMap(hint), Value::Map(map) => {
f(&mut IterValuePairsDecoder::new(self.cx, map))
})
}

#[cfg(feature = "alloc")]
#[inline]
fn decode_map_entries(self) -> Result<Self::DecodeMapEntries, C::Error> {
ensure!(self, hint, ExpectedMap(hint), Value::Map(map) => {
Ok(IterValuePairsDecoder::new(self.cx, map))
})
self.decode_map(f)
}

#[cfg(feature = "alloc")]
Expand Down
13 changes: 7 additions & 6 deletions crates/musli/src/wire/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,7 @@ where
type DecodePack = WireDecoder<'a, Limit<R>, OPT, C>;
type DecodeSome = Self;
type DecodeSequence = RemainingWireDecoder<'a, R, OPT, C>;
type DecodeSequenceHint = RemainingWireDecoder<'a, R, OPT, C>;
type DecodeMap = RemainingWireDecoder<'a, R, OPT, C>;
type DecodeMapHint = RemainingWireDecoder<'a, R, OPT, C>;
type DecodeMapEntries = RemainingWireDecoder<'a, R, OPT, C>;
type DecodeVariant = Self;

Expand Down Expand Up @@ -485,7 +483,7 @@ where
#[inline]
fn decode_sequence_hint<F, O>(self, _: &SequenceHint, f: F) -> Result<O, C::Error>
where
F: FnOnce(&mut Self::DecodeSequenceHint) -> Result<O, C::Error>,
F: FnOnce(&mut Self::DecodeSequence) -> Result<O, C::Error>,
{
self.decode_sequence(f)
}
Expand All @@ -504,14 +502,17 @@ where
#[inline]
fn decode_map_hint<F, O>(self, _: &MapHint, f: F) -> Result<O, C::Error>
where
F: FnOnce(&mut Self::DecodeMapHint) -> Result<O, C::Error>,
F: FnOnce(&mut Self::DecodeMap) -> Result<O, C::Error>,
{
self.decode_map(f)
}

#[inline]
fn decode_map_entries(self) -> Result<Self::DecodeMapEntries, C::Error> {
self.shared_decode_pair_sequence()
fn decode_map_entries<F, O>(self, f: F) -> Result<O, C::Error>
where
F: FnOnce(&mut Self::DecodeMapEntries) -> Result<O, C::Error>,
{
self.decode_map(f)
}

#[inline]
Expand Down

0 comments on commit f83c0c7

Please sign in to comment.