Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #221 from dtolnay/bound
Create enum for the different possible inferred bounds
- Loading branch information
Showing
3 changed files
with
53 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use proc_macro2::{Ident, Span}; | ||
use syn::punctuated::Punctuated; | ||
use syn::{Token, TypeParamBound}; | ||
|
||
pub type Supertraits = Punctuated<TypeParamBound, Token![+]>; | ||
|
||
pub enum InferredBound { | ||
Send, | ||
Sync, | ||
} | ||
|
||
pub fn has_bound(supertraits: &Supertraits, bound: &InferredBound) -> bool { | ||
for supertrait in supertraits { | ||
if let TypeParamBound::Trait(supertrait) = supertrait { | ||
if supertrait.path.is_ident(bound) | ||
|| supertrait.path.segments.len() == 3 | ||
&& (supertrait.path.segments[0].ident == "std" | ||
|| supertrait.path.segments[0].ident == "core") | ||
&& supertrait.path.segments[1].ident == "marker" | ||
&& supertrait.path.segments[2].ident == *bound | ||
{ | ||
return true; | ||
} | ||
} | ||
} | ||
false | ||
} | ||
|
||
impl InferredBound { | ||
fn as_str(&self) -> &str { | ||
match self { | ||
InferredBound::Send => "Send", | ||
InferredBound::Sync => "Sync", | ||
} | ||
} | ||
|
||
pub fn spanned_ident(&self, span: Span) -> Ident { | ||
Ident::new(self.as_str(), span) | ||
} | ||
} | ||
|
||
impl PartialEq<InferredBound> for Ident { | ||
fn eq(&self, bound: &InferredBound) -> bool { | ||
self == bound.as_str() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -318,6 +318,7 @@ | |
extern crate proc_macro; | ||
|
||
mod args; | ||
mod bound; | ||
mod expand; | ||
mod lifetime; | ||
mod parse; | ||
|