| 1 |
extern crate proc_macrorust-analyzer cargo blanket 0.4.0 proc_macro/; |
| 2 |
extern crate proc_macro2rust-analyzer cargo blanket 0.4.0 proc_macro2/; |
| 3 |
extern crate quoterust-analyzer cargo blanket 0.4.0 quote/; |
| 4 |
|
| 5 |
use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::HashSetrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/HashSet#; |
| 6 |
|
| 7 |
use quoterust-analyzer cargo quote 1.0.47 crate/::{quoterust-analyzer cargo quote 1.0.47 quote!, ToTokensrust-analyzer cargo quote 1.0.47 to_tokens/ToTokens#}; |
| 8 |
use synrust-analyzer cargo syn 2.0.119 crate/::{parse_macro_inputrust-analyzer cargo syn 2.0.119 parse_macro_input/, punctuatedrust-analyzer cargo syn 2.0.119 punctuated/::Punctuatedrust-analyzer cargo syn 2.0.119 punctuated/Punctuated#, spannedrust-analyzer cargo syn 2.0.119 spanned/::Spannedrust-analyzer cargo syn 2.0.119 spanned/Spanned#}; |
| 9 |
|
| 10 |
// --------------------------------------------------------------------------- |
| 11 |
|
| 12 |
mod defaultrust-analyzer cargo blanket 0.4.0 default/; |
| 13 |
mod deriverust-analyzer cargo blanket 0.4.0 derive/; |
| 14 |
mod typesrust-analyzer cargo blanket 0.4.0 types/; |
| 15 |
mod utilsrust-analyzer cargo blanket 0.4.0 utils/; |
| 16 |
|
| 17 |
// --------------------------------------------------------------------------- |
| 18 |
|
| 19 |
struct Argsrust-analyzer cargo blanket 0.4.0 Args# { |
| 20 |
defaultrust-analyzer cargo blanket 0.4.0 Args#default.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<synrust-analyzer cargo syn 2.0.119 crate/::Pathrust-analyzer cargo syn 2.0.119 path/Path#>, |
| 21 |
derivesrust-analyzer cargo blanket 0.4.0 Args#derives.: HashSetrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/HashSet#<typesrust-analyzer cargo blanket 0.4.0 types/::Typerust-analyzer cargo blanket 0.4.0 types/Type#>, |
| 22 |
} |
| 23 |
|
| 24 |
impl Argsrust-analyzer cargo blanket 0.4.0 Args# { |
| 25 |
fn from_metarust-analyzer cargo blanket 0.4.0 impl#[Args]from_meta().(arg: &synrust-analyzer cargo syn 2.0.119 crate/::Metarust-analyzer cargo syn 2.0.119 attr/Meta#) -> synrust-analyzer cargo syn 2.0.119 crate/::Resultrust-analyzer cargo syn 2.0.119 error/Result#<Selfrust-analyzer cargo blanket 0.4.0 impl#[Args]> { |
| 26 |
let mut default = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 27 |
let mut derives = HashSetrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/HashSet#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/impl#[`HashSet<T>`]new().(); |
| 28 |
|
| 29 |
match arg { |
| 30 |
synrust-analyzer cargo syn 2.0.119 crate/::Metarust-analyzer cargo syn 2.0.119 attr/Meta#::Listrust-analyzer cargo syn 2.0.119 attr/Meta#List#(ref l) if l.pathrust-analyzer cargo syn 2.0.119 attr/MetaList#path..to_token_streamrust-analyzer cargo quote 1.0.47 to_tokens/ToTokens#to_token_stream().().to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().() rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`PartialEq<&str>`]eq().==rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`PartialEq<&str>`]eq(). rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`PartialEq<&str>`]eq()."derive" => { |
| 31 |
let types = l.parse_args_withrust-analyzer cargo syn 2.0.119 attr/impl#[MetaList]parse_args_with().( |
| 32 |
Punctuatedrust-analyzer cargo syn 2.0.119 punctuated/Punctuated#::<synrust-analyzer cargo syn 2.0.119 crate/::Pathrust-analyzer cargo syn 2.0.119 path/Path#, synrust-analyzer cargo syn 2.0.119 crate/::Tokenrust-analyzer cargo syn 2.0.119 token/Token!![,]>::parse_separated_nonemptyrust-analyzer cargo syn 2.0.119 punctuated/impl#[`Punctuated<T, P>`]parse_separated_nonempty()., |
| 33 |
)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 34 |
for pair in types.into_pairsrust-analyzer cargo syn 2.0.119 punctuated/impl#[`Punctuated<T, P>`]into_pairs().() { |
| 35 |
if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(d) = typesrust-analyzer cargo blanket 0.4.0 types/::Typerust-analyzer cargo blanket 0.4.0 types/Type#::from_pathrust-analyzer cargo blanket 0.4.0 types/impl#[Type]from_path().(pair.valuerust-analyzer cargo syn 2.0.119 punctuated/impl#[`Pair<T, P>`]value().()) { |
| 36 |
derives.insertrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/impl#[`HashSet<T, S, A>`]insert().(d); |
| 37 |
} else { |
| 38 |
return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(synrust-analyzer cargo syn 2.0.119 crate/::Errorrust-analyzer cargo syn 2.0.119 error/Error#::newrust-analyzer cargo syn 2.0.119 error/impl#[Error]new().( |
| 39 |
pair.spanrust-analyzer cargo syn 2.0.119 spanned/impl#[T][Spanned]span().(), |
| 40 |
"unknown blanket derive option", |
| 41 |
)); |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
synrust-analyzer cargo syn 2.0.119 crate/::Metarust-analyzer cargo syn 2.0.119 attr/Meta#::NameValuerust-analyzer cargo syn 2.0.119 attr/Meta#NameValue#(ref n) if n.pathrust-analyzer cargo syn 2.0.119 attr/MetaNameValue#path..to_token_streamrust-analyzer cargo quote 1.0.47 to_tokens/ToTokens#to_token_stream().().to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().() rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`PartialEq<&str>`]eq().==rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`PartialEq<&str>`]eq(). rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`PartialEq<&str>`]eq()."default" => { |
| 46 |
match n.valuerust-analyzer cargo syn 2.0.119 attr/MetaNameValue#value. { |
| 47 |
synrust-analyzer cargo syn 2.0.119 crate/::Exprrust-analyzer cargo syn 2.0.119 expr/Expr#::Litrust-analyzer cargo syn 2.0.119 expr/Expr#Lit#(ref lit) => { |
| 48 |
if let synrust-analyzer cargo syn 2.0.119 crate/::Litrust-analyzer cargo syn 2.0.119 lit/Lit#::Strrust-analyzer cargo syn 2.0.119 lit/Lit#Str#(ref s) = lit.litrust-analyzer cargo syn 2.0.119 expr/ExprLit#lit. { |
| 49 |
match synrust-analyzer cargo syn 2.0.119 crate/::parse_strrust-analyzer cargo syn 2.0.119 parse_str().(&s.valuerust-analyzer cargo syn 2.0.119 lit/impl#[LitStr]value().()) { |
| 50 |
Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(path) if default.is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().() => { |
| 51 |
default = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(path); |
| 52 |
} |
| 53 |
Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(_) => { |
| 54 |
return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(synrust-analyzer cargo syn 2.0.119 crate/::Errorrust-analyzer cargo syn 2.0.119 error/Error#::newrust-analyzer cargo syn 2.0.119 error/impl#[Error]new().( |
| 55 |
s.spanrust-analyzer cargo syn 2.0.119 lit/impl#[LitStr]span().(), |
| 56 |
"duplicate default module given", |
| 57 |
)) |
| 58 |
} |
| 59 |
Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => { |
| 60 |
return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(synrust-analyzer cargo syn 2.0.119 crate/::Errorrust-analyzer cargo syn 2.0.119 error/Error#::newrust-analyzer cargo syn 2.0.119 error/impl#[Error]new().( |
| 61 |
s.spanrust-analyzer cargo syn 2.0.119 lit/impl#[LitStr]span().(), |
| 62 |
"expected module identifier", |
| 63 |
)) |
| 64 |
} |
| 65 |
} |
| 66 |
} else { |
| 67 |
return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(synrust-analyzer cargo syn 2.0.119 crate/::Errorrust-analyzer cargo syn 2.0.119 error/Error#::newrust-analyzer cargo syn 2.0.119 error/impl#[Error]new().(lit.litrust-analyzer cargo syn 2.0.119 expr/ExprLit#lit..spanrust-analyzer cargo syn 2.0.119 lit/value/impl#[Lit]span().(), "expected string literal")); |
| 68 |
} |
| 69 |
} |
| 70 |
synrust-analyzer cargo syn 2.0.119 crate/::Exprrust-analyzer cargo syn 2.0.119 expr/Expr#::Pathrust-analyzer cargo syn 2.0.119 expr/Expr#Path#(ref expr) => { |
| 71 |
if default.replacerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]replace().(expr.pathrust-analyzer cargo syn 2.0.119 expr/ExprPath#path..clonerust-analyzer cargo syn 2.0.119 gen/clone/impl#[`crate::Path`][Clone]clone().()).is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().() { |
| 72 |
return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(synrust-analyzer cargo syn 2.0.119 crate/::Errorrust-analyzer cargo syn 2.0.119 error/Error#::newrust-analyzer cargo syn 2.0.119 error/impl#[Error]new().( |
| 73 |
n.spanrust-analyzer cargo syn 2.0.119 spanned/impl#[T][Spanned]span().(), |
| 74 |
"duplicate default module given", |
| 75 |
)); |
| 76 |
} |
| 77 |
} |
| 78 |
_ => { |
| 79 |
return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(synrust-analyzer cargo syn 2.0.119 crate/::Errorrust-analyzer cargo syn 2.0.119 error/Error#::newrust-analyzer cargo syn 2.0.119 error/impl#[Error]new().( |
| 80 |
n.valuerust-analyzer cargo syn 2.0.119 attr/MetaNameValue#value..spanrust-analyzer cargo syn 2.0.119 spanned/impl#[T][Spanned]span().(), |
| 81 |
"expected path or string literal", |
| 82 |
)); |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
_ => return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(synrust-analyzer cargo syn 2.0.119 crate/::Errorrust-analyzer cargo syn 2.0.119 error/Error#::newrust-analyzer cargo syn 2.0.119 error/impl#[Error]new().(arg.spanrust-analyzer cargo syn 2.0.119 spanned/impl#[T][Spanned]span().(), "unexpected argument")), |
| 87 |
} |
| 88 |
|
| 89 |
Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo blanket 0.4.0 impl#[Args] { defaultrust-analyzer cargo blanket 0.4.0 Args#default., derivesrust-analyzer cargo blanket 0.4.0 Args#derives. }) |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
// --------------------------------------------------------------------------- |
| 94 |
|
| 95 |
/// Generate blanket implementations for a trait. |
| 96 |
/// |
| 97 |
/// This procedural macro must be used on a `trait` block. It can be used |
| 98 |
/// to either derive a blanket implementation, or to delegate the method |
| 99 |
/// implementations of a trait to a module. |
| 100 |
/// |
| 101 |
#[proc_macro_attribute] |
| 102 |
pub fn blanketrust-analyzer cargo blanket 0.4.0 blanket:( |
| 103 |
args: proc_macrorust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro crate/::TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream#, |
| 104 |
input: proc_macrorust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro crate/::TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream#, |
| 105 |
) -> proc_macrorust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro crate/::TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream# { |
| 106 |
// parse input |
| 107 |
let trait_ = parse_macro_inputrust-analyzer cargo syn 2.0.119 parse_macro_input/parse_macro_input!!(input as synrust-analyzer cargo syn 2.0.119 crate/::ItemTraitrust-analyzer cargo syn 2.0.119 item/ItemTrait#); |
| 108 |
let args = parse_macro_inputrust-analyzer cargo syn 2.0.119 parse_macro_input/parse_macro_input!!(args as synrust-analyzer cargo syn 2.0.119 crate/::Metarust-analyzer cargo syn 2.0.119 attr/Meta#); |
| 109 |
// parse macro arguments and immediately exit if they are invalid |
| 110 |
let args = match Argsrust-analyzer cargo blanket 0.4.0 Args#::from_metarust-analyzer cargo blanket 0.4.0 impl#[Args]from_meta().(&args) { |
| 111 |
Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(args) => args, |
| 112 |
Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e) => { |
| 113 |
let err = e.to_compile_errorrust-analyzer cargo syn 2.0.119 error/impl#[Error]to_compile_error().(); |
| 114 |
return proc_macrorust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro crate/::TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream#::fromrust-analyzer cargo proc-macro2 1.0.107 impl#[`proc_macro::TokenStream`][`From<TokenStream>`]from().(quoterust-analyzer cargo quote 1.0.47 quote!!(#err #trait_)); |
| 115 |
} |
| 116 |
}; |
| 117 |
// generate output |
| 118 |
let mut out = proc_macro2rust-analyzer cargo proc-macro2 1.0.107 crate/::TokenStreamrust-analyzer cargo proc-macro2 1.0.107 TokenStream#::newrust-analyzer cargo proc-macro2 1.0.107 impl#[TokenStream]new().(); |
| 119 |
// update trait methods declaration if given a `default = "..."` argument, |
| 120 |
// otherwise simply keep the output |
| 121 |
match args.defaultrust-analyzer cargo blanket 0.4.0 Args#default. { |
| 122 |
Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => out.extendrust-analyzer cargo proc-macro2 1.0.107 impl#[TokenStream][`Extend<TokenTree>`]extend().(quoterust-analyzer cargo quote 1.0.47 quote!!(#trait_)), |
| 123 |
Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(d) => match defaultrust-analyzer cargo blanket 0.4.0 default/::defer_trait_methodsrust-analyzer cargo blanket 0.4.0 default/defer_trait_methods().(trait_.clonerust-analyzer cargo syn 2.0.119 gen/clone/impl#[`crate::ItemTrait`][Clone]clone().(), d) { |
| 124 |
Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(trait_) => out.extendrust-analyzer cargo proc-macro2 1.0.107 impl#[TokenStream][`Extend<TokenTree>`]extend().(quoterust-analyzer cargo quote 1.0.47 quote!!(#trait_)), |
| 125 |
Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) => out.extendrust-analyzer cargo proc-macro2 1.0.107 impl#[TokenStream][`Extend<TokenTree>`]extend().(err.to_compile_errorrust-analyzer cargo syn 2.0.119 error/impl#[Error]to_compile_error().()), |
| 126 |
}, |
| 127 |
}; |
| 128 |
// add derived implementations |
| 129 |
for d in args.derivesrust-analyzer cargo blanket 0.4.0 Args#derives. { |
| 130 |
match d.defer_trait_methodsrust-analyzer cargo blanket 0.4.0 types/impl#[Type]defer_trait_methods().(&trait_) { |
| 131 |
Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(item) => out.extendrust-analyzer cargo proc-macro2 1.0.107 impl#[TokenStream][`Extend<TokenTree>`]extend().(quoterust-analyzer cargo quote 1.0.47 quote!!(#item)), |
| 132 |
Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e) => out.extendrust-analyzer cargo proc-macro2 1.0.107 impl#[TokenStream][`Extend<TokenTree>`]extend().(e.to_compile_errorrust-analyzer cargo syn 2.0.119 error/impl#[Error]to_compile_error().()), |
| 133 |
} |
| 134 |
} |
| 135 |
// // return the new `proc-macro2` token stream as a `proc-macro` stream |
| 136 |
proc_macrorust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro crate/::TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream#::fromrust-analyzer cargo proc-macro2 1.0.107 impl#[`proc_macro::TokenStream`][`From<TokenStream>`]from().(out) |
| 137 |
} |