blob: bfa59fddc5568057f968a3f6dfd018ae0e2faff9 [file] [log] [blame]
David Tolnay63e3dee2017-06-03 20:13:17 -07001use std::cmp::Ordering;
2use std::fmt::{self, Display};
3use std::hash::{Hash, Hasher};
4
David Tolnay98942562017-12-26 21:24:35 -05005use proc_macro2::{Span, Term};
David Tolnay63e3dee2017-06-03 20:13:17 -07006use unicode_xid::UnicodeXID;
7
David Tolnay63e3dee2017-06-03 20:13:17 -07008#[cfg_attr(feature = "extra-traits", derive(Debug))]
9#[cfg_attr(feature = "clone-impls", derive(Clone))]
10pub struct Lifetime {
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070011 pub sym: Term,
David Tolnay63e3dee2017-06-03 20:13:17 -070012 pub span: Span,
13}
14
15impl Lifetime {
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070016 pub fn new(sym: Term, span: Span) -> Self {
David Tolnay63e3dee2017-06-03 20:13:17 -070017 let s = sym.as_str();
18
19 if !s.starts_with('\'') {
David Tolnay51382052017-12-27 13:46:21 -050020 panic!(
21 "lifetime name must start with apostrophe as in \"'a\", \
22 got {:?}",
23 s
24 );
David Tolnay63e3dee2017-06-03 20:13:17 -070025 }
26
27 if s == "'" {
28 panic!("lifetime name must not be empty");
29 }
30
31 if s == "'_" {
32 panic!("\"'_\" is not a valid lifetime name");
33 }
34
35 fn xid_ok(s: &str) -> bool {
36 let mut chars = s.chars();
37 let first = chars.next().unwrap();
38 if !(UnicodeXID::is_xid_start(first) || first == '_') {
39 return false;
40 }
41 for ch in chars {
42 if !UnicodeXID::is_xid_continue(ch) {
43 return false;
44 }
45 }
46 true
47 }
48
49 if !xid_ok(&s[1..]) {
David Tolnaybb4ca9f2017-12-26 12:28:58 -050050 panic!("{:?} is not a valid lifetime name", s);
David Tolnay63e3dee2017-06-03 20:13:17 -070051 }
52
53 Lifetime {
54 sym: sym,
55 span: span,
56 }
57 }
58}
59
60impl Display for Lifetime {
61 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
62 self.sym.as_str().fmt(formatter)
63 }
64}
65
66impl PartialEq for Lifetime {
67 fn eq(&self, other: &Lifetime) -> bool {
68 self.sym.as_str() == other.sym.as_str()
69 }
70}
71
72impl Eq for Lifetime {}
73
74impl PartialOrd for Lifetime {
75 fn partial_cmp(&self, other: &Lifetime) -> Option<Ordering> {
76 Some(self.cmp(other))
77 }
78}
79
80impl Ord for Lifetime {
81 fn cmp(&self, other: &Lifetime) -> Ordering {
82 self.sym.as_str().cmp(other.sym.as_str())
83 }
84}
85
86impl Hash for Lifetime {
87 fn hash<H: Hasher>(&self, h: &mut H) {
88 self.sym.as_str().hash(h)
89 }
90}
91
92#[cfg(feature = "parsing")]
93pub mod parsing {
94 use super::*;
David Tolnayc5ab8c62017-12-26 16:43:39 -050095 use synom::Synom;
96 use cursor::Cursor;
David Tolnay203557a2017-12-27 23:59:33 -050097 use parse_error;
98 use synom::PResult;
David Tolnay63e3dee2017-06-03 20:13:17 -070099
100 impl Synom for Lifetime {
101 fn parse(input: Cursor) -> PResult<Self> {
102 let (rest, span, sym) = match input.word() {
103 Some(word) => word,
104 _ => return parse_error(),
105 };
106 if !sym.as_str().starts_with('\'') {
107 return parse_error();
108 }
109
David Tolnay51382052017-12-27 13:46:21 -0500110 Ok((
111 rest,
112 Lifetime {
113 sym: sym,
114 span: span,
115 },
116 ))
David Tolnay63e3dee2017-06-03 20:13:17 -0700117 }
118 }
119}
120
121#[cfg(feature = "printing")]
122mod printing {
123 use super::*;
David Tolnay51382052017-12-27 13:46:21 -0500124 use quote::{ToTokens, Tokens};
125 use proc_macro2::{TokenNode, TokenTree};
David Tolnay63e3dee2017-06-03 20:13:17 -0700126
127 impl ToTokens for Lifetime {
128 fn to_tokens(&self, tokens: &mut Tokens) {
129 tokens.append(TokenTree {
David Tolnay98942562017-12-26 21:24:35 -0500130 span: self.span,
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700131 kind: TokenNode::Term(self.sym),
David Tolnay63e3dee2017-06-03 20:13:17 -0700132 })
133 }
134 }
135}