-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
swap out std hashmap with the hashbrown one
- Loading branch information
glendc
committed
Apr 5, 2024
1 parent
13920c1
commit b5f7d9e
Showing
8 changed files
with
150 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,95 +1,9 @@ | ||
#![forbid(unsafe_code)] | ||
pub use venndb_macros::VennDB; | ||
|
||
mod errors; | ||
mod field; | ||
mod generate_db; | ||
mod parse_attrs; | ||
#[doc(hidden)] | ||
pub mod internal { | ||
//! Hidden thirdparty dependencies for venndb, | ||
//! not to be relied upon directly, as they may change at any time. | ||
use errors::Errors; | ||
use field::StructField; | ||
use parse_attrs::{FieldAttrs, TypeAttrs}; | ||
use proc_macro2::TokenStream; | ||
use quote::{format_ident, quote, ToTokens}; | ||
|
||
/// Derive macro generating VennDB functionality for this struct. | ||
#[proc_macro_derive(VennDB, attributes(venndb))] | ||
pub fn venndb(input: proc_macro::TokenStream) -> proc_macro::TokenStream { | ||
let ast = syn::parse_macro_input!(input as syn::DeriveInput); | ||
let gen = impl_from_args(&ast); | ||
gen.into() | ||
} | ||
|
||
/// Transform the input into a token stream containing any generated implementations, | ||
/// as well as all errors that occurred. | ||
fn impl_from_args(input: &syn::DeriveInput) -> TokenStream { | ||
let errors = &Errors::default(); | ||
let type_attrs = &TypeAttrs::parse(errors, input); | ||
let mut output_tokens = match &input.data { | ||
syn::Data::Struct(ds) => impl_from_args_struct( | ||
errors, | ||
&input.vis, | ||
&input.ident, | ||
type_attrs, | ||
&input.generics, | ||
ds, | ||
), | ||
syn::Data::Enum(_) => { | ||
errors.err(input, "`#[derive(VennDB)]` cannot be applied to enums"); | ||
TokenStream::new() | ||
} | ||
syn::Data::Union(_) => { | ||
errors.err(input, "`#[derive(VennDB)]` cannot be applied to unions"); | ||
TokenStream::new() | ||
} | ||
}; | ||
errors.to_tokens(&mut output_tokens); | ||
output_tokens | ||
} | ||
|
||
/// Implements `VennDB` for a `#[derive(VennDB)]` struct. | ||
fn impl_from_args_struct( | ||
errors: &Errors, | ||
vis: &syn::Visibility, | ||
name: &syn::Ident, | ||
type_attrs: &TypeAttrs, | ||
_generic_args: &syn::Generics, | ||
ds: &syn::DataStruct, | ||
) -> TokenStream { | ||
let fields = match &ds.fields { | ||
syn::Fields::Named(fields) => fields, | ||
syn::Fields::Unnamed(_) => { | ||
errors.err( | ||
&ds.struct_token, | ||
"`#![derive(VennDB)]` is not currently supported on tuple structs", | ||
); | ||
return TokenStream::new(); | ||
} | ||
syn::Fields::Unit => { | ||
errors.err( | ||
&ds.struct_token, | ||
"#![derive(VennDB)]` cannot be applied to unit structs", | ||
); | ||
return TokenStream::new(); | ||
} | ||
}; | ||
|
||
let fields: Vec<_> = fields | ||
.named | ||
.iter() | ||
.filter_map(|field| { | ||
let attrs = FieldAttrs::parse(errors, field); | ||
StructField::new(errors, field, attrs) | ||
}) | ||
.collect(); | ||
|
||
let name_db = match &type_attrs.name { | ||
Some(name) => format_ident!("{}", name.value()), | ||
None => format_ident!("{}DB", name), | ||
}; | ||
|
||
let db_code = generate_db::generate_db(name, &name_db, vis, &fields[..]); | ||
|
||
quote! { | ||
#db_code | ||
} | ||
pub use hashbrown::HashMap; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
[package] | ||
description = "macros for venndb, cannot be used directly" | ||
edition = "2021" | ||
homepage = "https://venndb.rs" | ||
license = "MIT OR Apache-2.0" | ||
name = "venndb-macros" | ||
readme = "README.md" | ||
repository = "https://github.com/plabayo/venndb" | ||
keywords = ["database", "db", "memory", "bits"] | ||
categories = ["database", "db"] | ||
authors = ["Glen De Cauwsemaecker <glen@plabayo.tech>"] | ||
version = "0.1.0" | ||
rust-version = "1.75.0" | ||
|
||
[package.metadata.docs.rs] | ||
all-features = true | ||
rustdoc-args = ["--cfg", "docsrs"] | ||
|
||
[dependencies] | ||
proc-macro2 = "1.0" | ||
quote = "1.0" | ||
syn = "2.0" | ||
|
||
[lib] | ||
proc-macro = true |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#![forbid(unsafe_code)] | ||
|
||
mod errors; | ||
mod field; | ||
mod generate_db; | ||
mod parse_attrs; | ||
|
||
use errors::Errors; | ||
use field::StructField; | ||
use parse_attrs::{FieldAttrs, TypeAttrs}; | ||
use proc_macro2::TokenStream; | ||
use quote::{format_ident, quote, ToTokens}; | ||
|
||
/// Derive macro generating VennDB functionality for this struct. | ||
#[proc_macro_derive(VennDB, attributes(venndb))] | ||
pub fn venndb(input: proc_macro::TokenStream) -> proc_macro::TokenStream { | ||
let ast = syn::parse_macro_input!(input as syn::DeriveInput); | ||
let gen: TokenStream = impl_from_args(&ast); | ||
gen.into() | ||
} | ||
|
||
/// Transform the input into a token stream containing any generated implementations, | ||
/// as well as all errors that occurred. | ||
fn impl_from_args(input: &syn::DeriveInput) -> TokenStream { | ||
let errors = &Errors::default(); | ||
let type_attrs = &TypeAttrs::parse(errors, input); | ||
let mut output_tokens = match &input.data { | ||
syn::Data::Struct(ds) => impl_from_args_struct( | ||
errors, | ||
&input.vis, | ||
&input.ident, | ||
type_attrs, | ||
&input.generics, | ||
ds, | ||
), | ||
syn::Data::Enum(_) => { | ||
errors.err(input, "`#[derive(VennDB)]` cannot be applied to enums"); | ||
TokenStream::new() | ||
} | ||
syn::Data::Union(_) => { | ||
errors.err(input, "`#[derive(VennDB)]` cannot be applied to unions"); | ||
TokenStream::new() | ||
} | ||
}; | ||
errors.to_tokens(&mut output_tokens); | ||
output_tokens | ||
} | ||
|
||
/// Implements `VennDB` for a `#[derive(VennDB)]` struct. | ||
fn impl_from_args_struct( | ||
errors: &Errors, | ||
vis: &syn::Visibility, | ||
name: &syn::Ident, | ||
type_attrs: &TypeAttrs, | ||
_generic_args: &syn::Generics, | ||
ds: &syn::DataStruct, | ||
) -> TokenStream { | ||
let fields = match &ds.fields { | ||
syn::Fields::Named(fields) => fields, | ||
syn::Fields::Unnamed(_) => { | ||
errors.err( | ||
&ds.struct_token, | ||
"`#![derive(VennDB)]` is not currently supported on tuple structs", | ||
); | ||
return TokenStream::new(); | ||
} | ||
syn::Fields::Unit => { | ||
errors.err( | ||
&ds.struct_token, | ||
"#![derive(VennDB)]` cannot be applied to unit structs", | ||
); | ||
return TokenStream::new(); | ||
} | ||
}; | ||
|
||
let fields: Vec<_> = fields | ||
.named | ||
.iter() | ||
.filter_map(|field| { | ||
let attrs = FieldAttrs::parse(errors, field); | ||
StructField::new(errors, field, attrs) | ||
}) | ||
.collect(); | ||
|
||
let name_db = match &type_attrs.name { | ||
Some(name) => format_ident!("{}", name.value()), | ||
None => format_ident!("{}DB", name), | ||
}; | ||
|
||
let db_code = generate_db::generate_db(name, &name_db, vis, &fields[..]); | ||
|
||
quote! { | ||
#db_code | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters