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