Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lang/derive/serde/src/lazy.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use proc_macro2::Literal;
use quote::{format_ident, quote};
use quote::{format_ident, quote, ToTokens};
use syn::{spanned::Spanned, Fields, Item};

pub fn gen_lazy(input: proc_macro::TokenStream) -> syn::Result<proc_macro2::TokenStream> {
Expand All @@ -24,7 +24,10 @@ pub fn gen_lazy(input: proc_macro::TokenStream) -> syn::Result<proc_macro2::Toke
.enumerate()
.map(|(i, size)| (Literal::usize_unsuffixed(i), size))
.map(|(i, size)| quote! { Some(#i) => { #size } });

let sized = enm
.variants
.iter()
.all(|variant| matches!(variant.fields, Fields::Unit));
(
&enm.ident,
&enm.generics,
Expand All @@ -34,7 +37,7 @@ pub fn gen_lazy(input: proc_macro::TokenStream) -> syn::Result<proc_macro2::Toke
_ => unreachable!(),
}
},
quote!(false),
sized.to_token_stream(),
)
}
Item::Union(_) => return Err(syn::Error::new(item.span(), "Unions are not supported")),
Expand Down
11 changes: 10 additions & 1 deletion lang/src/lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl_sized!(f64);
impl_sized!(Pubkey);

impl<T: Lazy, const N: usize> Lazy for [T; N] {
const SIZED: bool = T::SIZED;
const SIZED: bool = N == 0 || T::SIZED;

#[inline(always)]
fn size_of(buf: &[u8]) -> usize {
Expand Down Expand Up @@ -196,6 +196,15 @@ mod tests {
len!(MyEnum::Unnamed(1, 2))
);
assert!(!MyEnum::SIZED);

#[derive(AnchorSerialize, AnchorDeserialize)]
enum UnitEnum {
A,
B,
}
assert_eq!(UnitEnum::size_of(&[0]), len!(UnitEnum::A));
assert_eq!(UnitEnum::size_of(&[1]), len!(UnitEnum::B));
assert!(UnitEnum::SIZED);
}

#[test]
Expand Down