Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 1 | #![cfg_attr(not(procmacro2_semver_exempt), allow(dead_code))] |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 2 | |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 3 | use std::fmt; |
| 4 | use std::iter; |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 5 | use std::panic; |
David Tolnay | 3d9d6ad | 2018-05-18 10:51:55 -0700 | [diff] [blame^] | 6 | use std::str::FromStr; |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 7 | |
| 8 | use proc_macro; |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 9 | use stable; |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 10 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 11 | use {Delimiter, Group, Punct, Spacing, TokenTree}; |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 12 | |
| 13 | #[derive(Clone)] |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 14 | pub enum TokenStream { |
| 15 | Nightly(proc_macro::TokenStream), |
| 16 | Stable(stable::TokenStream), |
| 17 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 18 | |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 19 | pub enum LexError { |
| 20 | Nightly(proc_macro::LexError), |
| 21 | Stable(stable::LexError), |
| 22 | } |
| 23 | |
| 24 | fn nightly_works() -> bool { |
| 25 | use std::sync::atomic::*; |
| 26 | static WORKS: AtomicUsize = ATOMIC_USIZE_INIT; |
| 27 | |
| 28 | match WORKS.load(Ordering::SeqCst) { |
| 29 | 1 => return false, |
| 30 | 2 => return true, |
| 31 | _ => {} |
| 32 | } |
| 33 | let works = panic::catch_unwind(|| proc_macro::Span::call_site()).is_ok(); |
| 34 | WORKS.store(works as usize + 1, Ordering::SeqCst); |
| 35 | works |
| 36 | } |
| 37 | |
| 38 | fn mismatch() -> ! { |
| 39 | panic!("stable/nightly mismatch") |
| 40 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 41 | |
| 42 | impl TokenStream { |
| 43 | pub fn empty() -> TokenStream { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 44 | if nightly_works() { |
| 45 | TokenStream::Nightly(proc_macro::TokenStream::empty()) |
| 46 | } else { |
| 47 | TokenStream::Stable(stable::TokenStream::empty()) |
| 48 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | pub fn is_empty(&self) -> bool { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 52 | match self { |
| 53 | TokenStream::Nightly(tts) => tts.is_empty(), |
| 54 | TokenStream::Stable(tts) => tts.is_empty(), |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | fn unwrap_nightly(self) -> proc_macro::TokenStream { |
| 59 | match self { |
| 60 | TokenStream::Nightly(s) => s, |
| 61 | TokenStream::Stable(_) => mismatch(), |
| 62 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
| 66 | impl FromStr for TokenStream { |
| 67 | type Err = LexError; |
| 68 | |
| 69 | fn from_str(src: &str) -> Result<TokenStream, LexError> { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 70 | if nightly_works() { |
| 71 | Ok(TokenStream::Nightly(src.parse()?)) |
| 72 | } else { |
| 73 | Ok(TokenStream::Stable(src.parse()?)) |
| 74 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
| 78 | impl fmt::Display for TokenStream { |
| 79 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 80 | match self { |
| 81 | TokenStream::Nightly(tts) => tts.fmt(f), |
| 82 | TokenStream::Stable(tts) => tts.fmt(f), |
| 83 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
| 87 | impl From<proc_macro::TokenStream> for TokenStream { |
| 88 | fn from(inner: proc_macro::TokenStream) -> TokenStream { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 89 | TokenStream::Nightly(inner) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
| 93 | impl From<TokenStream> for proc_macro::TokenStream { |
| 94 | fn from(inner: TokenStream) -> proc_macro::TokenStream { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 95 | match inner { |
| 96 | TokenStream::Nightly(inner) => inner, |
| 97 | TokenStream::Stable(inner) => inner.to_string().parse().unwrap(), |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | impl From<stable::TokenStream> for TokenStream { |
| 103 | fn from(inner: stable::TokenStream) -> TokenStream { |
| 104 | TokenStream::Stable(inner) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 108 | impl From<TokenTree> for TokenStream { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 109 | fn from(token: TokenTree) -> TokenStream { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 110 | if !nightly_works() { |
David Tolnay | 3d9d6ad | 2018-05-18 10:51:55 -0700 | [diff] [blame^] | 111 | return TokenStream::Stable(token.into()); |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 112 | } |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame] | 113 | let tt: proc_macro::TokenTree = match token { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 114 | TokenTree::Group(tt) => { |
| 115 | let delim = match tt.delimiter() { |
| 116 | Delimiter::Parenthesis => proc_macro::Delimiter::Parenthesis, |
| 117 | Delimiter::Bracket => proc_macro::Delimiter::Bracket, |
| 118 | Delimiter::Brace => proc_macro::Delimiter::Brace, |
| 119 | Delimiter::None => proc_macro::Delimiter::None, |
| 120 | }; |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame] | 121 | let span = tt.span(); |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 122 | let mut group = proc_macro::Group::new(delim, tt.stream.inner.unwrap_nightly()); |
| 123 | group.set_span(span.inner.unwrap_nightly()); |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame] | 124 | group.into() |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 125 | } |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 126 | TokenTree::Punct(tt) => { |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame] | 127 | let spacing = match tt.spacing() { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 128 | Spacing::Joint => proc_macro::Spacing::Joint, |
| 129 | Spacing::Alone => proc_macro::Spacing::Alone, |
| 130 | }; |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 131 | let mut op = proc_macro::Punct::new(tt.as_char(), spacing); |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 132 | op.set_span(tt.span().inner.unwrap_nightly()); |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame] | 133 | op.into() |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 134 | } |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 135 | TokenTree::Ident(tt) => tt.inner.unwrap_nightly().into(), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 136 | TokenTree::Literal(tt) => tt.inner.unwrap_nightly().into(), |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 137 | }; |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 138 | TokenStream::Nightly(tt.into()) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 142 | impl iter::FromIterator<TokenTree> for TokenStream { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 143 | fn from_iter<I: IntoIterator<Item = TokenTree>>(trees: I) -> Self { |
| 144 | if nightly_works() { |
David Tolnay | 3d9d6ad | 2018-05-18 10:51:55 -0700 | [diff] [blame^] | 145 | let trees = trees |
| 146 | .into_iter() |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 147 | .map(TokenStream::from) |
David Tolnay | 3d9d6ad | 2018-05-18 10:51:55 -0700 | [diff] [blame^] | 148 | .flat_map(|t| match t { |
| 149 | TokenStream::Nightly(s) => s, |
| 150 | TokenStream::Stable(_) => mismatch(), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 151 | }); |
| 152 | TokenStream::Nightly(trees.collect()) |
| 153 | } else { |
| 154 | TokenStream::Stable(trees.into_iter().collect()) |
| 155 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 159 | impl Extend<TokenTree> for TokenStream { |
| 160 | fn extend<I: IntoIterator<Item = TokenTree>>(&mut self, streams: I) { |
| 161 | match self { |
| 162 | TokenStream::Nightly(tts) => { |
David Tolnay | 3d9d6ad | 2018-05-18 10:51:55 -0700 | [diff] [blame^] | 163 | *tts = tts |
| 164 | .clone() |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 165 | .into_iter() |
| 166 | .chain( |
David Tolnay | 3d9d6ad | 2018-05-18 10:51:55 -0700 | [diff] [blame^] | 167 | streams |
| 168 | .into_iter() |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 169 | .map(TokenStream::from) |
David Tolnay | 3d9d6ad | 2018-05-18 10:51:55 -0700 | [diff] [blame^] | 170 | .flat_map(|t| match t { |
| 171 | TokenStream::Nightly(tts) => tts.into_iter(), |
| 172 | _ => panic!(), |
| 173 | }), |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 174 | ) |
| 175 | .collect(); |
| 176 | } |
| 177 | TokenStream::Stable(tts) => tts.extend(streams), |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 182 | impl fmt::Debug for TokenStream { |
| 183 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 184 | match self { |
| 185 | TokenStream::Nightly(tts) => tts.fmt(f), |
| 186 | TokenStream::Stable(tts) => tts.fmt(f), |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | impl From<proc_macro::LexError> for LexError { |
| 192 | fn from(e: proc_macro::LexError) -> LexError { |
| 193 | LexError::Nightly(e) |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | impl From<stable::LexError> for LexError { |
| 198 | fn from(e: stable::LexError) -> LexError { |
| 199 | LexError::Stable(e) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
| 203 | impl fmt::Debug for LexError { |
| 204 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 205 | match self { |
| 206 | LexError::Nightly(e) => e.fmt(f), |
| 207 | LexError::Stable(e) => e.fmt(f), |
| 208 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 212 | pub enum TokenTreeIter { |
| 213 | Nightly(proc_macro::token_stream::IntoIter), |
| 214 | Stable(stable::TokenTreeIter), |
| 215 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 216 | |
| 217 | impl IntoIterator for TokenStream { |
| 218 | type Item = TokenTree; |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 219 | type IntoIter = TokenTreeIter; |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 220 | |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 221 | fn into_iter(self) -> TokenTreeIter { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 222 | match self { |
| 223 | TokenStream::Nightly(tts) => TokenTreeIter::Nightly(tts.into_iter()), |
| 224 | TokenStream::Stable(tts) => TokenTreeIter::Stable(tts.into_iter()), |
| 225 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 229 | impl Iterator for TokenTreeIter { |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 230 | type Item = TokenTree; |
| 231 | |
| 232 | fn next(&mut self) -> Option<TokenTree> { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 233 | let token = match self { |
| 234 | TokenTreeIter::Nightly(iter) => iter.next()?, |
| 235 | TokenTreeIter::Stable(iter) => return iter.next(), |
| 236 | }; |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame] | 237 | Some(match token { |
| 238 | proc_macro::TokenTree::Group(tt) => { |
| 239 | let delim = match tt.delimiter() { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 240 | proc_macro::Delimiter::Parenthesis => Delimiter::Parenthesis, |
| 241 | proc_macro::Delimiter::Bracket => Delimiter::Bracket, |
| 242 | proc_macro::Delimiter::Brace => Delimiter::Brace, |
| 243 | proc_macro::Delimiter::None => Delimiter::None, |
| 244 | }; |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 245 | let stream = ::TokenStream::_new(TokenStream::Nightly(tt.stream())); |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 246 | let mut g = Group::new(delim, stream); |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 247 | g.set_span(::Span::_new(Span::Nightly(tt.span()))); |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 248 | g.into() |
| 249 | } |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 250 | proc_macro::TokenTree::Punct(tt) => { |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame] | 251 | let spacing = match tt.spacing() { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 252 | proc_macro::Spacing::Joint => Spacing::Joint, |
| 253 | proc_macro::Spacing::Alone => Spacing::Alone, |
| 254 | }; |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 255 | let mut o = Punct::new(tt.as_char(), spacing); |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 256 | o.set_span(::Span::_new(Span::Nightly(tt.span()))); |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 257 | o.into() |
| 258 | } |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 259 | proc_macro::TokenTree::Ident(s) => ::Ident::_new(Ident::Nightly(s)).into(), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 260 | proc_macro::TokenTree::Literal(l) => ::Literal::_new(Literal::Nightly(l)).into(), |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 261 | }) |
| 262 | } |
| 263 | |
| 264 | fn size_hint(&self) -> (usize, Option<usize>) { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 265 | match self { |
| 266 | TokenTreeIter::Nightly(tts) => tts.size_hint(), |
| 267 | TokenTreeIter::Stable(tts) => tts.size_hint(), |
| 268 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 272 | impl fmt::Debug for TokenTreeIter { |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 273 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 274 | f.debug_struct("TokenTreeIter").finish() |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 278 | pub use stable::FileName; |
Nika Layzell | b35a9a3 | 2017-12-30 14:34:35 -0500 | [diff] [blame] | 279 | |
| 280 | // NOTE: We have to generate our own filename object here because we can't wrap |
| 281 | // the one provided by proc_macro. |
| 282 | #[derive(Clone, PartialEq, Eq)] |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 283 | pub enum SourceFile { |
| 284 | Nightly(proc_macro::SourceFile, FileName), |
| 285 | Stable(stable::SourceFile), |
| 286 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 287 | |
| 288 | impl SourceFile { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 289 | fn nightly(sf: proc_macro::SourceFile) -> Self { |
| 290 | let filename = stable::file_name(sf.path().to_string()); |
| 291 | SourceFile::Nightly(sf, filename) |
Nika Layzell | b35a9a3 | 2017-12-30 14:34:35 -0500 | [diff] [blame] | 292 | } |
| 293 | |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 294 | /// Get the path to this source file as a string. |
Nika Layzell | b35a9a3 | 2017-12-30 14:34:35 -0500 | [diff] [blame] | 295 | pub fn path(&self) -> &FileName { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 296 | match self { |
| 297 | SourceFile::Nightly(_, f) => f, |
| 298 | SourceFile::Stable(a) => a.path(), |
| 299 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | pub fn is_real(&self) -> bool { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 303 | match self { |
| 304 | SourceFile::Nightly(a, _) => a.is_real(), |
| 305 | SourceFile::Stable(a) => a.is_real(), |
| 306 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
Nika Layzell | b35a9a3 | 2017-12-30 14:34:35 -0500 | [diff] [blame] | 310 | impl AsRef<FileName> for SourceFile { |
| 311 | fn as_ref(&self) -> &FileName { |
| 312 | self.path() |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
| 316 | impl fmt::Debug for SourceFile { |
| 317 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 318 | match self { |
| 319 | SourceFile::Nightly(a, _) => a.fmt(f), |
| 320 | SourceFile::Stable(a) => a.fmt(f), |
| 321 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
Nika Layzell | 1ecb6ce | 2017-12-30 14:34:05 -0500 | [diff] [blame] | 325 | pub struct LineColumn { |
| 326 | pub line: usize, |
| 327 | pub column: usize, |
| 328 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 329 | |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame] | 330 | #[derive(Copy, Clone)] |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 331 | pub enum Span { |
| 332 | Nightly(proc_macro::Span), |
| 333 | Stable(stable::Span), |
Sergio Benitez | 1380508 | 2018-01-04 01:25:45 -0800 | [diff] [blame] | 334 | } |
| 335 | |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 336 | impl Span { |
| 337 | pub fn call_site() -> Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 338 | if nightly_works() { |
| 339 | Span::Nightly(proc_macro::Span::call_site()) |
| 340 | } else { |
| 341 | Span::Stable(stable::Span::call_site()) |
| 342 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 343 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 344 | |
Alex Crichton | e6085b7 | 2017-11-21 07:24:25 -0800 | [diff] [blame] | 345 | pub fn def_site() -> Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 346 | if nightly_works() { |
| 347 | Span::Nightly(proc_macro::Span::def_site()) |
| 348 | } else { |
| 349 | Span::Stable(stable::Span::def_site()) |
| 350 | } |
Alex Crichton | 998f642 | 2017-11-19 08:06:27 -0800 | [diff] [blame] | 351 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 352 | |
David Tolnay | 4e8e397 | 2018-01-05 18:10:22 -0800 | [diff] [blame] | 353 | pub fn resolved_at(&self, other: Span) -> Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 354 | match (self, other) { |
| 355 | (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.resolved_at(b)), |
| 356 | (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.resolved_at(b)), |
| 357 | _ => mismatch(), |
| 358 | } |
David Tolnay | 4e8e397 | 2018-01-05 18:10:22 -0800 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | pub fn located_at(&self, other: Span) -> Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 362 | match (self, other) { |
| 363 | (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.located_at(b)), |
| 364 | (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.located_at(b)), |
| 365 | _ => mismatch(), |
| 366 | } |
David Tolnay | 4e8e397 | 2018-01-05 18:10:22 -0800 | [diff] [blame] | 367 | } |
| 368 | |
David Tolnay | 16a1720 | 2017-12-31 10:47:24 -0500 | [diff] [blame] | 369 | pub fn unstable(self) -> proc_macro::Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 370 | match self { |
| 371 | Span::Nightly(s) => s, |
| 372 | Span::Stable(_) => mismatch(), |
| 373 | } |
David Tolnay | 16a1720 | 2017-12-31 10:47:24 -0500 | [diff] [blame] | 374 | } |
| 375 | |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 376 | #[cfg(procmacro2_semver_exempt)] |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 377 | pub fn source_file(&self) -> SourceFile { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 378 | match self { |
| 379 | Span::Nightly(s) => SourceFile::nightly(s.source_file()), |
| 380 | Span::Stable(s) => SourceFile::Stable(s.source_file()), |
| 381 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 382 | } |
| 383 | |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 384 | #[cfg(procmacro2_semver_exempt)] |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 385 | pub fn start(&self) -> LineColumn { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 386 | match self { |
| 387 | Span::Nightly(s) => { |
| 388 | let proc_macro::LineColumn { line, column } = s.start(); |
| 389 | LineColumn { line, column } |
| 390 | } |
| 391 | Span::Stable(s) => { |
| 392 | let stable::LineColumn { line, column } = s.start(); |
| 393 | LineColumn { line, column } |
| 394 | } |
| 395 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 396 | } |
| 397 | |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 398 | #[cfg(procmacro2_semver_exempt)] |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 399 | pub fn end(&self) -> LineColumn { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 400 | match self { |
| 401 | Span::Nightly(s) => { |
| 402 | let proc_macro::LineColumn { line, column } = s.end(); |
| 403 | LineColumn { line, column } |
| 404 | } |
| 405 | Span::Stable(s) => { |
| 406 | let stable::LineColumn { line, column } = s.end(); |
| 407 | LineColumn { line, column } |
| 408 | } |
| 409 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 410 | } |
| 411 | |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 412 | #[cfg(procmacro2_semver_exempt)] |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 413 | pub fn join(&self, other: Span) -> Option<Span> { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 414 | let ret = match (self, other) { |
| 415 | (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.join(b)?), |
| 416 | (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.join(b)?), |
| 417 | _ => return None, |
| 418 | }; |
| 419 | Some(ret) |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 420 | } |
Alex Crichton | e24f734 | 2018-04-05 17:58:11 -0700 | [diff] [blame] | 421 | |
| 422 | pub fn eq(&self, other: &Span) -> bool { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 423 | match (self, other) { |
| 424 | (Span::Nightly(a), Span::Nightly(b)) => a.eq(b), |
| 425 | (Span::Stable(a), Span::Stable(b)) => a.eq(b), |
| 426 | _ => false, |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | fn unwrap_nightly(self) -> proc_macro::Span { |
| 431 | match self { |
| 432 | Span::Nightly(s) => s, |
| 433 | Span::Stable(_) => mismatch(), |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | impl From<proc_macro::Span> for ::Span { |
| 439 | fn from(proc_span: proc_macro::Span) -> ::Span { |
| 440 | ::Span::_new(Span::Nightly(proc_span)) |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | impl From<stable::Span> for Span { |
| 445 | fn from(inner: stable::Span) -> Span { |
| 446 | Span::Stable(inner) |
Alex Crichton | e24f734 | 2018-04-05 17:58:11 -0700 | [diff] [blame] | 447 | } |
Alex Crichton | 998f642 | 2017-11-19 08:06:27 -0800 | [diff] [blame] | 448 | } |
| 449 | |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 450 | impl fmt::Debug for Span { |
| 451 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 452 | match self { |
| 453 | Span::Nightly(s) => s.fmt(f), |
| 454 | Span::Stable(s) => s.fmt(f), |
| 455 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 456 | } |
| 457 | } |
| 458 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 459 | #[derive(Clone)] |
| 460 | pub enum Ident { |
| 461 | Nightly(proc_macro::Ident), |
| 462 | Stable(stable::Ident), |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 463 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 464 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 465 | impl Ident { |
| 466 | pub fn new(string: &str, span: Span) -> Ident { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 467 | match span { |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 468 | Span::Nightly(s) => Ident::Nightly(proc_macro::Ident::new(string, s)), |
| 469 | Span::Stable(s) => Ident::Stable(stable::Ident::new(string, s)), |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 470 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 471 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 472 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 473 | pub fn new_raw(string: &str, span: Span) -> Ident { |
| 474 | match span { |
| 475 | Span::Nightly(s) => Ident::Nightly(proc_macro::Ident::new_raw(string, s)), |
| 476 | Span::Stable(s) => Ident::Stable(stable::Ident::new_raw(string, s)), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 477 | } |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | pub fn span(&self) -> Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 481 | match self { |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 482 | Ident::Nightly(t) => Span::Nightly(t.span()), |
| 483 | Ident::Stable(t) => Span::Stable(t.span()), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 484 | } |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | pub fn set_span(&mut self, span: Span) { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 488 | match (self, span) { |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 489 | (Ident::Nightly(t), Span::Nightly(s)) => t.set_span(s), |
| 490 | (Ident::Stable(t), Span::Stable(s)) => t.set_span(s), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 491 | _ => mismatch(), |
| 492 | } |
| 493 | } |
| 494 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 495 | fn unwrap_nightly(self) -> proc_macro::Ident { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 496 | match self { |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 497 | Ident::Nightly(s) => s, |
| 498 | Ident::Stable(_) => mismatch(), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 499 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 503 | impl fmt::Display for Ident { |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 504 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 505 | match self { |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 506 | Ident::Nightly(t) => t.fmt(f), |
| 507 | Ident::Stable(t) => t.fmt(f), |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | impl fmt::Debug for Ident { |
| 513 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 514 | match self { |
| 515 | Ident::Nightly(t) => t.fmt(f), |
| 516 | Ident::Stable(t) => t.fmt(f), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 517 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | |
| 521 | #[derive(Clone)] |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 522 | pub enum Literal { |
| 523 | Nightly(proc_macro::Literal), |
| 524 | Stable(stable::Literal), |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 525 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 526 | |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 527 | macro_rules! suffixed_numbers { |
| 528 | ($($name:ident => $kind:ident,)*) => ($( |
| 529 | pub fn $name(n: $kind) -> Literal { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 530 | if nightly_works() { |
| 531 | Literal::Nightly(proc_macro::Literal::$name(n)) |
| 532 | } else { |
| 533 | Literal::Stable(stable::Literal::$name(n)) |
| 534 | } |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 535 | } |
| 536 | )*) |
| 537 | } |
| 538 | |
| 539 | macro_rules! unsuffixed_integers { |
| 540 | ($($name:ident => $kind:ident,)*) => ($( |
| 541 | pub fn $name(n: $kind) -> Literal { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 542 | if nightly_works() { |
| 543 | Literal::Nightly(proc_macro::Literal::$name(n)) |
| 544 | } else { |
| 545 | Literal::Stable(stable::Literal::$name(n)) |
| 546 | } |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 547 | } |
| 548 | )*) |
| 549 | } |
| 550 | |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 551 | impl Literal { |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 552 | suffixed_numbers! { |
| 553 | u8_suffixed => u8, |
| 554 | u16_suffixed => u16, |
| 555 | u32_suffixed => u32, |
| 556 | u64_suffixed => u64, |
| 557 | usize_suffixed => usize, |
| 558 | i8_suffixed => i8, |
| 559 | i16_suffixed => i16, |
| 560 | i32_suffixed => i32, |
| 561 | i64_suffixed => i64, |
| 562 | isize_suffixed => isize, |
| 563 | |
| 564 | f32_suffixed => f32, |
| 565 | f64_suffixed => f64, |
| 566 | } |
| 567 | |
| 568 | unsuffixed_integers! { |
| 569 | u8_unsuffixed => u8, |
| 570 | u16_unsuffixed => u16, |
| 571 | u32_unsuffixed => u32, |
| 572 | u64_unsuffixed => u64, |
| 573 | usize_unsuffixed => usize, |
| 574 | i8_unsuffixed => i8, |
| 575 | i16_unsuffixed => i16, |
| 576 | i32_unsuffixed => i32, |
| 577 | i64_unsuffixed => i64, |
| 578 | isize_unsuffixed => isize, |
| 579 | } |
| 580 | |
| 581 | pub fn f32_unsuffixed(f: f32) -> Literal { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 582 | if nightly_works() { |
| 583 | Literal::Nightly(proc_macro::Literal::f32_unsuffixed(f)) |
| 584 | } else { |
| 585 | Literal::Stable(stable::Literal::f32_unsuffixed(f)) |
| 586 | } |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | pub fn f64_unsuffixed(f: f64) -> Literal { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 590 | if nightly_works() { |
| 591 | Literal::Nightly(proc_macro::Literal::f64_unsuffixed(f)) |
| 592 | } else { |
| 593 | Literal::Stable(stable::Literal::f64_unsuffixed(f)) |
| 594 | } |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 595 | } |
| 596 | |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 597 | pub fn string(t: &str) -> Literal { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 598 | if nightly_works() { |
| 599 | Literal::Nightly(proc_macro::Literal::string(t)) |
| 600 | } else { |
| 601 | Literal::Stable(stable::Literal::string(t)) |
| 602 | } |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | pub fn character(t: char) -> Literal { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 606 | if nightly_works() { |
| 607 | Literal::Nightly(proc_macro::Literal::character(t)) |
| 608 | } else { |
| 609 | Literal::Stable(stable::Literal::character(t)) |
| 610 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | pub fn byte_string(bytes: &[u8]) -> Literal { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 614 | if nightly_works() { |
| 615 | Literal::Nightly(proc_macro::Literal::byte_string(bytes)) |
| 616 | } else { |
| 617 | Literal::Stable(stable::Literal::byte_string(bytes)) |
| 618 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 619 | } |
| 620 | |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 621 | pub fn span(&self) -> Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 622 | match self { |
| 623 | Literal::Nightly(lit) => Span::Nightly(lit.span()), |
| 624 | Literal::Stable(lit) => Span::Stable(lit.span()), |
| 625 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 626 | } |
| 627 | |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 628 | pub fn set_span(&mut self, span: Span) { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 629 | match (self, span) { |
| 630 | (Literal::Nightly(lit), Span::Nightly(s)) => lit.set_span(s), |
| 631 | (Literal::Stable(lit), Span::Stable(s)) => lit.set_span(s), |
| 632 | _ => mismatch(), |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | fn unwrap_nightly(self) -> proc_macro::Literal { |
| 637 | match self { |
| 638 | Literal::Nightly(s) => s, |
| 639 | Literal::Stable(_) => mismatch(), |
| 640 | } |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | impl From<stable::Literal> for Literal { |
| 645 | fn from(s: stable::Literal) -> Literal { |
| 646 | Literal::Stable(s) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 647 | } |
| 648 | } |
| 649 | |
| 650 | impl fmt::Display for Literal { |
| 651 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 652 | match self { |
| 653 | Literal::Nightly(t) => t.fmt(f), |
| 654 | Literal::Stable(t) => t.fmt(f), |
| 655 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 656 | } |
| 657 | } |
| 658 | |
| 659 | impl fmt::Debug for Literal { |
| 660 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 661 | match self { |
| 662 | Literal::Nightly(t) => t.fmt(f), |
| 663 | Literal::Stable(t) => t.fmt(f), |
| 664 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 665 | } |
| 666 | } |