1#![doc = include_str!("../README.md")]
2
3mod analyze;
4mod attr;
5mod codegen;
6mod rewrite;
7mod selection;
8
9use proc_macro::TokenStream;
10use quote::quote;
11use syn::ItemTrait;
12
13use crate::attr::Config;
14
15#[proc_macro_attribute]
22pub fn auto_tuple(args: TokenStream, input: TokenStream) -> TokenStream {
23 match expand(args.into(), input.into()) {
24 Ok(ts) => ts.into(),
25 Err(err) => err.to_compile_error().into(),
26 }
27}
28
29fn expand(
30 args: proc_macro2::TokenStream, input: proc_macro2::TokenStream,
31) -> syn::Result<proc_macro2::TokenStream> {
32 let cfg = Config::from_tokens(args)?;
33 let trait_: ItemTrait = syn::parse2(input)?;
34
35 if trait_.modifiers.auto_token.is_some() {
36 return Err(syn::Error::new_spanned(
37 &trait_,
38 "auto traits are not supported by `#[auto_tuple]`",
39 ));
40 }
41
42 let selected = selection::select(&trait_, &cfg.selection)?;
43 let track =
44 analyze::decide_track(&trait_, &selected.methods, &selected.consts, &selected.types);
45
46 let mut generated = Vec::new();
47 let vis = cfg.vis.clone().unwrap_or_else(|| trait_.vis.clone());
48 for n in cfg.sizes.clone() {
49 generated.push(codegen::generate(&trait_, track, &selected, n, &vis, cfg.name.as_deref())?);
50 }
51
52 Ok(quote!(#trait_ #(#generated)*))
53}
54
55#[cfg(test)]
56mod proptests {
57 use proc_macro2::TokenStream;
58 use proptest::prelude::*;
59
60 use super::expand;
61
62 fn ident_strategy() -> impl Strategy<Value = String> {
65 prop_oneof![
66 "__T0", "__T1", "__TA0", "__TA1", "__L0_a", "__C0_N", "__arg0", "T", "U", "N", "a",
67 "foo", "bar", "x", "y",
68 ]
69 }
70
71 fn trait_generics_strategy() -> impl Strategy<Value = String> {
73 prop_oneof![
74 Just(String::new()),
75 Just("<T>".to_string()),
76 Just("<T, U>".to_string()),
77 Just("<'a>".to_string()),
78 Just("<const N: usize>".to_string()),
79 Just("<T: Clone>".to_string()),
80 Just("<T = u32>".to_string()),
81 ]
82 }
83
84 fn method_strategy() -> impl Strategy<Value = &'static str> {
87 prop::sample::select(vec![
88 "fn foo(&self) -> usize;",
89 "fn foo(&mut self, x: usize) -> usize;",
90 "fn foo(self);",
91 "fn foo();",
92 "fn foo<X>(x: X) -> X;",
93 "fn foo<T: Clone>(&self, x: T) -> T;",
94 "fn foo(&self, x: &Self);",
95 "fn foo(&mut self, x: &mut Self);",
96 "fn foo(&self) -> Box<Self>;",
97 "fn foo(&self) -> Vec<Self> where Self: Sized;",
98 "async fn foo(&self) -> usize;",
99 "unsafe fn foo(&self) -> usize;",
100 "fn foo(&self) -> impl Iterator<Item = Self>;",
101 "fn foo(&self, x: Self::Output) -> Self::Output;",
102 "fn foo(&self) -> usize where Self: Sized;",
103 ])
104 }
105
106 fn extra_item_strategy() -> impl Strategy<Value = &'static str> {
108 prop::sample::select(vec![
109 "const MAX: usize;",
110 "type Out;",
111 "type Out: Clone;",
112 "type Out = usize;",
113 ])
114 }
115
116 proptest! {
117 #[test]
122 fn arbitrary_trait_never_panics(
123 trait_name in ident_strategy(),
124 trait_generics in trait_generics_strategy(),
125 methods in prop::collection::vec(method_strategy(), 0..6),
126 extra_items in prop::collection::vec(extra_item_strategy(), 0..3),
127 args in prop::sample::select(vec![
128 "", "0..=1", "@all", "@all_methods, 0..=2", "@all_types",
129 "@all_constants", "@all, -@all", "@all_ref_methods, -foo",
130 "[@all_methods, Out], -foo", "@all_required", "@all_default",
131 "@missing", "-", "[]",
132 ]),
133 ) {
134 let body = [methods.join(" "), extra_items.join(" ")].join(" ");
135 let src = format!("trait {trait_name}{trait_generics} {{ {body} }}");
136 let tokens: TokenStream = src.parse().unwrap();
137 syn::parse2::<syn::ItemTrait>(tokens.clone()).unwrap();
140 let _ = expand(args.parse().unwrap(), tokens);
141 }
142 }
143}