Skip to content

Commit

Permalink
Fix error for non Copy enum ids
Browse files Browse the repository at this point in the history
Add/Fixup lifetime for generated code for enum ext id finding.
Lifetime __deku needs to be added to the deku_id_type, and the impl.
  • Loading branch information
wcampbell0x2a committed Jan 21, 2024
1 parent 1e96f86 commit e32a52f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
9 changes: 7 additions & 2 deletions deku-derive/src/macros/deku_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,12 @@ fn emit_enum(input: &DekuData) -> Result<TokenStream, syn::Error> {

// Implement `DekuEnumExt`
if let Some(deku_id_type) = deku_id_type {
tokens.extend(quote! {
impl #imp DekuEnumExt<#lifetime, (#deku_id_type)> for #ident #wher {
if !imp.to_token_stream().is_empty() {
// Generics (#imp) are not supported, as our __deku
// would need to be appended to #imp
} else {
tokens.extend(quote! {
impl<'__deku> #imp ::#crate_::DekuEnumExt<#lifetime, (#deku_id_type)> for #ident #wher {
fn deku_id(&self) -> core::result::Result<(#deku_id_type), DekuError> {
match self {
#(#deku_ids ,)*
Expand All @@ -413,6 +417,7 @@ fn emit_enum(input: &DekuData) -> Result<TokenStream, syn::Error> {
}
}
});
}
}

// println!("{}", tokens.to_string());
Expand Down
5 changes: 5 additions & 0 deletions deku-derive/src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use syn::parse::Parser;
use syn::punctuated::Punctuated;
use syn::spanned::Spanned;
use syn::token::Comma;
use syn::Lifetime;

use crate::Num;

Expand Down Expand Up @@ -211,6 +212,10 @@ fn gen_type_from_ctx_id(
if let syn::FnArg::Typed(pat_type) = arg {
if let syn::Pat::Ident(ident) = &*pat_type.pat {
if id == ident.ident {
let mut pat_type = pat_type.clone();
if let syn::Type::Reference(r) = pat_type.ty.as_mut() {
r.lifetime = Some(Lifetime::new("'__deku", Span::call_site()));
}
let ty = &pat_type.ty;
t = Some(quote! {#ty});
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ pub trait DekuUpdate {
}

/// "Extended Enum" trait: obtain additional enum information
pub trait DekuEnumExt<'a, T> {
pub trait DekuEnumExt<'__deku, T> {
/// Obtain `id` of a given enum variant
fn deku_id(&self) -> Result<T, DekuError>;
}
Expand Down

0 comments on commit e32a52f

Please sign in to comment.