Skip to content

Commit

Permalink
Use angle brackets in a all derived type accesses
Browse files Browse the repository at this point in the history
  • Loading branch information
fwcd committed Aug 3, 2023
1 parent da9a8e8 commit c2e4a1e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lightpack-derive/src/pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn derive_pack(input: TokenStream) -> TokenStream {
.map(type_to_turbofish);

quote! {
#(#fields.pack::<B>(buffer); let buffer = &mut buffer[#turbofish_tys::SIZE..];)*
#(#fields.pack::<B>(buffer); let buffer = &mut buffer[<#turbofish_tys>::SIZE..];)*
}
},
Data::Enum(_) => {
Expand Down
4 changes: 2 additions & 2 deletions lightpack-derive/src/size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn derive_size(input: TokenStream) -> TokenStream {

field_tys.into_iter()
.map(type_to_turbofish)
.map(|t| quote! { #t::SIZE })
.map(|t| quote! { <#t>::SIZE })
.reduce(|e1, e2| quote! { #e1 + #e2 })
.unwrap_or_else(|| quote! { 0 })
},
Expand All @@ -31,7 +31,7 @@ pub fn derive_size(input: TokenStream) -> TokenStream {
.expect("#[derive(Size)] currently only supports enums with a #[repr]");

quote! {
#repr_type::SIZE
<#repr_type>::SIZE
}
},
Data::Union(_) => unimplemented!("#[derive(Size)] is not supported for unions yet!"),
Expand Down
4 changes: 2 additions & 2 deletions lightpack-derive/src/unpack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ pub fn derive_unpack(input: TokenStream) -> TokenStream {
.map(|f| (f.ident.as_ref().expect("#[derive(Unpack)] requires fields to be named"), type_to_turbofish(f.ty.clone())))
.unzip();
quote! {
#(let #fields = #turbofish_tys::unpack::<B>(buffer)?; let buffer = &buffer[#turbofish_tys::SIZE..];)*
#(let #fields = <#turbofish_tys>::unpack::<B>(buffer)?; let buffer = &buffer[<#turbofish_tys>::SIZE..];)*
Ok(#name { #(#fields),* })
}
},
Fields::Unnamed(fs) => {
let vars: Vec<Ident> = (0..fs.unnamed.len()).map(|i| Ident::new(&format!("x{}", i), Span::call_site())).collect();
let turbofish_tys: Vec<Type> = fs.unnamed.iter().map(|f| type_to_turbofish(f.ty.clone())).collect();
quote! {
#(let #vars = #turbofish_tys::unpack::<B>(buffer)?; let buffer = &buffer[#turbofish_tys::SIZE..];)*
#(let #vars = <#turbofish_tys>::unpack::<B>(buffer)?; let buffer = &buffer[<#turbofish_tys>::SIZE..];)*
Ok(#name(#(#vars),*))
}
},
Expand Down
8 changes: 8 additions & 0 deletions tests/roundtrip_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,11 @@ fn arrays() {
assert_roundtrips!([(false, true), (true, true)]);
assert_roundtrips!(['H', 'e', 'l', 'l', 'o']);
}

#[test]
fn tuple_struct_array() {
#[derive(Size, Pack, Unpack, Debug, PartialEq, Eq)]
struct Echo([u8; 4]);

assert_roundtrips!(Echo([0, 1, 2, 3]));
}

0 comments on commit c2e4a1e

Please sign in to comment.