Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow generics for borsh storage key derive #930

Merged
merged 6 commits into from
Sep 27, 2022
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix codegen without where clause
  • Loading branch information
austinabell committed Sep 26, 2022
commit 134087302128d2a5621f3468647f6e06ba87d0ad
11 changes: 9 additions & 2 deletions near-sdk-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use self::core_impl::*;
use proc_macro2::Span;
use quote::{quote, ToTokens};
use syn::visit::Visit;
use syn::{File, ItemEnum, ItemImpl, ItemStruct, ItemTrait};
use syn::{parse_quote, File, ItemEnum, ItemImpl, ItemStruct, ItemTrait, WhereClause};

/// This attribute macro is used on a struct and its implementations
/// to generate the necessary code to expose `pub` methods from the contract as well
Expand Down Expand Up @@ -278,8 +278,15 @@ pub fn borsh_storage_key(item: TokenStream) -> TokenStream {
);
};
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
let predicate = parse_quote!(#name #ty_generics: ::near_sdk::borsh::BorshSerialize);
let where_clause: WhereClause = if let Some(mut w) = where_clause.cloned() {
w.predicates.push(predicate);
parse_quote!(#w)
} else {
parse_quote!(where #predicate)
};
TokenStream::from(quote! {
impl #impl_generics near_sdk::__private::BorshIntoStorageKey for #name #ty_generics #where_clause #name #ty_generics: ::near_sdk::borsh::BorshSerialize {}
impl #impl_generics near_sdk::__private::BorshIntoStorageKey for #name #ty_generics #where_clause {}
})
}

Expand Down