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

Rename methods to be more appropriate and limit use #127

Merged
merged 1 commit into from
Apr 13, 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
12 changes: 6 additions & 6 deletions crates/musli-descriptive/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use core::mem::take;
use alloc::vec::Vec;

use musli::de::{
Decode, Decoder, MapDecoder, MapEntriesDecoder, MapEntryDecoder, NumberHint, NumberVisitor,
PackDecoder, SequenceDecoder, SizeHint, Skip, StructDecoder, StructFieldDecoder,
StructFieldsDecoder, TupleDecoder, TypeHint, ValueVisitor, VariantDecoder, Visit, Visitor,
Decode, DecodeUnsized, Decoder, MapDecoder, MapEntriesDecoder, MapEntryDecoder, NumberHint,
NumberVisitor, PackDecoder, SequenceDecoder, SizeHint, Skip, StructDecoder, StructFieldDecoder,
StructFieldsDecoder, TupleDecoder, TypeHint, ValueVisitor, VariantDecoder, Visitor,
};
use musli::hint::{StructHint, TupleHint, UnsizedStructHint};
use musli::Context;
Expand Down Expand Up @@ -292,12 +292,12 @@ where
}

#[inline]
fn visit<T, F, O>(self, f: F) -> Result<O, Self::Error>
fn decode_unsized<T, F, O>(self, f: F) -> Result<O, Self::Error>
where
T: ?Sized + Visit<'de, Self::Mode>,
T: ?Sized + DecodeUnsized<'de, Self::Mode>,
F: FnOnce(&T) -> Result<O, Self::Error>,
{
self.cx.visit(self, f)
self.cx.decode_unsized(self, f)
}

#[inline]
Expand Down
8 changes: 4 additions & 4 deletions crates/musli-json/src/de/key_decoder.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::fmt;

use musli::de::{
Decode, Decoder, NumberHint, SizeHint, Skip, TypeHint, ValueVisitor, Visit, Visitor,
Decode, DecodeUnsized, Decoder, NumberHint, SizeHint, Skip, TypeHint, ValueVisitor, Visitor,
};
use musli::Context;

Expand Down Expand Up @@ -80,12 +80,12 @@ where
}

#[inline]
fn visit<T, F, O>(self, f: F) -> Result<O, Self::Error>
fn decode_unsized<T, F, O>(self, f: F) -> Result<O, Self::Error>
where
T: ?Sized + Visit<'de, Self::Mode>,
T: ?Sized + DecodeUnsized<'de, Self::Mode>,
F: FnOnce(&T) -> Result<O, Self::Error>,
{
self.cx.visit(self, f)
self.cx.decode_unsized(self, f)
}

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

use musli::de::{
Decode, Decoder, NumberHint, NumberVisitor, SequenceDecoder, SizeHint, Skip, TypeHint,
ValueVisitor, Visit, Visitor,
Decode, DecodeUnsized, Decoder, NumberHint, NumberVisitor, SequenceDecoder, SizeHint, Skip,
TypeHint, ValueVisitor, Visitor,
};
use musli::hint::{StructHint, TupleHint, UnsizedStructHint};
use musli::Context;
Expand Down Expand Up @@ -155,12 +155,12 @@ where
}

#[inline]
fn visit<T, F, O>(self, f: F) -> Result<O, Self::Error>
fn decode_unsized<T, F, O>(self, f: F) -> Result<O, Self::Error>
where
T: ?Sized + Visit<'de, Self::Mode>,
T: ?Sized + DecodeUnsized<'de, Self::Mode>,
F: FnOnce(&T) -> Result<O, Self::Error>,
{
self.cx.visit(self, f)
self.cx.decode_unsized(self, f)
}

#[inline]
Expand Down
102 changes: 53 additions & 49 deletions crates/musli-macros/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,19 @@ fn decode_enum(cx: &Ctxt<'_>, e: &Build<'_>, en: &Enum) -> Result<TokenStream> {
let name_type;

match en.name_method {
NameMethod::Visit => {
NameMethod::Value => {
for v in &en.variants {
variant_output_tags.push((v, v.name.clone(), v.name.clone()));
}

let decode_t_decode = &e.decode_t_decode;

decode_tag = quote!(#decode_t_decode(#ctx_var, #variant_decoder_var)?);
output_enum = None;
fallback = quote!(_ => #fallback);
name_type = en.name_type.clone();
}
NameMethod::Unsized(method) => {
let mut tag_variants = Vec::new();
let output_type = e.cx.type_with_span("VariantTag", en.span);

Expand All @@ -209,9 +221,10 @@ fn decode_enum(cx: &Ctxt<'_>, e: &Build<'_>, en: &Enum) -> Result<TokenStream> {
let arms = tag_variants.iter().map(|o| o.as_arm(option_some));

let visit_type = &en.name_type;
let method = method.as_method_name();

decode_tag = quote! {
#decoder_t::visit(#variant_decoder_var, |#value_var: &#visit_type| {
#decoder_t::#method(#variant_decoder_var, |#value_var: &#visit_type| {
#result_ok(match #value_var {
#(#arms,)*
_ => #option_none,
Expand Down Expand Up @@ -256,18 +269,6 @@ fn decode_enum(cx: &Ctxt<'_>, e: &Build<'_>, en: &Enum) -> Result<TokenStream> {
fallback = quote!(#option_none => { #fallback });
name_type = syn::parse_quote!(#option<#output_type>);
}
NameMethod::Value => {
for v in &en.variants {
variant_output_tags.push((v, v.name.clone(), v.name.clone()));
}

let decode_t_decode = &e.decode_t_decode;

decode_tag = quote!(#decode_t_decode(#ctx_var, #variant_decoder_var)?);
output_enum = None;
fallback = quote!(_ => #fallback);
name_type = en.name_type.clone();
}
}

let Some(enum_tagging) = en.enum_tagging else {
Expand Down Expand Up @@ -395,7 +396,7 @@ fn decode_enum(cx: &Ctxt<'_>, e: &Build<'_>, en: &Enum) -> Result<TokenStream> {
}
};
}
NameMethod::Visit => {
NameMethod::Unsized(method) => {
outcome_enum = Some(quote! {
enum #outcome_type { Tag, Skip }
});
Expand All @@ -407,9 +408,10 @@ fn decode_enum(cx: &Ctxt<'_>, e: &Build<'_>, en: &Enum) -> Result<TokenStream> {
});

let visit_type = &en.name_type;
let method = method.as_method_name();

let decode_outcome = quote! {
#decoder_t::visit(#field_name_var, |#value_var: &#visit_type| {
#decoder_t::#method(#field_name_var, |#value_var: &#visit_type| {
#result_ok(match #value_var {
#field_tag => #outcome_type::Tag,
#value_var => {
Expand Down Expand Up @@ -532,7 +534,36 @@ fn decode_enum(cx: &Ctxt<'_>, e: &Build<'_>, en: &Enum) -> Result<TokenStream> {
let decode_match;

match en.name_method {
NameMethod::Visit => {
NameMethod::Value => {
field_alloc = None;

decode_match = quote! {
match #decode_t_decode(#ctx_var, decoder)? {
#tag => {
let #variant_decoder_var = #struct_field_decoder_t::decode_field_value(#entry_var)?;
#tag_var = #option_some(#decode_tag);
}
#content => {
let #option_some(#variant_tag_var) = #tag_var else {
return #result_err(#context_t::invalid_field_tag(#ctx_var, #type_name, &#tag));
};

let #body_decoder_var = #struct_field_decoder_t::decode_field_value(#entry_var)?;

break #result_ok(match #variant_tag_var {
#(#patterns,)*
#fallback
});
}
#field_var => {
if #skip_field(#entry_var)? {
return #result_err(#context_t::invalid_field_tag(#ctx_var, #type_name, &#field_var));
}
}
}
};
}
NameMethod::Unsized(method) => {
outcome_enum = quote! {
enum #outcome_type { Tag, Content, Skip }
};
Expand All @@ -544,9 +575,10 @@ fn decode_enum(cx: &Ctxt<'_>, e: &Build<'_>, en: &Enum) -> Result<TokenStream> {
});

let visit_type = &en.name_type;
let method = method.as_method_name();

decode_match = quote! {
let outcome = #decoder_t::visit(#field_name_var, |#value_var: &#visit_type| {
let outcome = #decoder_t::#method(#field_name_var, |#value_var: &#visit_type| {
#result_ok(match #value_var {
#tag => #outcome_type::Tag,
#content => #outcome_type::Content,
Expand Down Expand Up @@ -585,35 +617,6 @@ fn decode_enum(cx: &Ctxt<'_>, e: &Build<'_>, en: &Enum) -> Result<TokenStream> {
}
};
}
NameMethod::Value => {
field_alloc = None;

decode_match = quote! {
match #decode_t_decode(#ctx_var, decoder)? {
#tag => {
let #variant_decoder_var = #struct_field_decoder_t::decode_field_value(#entry_var)?;
#tag_var = #option_some(#decode_tag);
}
#content => {
let #option_some(#variant_tag_var) = #tag_var else {
return #result_err(#context_t::invalid_field_tag(#ctx_var, #type_name, &#tag));
};

let #body_decoder_var = #struct_field_decoder_t::decode_field_value(#entry_var)?;

break #result_ok(match #variant_tag_var {
#(#patterns,)*
#fallback
});
}
#field_var => {
if #skip_field(#entry_var)? {
return #result_err(#context_t::invalid_field_tag(#ctx_var, #type_name, &#field_var));
}
}
}
};
}
};

let enter = cx.trace.then(|| {
Expand Down Expand Up @@ -859,7 +862,7 @@ fn decode_tagged(

name_type = st.name_type.clone();
}
NameMethod::Visit => {
NameMethod::Unsized(method) => {
let mut outputs = Vec::new();
let output_type =
e.cx.type_with_span("TagVisitorOutput", e.input.ident.span());
Expand Down Expand Up @@ -904,9 +907,10 @@ fn decode_tagged(
});

let visit_type = &st.name_type;
let method = method.as_method_name();

decode_tag = quote! {
#decoder_t::visit(#struct_decoder_var, |#value_var: &#visit_type| {
#decoder_t::#method(#struct_decoder_var, |#value_var: &#visit_type| {
#result_ok(match #value_var {
#(#patterns,)*
#value_var => {
Expand Down
22 changes: 19 additions & 3 deletions crates/musli-macros/src/expander.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,29 @@ use crate::internals::name::NameAll;
use crate::internals::tokens::Tokens;
use crate::internals::{Ctxt, Expansion, Mode, Only, Result};

#[derive(Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Copy)]
pub(crate) enum UnsizedMethod {
Default,
Bytes,
}

impl UnsizedMethod {
/// Get corresponding decoder method name to use.
pub(crate) fn as_method_name(&self) -> syn::Ident {
match self {
Self::Default => syn::Ident::new("decode_unsized", Span::call_site()),
Self::Bytes => syn::Ident::new("decode_unsized_bytes", Span::call_site()),
}
}
}

#[derive(Default, Clone, Copy)]
pub(crate) enum NameMethod {
/// Load the tag by visit.
Visit,
/// Load the tag by value.
#[default]
Value,
/// Load the tag by visit.
Unsized(UnsizedMethod),
}

pub(crate) struct FieldData<'a> {
Expand Down
27 changes: 17 additions & 10 deletions crates/musli-macros/src/internals/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use syn::Token;

use crate::de::{build_call, build_reference};
use crate::expander::{
self, Data, EnumData, Expander, FieldData, NameMethod, StructData, VariantData,
self, Data, EnumData, Expander, FieldData, NameMethod, StructData, UnsizedMethod, VariantData,
};

use super::attr::{EnumTagging, Packing};
Expand Down Expand Up @@ -135,7 +135,7 @@ impl Body<'_> {

pub(crate) fn name_local_type(&self) -> syn::Type {
match self.name_method {
NameMethod::Visit => syn::Type::Reference(syn::TypeReference {
NameMethod::Unsized(..) => syn::Type::Reference(syn::TypeReference {
and_token: <Token![&]>::default(),
lifetime: None,
mutability: None,
Expand Down Expand Up @@ -169,7 +169,7 @@ impl Enum<'_> {

pub(crate) fn name_local_type(&self) -> syn::Type {
match self.name_method {
NameMethod::Visit => syn::Type::Reference(syn::TypeReference {
NameMethod::Unsized(..) => syn::Type::Reference(syn::TypeReference {
and_token: <Token![&]>::default(),
lifetime: None,
mutability: None,
Expand Down Expand Up @@ -546,14 +546,21 @@ fn split_name(

fn determine_name_method(ty: &syn::Type) -> (NameMethod, Option<NameAll>) {
match ty {
syn::Type::Path(syn::TypePath { qself: None, path }) => {
if path.is_ident("str") {
return (NameMethod::Visit, Some(NameAll::Name));
}
}
syn::Type::Slice(..) => {
return (NameMethod::Visit, None);
syn::Type::Path(syn::TypePath { qself: None, path }) if path.is_ident("str") => {
return (
NameMethod::Unsized(UnsizedMethod::Default),
Some(NameAll::Name),
);
}
syn::Type::Slice(syn::TypeSlice { elem, .. }) => match &**elem {
syn::Type::Path(syn::TypePath { qself: None, path }) if path.is_ident("u8") => {
return (
NameMethod::Unsized(UnsizedMethod::Bytes),
Some(NameAll::Name),
);
}
_ => {}
},
_ => {}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/musli-macros/src/internals/name.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::fmt;
use core::mem::take;

use crate::expander::NameMethod;
use crate::expander::{NameMethod, UnsizedMethod};

#[derive(Default, Debug, Clone, Copy)]
#[allow(clippy::enum_variant_names)]
Expand Down Expand Up @@ -41,7 +41,7 @@ impl NameAll {
pub(crate) fn name_method(&self) -> NameMethod {
match self {
NameAll::Index => NameMethod::Value,
_ => NameMethod::Visit,
_ => NameMethod::Unsized(UnsizedMethod::Default),
}
}

Expand Down
Loading