Skip to content

Commit

Permalink
feat: skip any decoding for unit structs/variants
Browse files Browse the repository at this point in the history
  • Loading branch information
dnaka91 committed Sep 13, 2023
1 parent 86536c9 commit 8f5a4ec
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 105 deletions.
54 changes: 33 additions & 21 deletions crates/stef-build/src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
#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<Self> {
#body
}
}
}
Expand Down Expand Up @@ -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)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ impl ::stef::Encode for Sample {
}
impl ::stef::Decode for Sample {
fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result<Self> {
loop {
match ::stef::buf::decode_id(r)? {
::stef::buf::END_MARKER => break,
_ => continue,
}
}
Ok(Self)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ impl ::stef::Encode for Sample {
}
impl ::stef::Decode for Sample {
fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result<Self> {
loop {
match ::stef::buf::decode_id(r)? {
::stef::buf::END_MARKER => break,
_ => continue,
}
}
Ok(Self)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ impl ::stef::Encode for Sample {
}
impl ::stef::Decode for Sample {
fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result<Self> {
loop {
match ::stef::buf::decode_id(r)? {
::stef::buf::END_MARKER => break,
_ => continue,
}
}
Ok(Self)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ impl ::stef::Encode for Sample {
}
impl ::stef::Decode for Sample {
fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result<Self> {
loop {
match ::stef::buf::decode_id(r)? {
::stef::buf::END_MARKER => break,
_ => continue,
}
}
Ok(Self)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ impl ::stef::Encode for Sample {
}
impl ::stef::Decode for Sample {
fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result<Self> {
loop {
match ::stef::buf::decode_id(r)? {
::stef::buf::END_MARKER => break,
_ => continue,
}
}
Ok(Self)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,7 @@ impl ::stef::Encode for Sample {
impl ::stef::Decode for Sample {
fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result<Self> {
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<u32> = None;
let mut n1: Option<u64> = None;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,7 @@ where
{
fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result<Self> {
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<A> = None;
let mut n1: Option<B> = None;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,7 @@ impl ::stef::Encode for Sample {
impl ::stef::Decode for Sample {
fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result<Self> {
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<u32> = None;
let mut n1: Option<u64> = None;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,7 @@ where
{
fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result<Self> {
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<u32> = None;
let mut n1: Option<u64> = None;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,7 @@ pub mod a {
impl ::stef::Decode for Sample {
fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result<Self> {
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)),
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,7 @@ impl ::stef::Encode for Sample {
impl ::stef::Decode for Sample {
fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result<Self> {
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<u32> = None;
let mut n1: Option<u64> = None;
Expand Down

0 comments on commit 8f5a4ec

Please sign in to comment.