Skip to content

Commit

Permalink
update dependencies and improving proc macro
Browse files Browse the repository at this point in the history
  • Loading branch information
ABouttefeux committed Sep 24, 2023
1 parent 07dc521 commit 4c1768b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ num-traits = "0.2.16"
rand = "0.8.5"
rand_distr = "0.4.3"
crossbeam = "0.8.2"
rayon = "1.7"
rayon = "1.8"
serde = { version = "1.0", features = ["derive"], optional = true }
lattice_qcd_rs-procedural_macro = { path = "procedural_macro", version = "0.3.0" }

Expand Down
4 changes: 4 additions & 0 deletions procedural_macro/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# v0.3.0

- update version-sync to 0.9.5
- added doc on direction const item

# v0.2.1

Expand Down
2 changes: 1 addition & 1 deletion procedural_macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ syn = "2.0"
proc-macro2 = "1.0"

[dev-dependencies]
version-sync = "0.9.4"
version-sync = "0.9.5"
32 changes: 23 additions & 9 deletions procedural_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@
mod test;

use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::quote;
use syn::Ident;

/// Maximum dimension to impl [`Direction`] for.
const MAX_DIM: usize = 127;
Expand All @@ -157,25 +159,34 @@ pub fn implement_direction_list(_item: TokenStream) -> TokenStream {
Direction{index_dir: #j, is_positive: true}
});
}
//let u_ident = syn::Ident::new(&format!("U{}", i), proc_macro2::Span::call_site());
let u_dir_ident = syn::Ident::new(&format!("U{i}_DIR"), proc_macro2::Span::call_site());
let u_dir_pos_ident =
syn::Ident::new(&format!("U{i}_DIR_POS"), proc_macro2::Span::call_site());

let u_dir_ident = Ident::new(&format!("U{i}_DIR"), Span::call_site());
let u_dir_pos_ident = Ident::new(&format!("U{i}_DIR_POS"), Span::call_site());
// we store the values in array so we can access them as fast as possible.
let comment_all = format!(
"Array of all [`Direction`] in dimension {i}. Added automatically by procedural macro."
);
let comment_pos = format!("Array of positive [`Direction`] in dimension {i}. Added automatically by procedural macro.");

let s = quote! {
#[doc=#comment_all]
const #u_dir_ident: [Direction<#i>; #i * 2] = [ #(#array_direction),* ];
#[doc=#comment_pos]
const #u_dir_pos_ident: [Direction<#i>; #i] = [ #(#array_direction_positives),* ];

impl DirectionList for Direction<#i> {
#[inline]
fn directions() -> & 'static [Self] {
fn directions() -> &'static [Self] {
&#u_dir_ident
}

#[inline]
fn positive_directions() -> & 'static [Self] {
fn positive_directions() -> &'static [Self] {
&#u_dir_pos_ident
}
}
};

implem.push(s);
}
// We need to concat the final implems togethers.
Expand All @@ -195,7 +206,8 @@ const MAX_DIM_FROM_IMPLEM: usize = 10;
#[proc_macro]
pub fn implement_direction_from(_item: TokenStream) -> TokenStream {
// implementation of the error returned by the TryFrom trait.
let mut implem = vec![quote! {
let mut implem = Vec::with_capacity(MAX_DIM_FROM_IMPLEM * (MAX_DIM_FROM_IMPLEM - 1) / 2 + 1);
implem.push(quote! {
use std::convert::TryFrom;

/// Error return by [`TryFrom`] for Directions.
Expand All @@ -216,7 +228,7 @@ pub fn implement_direction_from(_item: TokenStream) -> TokenStream {
}

impl std::error::Error for DirectionConversionError {}
}];
});

for i in 1_usize..MAX_DIM_FROM_IMPLEM {
for j in i + 1..=MAX_DIM_FROM_IMPLEM {
Expand All @@ -239,6 +251,7 @@ pub fn implement_direction_from(_item: TokenStream) -> TokenStream {

impl TryFrom<Direction<#j>> for Direction<#i> {
type Error = DirectionConversionError;

fn try_from(from: Direction<#j>) -> Result<Self, Self::Error> {
Self::new(from.index(), from.is_positive())
.ok_or(DirectionConversionError::IndexOutOfBound)
Expand All @@ -247,6 +260,7 @@ pub fn implement_direction_from(_item: TokenStream) -> TokenStream {

impl TryFrom<&Direction<#j>> for Direction<#i> {
type Error = DirectionConversionError;

fn try_from(from: &Direction<#j>) -> Result<Self, Self::Error> {
Self::new(from.index(), from.is_positive())
.ok_or(DirectionConversionError::IndexOutOfBound)
Expand All @@ -256,9 +270,9 @@ pub fn implement_direction_from(_item: TokenStream) -> TokenStream {
}
}

// We need to concat the final implems togethers.
let final_stream = quote! {
#(#implem)*
};
// We need to concat the final implems togethers.
final_stream.into()
}

0 comments on commit 4c1768b

Please sign in to comment.