-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathutils.rs
106 lines (93 loc) · 2.95 KB
/
utils.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use quote::{format_ident, quote_spanned};
use syn::{
punctuated::Punctuated,
token::{self, Comma},
*,
};
pub(crate) const DEFAULT_LIFETIME_NAME: &str = "'_pin";
pub(crate) type Variants = Punctuated<Variant, token::Comma>;
pub(crate) use Mutability::{Immutable, Mutable};
#[derive(Clone, Copy, Eq, PartialEq)]
pub(crate) enum Mutability {
Mutable,
Immutable,
}
/// Creates the ident of projected type from the ident of the original type.
pub(crate) fn proj_ident(ident: &Ident, mutability: Mutability) -> Ident {
if mutability == Mutable {
format_ident!("__{}Projection", ident)
} else {
format_ident!("__{}ProjectionRef", ident)
}
}
/// Determines the lifetime names. Ensure it doesn't overlap with any existing lifetime names.
pub(crate) fn determine_lifetime_name(
lifetime_name: &mut String,
generics: &Punctuated<GenericParam, Comma>,
) {
let existing_lifetimes: Vec<String> = generics
.iter()
.filter_map(|param| {
if let GenericParam::Lifetime(LifetimeDef { lifetime, .. }) = param {
Some(lifetime.to_string())
} else {
None
}
})
.collect();
while existing_lifetimes.iter().any(|name| name.starts_with(&**lifetime_name)) {
lifetime_name.push('_');
}
}
/// Inserts a `lifetime` at position `0` of `generics.params`.
pub(crate) fn insert_lifetime(generics: &mut Generics, lifetime: Lifetime) {
if generics.lt_token.is_none() {
generics.lt_token = Some(token::Lt::default())
}
if generics.gt_token.is_none() {
generics.gt_token = Some(token::Gt::default())
}
generics.params.insert(
0,
GenericParam::Lifetime(LifetimeDef {
attrs: Vec::new(),
lifetime,
colon_token: None,
bounds: Punctuated::new(),
}),
);
}
/// Determines the visibility of the projected type and projection method.
pub(crate) fn determine_visibility(vis: &Visibility) -> Visibility {
if let Visibility::Public(token) = vis {
syn::parse2(quote_spanned! { token.pub_token.span =>
pub(crate)
})
.unwrap()
} else {
vis.clone()
}
}
pub(crate) fn collect_cfg(attrs: &[Attribute]) -> Vec<Attribute> {
attrs.iter().filter(|attr| attr.path.is_ident("cfg")).cloned().collect()
}
pub(crate) trait VecExt {
fn position(&self, ident: &str) -> Option<usize>;
fn find_remove(&mut self, ident: &str) -> Option<Attribute>;
}
impl VecExt for Vec<Attribute> {
fn position(&self, ident: &str) -> Option<usize> {
self.iter().position(|attr| attr.path.is_ident(ident))
}
fn find_remove(&mut self, ident: &str) -> Option<Attribute> {
self.position(ident).map(|i| self.remove(i))
}
}
macro_rules! error {
($span:expr, $msg:expr) => {
syn::Error::new_spanned(&$span, $msg)
};
($span:expr, $($tt:tt)*) => {
error!($span, format!($($tt)*))
};
}