Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ syn = "2.0"
quote = "1.0"
proc-macro2 = "1.0"

[dev-dependencies]
static_assertions = "1.1"

[workspace]
exclude = ["benches", "compile_fail", "tools/compile_fail_utils"]
members = ["errors", ".", "benches"]
Expand Down
2 changes: 1 addition & 1 deletion examples/demonstrations/all_tuples.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! An example of using `all_tuples!`

#![cfg_attr(any(docsrs), feature(rustdoc_internals))]
#![cfg_attr(docsrs, feature(rustdoc_internals))]

use variadics_please::all_tuples;

Expand Down
118 changes: 82 additions & 36 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,15 @@ impl Parse for AllTuples {
let macro_ident = input.parse::<Ident>()?;
input.parse::<Comma>()?;
let start = input.parse::<LitInt>()?.base10_parse()?;

if start > 1 && fake_variadic {
return Err(Error::new(
input.span(),
"#[doc(fake_variadic)] only works when the tuple with length one is included",
));
}

input.parse::<Comma>()?;
let end_span = input.span();
let end = input.parse::<LitInt>()?.base10_parse()?;
input.parse::<Comma>()?;

if end < start {
return Err(Error::new(end_span, "`start` should <= `end`"));
}

let mut idents = vec![input.parse::<Ident>()?];
while input.parse::<Comma>().is_ok() {
idents.push(input.parse::<Ident>()?);
Expand Down Expand Up @@ -182,13 +180,20 @@ pub fn all_tuples(input: TokenStream) -> TokenStream {
})
.collect::<Vec<_>>();
let macro_ident = &input.macro_ident;
let invocations = (input.start..=input.end).map(|n| {
let ident_tuples = choose_ident_tuples(&input, &ident_tuples, n);
let attrs = attrs(&input, n);
quote! {
#macro_ident!(#attrs #ident_tuples);
}
});
let invocations = (input.start..=input.end)
.chain(if input.fake_variadic && input.start > 1 {
// chain n = 1
vec![1]
} else {
vec![]
})
.map(|n| {
let ident_tuples = choose_ident_tuples(&input, &ident_tuples, n);
let attrs = attrs(&input, n);
quote! {
#macro_ident!(#attrs #ident_tuples);
}
});
TokenStream::from(quote! {
#(
#invocations
Expand Down Expand Up @@ -262,13 +267,19 @@ pub fn all_tuples_enumerated(input: TokenStream) -> TokenStream {
})
.collect::<Vec<_>>();
let macro_ident = &input.macro_ident;
let invocations = (input.start..=input.end).map(|n| {
let ident_tuples = choose_ident_tuples_enumerated(&input, &ident_tuples, n);
let attrs = attrs(&input, n);
quote! {
#macro_ident!(#attrs #ident_tuples);
}
});
let invocations = (input.start..=input.end)
.chain(if input.fake_variadic && input.start > 1 {
vec![1]
} else {
vec![]
})
.map(|n| {
let ident_tuples = choose_ident_tuples_enumerated(&input, &ident_tuples, n);
let attrs = attrs(&input, n);
quote! {
#macro_ident!(#attrs #ident_tuples);
}
});
TokenStream::from(quote! {
#(
#invocations
Expand Down Expand Up @@ -408,13 +419,19 @@ pub fn all_tuples_with_size(input: TokenStream) -> TokenStream {
})
.collect::<Vec<_>>();
let macro_ident = &input.macro_ident;
let invocations = (input.start..=input.end).map(|n| {
let ident_tuples = choose_ident_tuples(&input, &ident_tuples, n);
let attrs = attrs(&input, n);
quote! {
#macro_ident!(#n, #attrs #ident_tuples);
}
});
let invocations = (input.start..=input.end)
.chain(if input.fake_variadic && input.start > 1 {
vec![1]
} else {
vec![]
})
.map(|n| {
let ident_tuples = choose_ident_tuples(&input, &ident_tuples, n);
let attrs = attrs(&input, n);
quote! {
#macro_ident!(#n, #attrs #ident_tuples);
}
});
TokenStream::from(quote! {
#(
#invocations
Expand Down Expand Up @@ -476,8 +493,8 @@ fn choose_ident_tuples_enumerated(
}
}

fn to_ident_tuple(idents: impl Iterator<Item = Ident>, len: usize) -> TokenStream2 {
if len < 2 {
fn to_ident_tuple(idents: impl Iterator<Item = Ident>, generic_num: usize) -> TokenStream2 {
if generic_num < 2 {
quote! { #(#idents)* }
} else {
quote! { (#(#idents),*) }
Expand Down Expand Up @@ -505,14 +522,43 @@ fn attrs(input: &AllTuples, n: usize) -> TokenStream2 {
if n == 1 {
let doc = LitStr::new(
&format!(
"This trait is implemented for tuples down to {} up to {} items long.",
input.start, input.end
"This trait is implemented for tuple{s1} {range} item{s2} long.",
range = if input.start == input.end {
format!("exactly {}", input.start)
} else {
format!(
"{down}up to {up}",
down = if input.start != 0 {
format!("down to {} ", input.start)
} else {
"".to_string()
},
up = input.end
)
},
s1 = if input.end > input.start { "s" } else { "" },
s2 = if input.end >= input.start && input.end > 1 {
"s"
} else {
""
}
),
Span2::call_site(),
);
quote! {
#[cfg_attr(#cfg, doc(fake_variadic))]
#[cfg_attr(#cfg, doc = #doc)]
if input.start <= 1 && input.end >= 1 {
// n == 1 and it's included
quote! {
#[cfg_attr(#cfg, doc(fake_variadic))]
#[cfg_attr(#cfg, doc = #doc)]
}
} else {
// n == 1 but it's not included,
// only generate if #cfg
quote! {
#[cfg(#cfg)]
#[doc(fake_variadic)]
#[doc = #doc]
}
}
} else {
quote! { #[cfg_attr(#cfg, doc(hidden))] }
Expand Down
72 changes: 72 additions & 0 deletions tests/basic_all_tuples.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#![allow(missing_docs, dead_code)]

use static_assertions::{assert_impl_one, assert_not_impl_any};
use variadics_please::all_tuples;

trait Foo {}

macro_rules! foo {
($($t: ident),* $(,)?) => {
impl<$($t),*> Foo for ($($t,)*) {}
};
}

// [0, 2]
all_tuples!(foo, 0, 2, T);

// no {3}

// [4, 5]
all_tuples!(foo, 4, 5, T);

trait Bar {}

macro_rules! bar {
($(($t1: ident, $t2: ident)),* $(,)?) => {
impl<$($t1,)* $($t2),*> Bar for ($(($t1, $t2),)*) {}
};
}

// [0, 2]
all_tuples!(bar, 0, 2, T, U);

// no {3}

// [4, 5]
all_tuples!(bar, 4, 5, T, U);

#[test]
fn basic_test() {
// 0
assert_impl_one!((): Foo);
// 1
assert_impl_one!(((),): Foo);
// 2
assert_impl_one!(((), ()): Foo);
// no 3
assert_not_impl_any!(((), (), ()): Foo);

// 4
assert_impl_one!(((), (), (), ()): Foo);
// 5
assert_impl_one!(((), (), (), (), ()): Foo);
// no 6
assert_not_impl_any!(((), (), (), (), (), ()): Foo);

// ((T, U), ..)
// 0
assert_impl_one!((): Bar);
// 1
assert_impl_one!((((), ()),): Bar);
// 2
assert_impl_one!((((),()), ((),())): Bar);
// no 3
assert_not_impl_any!((((),()), ((),()), ((),())): Bar);

// 4
assert_impl_one!((((),()), ((),()), ((),()), ((),())): Bar);
// 5
assert_impl_one!((((),()), ((),()), ((),()), ((),()), ((),())): Bar);
// no 6
assert_not_impl_any!((((),()), ((),()), ((),()), ((),()), ((),()), ((),())): Bar);
}
72 changes: 72 additions & 0 deletions tests/basic_all_tuples_enumerated.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#![allow(missing_docs, dead_code)]

use static_assertions::{assert_impl_one, assert_not_impl_any};
use variadics_please::all_tuples_enumerated;

trait Foo {}

macro_rules! foo {
($(($_: literal, $t: ident)),* $(,)?) => {
impl<$($t),*> Foo for ($($t,)*) {}
};
}

// [0, 2]
all_tuples_enumerated!(foo, 0, 2, T);

// no {3}

// [4, 5]
all_tuples_enumerated!(foo, 4, 5, T);

trait Bar {}

macro_rules! bar {
($(($_: literal, $t1: ident, $t2: ident)),* $(,)?) => {
impl<$($t1,)* $($t2),*> Bar for ($(($t1, $t2),)*) {}
};
}

// [0, 2]
all_tuples_enumerated!(bar, 0, 2, T, U);

// no {3}

// [4, 5]
all_tuples_enumerated!(bar, 4, 5, T, U);

#[test]
fn basic_test() {
// 0
assert_impl_one!((): Foo);
// 1
assert_impl_one!(((),): Foo);
// 2
assert_impl_one!(((), ()): Foo);
// no 3
assert_not_impl_any!(((), (), ()): Foo);

// 4
assert_impl_one!(((), (), (), ()): Foo);
// 5
assert_impl_one!(((), (), (), (), ()): Foo);
// no 6
assert_not_impl_any!(((), (), (), (), (), ()): Foo);

// ((T, U), ..)
// 0
assert_impl_one!((): Bar);
// 1
assert_impl_one!((((), ()),): Bar);
// 2
assert_impl_one!((((),()), ((),())): Bar);
// no 3
assert_not_impl_any!((((),()), ((),()), ((),())): Bar);

// 4
assert_impl_one!((((),()), ((),()), ((),()), ((),())): Bar);
// 5
assert_impl_one!((((),()), ((),()), ((),()), ((),()), ((),())): Bar);
// no 6
assert_not_impl_any!((((),()), ((),()), ((),()), ((),()), ((),()), ((),())): Bar);
}
Loading
Loading