Skip to content

Commit

Permalink
fix: address review comments on variadics
Browse files Browse the repository at this point in the history
  • Loading branch information
jhellerstein committed Oct 23, 2024
1 parent 64d8442 commit 5cc5c37
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 44 deletions.
10 changes: 1 addition & 9 deletions lattices/src/ght_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,15 +454,7 @@ mod test {
use variadics_macro::tuple;

#[test]
fn test_tuple_macro() {
let tup = var_expr!(1, 2, 3, "four");
let a = tuple!(tup, 4);
assert_eq!(a, (1, 2, 3, "four"));

let tup = var_expr!(1, 2, var_expr!(3));
let b = tuple!(tup, 3);
assert_eq!(b, (1, 2, (3, ())));

fn test_ght_with_tuple_macro() {
type MyRoot = GhtType!(u16, u32 => u64: VariadicCountedHashSet);

let mut trie1 = MyRoot::default();
Expand Down
1 change: 0 additions & 1 deletion variadics/src/variadic_collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ pub trait VariadicCollection: Extend<Self::Schema> {
/// The Schema (aka Variadic type) associated with tuples in this set
type Schema: PartialEqVariadic;

/// Insert an element into the set, return true if successful
/// Insert an element into the set, return true if successful
fn insert(&mut self, element: Self::Schema) -> bool;

Expand Down
18 changes: 16 additions & 2 deletions variadics_macro/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
## `Tuplize` Macro
## `tuple!` Macro

Create a tuple from a Variadic type known at compile time.
Create a tuple from a Variadic type known at compile time.

Example usage:
```
use variadics::var_expr;
use variadics_macro::tuple;
let tup = var_expr!(1, 2, 3, "four");
let a = tuple!(tup, 4);
assert_eq!(a, (1, 2, 3, "four"));
let tup = var_expr!(1, 2, var_expr!(3));
let b = tuple!(tup, 3);
assert_eq!(b, (1, 2, (3, ())));
```
42 changes: 10 additions & 32 deletions variadics_macro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![doc = include_str!("../README.md")]
extern crate proc_macro;

use proc_macro::TokenStream;
Expand All @@ -20,42 +21,19 @@ impl Parse for InputLen {
}
}

fn gen_pattern(c: &str, remaining: usize, current: usize) -> proc_macro2::TokenStream {
if remaining >= 1 {
let identifier = format_ident!("{}", c.repeat(current));
let inner = gen_pattern(c, remaining - 1, current + 1);
quote! { (#identifier, #inner) }
} else {
quote! { () }
}
}

fn gen_tuple(c: &str, remaining: usize, current: usize) -> proc_macro2::TokenStream {
let identifier = format_ident!("{}", c.repeat(current));

if remaining > 1 {
// Continue to generate the next elements in the tuple.
let inner = gen_tuple(c, remaining - 1, current + 1);
if current == 1 {
// At the first call, wrap all accumulated elements in a tuple.
quote! { (#identifier, #inner) }
} else {
// Accumulate elements by appending them.
quote! { #identifier, #inner }
}
} else {
// The base case of recursion: return the last element.
quote! { #identifier }
}
}

#[proc_macro]
pub fn tuple(ts: TokenStream) -> TokenStream {
let InputLen { input, len } = parse_macro_input!(ts as InputLen);
let c = "x";
let len = len.base10_parse::<usize>().unwrap();
let pattern = gen_pattern(c, len, 1);
let tuple = gen_tuple(c, len, 1);
// let pattern = gen_pattern(len, 1);
let pattern = (0..len)
.rev()
.map(|i| format_ident!("x{}", i))
.fold(quote! { () }, |rest, item| quote! { (#item, #rest) });
let idents = (0..len).map(|i| format_ident!("x{}", i));
let tuple = quote! {
( #( #idents, )* )
};

// Create the assignment statement
let expanded = quote! {
Expand Down

0 comments on commit 5cc5c37

Please sign in to comment.