Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more hint structures #114

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions crates/musli-descriptive/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use alloc::vec::Vec;
use musli::de::{
Decode, Decoder, MapDecoder, MapEntriesDecoder, MapEntryDecoder, NumberHint, NumberVisitor,
PackDecoder, SequenceDecoder, SizeHint, Skip, StructDecoder, StructFieldDecoder,
StructFieldsDecoder, StructHint, TupleDecoder, TypeHint, UnsizedStructHint, ValueVisitor,
VariantDecoder, Visitor,
StructFieldsDecoder, TupleDecoder, TypeHint, ValueVisitor, VariantDecoder, Visitor,
};
use musli::hint::{StructHint, TupleHint, UnsizedStructHint};
use musli::Context;
use musli_common::int::continuation as c;
use musli_storage::de::StorageDecoder;
Expand Down Expand Up @@ -674,16 +674,17 @@ where
}

#[inline]
fn decode_tuple<F, O>(mut self, len: usize, f: F) -> Result<O, C::Error>
fn decode_tuple<F, O>(mut self, hint: &TupleHint, f: F) -> Result<O, C::Error>
where
F: FnOnce(&mut Self::DecodeTuple) -> Result<O, C::Error>,
{
let pos = self.cx.mark();
let actual = self.decode_prefix(Kind::Sequence, pos)?;

if len != actual {
if hint.size != actual {
return Err(self.cx.message(format_args!(
"Tuple length mismatch: len: {len}, actual: {actual}"
"Tuple length {} did not match actual {actual}",
hint.size
)));
}

Expand Down
29 changes: 15 additions & 14 deletions crates/musli-descriptive/src/en.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use musli::en::{
Encoder, MapEncoder, MapEntriesEncoder, MapEntryEncoder, PackEncoder, SequenceEncoder,
StructEncoder, StructFieldEncoder, TupleEncoder, VariantEncoder,
};
use musli::hint::{MapHint, SequenceHint, StructHint, TupleHint};
use musli::{Buf, Context, Encode};
use musli_common::int::continuation as c;
use musli_storage::en::StorageEncoder;
Expand Down Expand Up @@ -262,32 +263,32 @@ where
}

#[inline]
fn encode_sequence(mut self, len: usize) -> Result<Self::EncodeSequence, C::Error> {
encode_prefix::<_, _, OPT>(self.cx, self.writer.borrow_mut(), Kind::Sequence, len)?;
fn encode_sequence(mut self, hint: &SequenceHint) -> Result<Self::EncodeSequence, C::Error> {
encode_prefix::<_, _, OPT>(self.cx, self.writer.borrow_mut(), Kind::Sequence, hint.size)?;
Ok(self)
}

#[inline]
fn encode_tuple(mut self, len: usize) -> Result<Self::EncodeSequence, C::Error> {
encode_prefix::<_, _, OPT>(self.cx, self.writer.borrow_mut(), Kind::Sequence, len)?;
fn encode_tuple(mut self, hint: &TupleHint) -> Result<Self::EncodeSequence, C::Error> {
encode_prefix::<_, _, OPT>(self.cx, self.writer.borrow_mut(), Kind::Sequence, hint.size)?;
Ok(self)
}

#[inline]
fn encode_map(mut self, len: usize) -> Result<Self::EncodeMap, C::Error> {
encode_prefix::<_, _, OPT>(self.cx, self.writer.borrow_mut(), Kind::Map, len)?;
fn encode_map(mut self, hint: &MapHint) -> Result<Self::EncodeMap, C::Error> {
encode_prefix::<_, _, OPT>(self.cx, self.writer.borrow_mut(), Kind::Map, hint.size)?;
Ok(self)
}

#[inline]
fn encode_map_entries(mut self, len: usize) -> Result<Self::EncodeMapEntries, C::Error> {
encode_prefix::<_, _, OPT>(self.cx, self.writer.borrow_mut(), Kind::Map, len)?;
fn encode_map_entries(mut self, hint: &MapHint) -> Result<Self::EncodeMapEntries, C::Error> {
encode_prefix::<_, _, OPT>(self.cx, self.writer.borrow_mut(), Kind::Map, hint.size)?;
Ok(self)
}

#[inline]
fn encode_struct(mut self, len: usize) -> Result<Self::EncodeStruct, C::Error> {
encode_prefix::<_, _, OPT>(self.cx, self.writer.borrow_mut(), Kind::Map, len)?;
fn encode_struct(mut self, hint: &StructHint) -> Result<Self::EncodeStruct, C::Error> {
encode_prefix::<_, _, OPT>(self.cx, self.writer.borrow_mut(), Kind::Map, hint.size)?;
Ok(self)
}

Expand All @@ -313,28 +314,28 @@ where
fn encode_tuple_variant<T>(
mut self,
tag: &T,
len: usize,
hint: &TupleHint,
) -> Result<Self::EncodeTupleVariant, C::Error>
where
T: ?Sized + Encode<C::Mode>,
{
self.writer.write_byte(self.cx, VARIANT.byte())?;
SelfEncoder::<_, OPT, _>::new(self.cx, self.writer.borrow_mut()).encode(tag)?;
self.encode_tuple(len)
self.encode_tuple(hint)
}

#[inline]
fn encode_struct_variant<T>(
mut self,
tag: &T,
len: usize,
hint: &StructHint,
) -> Result<Self::EncodeStructVariant, C::Error>
where
T: ?Sized + Encode<C::Mode>,
{
self.writer.write_byte(self.cx, VARIANT.byte())?;
SelfEncoder::<_, OPT, _>::new(self.cx, self.writer.borrow_mut()).encode(tag)?;
self.encode_struct(len)
self.encode_struct(hint)
}
}

Expand Down
11 changes: 6 additions & 5 deletions crates/musli-json/src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ use core::str;
use alloc::vec::Vec;

use musli::de::{
Decode, Decoder, NumberHint, NumberVisitor, SequenceDecoder, SizeHint, Skip, StructHint,
TypeHint, UnsizedStructHint, ValueVisitor, Visitor,
Decode, Decoder, NumberHint, NumberVisitor, SequenceDecoder, SizeHint, Skip, TypeHint,
ValueVisitor, Visitor,
};
use musli::hint::{StructHint, TupleHint, UnsizedStructHint};
use musli::Context;

#[cfg(not(feature = "parse-full"))]
Expand Down Expand Up @@ -403,11 +404,11 @@ where
}

#[inline]
fn decode_tuple<F, O>(self, len: usize, f: F) -> Result<O, C::Error>
fn decode_tuple<F, O>(self, hint: &TupleHint, f: F) -> Result<O, C::Error>
where
F: FnOnce(&mut Self::DecodeTuple) -> Result<O, C::Error>,
{
let mut decoder = JsonSequenceDecoder::new(self.cx, Some(len), self.parser)?;
let mut decoder = JsonSequenceDecoder::new(self.cx, Some(hint.size), self.parser)?;
let output = f(&mut decoder)?;
decoder.skip_sequence_remaining()?;
Ok(output)
Expand Down Expand Up @@ -453,7 +454,7 @@ where

#[inline]
fn decode_struct_fields(self, hint: &StructHint) -> Result<Self::DecodeStructFields, C::Error> {
JsonObjectDecoder::new(self.cx, Some(hint.fields), self.parser)
JsonObjectDecoder::new(self.cx, Some(hint.size), self.parser)
}

#[inline]
Expand Down
15 changes: 8 additions & 7 deletions crates/musli-json/src/en/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use self::variant_encoder::JsonVariantEncoder;
use core::fmt;

use musli::en::{Encoder, SequenceEncoder};
use musli::hint::{MapHint, SequenceHint, StructHint, TupleHint};
use musli::{Context, Encode};

use crate::writer::Writer;
Expand Down Expand Up @@ -275,27 +276,27 @@ where
}

#[inline]
fn encode_sequence(self, _: usize) -> Result<Self::EncodeSequence, C::Error> {
fn encode_sequence(self, _: &SequenceHint) -> Result<Self::EncodeSequence, C::Error> {
JsonArrayEncoder::new(self.cx, self.writer)
}

#[inline]
fn encode_tuple(self, _: usize) -> Result<Self::EncodeTuple, C::Error> {
fn encode_tuple(self, _: &TupleHint) -> Result<Self::EncodeTuple, C::Error> {
JsonArrayEncoder::new(self.cx, self.writer)
}

#[inline]
fn encode_map(self, _: usize) -> Result<Self::EncodeMap, C::Error> {
fn encode_map(self, _: &MapHint) -> Result<Self::EncodeMap, C::Error> {
JsonObjectEncoder::new(self.cx, self.writer)
}

#[inline]
fn encode_map_entries(self, _: usize) -> Result<Self::EncodeMapEntries, C::Error> {
fn encode_map_entries(self, _: &MapHint) -> Result<Self::EncodeMapEntries, C::Error> {
JsonObjectEncoder::new(self.cx, self.writer)
}

#[inline]
fn encode_struct(self, _: usize) -> Result<Self::EncodeStruct, C::Error> {
fn encode_struct(self, _: &StructHint) -> Result<Self::EncodeStruct, C::Error> {
JsonObjectEncoder::new(self.cx, self.writer)
}

Expand All @@ -308,7 +309,7 @@ where
fn encode_tuple_variant<T>(
mut self,
tag: &T,
_: usize,
_: &TupleHint,
) -> Result<Self::EncodeTupleVariant, C::Error>
where
T: ?Sized + Encode<C::Mode>,
Expand All @@ -323,7 +324,7 @@ where
fn encode_struct_variant<T>(
mut self,
tag: &T,
_: usize,
_: &StructHint,
) -> Result<Self::EncodeStructVariant, C::Error>
where
T: ?Sized + Encode<C::Mode>,
Expand Down
15 changes: 6 additions & 9 deletions crates/musli-macros/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,6 @@ fn decode_enum(cx: &Ctxt<'_>, e: &Build<'_>, en: &Enum) -> Result<TokenStream> {
let Tokens {
as_decoder_t,
buf_t,
build_struct_hint,
build_unsized_struct_hint,
context_t,
decoder_t,
fmt,
Expand Down Expand Up @@ -485,9 +483,9 @@ fn decode_enum(cx: &Ctxt<'_>, e: &Build<'_>, en: &Enum) -> Result<TokenStream> {
let #buffer_var = #decoder_t::decode_buffer(#decoder_var)?;
let st = #as_decoder_t::as_decoder(&#buffer_var)?;

static #struct_hint_static: &#unsized_struct_hint = &#build_unsized_struct_hint();
static #struct_hint_static: #unsized_struct_hint = #unsized_struct_hint::new();

let #variant_tag_var = #decoder_t::decode_unsized_struct(st, #struct_hint_static, |st| {
let #variant_tag_var = #decoder_t::decode_unsized_struct(st, &#struct_hint_static, |st| {
let #variant_tag_var #name_type = {
let #variant_decoder_var = loop {
let #option_some(mut #entry_var) = #struct_decoder_t::decode_field(st)? else {
Expand Down Expand Up @@ -666,11 +664,11 @@ fn decode_enum(cx: &Ctxt<'_>, e: &Build<'_>, en: &Enum) -> Result<TokenStream> {
#output_enum
#outcome_enum

static #struct_hint_static: &#struct_hint = &#build_struct_hint(2);
static #struct_hint_static: #struct_hint = #struct_hint::with_size(2);

#enter

#decoder_t::decode_struct(#decoder_var, #struct_hint_static, move |#struct_decoder_var| {
#decoder_t::decode_struct(#decoder_var, &#struct_hint_static, move |#struct_decoder_var| {
let mut #tag_var = #option_none;

let #output_var = loop {
Expand Down Expand Up @@ -731,7 +729,6 @@ fn decode_tagged(

let Tokens {
buf_t,
build_struct_hint,
context_t,
decoder_t,
default_function,
Expand Down Expand Up @@ -1005,9 +1002,9 @@ fn decode_tagged(

#enter

static #struct_hint_static: &#struct_hint = &#build_struct_hint(#fields_len);
static #struct_hint_static: #struct_hint = #struct_hint::with_size(#fields_len);

#decoder_t::decode_struct(#decoder_var, #struct_hint_static, move |#type_decoder_var| {
#decoder_t::decode_struct(#decoder_var, &#struct_hint_static, move |#type_decoder_var| {
while let #option_some(mut #struct_decoder_var) = #struct_decoder_t::decode_field(#type_decoder_var)? {
#field_alloc
#tag_stmt
Expand Down
Loading