From 8f5a4ec3cd7377ac2f0eb7183c201fbf388d9ce2 Mon Sep 17 00:00:00 2001 From: Dominik Nakamura Date: Wed, 13 Sep 2023 17:53:28 +0900 Subject: [PATCH] feat: skip any decoding for unit structs/variants --- crates/stef-build/src/decode.rs | 54 +++++++++++-------- ...ompiler__compile@attribute-multi.stef.snap | 6 --- ...mpiler__compile@attribute-single.stef.snap | 6 --- ...compiler__compile@attribute-unit.stef.snap | 6 --- ...piler__compile@attributes-min-ws.stef.snap | 6 --- .../compiler__compile@attributes.stef.snap | 6 --- .../compiler__compile@enum-basic.stef.snap | 10 +--- .../compiler__compile@enum-generics.stef.snap | 10 +--- .../compiler__compile@enum-many-ws.stef.snap | 10 +--- .../compiler__compile@enum-min-ws.stef.snap | 10 +--- .../compiler__compile@module-basic.stef.snap | 10 +--- .../compiler__compile@schema-basic.stef.snap | 10 +--- 12 files changed, 39 insertions(+), 105 deletions(-) diff --git a/crates/stef-build/src/decode.rs b/crates/stef-build/src/decode.rs index 88e665d..8ac1238 100644 --- a/crates/stef-build/src/decode.rs +++ b/crates/stef-build/src/decode.rs @@ -17,20 +17,28 @@ pub fn compile_struct( let field_matches = compile_field_matches(fields); let field_assigns = compile_field_assigns(fields); - quote! { - impl #generics ::stef::Decode for #name #generics #generics_where { - fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { - #field_vars + let body = if matches!(fields, Fields::Unit) { + quote! { Ok(Self) } + } else { + quote! { + #field_vars - loop{ - match ::stef::buf::decode_id(r)? { - ::stef::buf::END_MARKER => break, - #field_matches - _ => continue, - } + loop{ + match ::stef::buf::decode_id(r)? { + ::stef::buf::END_MARKER => break, + #field_matches + _ => continue, } + } + + Ok(Self #field_assigns) + } + }; - Ok(Self #field_assigns) + quote! { + impl #generics ::stef::Decode for #name #generics #generics_where { + fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { + #body } } } @@ -75,19 +83,23 @@ fn compile_variant( let field_matches = compile_field_matches(fields); let field_assigns = compile_field_assigns(fields); - quote! { - #id => { - #field_vars + if matches!(fields, Fields::Unit) { + quote! { #id => Ok(Self::#name) } + } else { + quote! { + #id => { + #field_vars - loop{ - match ::stef::buf::decode_id(r)? { - ::stef::buf::END_MARKER => break, - #field_matches - _ => continue, + loop{ + match ::stef::buf::decode_id(r)? { + ::stef::buf::END_MARKER => break, + #field_matches + _ => continue, + } } - } - Ok(Self::#name #field_assigns) + Ok(Self::#name #field_assigns) + } } } } diff --git a/crates/stef-build/tests/snapshots/compiler__compile@attribute-multi.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@attribute-multi.stef.snap index c91e78b..e3b9fff 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@attribute-multi.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@attribute-multi.stef.snap @@ -9,12 +9,6 @@ impl ::stef::Encode for Sample { } impl ::stef::Decode for Sample { fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { - loop { - match ::stef::buf::decode_id(r)? { - ::stef::buf::END_MARKER => break, - _ => continue, - } - } Ok(Self) } } diff --git a/crates/stef-build/tests/snapshots/compiler__compile@attribute-single.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@attribute-single.stef.snap index 6c91cbc..30c6278 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@attribute-single.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@attribute-single.stef.snap @@ -9,12 +9,6 @@ impl ::stef::Encode for Sample { } impl ::stef::Decode for Sample { fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { - loop { - match ::stef::buf::decode_id(r)? { - ::stef::buf::END_MARKER => break, - _ => continue, - } - } Ok(Self) } } diff --git a/crates/stef-build/tests/snapshots/compiler__compile@attribute-unit.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@attribute-unit.stef.snap index b40f1c6..98b85f2 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@attribute-unit.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@attribute-unit.stef.snap @@ -9,12 +9,6 @@ impl ::stef::Encode for Sample { } impl ::stef::Decode for Sample { fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { - loop { - match ::stef::buf::decode_id(r)? { - ::stef::buf::END_MARKER => break, - _ => continue, - } - } Ok(Self) } } diff --git a/crates/stef-build/tests/snapshots/compiler__compile@attributes-min-ws.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@attributes-min-ws.stef.snap index 59bb46f..69aa573 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@attributes-min-ws.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@attributes-min-ws.stef.snap @@ -9,12 +9,6 @@ impl ::stef::Encode for Sample { } impl ::stef::Decode for Sample { fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { - loop { - match ::stef::buf::decode_id(r)? { - ::stef::buf::END_MARKER => break, - _ => continue, - } - } Ok(Self) } } diff --git a/crates/stef-build/tests/snapshots/compiler__compile@attributes.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@attributes.stef.snap index c8897f9..bfadd0f 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@attributes.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@attributes.stef.snap @@ -9,12 +9,6 @@ impl ::stef::Encode for Sample { } impl ::stef::Decode for Sample { fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { - loop { - match ::stef::buf::decode_id(r)? { - ::stef::buf::END_MARKER => break, - _ => continue, - } - } Ok(Self) } } diff --git a/crates/stef-build/tests/snapshots/compiler__compile@enum-basic.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@enum-basic.stef.snap index a95729a..6f3a856 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@enum-basic.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@enum-basic.stef.snap @@ -44,15 +44,7 @@ impl ::stef::Encode for Sample { impl ::stef::Decode for Sample { fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { match ::stef::buf::decode_id(r)? { - 1 => { - loop { - match ::stef::buf::decode_id(r)? { - ::stef::buf::END_MARKER => break, - _ => continue, - } - } - Ok(Self::One) - } + 1 => Ok(Self::One), 2 => { let mut n0: Option = None; let mut n1: Option = None; diff --git a/crates/stef-build/tests/snapshots/compiler__compile@enum-generics.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@enum-generics.stef.snap index cf8a2ea..cd16e44 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@enum-generics.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@enum-generics.stef.snap @@ -43,15 +43,7 @@ where { fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { match ::stef::buf::decode_id(r)? { - 1 => { - loop { - match ::stef::buf::decode_id(r)? { - ::stef::buf::END_MARKER => break, - _ => continue, - } - } - Ok(Self::One) - } + 1 => Ok(Self::One), 2 => { let mut n0: Option = None; let mut n1: Option = None; diff --git a/crates/stef-build/tests/snapshots/compiler__compile@enum-many-ws.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@enum-many-ws.stef.snap index 89649e2..f761cee 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@enum-many-ws.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@enum-many-ws.stef.snap @@ -39,15 +39,7 @@ impl ::stef::Encode for Sample { impl ::stef::Decode for Sample { fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { match ::stef::buf::decode_id(r)? { - 1 => { - loop { - match ::stef::buf::decode_id(r)? { - ::stef::buf::END_MARKER => break, - _ => continue, - } - } - Ok(Self::One) - } + 1 => Ok(Self::One), 2 => { let mut n0: Option = None; let mut n1: Option = None; diff --git a/crates/stef-build/tests/snapshots/compiler__compile@enum-min-ws.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@enum-min-ws.stef.snap index 3779ec8..3abe423 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@enum-min-ws.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@enum-min-ws.stef.snap @@ -46,15 +46,7 @@ where { fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { match ::stef::buf::decode_id(r)? { - 1 => { - loop { - match ::stef::buf::decode_id(r)? { - ::stef::buf::END_MARKER => break, - _ => continue, - } - } - Ok(Self::One) - } + 1 => Ok(Self::One), 2 => { let mut n0: Option = None; let mut n1: Option = None; diff --git a/crates/stef-build/tests/snapshots/compiler__compile@module-basic.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@module-basic.stef.snap index 79200b4..24641fc 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@module-basic.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@module-basic.stef.snap @@ -21,15 +21,7 @@ pub mod a { impl ::stef::Decode for Sample { fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { match ::stef::buf::decode_id(r)? { - 1 => { - loop { - match ::stef::buf::decode_id(r)? { - ::stef::buf::END_MARKER => break, - _ => continue, - } - } - Ok(Self::One) - } + 1 => Ok(Self::One), id => Err(Error::UnknownVariant(id)), } } diff --git a/crates/stef-build/tests/snapshots/compiler__compile@schema-basic.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@schema-basic.stef.snap index 76bddd8..c8e34e8 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@schema-basic.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@schema-basic.stef.snap @@ -76,15 +76,7 @@ impl ::stef::Encode for Sample { impl ::stef::Decode for Sample { fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { match ::stef::buf::decode_id(r)? { - 1 => { - loop { - match ::stef::buf::decode_id(r)? { - ::stef::buf::END_MARKER => break, - _ => continue, - } - } - Ok(Self::One) - } + 1 => Ok(Self::One), 2 => { let mut n0: Option = None; let mut n1: Option = None;