Skip to content

Commit

Permalink
Replace syn::Index::from by proc_macro2::Literal::usize_unsuffixed
Browse files Browse the repository at this point in the history
  • Loading branch information
hchataing committed May 22, 2024
1 parent cb14a2a commit b7490fe
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
10 changes: 5 additions & 5 deletions pdl-compiler/src/backends/rust/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ impl<'a> FieldParser<'a> {
// The element width is not known, but the array
// element count is known statically. Parse elements
// item by item as an array.
let count = syn::Index::from(*count);
let count = proc_macro2::Literal::usize_unsuffixed(*count);
self.tokens.extend(quote! {
// TODO(mgeisler): use
// https://doc.rust-lang.org/std/array/fn.try_from_fn.html
Expand Down Expand Up @@ -466,12 +466,12 @@ impl<'a> FieldParser<'a> {
(ElementWidth::Static(element_width), ArrayShape::Static(count)) => {
// The element width is known, and the array element
// count is known statically.
let count = syn::Index::from(*count);
let count = proc_macro2::Literal::usize_unsuffixed(*count);
// This creates a nicely formatted size.
let array_size = if element_width == 1 {
quote!(#count)
} else {
let element_width = syn::Index::from(element_width);
let element_width = proc_macro2::Literal::usize_unsuffixed(element_width);
quote!(#count * #element_width)
};
self.check_size(&span, &quote! { #array_size });
Expand Down Expand Up @@ -511,7 +511,7 @@ impl<'a> FieldParser<'a> {
};
let count_field = format_ident!("{id}_count");
let array_count = if element_width != 1 {
let element_width = syn::Index::from(element_width);
let element_width = proc_macro2::Literal::usize_unsuffixed(element_width);
self.tokens.extend(quote! {
if #array_size % #element_width != 0 {
return Err(DecodeError::InvalidArraySize {
Expand Down Expand Up @@ -738,7 +738,7 @@ impl<'a> FieldParser<'a> {
0,
"Payload field offset from end of packet is not a multiple of 8"
);
let offset_from_end = syn::Index::from(offset_from_end / 8);
let offset_from_end = proc_macro2::Literal::usize_unsuffixed(offset_from_end / 8);
self.check_size(self.span, &quote!(#offset_from_end));
self.tokens.extend(quote! {
let payload = #span[..#span.len() - #offset_from_end].to_vec();
Expand Down
11 changes: 6 additions & 5 deletions pdl-compiler/src/backends/rust/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl std::ops::AddAssign<&RuntimeSize> for RuntimeSize {

impl quote::ToTokens for RuntimeSize {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
let constant = syn::Index::from(self.constant);
let constant = proc_macro2::Literal::usize_unsuffixed(self.constant);
tokens.extend(match self {
RuntimeSize { variable, .. } if variable.is_empty() => quote! { #constant },
RuntimeSize { variable, constant: 0 } => quote! { #(#variable)+* },
Expand Down Expand Up @@ -336,11 +336,12 @@ impl Encoder {
}
(ast::FieldDesc::Array { width: Some(width), .. }, _)
| (ast::FieldDesc::Array { .. }, Some(ast::DeclDesc::Enum { width, .. })) => {
let byte_width = syn::Index::from(width / 8);
if byte_width.index == 1 {
let size = width / 8;
if size == 1 {
quote! { self.#field_name.len() }
} else {
quote! { (self.#field_name.len() * #byte_width) }
let size = proc_macro2::Literal::usize_unsuffixed(size);
quote! { (self.#field_name.len() * #size) }
}
}
(ast::FieldDesc::Array { .. }, _) => {
Expand Down Expand Up @@ -467,7 +468,7 @@ impl Encoder {
self.tokens.extend(match values.as_slice() {
[] => {
let buf = format_ident!("{}", self.buf);
let count = syn::Index::from(self.bit_shift / 8);
let count = proc_macro2::Literal::usize_unsuffixed(self.bit_shift / 8);
quote! {
#buf.put_bytes(0, #count);
}
Expand Down

0 comments on commit b7490fe

Please sign in to comment.