Skip to content

Commit

Permalink
feat: adjust encoding of option<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
dnaka91 committed Oct 10, 2023
1 parent 8855572 commit 39c6ccb
Show file tree
Hide file tree
Showing 13 changed files with 219 additions and 122 deletions.
71 changes: 30 additions & 41 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ license = "MIT"
[workspace.dependencies]
color-eyre = { version = "0.6.2", default-features = false }
indoc = "2.0.4"
insta = { version = "1.33.0", features = ["glob"] }
insta = { version = "1.34.0", features = ["glob"] }
miette = "5.10.0"
mimalloc = "0.1.39"
proc-macro2 = { version = "1.0.67", default-features = false }
proc-macro2 = { version = "1.0.69", default-features = false }
quote = { version = "1.0.33", default-features = false }
syn = "2.0.38"
thiserror = "1.0.49"
Expand Down
4 changes: 4 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ bench:
check:
cargo hack clippy --workspace --feature-powerset --no-dev-deps

# Format the code of all Rust crates
fmt:
cargo +nightly fmt --all

# Start up the local server for the book
@book:
cd book && just dev
Expand Down
14 changes: 11 additions & 3 deletions crates/stef-build/src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,11 @@ fn compile_field_matches(fields: &Fields<'_>) -> TokenStream {
}| {
let id = proc_macro2::Literal::u32_unsuffixed(id.0);
let name = proc_macro2::Ident::new(name, Span::call_site());
let ty = compile_data_type(ty);
let ty = compile_data_type(if let DataType::Option(ty) = ty {
ty
} else {
ty
});

quote! { #id => #name = Some(#ty?) }
},
Expand All @@ -159,7 +163,11 @@ fn compile_field_matches(fields: &Fields<'_>) -> TokenStream {
.map(|(idx, UnnamedField { ty, id })| {
let id = proc_macro2::Literal::u32_unsuffixed(id.0);
let name = Ident::new(&format!("n{idx}"), Span::call_site());
let ty = compile_data_type(ty);
let ty = compile_data_type(if let DataType::Option(ty) = ty {
ty
} else {
ty
});

quote! { #id => #name = Some(#ty?) }
});
Expand Down Expand Up @@ -246,7 +254,7 @@ fn compile_data_type(ty: &DataType<'_>) -> TokenStream {
DataType::Vec(_ty) => quote! { ::stef::buf::decode_vec(r) },
DataType::HashMap(_kv) => quote! { ::stef::buf::decode_hash_map(r) },
DataType::HashSet(_ty) => quote! { ::stef::buf::decode_hash_set(r) },
DataType::Option(ty) => compile_data_type(ty),
DataType::Option(_ty) => quote! { ::stef::buf::decode_option(r) },
DataType::NonZero(ty) => match **ty {
DataType::U8 => quote! { NonZeroU8::decode(r) },
DataType::U16 => quote! { NonZeroU16::decode(r) },
Expand Down
2 changes: 2 additions & 0 deletions crates/stef-build/src/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ fn compile_alias(

quote! {
#comment
#[allow(dead_code)]
pub type #alias = #target;
}
}
Expand Down Expand Up @@ -583,6 +584,7 @@ mod tests {
"#};
let expect = indoc! {r#"
/// Hello world!
#[allow(dead_code)]
pub type Sample = String;
"#};

Expand Down
18 changes: 13 additions & 5 deletions crates/stef-build/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ fn compile_struct_fields(fields: &Fields<'_>) -> TokenStream {
}| {
let id = proc_macro2::Literal::u32_unsuffixed(id.0);
let name = proc_macro2::Ident::new(name, Span::call_site());
let ty = compile_data_type(ty, quote! { self.#name });

quote! { ::stef::buf::encode_field(w, #id, |w| { #ty }); }
if matches!(ty, DataType::Option(_)) {
quote! { ::stef::buf::encode_field_option(w, #id, &self.#name); }
} else {
let ty = compile_data_type(ty, quote! { self.#name });
quote! { ::stef::buf::encode_field(w, #id, |w| { #ty }); }
}
},
);

Expand Down Expand Up @@ -154,9 +158,13 @@ fn compile_variant_fields(fields: &Fields<'_>) -> TokenStream {
}| {
let id = proc_macro2::Literal::u32_unsuffixed(id.0);
let name = proc_macro2::Ident::new(name, Span::call_site());
let ty = compile_data_type(ty, quote! { *#name });

quote! { ::stef::buf::encode_field(w, #id, |w| { #ty }); }
if matches!(ty, DataType::Option(_)) {
quote! { ::stef::buf::encode_field_option(w, #id, &#name); }
} else {
let ty = compile_data_type(ty, quote! { *#name });
quote! { ::stef::buf::encode_field(w, #id, |w| { #ty }); }
}
},
);

Expand Down Expand Up @@ -221,7 +229,7 @@ fn compile_data_type(ty: &DataType<'_>, name: TokenStream) -> TokenStream {
DataType::Vec(_ty) => quote! { ::stef::buf::encode_vec(w, &#name) },
DataType::HashMap(_kv) => quote! { ::stef::buf::encode_hash_map(w, #name) },
DataType::HashSet(_ty) => quote! { ::stef::buf::encode_hash_set(w, #name) },
DataType::Option(_ty) => quote! { ::stef::buf::encode_option(w, #name) },
DataType::Option(_ty) => quote! { ::stef::buf::encode_option(w, &#name) },
DataType::BoxString => quote! { ::stef::buf::encode_string(w, &*#name) },
DataType::BoxBytes => quote! { ::stef::buf::encode_bytes(w, &*#name) },
DataType::Tuple(types) => match types.len() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ expression: "/// Sample type alias.\ntype Sample = u32;"
input_file: crates/stef-parser/tests/inputs/alias-basic.stef
---
/// Sample type alias.
#[allow(dead_code)]
pub type Sample = u32;

Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl ::stef::Encode for Sample {
3,
|w| { ::stef::buf::encode_hash_set(w, self.f3) },
);
::stef::buf::encode_field(w, 4, |w| { ::stef::buf::encode_option(w, self.f4) });
::stef::buf::encode_field_option(w, 4, &self.f4);
::stef::buf::encode_field(w, 5, |w| { (self.f5).encode(w) });
::stef::buf::encode_u32(w, ::stef::buf::END_MARKER);
}
Expand Down
Loading

0 comments on commit 39c6ccb

Please sign in to comment.