Debian Codegraph @

src/utils.rs

raw symbols SCIP
1 use quoterust-analyzer cargo quote 1.0.47 crate/::quote_spannedrust-analyzer cargo quote 1.0.47 quote_spanned!;
2 use synrust-analyzer cargo syn 2.0.119 crate/::{parse_quoterust-analyzer cargo syn 2.0.119 parse_quote/, 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#, GenericParamrust-analyzer cargo syn 2.0.119 generics/GenericParam#, Tokenrust-analyzer cargo syn 2.0.119 token/Token!};
3
4 /// Convert a function signature to a function call with the same arguments.
5 ///
6 /// Given `function(a: &str, b: usize)`, get `function(a, b)`.
7 pub fn signature_to_function_callrust-analyzer cargo blanket 0.4.0 utils/signature_to_function_call().(sig: &synrust-analyzer cargo syn 2.0.119 crate/::Signaturerust-analyzer cargo syn 2.0.119 item/Signature#) -> synrust-analyzer cargo syn 2.0.119 crate/::Resultrust-analyzer cargo syn 2.0.119 error/Result#<synrust-analyzer cargo syn 2.0.119 crate/::ExprCallrust-analyzer cargo syn 2.0.119 expr/ExprCall#> {
8 // Simply use the function ident as the function expression.
9 let funcexpr = synrust-analyzer cargo syn 2.0.119 crate/::ExprPathrust-analyzer cargo syn 2.0.119 expr/ExprPath# {
10 attrsrust-analyzer cargo syn 2.0.119 expr/ExprPath#attrs.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(),
11 qselfrust-analyzer cargo syn 2.0.119 expr/ExprPath#qself.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
12 pathrust-analyzer cargo syn 2.0.119 expr/ExprPath#path.: sig.identrust-analyzer cargo syn 2.0.119 item/Signature#ident..clonerust-analyzer cargo proc-macro2 1.0.107 impl#[Ident][Clone]clone().().intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(),
13 };
14
15 // Extract arguments from the method signature names
16 let mut funcargs = Punctuatedrust-analyzer cargo syn 2.0.119 punctuated/Punctuated#::newrust-analyzer cargo syn 2.0.119 punctuated/impl#[`Punctuated<T, P>`]new().();
17 for item in &sig.inputsrust-analyzer cargo syn 2.0.119 item/Signature#inputs. {
18 match item {
19 synrust-analyzer cargo syn 2.0.119 crate/::FnArgrust-analyzer cargo syn 2.0.119 item/FnArg#::Receiverrust-analyzer cargo syn 2.0.119 item/FnArg#Receiver#(recv) => {
20 let span = recv.self_tokenrust-analyzer cargo syn 2.0.119 item/Receiver#self_token..spanrust-analyzer cargo syn 2.0.119 token/SelfValue#span.;
21 funcargs.pushrust-analyzer cargo syn 2.0.119 punctuated/impl#[`Punctuated<T, P>`]push().(synrust-analyzer cargo syn 2.0.119 crate/::parse2rust-analyzer cargo syn 2.0.119 parse2().(quote_spannedrust-analyzer cargo quote 1.0.47 quote_spanned!!(span=> self))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().);
22 }
23 synrust-analyzer cargo syn 2.0.119 crate/::FnArgrust-analyzer cargo syn 2.0.119 item/FnArg#::Typedrust-analyzer cargo syn 2.0.119 item/FnArg#Typed#(argty) => {
24 if let synrust-analyzer cargo syn 2.0.119 crate/::Patrust-analyzer cargo syn 2.0.119 pat/Pat#::Identrust-analyzer cargo syn 2.0.119 pat/Pat#Ident#(ref id) = *rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T, A>`][Deref]deref().argty.patrust-analyzer cargo syn 2.0.119 pat/PatType#pat. {
25 let argpath = synrust-analyzer cargo syn 2.0.119 crate/::ExprPathrust-analyzer cargo syn 2.0.119 expr/ExprPath# {
26 attrsrust-analyzer cargo syn 2.0.119 expr/ExprPath#attrs.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(),
27 qselfrust-analyzer cargo syn 2.0.119 expr/ExprPath#qself.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
28 pathrust-analyzer cargo syn 2.0.119 expr/ExprPath#path.: id.identrust-analyzer cargo syn 2.0.119 pat/PatIdent#ident..clonerust-analyzer cargo proc-macro2 1.0.107 impl#[Ident][Clone]clone().().intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(),
29 };
30 funcargs.pushrust-analyzer cargo syn 2.0.119 punctuated/impl#[`Punctuated<T, P>`]push().(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#(argpath));
31 } else {
32 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().(argty.spanrust-analyzer cargo syn 2.0.119 spanned/impl#[T][Spanned]span().(), "expected identifier"));
33 }
34 }
35 }
36 }
37
38 // Return the function call as an expression
39 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(synrust-analyzer cargo syn 2.0.119 crate/::ExprCallrust-analyzer cargo syn 2.0.119 expr/ExprCall# {
40 attrsrust-analyzer cargo syn 2.0.119 expr/ExprCall#attrs.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(),
41 paren_tokenrust-analyzer cargo syn 2.0.119 expr/ExprCall#paren_token.: synrust-analyzer cargo syn 2.0.119 crate/::tokenrust-analyzer cargo syn 2.0.119 token/::Parenrust-analyzer cargo syn 2.0.119 token/Paren#::defaultrust-analyzer cargo syn 2.0.119 token/impl#[Paren][Default]default().(),
42 funcrust-analyzer cargo syn 2.0.119 expr/ExprCall#func.: Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(funcexpr.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()),
43 argsrust-analyzer cargo syn 2.0.119 expr/ExprCall#args.: funcargs,
44 })
45 }
46
47 /// Convert a function signature to a static method call with the same arguments.
48 ///
49 /// The call need to be modified to use a fully qualified path to qualify the
50 /// method, otherwise it may conflict with other methods in the namespace.
51 pub fn signature_to_associated_function_callrust-analyzer cargo blanket 0.4.0 utils/signature_to_associated_function_call().(
52 sig: &synrust-analyzer cargo syn 2.0.119 crate/::Signaturerust-analyzer cargo syn 2.0.119 item/Signature#,
53 trait_ident: &synrust-analyzer cargo syn 2.0.119 crate/::Identrust-analyzer cargo proc-macro2 1.0.107 Ident#,
54 generic_type: &synrust-analyzer cargo syn 2.0.119 crate/::Identrust-analyzer cargo proc-macro2 1.0.107 Ident#,
55 trait_generic_names: &synrust-analyzer cargo syn 2.0.119 crate/::Genericsrust-analyzer cargo syn 2.0.119 generics/Generics#,
56 ) -> synrust-analyzer cargo syn 2.0.119 crate/::Resultrust-analyzer cargo syn 2.0.119 error/Result#<synrust-analyzer cargo syn 2.0.119 crate/::ExprCallrust-analyzer cargo syn 2.0.119 expr/ExprCall#> {
57 let mut call = signature_to_function_callrust-analyzer cargo blanket 0.4.0 utils/signature_to_function_call().(sig)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
58 if let 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 mut exprpath) = call.funcrust-analyzer cargo syn 2.0.119 expr/ExprCall#func..as_mutrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T, A>`][`AsMut<T>`]as_mut().() {
59 let span = generic_type.spanrust-analyzer cargo proc-macro2 1.0.107 impl#[Ident]span().();
60 exprpath.qselfrust-analyzer cargo syn 2.0.119 expr/ExprPath#qself. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(synrust-analyzer cargo syn 2.0.119 crate/::QSelfrust-analyzer cargo syn 2.0.119 path/QSelf# {
61 lt_tokenrust-analyzer cargo syn 2.0.119 path/QSelf#lt_token.: Tokenrust-analyzer cargo syn 2.0.119 token/Token!![<](span),
62 tyrust-analyzer cargo syn 2.0.119 path/QSelf#ty.: Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(parse_quoterust-analyzer cargo syn 2.0.119 parse_quote/parse_quote!!(#generic_type)),
63 positionrust-analyzer cargo syn 2.0.119 path/QSelf#position.: 1,
64 as_tokenrust-analyzer cargo syn 2.0.119 path/QSelf#as_token.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Tokenrust-analyzer cargo syn 2.0.119 token/Token!![as](span)),
65 gt_tokenrust-analyzer cargo syn 2.0.119 path/QSelf#gt_token.: Tokenrust-analyzer cargo syn 2.0.119 token/Token!![>](span),
66 });
67
68 let path_arguments = if trait_generic_names.paramsrust-analyzer cargo syn 2.0.119 generics/Generics#params..is_emptyrust-analyzer cargo syn 2.0.119 punctuated/impl#[`Punctuated<T, P>`]is_empty().() {
69 synrust-analyzer cargo syn 2.0.119 crate/::PathArgumentsrust-analyzer cargo syn 2.0.119 path/PathArguments#::Nonerust-analyzer cargo syn 2.0.119 path/PathArguments#None#
70 } else {
71 synrust-analyzer cargo syn 2.0.119 crate/::PathArgumentsrust-analyzer cargo syn 2.0.119 path/PathArguments#::AngleBracketedrust-analyzer cargo syn 2.0.119 path/PathArguments#AngleBracketed#(parse_quoterust-analyzer cargo syn 2.0.119 parse_quote/parse_quote!!(#trait_generic_names))
72 };
73
74 exprpath.pathrust-analyzer cargo syn 2.0.119 expr/ExprPath#path..segmentsrust-analyzer cargo syn 2.0.119 path/Path#segments..insertrust-analyzer cargo syn 2.0.119 punctuated/impl#[`Punctuated<T, P>`]insert().(
75 0,
76 synrust-analyzer cargo syn 2.0.119 crate/::PathSegmentrust-analyzer cargo syn 2.0.119 path/PathSegment# {
77 identrust-analyzer cargo syn 2.0.119 path/PathSegment#ident.: trait_ident.clonerust-analyzer cargo proc-macro2 1.0.107 impl#[Ident][Clone]clone().(),
78 argumentsrust-analyzer cargo syn 2.0.119 path/PathSegment#arguments.: path_arguments,
79 },
80 );
81 }
82 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(call)
83 }
84
85 /// Convert a function signature to a method call with the same arguments.
86 ///
87 /// Given `fn function(&self, a: &str, b: usize)`, get `self.function(a, b)`.
88 pub fn signature_to_method_callrust-analyzer cargo blanket 0.4.0 utils/signature_to_method_call().(sig: &synrust-analyzer cargo syn 2.0.119 crate/::Signaturerust-analyzer cargo syn 2.0.119 item/Signature#) -> synrust-analyzer cargo syn 2.0.119 crate/::Resultrust-analyzer cargo syn 2.0.119 error/Result#<synrust-analyzer cargo syn 2.0.119 crate/::ExprMethodCallrust-analyzer cargo syn 2.0.119 expr/ExprMethodCall#> {
89 // Extract receiver
90 let receiver = sig.receiverrust-analyzer cargo syn 2.0.119 item/impl#[Signature]receiver().().expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("method has no receiver");
91 let span = receiver.spanrust-analyzer cargo syn 2.0.119 spanned/impl#[T][Spanned]span().();
92
93 // Extract arguments
94 let mut funcargs = Punctuatedrust-analyzer cargo syn 2.0.119 punctuated/Punctuated#::newrust-analyzer cargo syn 2.0.119 punctuated/impl#[`Punctuated<T, P>`]new().();
95 for item in &sig.inputsrust-analyzer cargo syn 2.0.119 item/Signature#inputs. {
96 match item {
97 synrust-analyzer cargo syn 2.0.119 crate/::FnArgrust-analyzer cargo syn 2.0.119 item/FnArg#::Receiverrust-analyzer cargo syn 2.0.119 item/FnArg#Receiver#(_) => {}
98 synrust-analyzer cargo syn 2.0.119 crate/::FnArgrust-analyzer cargo syn 2.0.119 item/FnArg#::Typedrust-analyzer cargo syn 2.0.119 item/FnArg#Typed#(argty) => {
99 if let synrust-analyzer cargo syn 2.0.119 crate/::Patrust-analyzer cargo syn 2.0.119 pat/Pat#::Identrust-analyzer cargo syn 2.0.119 pat/Pat#Ident#(ref id) = *rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T, A>`][Deref]deref().argty.patrust-analyzer cargo syn 2.0.119 pat/PatType#pat. {
100 let argpath = synrust-analyzer cargo syn 2.0.119 crate/::ExprPathrust-analyzer cargo syn 2.0.119 expr/ExprPath# {
101 attrsrust-analyzer cargo syn 2.0.119 expr/ExprPath#attrs.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(),
102 qselfrust-analyzer cargo syn 2.0.119 expr/ExprPath#qself.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
103 pathrust-analyzer cargo syn 2.0.119 expr/ExprPath#path.: id.identrust-analyzer cargo syn 2.0.119 pat/PatIdent#ident..clonerust-analyzer cargo proc-macro2 1.0.107 impl#[Ident][Clone]clone().().intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(),
104 };
105 funcargs.pushrust-analyzer cargo syn 2.0.119 punctuated/impl#[`Punctuated<T, P>`]push().(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#(argpath));
106 } else {
107 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().(argty.spanrust-analyzer cargo syn 2.0.119 spanned/impl#[T][Spanned]span().(), "expected identifier"));
108 }
109 }
110 }
111 }
112
113 // Write the method call
114 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(synrust-analyzer cargo syn 2.0.119 crate/::ExprMethodCallrust-analyzer cargo syn 2.0.119 expr/ExprMethodCall# {
115 attrsrust-analyzer cargo syn 2.0.119 expr/ExprMethodCall#attrs.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(),
116 receiverrust-analyzer cargo syn 2.0.119 expr/ExprMethodCall#receiver.: Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(synrust-analyzer cargo syn 2.0.119 crate/::parse2rust-analyzer cargo syn 2.0.119 parse2().(quote_spannedrust-analyzer cargo quote 1.0.47 quote_spanned!!(span=> self))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().),
117 dot_tokenrust-analyzer cargo syn 2.0.119 expr/ExprMethodCall#dot_token.: synrust-analyzer cargo syn 2.0.119 crate/::tokenrust-analyzer cargo syn 2.0.119 token/::Dotrust-analyzer cargo syn 2.0.119 token/Dot# {
118 spansrust-analyzer cargo syn 2.0.119 token/Dot#spans.: [sig.spanrust-analyzer cargo syn 2.0.119 spanned/impl#[T][Spanned]span().()],
119 },
120 methodrust-analyzer cargo syn 2.0.119 expr/ExprMethodCall#method.: sig.identrust-analyzer cargo syn 2.0.119 item/Signature#ident..clonerust-analyzer cargo proc-macro2 1.0.107 impl#[Ident][Clone]clone().(),
121 turbofishrust-analyzer cargo syn 2.0.119 expr/ExprMethodCall#turbofish.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
122 paren_tokenrust-analyzer cargo syn 2.0.119 expr/ExprMethodCall#paren_token.: synrust-analyzer cargo syn 2.0.119 crate/::tokenrust-analyzer cargo syn 2.0.119 token/::Parenrust-analyzer cargo syn 2.0.119 token/Paren#::defaultrust-analyzer cargo syn 2.0.119 token/impl#[Paren][Default]default().(),
123 argsrust-analyzer cargo syn 2.0.119 expr/ExprMethodCall#args.: funcargs,
124 })
125 }
126
127 /// Prepend a module path to a function call name.
128 pub fn prepend_function_pathrust-analyzer cargo blanket 0.4.0 utils/prepend_function_path().(call: &mut synrust-analyzer cargo syn 2.0.119 crate/::ExprCallrust-analyzer cargo syn 2.0.119 expr/ExprCall#, module: 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/::Resultrust-analyzer cargo syn 2.0.119 error/Result#<()> {
129 if let 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 mut path) = *rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T, A>`][Deref]deref().call.funcrust-analyzer cargo syn 2.0.119 expr/ExprCall#func. {
130 for (i, segment) in module.segmentsrust-analyzer cargo syn 2.0.119 path/Path#segments..into_iterrust-analyzer cargo syn 2.0.119 punctuated/impl#[`Punctuated<T, P>`][IntoIterator]into_iter().().enumeraterust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#enumerate().() {
131 path.pathrust-analyzer cargo syn 2.0.119 expr/ExprPath#path..segmentsrust-analyzer cargo syn 2.0.119 path/Path#segments..insertrust-analyzer cargo syn 2.0.119 punctuated/impl#[`Punctuated<T, P>`]insert().(i, segment);
132 }
133 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
134 } else {
135 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().(call.funcrust-analyzer cargo syn 2.0.119 expr/ExprCall#func..spanrust-analyzer cargo syn 2.0.119 spanned/impl#[T][Spanned]span().(), "expected path"))
136 }
137 }
138
139 /// Deref an expression and wrap it in brackets to preserve operation priority.
140 pub fn deref_exprrust-analyzer cargo blanket 0.4.0 utils/deref_expr().(expr: synrust-analyzer cargo syn 2.0.119 crate/::Exprrust-analyzer cargo syn 2.0.119 expr/Expr#) -> synrust-analyzer cargo syn 2.0.119 crate/::Exprrust-analyzer cargo syn 2.0.119 expr/Expr# {
141 synrust-analyzer cargo syn 2.0.119 crate/::Exprrust-analyzer cargo syn 2.0.119 expr/Expr#::Parenrust-analyzer cargo syn 2.0.119 expr/Expr#Paren#(synrust-analyzer cargo syn 2.0.119 crate/::ExprParenrust-analyzer cargo syn 2.0.119 expr/ExprParen# {
142 attrsrust-analyzer cargo syn 2.0.119 expr/ExprParen#attrs.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(),
143 paren_tokenrust-analyzer cargo syn 2.0.119 expr/ExprParen#paren_token.: synrust-analyzer cargo syn 2.0.119 crate/::tokenrust-analyzer cargo syn 2.0.119 token/::Parenrust-analyzer cargo syn 2.0.119 token/Paren#::defaultrust-analyzer cargo syn 2.0.119 token/impl#[Paren][Default]default().(),
144 exprrust-analyzer cargo syn 2.0.119 expr/ExprParen#expr.: Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(synrust-analyzer cargo syn 2.0.119 crate/::Exprrust-analyzer cargo syn 2.0.119 expr/Expr#::Unaryrust-analyzer cargo syn 2.0.119 expr/Expr#Unary#(synrust-analyzer cargo syn 2.0.119 crate/::ExprUnaryrust-analyzer cargo syn 2.0.119 expr/ExprUnary# {
145 attrsrust-analyzer cargo syn 2.0.119 expr/ExprUnary#attrs.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(),
146 oprust-analyzer cargo syn 2.0.119 expr/ExprUnary#op.: synrust-analyzer cargo syn 2.0.119 crate/::UnOprust-analyzer cargo syn 2.0.119 op/UnOp#::Derefrust-analyzer cargo syn 2.0.119 op/UnOp#Deref#(parse_quoterust-analyzer cargo syn 2.0.119 parse_quote/parse_quote!!(*)),
147 exprrust-analyzer cargo syn 2.0.119 expr/ExprUnary#expr.: Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(expr),
148 })),
149 })
150 }
151
152 /// Build a generic identifier suitable for the given trait.
153 ///
154 /// This function extracts the initials of the trait identifier. If this results
155 /// in a generic type identifier already present in the generics of that trait,
156 /// as many underscores are added to the end of the identifier.
157 ///
158 /// Given a trait `MyAmazingTrait`, get `MAT`.
159 pub fn trait_to_generic_identrust-analyzer cargo blanket 0.4.0 utils/trait_to_generic_ident().(trait_: &synrust-analyzer cargo syn 2.0.119 crate/::ItemTraitrust-analyzer cargo syn 2.0.119 item/ItemTrait#) -> synrust-analyzer cargo syn 2.0.119 crate/::Identrust-analyzer cargo proc-macro2 1.0.107 Ident# {
160 let mut raw = trait_
161 .identrust-analyzer cargo syn 2.0.119 item/ItemTrait#ident.
162 .to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()
163 .charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().()
164 .filterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter().(|c| c.is_uppercaserust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]is_uppercase().())
165 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>();
166 loop {
167 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().trait_.genericsrust-analyzer cargo syn 2.0.119 item/ItemTrait#generics..paramsrust-analyzer cargo syn 2.0.119 generics/Generics#params..iterrust-analyzer cargo syn 2.0.119 punctuated/impl#[`Punctuated<T, P>`]iter().().anyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#any().(|g| match g {
168 synrust-analyzer cargo syn 2.0.119 crate/::GenericParamrust-analyzer cargo syn 2.0.119 generics/GenericParam#::Typerust-analyzer cargo syn 2.0.119 generics/GenericParam#Type#(param) if param.identrust-analyzer cargo syn 2.0.119 generics/TypeParam#ident. rust-analyzer cargo proc-macro2 1.0.107 impl#[Ident][`PartialEq<T>`]eq().==rust-analyzer cargo proc-macro2 1.0.107 impl#[Ident][`PartialEq<T>`]eq(). rust-analyzer cargo proc-macro2 1.0.107 impl#[Ident][`PartialEq<T>`]eq().raw => true,
169 synrust-analyzer cargo syn 2.0.119 crate/::GenericParamrust-analyzer cargo syn 2.0.119 generics/GenericParam#::Construst-analyzer cargo syn 2.0.119 generics/GenericParam#Const#(param) if param.identrust-analyzer cargo syn 2.0.119 generics/ConstParam#ident. rust-analyzer cargo proc-macro2 1.0.107 impl#[Ident][`PartialEq<T>`]eq().==rust-analyzer cargo proc-macro2 1.0.107 impl#[Ident][`PartialEq<T>`]eq(). rust-analyzer cargo proc-macro2 1.0.107 impl#[Ident][`PartialEq<T>`]eq().raw => true,
170 _ => false,
171 }) {
172 break;
173 } else {
174 raw.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().('_');
175 }
176 }
177
178 synrust-analyzer cargo syn 2.0.119 crate/::Identrust-analyzer cargo proc-macro2 1.0.107 Ident#::newrust-analyzer cargo proc-macro2 1.0.107 impl#[Ident]new().(&raw, trait_.identrust-analyzer cargo syn 2.0.119 item/ItemTrait#ident..spanrust-analyzer cargo proc-macro2 1.0.107 impl#[Ident]span().())
179 }
180
181 /// Convert a generic type declaration to a generic with the same arguments.
182 ///
183 /// Given a generic section `<T: 'static + Send>`, get simply `<T>`.
184 pub fn generics_declaration_to_genericsrust-analyzer cargo blanket 0.4.0 utils/generics_declaration_to_generics().(
185 generics: &Punctuatedrust-analyzer cargo syn 2.0.119 punctuated/Punctuated#<GenericParamrust-analyzer cargo syn 2.0.119 generics/GenericParam#, Tokenrust-analyzer cargo syn 2.0.119 token/Token!![,]>,
186 ) -> synrust-analyzer cargo syn 2.0.119 crate/::Resultrust-analyzer cargo syn 2.0.119 error/Result#<Punctuatedrust-analyzer cargo syn 2.0.119 punctuated/Punctuated#<GenericParamrust-analyzer cargo syn 2.0.119 generics/GenericParam#, Tokenrust-analyzer cargo syn 2.0.119 token/Token!![,]>> {
187 generics
188 .iterrust-analyzer cargo syn 2.0.119 punctuated/impl#[`Punctuated<T, P>`]iter().()
189 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|gen| match gen {
190 synrust-analyzer cargo syn 2.0.119 crate/::GenericParamrust-analyzer cargo syn 2.0.119 generics/GenericParam#::Typerust-analyzer cargo syn 2.0.119 generics/GenericParam#Type#(t) => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(synrust-analyzer cargo syn 2.0.119 crate/::GenericParamrust-analyzer cargo syn 2.0.119 generics/GenericParam#::Typerust-analyzer cargo syn 2.0.119 generics/GenericParam#Type#(synrust-analyzer cargo syn 2.0.119 crate/::TypeParamrust-analyzer cargo syn 2.0.119 generics/TypeParam# {
191 attrsrust-analyzer cargo syn 2.0.119 generics/TypeParam#attrs.: t.attrsrust-analyzer cargo syn 2.0.119 generics/TypeParam#attrs..clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][Clone]clone().(),
192 identrust-analyzer cargo syn 2.0.119 generics/TypeParam#ident.: t.identrust-analyzer cargo syn 2.0.119 generics/TypeParam#ident..clonerust-analyzer cargo proc-macro2 1.0.107 impl#[Ident][Clone]clone().(),
193 colon_tokenrust-analyzer cargo syn 2.0.119 generics/TypeParam#colon_token.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
194 boundsrust-analyzer cargo syn 2.0.119 generics/TypeParam#bounds.: Punctuatedrust-analyzer cargo syn 2.0.119 punctuated/Punctuated#::newrust-analyzer cargo syn 2.0.119 punctuated/impl#[`Punctuated<T, P>`]new().(),
195 eq_tokenrust-analyzer cargo syn 2.0.119 generics/TypeParam#eq_token.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
196 defaultrust-analyzer cargo syn 2.0.119 generics/TypeParam#default.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
197 })),
198 synrust-analyzer cargo syn 2.0.119 crate/::GenericParamrust-analyzer cargo syn 2.0.119 generics/GenericParam#::Lifetimerust-analyzer cargo syn 2.0.119 generics/GenericParam#Lifetime#(l) => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(synrust-analyzer cargo syn 2.0.119 crate/::GenericParamrust-analyzer cargo syn 2.0.119 generics/GenericParam#::Lifetimerust-analyzer cargo syn 2.0.119 generics/GenericParam#Lifetime#(synrust-analyzer cargo syn 2.0.119 crate/::LifetimeParamrust-analyzer cargo syn 2.0.119 generics/LifetimeParam# {
199 attrsrust-analyzer cargo syn 2.0.119 generics/LifetimeParam#attrs.: l.attrsrust-analyzer cargo syn 2.0.119 generics/LifetimeParam#attrs..clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][Clone]clone().(),
200 lifetimerust-analyzer cargo syn 2.0.119 generics/LifetimeParam#lifetime.: l.lifetimerust-analyzer cargo syn 2.0.119 generics/LifetimeParam#lifetime..clonerust-analyzer cargo syn 2.0.119 lifetime/impl#[Lifetime][Clone]clone().(),
201 colon_tokenrust-analyzer cargo syn 2.0.119 generics/LifetimeParam#colon_token.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
202 boundsrust-analyzer cargo syn 2.0.119 generics/LifetimeParam#bounds.: Punctuatedrust-analyzer cargo syn 2.0.119 punctuated/Punctuated#::newrust-analyzer cargo syn 2.0.119 punctuated/impl#[`Punctuated<T, P>`]new().(),
203 })),
204 synrust-analyzer cargo syn 2.0.119 crate/::GenericParamrust-analyzer cargo syn 2.0.119 generics/GenericParam#::Construst-analyzer cargo syn 2.0.119 generics/GenericParam#Const#(c) => {
205 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().(c.spanrust-analyzer cargo syn 2.0.119 spanned/impl#[T][Spanned]span().(), "cannot handle const generics"))
206 }
207 })
208 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().()
209 }
210
211 #[cfg(test)]
212 mod testsrust-analyzer cargo blanket 0.4.0 utils/tests/ {
213
214 use synrust-analyzer cargo syn 2.0.119 crate/::parse_quoterust-analyzer cargo syn 2.0.119 parse_quote/;
215
216 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
217 fn prepend_function_pathrust-analyzer cargo blanket 0.4.0 utils/tests/prepend_function_path().() {
218 let path = parse_quoterust-analyzer cargo syn 2.0.119 parse_quote/parse_quote!!(crate::qualified::path);
219 let mut call = parse_quoterust-analyzer cargo syn 2.0.119 parse_quote/parse_quote!!(myfunction(arg1, arg2));
220 superrust-analyzer cargo blanket 0.4.0 utils/::prepend_function_pathrust-analyzer cargo blanket 0.4.0 utils/prepend_function_path().(&mut call, path).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
221 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
222 call,
223 parse_quoterust-analyzer cargo syn 2.0.119 parse_quote/parse_quote!!(crate::qualified::path::myfunction(arg1, arg2))
224 );
225 }
226
227 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
228 fn deref_exprrust-analyzer cargo blanket 0.4.0 utils/tests/deref_expr().() {
229 let expr = parse_quoterust-analyzer cargo syn 2.0.119 parse_quote/parse_quote!!(self);
230 let dereffed = superrust-analyzer cargo blanket 0.4.0 utils/::deref_exprrust-analyzer cargo blanket 0.4.0 utils/deref_expr().(expr);
231 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(dereffed, parse_quoterust-analyzer cargo syn 2.0.119 parse_quote/parse_quote!!((*self)));
232 }
233
234 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
235 fn trait_to_generic_identrust-analyzer cargo blanket 0.4.0 utils/tests/trait_to_generic_ident().() {
236 let trait_ = synrust-analyzer cargo syn 2.0.119 crate/::parse_quoterust-analyzer cargo syn 2.0.119 parse_quote/parse_quote!!(
237 trait Trait {}
238 );
239 let expected: synrust-analyzer cargo syn 2.0.119 crate/::Identrust-analyzer cargo proc-macro2 1.0.107 Ident# = synrust-analyzer cargo syn 2.0.119 crate/::parse_quoterust-analyzer cargo syn 2.0.119 parse_quote/parse_quote!!(T);
240 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(superrust-analyzer cargo blanket 0.4.0 utils/::trait_to_generic_identrust-analyzer cargo blanket 0.4.0 utils/trait_to_generic_ident().(&trait_), expected);
241
242 let trait_ = synrust-analyzer cargo syn 2.0.119 crate/::parse_quoterust-analyzer cargo syn 2.0.119 parse_quote/parse_quote!!(
243 trait SomeTrait {}
244 );
245 let expected: synrust-analyzer cargo syn 2.0.119 crate/::Identrust-analyzer cargo proc-macro2 1.0.107 Ident# = synrust-analyzer cargo syn 2.0.119 crate/::parse_quoterust-analyzer cargo syn 2.0.119 parse_quote/parse_quote!!(ST);
246 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(superrust-analyzer cargo blanket 0.4.0 utils/::trait_to_generic_identrust-analyzer cargo blanket 0.4.0 utils/trait_to_generic_ident().(&trait_), expected);
247
248 let trait_ = synrust-analyzer cargo syn 2.0.119 crate/::parse_quoterust-analyzer cargo syn 2.0.119 parse_quote/parse_quote!!(
249 trait Trait<T> {}
250 );
251 let expected: synrust-analyzer cargo syn 2.0.119 crate/::Identrust-analyzer cargo proc-macro2 1.0.107 Ident# = synrust-analyzer cargo syn 2.0.119 crate/::parse_quoterust-analyzer cargo syn 2.0.119 parse_quote/parse_quote!!(T_);
252 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(superrust-analyzer cargo blanket 0.4.0 utils/::trait_to_generic_identrust-analyzer cargo blanket 0.4.0 utils/trait_to_generic_ident().(&trait_), expected);
253 }
254 }