Skip to content

Commit cfa9c10

Browse files
authored
Merge pull request #221 from dtolnay/bound
Create enum for the different possible inferred bounds
2 parents 4bcf6d7 + fd5b9d4 commit cfa9c10

File tree

3 files changed

+53
-23
lines changed

3 files changed

+53
-23
lines changed

src/bound.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use proc_macro2::{Ident, Span};
2+
use syn::punctuated::Punctuated;
3+
use syn::{Token, TypeParamBound};
4+
5+
pub type Supertraits = Punctuated<TypeParamBound, Token![+]>;
6+
7+
pub enum InferredBound {
8+
Send,
9+
Sync,
10+
}
11+
12+
pub fn has_bound(supertraits: &Supertraits, bound: &InferredBound) -> bool {
13+
for supertrait in supertraits {
14+
if let TypeParamBound::Trait(supertrait) = supertrait {
15+
if supertrait.path.is_ident(bound)
16+
|| supertrait.path.segments.len() == 3
17+
&& (supertrait.path.segments[0].ident == "std"
18+
|| supertrait.path.segments[0].ident == "core")
19+
&& supertrait.path.segments[1].ident == "marker"
20+
&& supertrait.path.segments[2].ident == *bound
21+
{
22+
return true;
23+
}
24+
}
25+
}
26+
false
27+
}
28+
29+
impl InferredBound {
30+
fn as_str(&self) -> &str {
31+
match self {
32+
InferredBound::Send => "Send",
33+
InferredBound::Sync => "Sync",
34+
}
35+
}
36+
37+
pub fn spanned_ident(&self, span: Span) -> Ident {
38+
Ident::new(self.as_str(), span)
39+
}
40+
}
41+
42+
impl PartialEq<InferredBound> for Ident {
43+
fn eq(&self, bound: &InferredBound) -> bool {
44+
self == bound.as_str()
45+
}
46+
}

src/expand.rs

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::bound::{has_bound, InferredBound, Supertraits};
12
use crate::lifetime::{AddLifetimeToImplTrait, CollectLifetimes};
23
use crate::parse::Item;
34
use crate::receiver::{has_self_in_block, has_self_in_sig, mut_pat, ReplaceSelf};
@@ -10,7 +11,7 @@ use syn::visit_mut::{self, VisitMut};
1011
use syn::{
1112
parse_quote, parse_quote_spanned, Attribute, Block, FnArg, GenericParam, Generics, Ident,
1213
ImplItem, Lifetime, LifetimeDef, Pat, PatIdent, Receiver, ReturnType, Signature, Stmt, Token,
13-
TraitItem, Type, TypeParamBound, TypePath, WhereClause,
14+
TraitItem, Type, TypePath, WhereClause,
1415
};
1516

1617
impl ToTokens for Item {
@@ -51,8 +52,6 @@ impl Context<'_> {
5152
}
5253
}
5354

54-
type Supertraits = Punctuated<TypeParamBound, Token![+]>;
55-
5655
pub fn expand(input: &mut Item, is_local: bool) {
5756
match input {
5857
Item::Trait(input) => {
@@ -235,7 +234,7 @@ fn transform_sig(
235234
reference: Some(_),
236235
mutability: None,
237236
..
238-
})) => Ident::new("Sync", default_span),
237+
})) => InferredBound::Sync,
239238
Some(FnArg::Typed(arg))
240239
if match (arg.pat.as_ref(), arg.ty.as_ref()) {
241240
(Pat::Ident(pat), Type::Reference(ty)) => {
@@ -244,9 +243,9 @@ fn transform_sig(
244243
_ => false,
245244
} =>
246245
{
247-
Ident::new("Sync", default_span)
246+
InferredBound::Sync
248247
}
249-
_ => Ident::new("Send", default_span),
248+
_ => InferredBound::Send,
250249
};
251250

252251
let assume_bound = match context {
@@ -258,6 +257,7 @@ fn transform_sig(
258257
where_clause.predicates.push(if assume_bound || is_local {
259258
parse_quote_spanned!(default_span=> Self: 'async_trait)
260259
} else {
260+
let bound = bound.spanned_ident(default_span);
261261
parse_quote_spanned!(default_span=> Self: ::core::marker::#bound + 'async_trait)
262262
});
263263
}
@@ -402,23 +402,6 @@ fn positional_arg(i: usize, pat: &Pat) -> Ident {
402402
format_ident!("__arg{}", i, span = span)
403403
}
404404

405-
fn has_bound(supertraits: &Supertraits, marker: &Ident) -> bool {
406-
for bound in supertraits {
407-
if let TypeParamBound::Trait(bound) = bound {
408-
if bound.path.is_ident(marker)
409-
|| bound.path.segments.len() == 3
410-
&& (bound.path.segments[0].ident == "std"
411-
|| bound.path.segments[0].ident == "core")
412-
&& bound.path.segments[1].ident == "marker"
413-
&& bound.path.segments[2].ident == *marker
414-
{
415-
return true;
416-
}
417-
}
418-
}
419-
false
420-
}
421-
422405
fn contains_associated_type_impl_trait(context: Context, ret: &mut Type) -> bool {
423406
struct AssociatedTypeImplTraits<'a> {
424407
set: &'a Set<Ident>,

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@
318318
extern crate proc_macro;
319319

320320
mod args;
321+
mod bound;
321322
mod expand;
322323
mod lifetime;
323324
mod parse;

0 commit comments

Comments
 (0)