Skip to content

Commit

Permalink
Add type_name macro
Browse files Browse the repository at this point in the history
  • Loading branch information
ChHecker committed Sep 5, 2023
1 parent d0bfb57 commit 052cdd4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
2 changes: 2 additions & 0 deletions redb-derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
[package]
name = "redb-derive"
version = "0.1.0"
license = "MIT OR Apache-2.0"
edition = "2021"

[lib]
proc-macro = true

[dependencies]
proc-macro2 = "1.0.66"
quote = "1.0.33"
syn = "2.0.29"
19 changes: 13 additions & 6 deletions redb-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@ use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, DeriveInput};

#[proc_macro_derive(RedbValue, attributes(fixed_width))]
#[proc_macro_derive(RedbValue, attributes(fixed_width, type_name))]
pub fn redb_value(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let name = input.ident;
let width = match &input.attrs.get(0) {
Some(_) => quote!(Some(std::mem::size_of::<Self>())),
None => quote!(None),
};

let mut width = quote!(None);
let mut type_name = quote!(stringify!(#name));
for attr in input.attrs {
if attr.path().is_ident("fixed_width") {
width = quote!(Some(std::mem::size_of::<Self>()));
}
if attr.path().is_ident("type_name") {
type_name = attr.meta.require_list().unwrap().tokens.clone()
}
}

let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();

Expand Down Expand Up @@ -38,7 +45,7 @@ pub fn redb_value(input: TokenStream) -> TokenStream {
}

fn type_name() -> redb::TypeName {
redb::TypeName::new(stringify!($t))
redb::TypeName::new(#type_name)
}
}
};
Expand Down

0 comments on commit 052cdd4

Please sign in to comment.