| 1 |
//! [![github]](https://github.com/dtolnay/proc-macro2) [![crates-io]](https://crates.io/crates/proc-macro2) [![docs-rs]](crate) |
| 2 |
//! |
| 3 |
//! [github]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github |
| 4 |
//! [crates-io]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust |
| 5 |
//! [docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs |
| 6 |
//! |
| 7 |
//! <br> |
| 8 |
//! |
| 9 |
//! A wrapper around the procedural macro API of the compiler's [`proc_macro`] |
| 10 |
//! crate. This library serves two purposes: |
| 11 |
//! |
| 12 |
//! - **Bring proc-macro-like functionality to other contexts like build.rs and |
| 13 |
//! main.rs.** Types from `proc_macro` are entirely specific to procedural |
| 14 |
//! macros and cannot ever exist in code outside of a procedural macro. |
| 15 |
//! Meanwhile `proc_macro2` types may exist anywhere including non-macro code. |
| 16 |
//! By developing foundational libraries like [syn] and [quote] against |
| 17 |
//! `proc_macro2` rather than `proc_macro`, the procedural macro ecosystem |
| 18 |
//! becomes easily applicable to many other use cases and we avoid |
| 19 |
//! reimplementing non-macro equivalents of those libraries. |
| 20 |
//! |
| 21 |
//! - **Make procedural macros unit testable.** As a consequence of being |
| 22 |
//! specific to procedural macros, nothing that uses `proc_macro` can be |
| 23 |
//! executed from a unit test. In order for helper libraries or components of |
| 24 |
//! a macro to be testable in isolation, they must be implemented using |
| 25 |
//! `proc_macro2`. |
| 26 |
//! |
| 27 |
//! [syn]: https://github.com/dtolnay/syn |
| 28 |
//! [quote]: https://github.com/dtolnay/quote |
| 29 |
//! |
| 30 |
//! # Usage |
| 31 |
//! |
| 32 |
//! The skeleton of a typical procedural macro typically looks like this: |
| 33 |
//! |
| 34 |
//! ``` |
| 35 |
//! extern crate proc_macro; |
| 36 |
//! |
| 37 |
//! # const IGNORE: &str = stringify! { |
| 38 |
//! #[proc_macro_derive(MyDerive)] |
| 39 |
//! # }; |
| 40 |
//! # #[cfg(wrap_proc_macro)] |
| 41 |
//! pub fn my_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream { |
| 42 |
//! let input = proc_macro2::TokenStream::from(input); |
| 43 |
//! |
| 44 |
//! let output: proc_macro2::TokenStream = { |
| 45 |
//! /* transform input */ |
| 46 |
//! # input |
| 47 |
//! }; |
| 48 |
//! |
| 49 |
//! proc_macro::TokenStream::from(output) |
| 50 |
//! } |
| 51 |
//! ``` |
| 52 |
//! |
| 53 |
//! If parsing with [Syn], you'll use [`parse_macro_input!`] instead to |
| 54 |
//! propagate parse errors correctly back to the compiler when parsing fails. |
| 55 |
//! |
| 56 |
//! [`parse_macro_input!`]: https://docs.rs/syn/2.0/syn/macro.parse_macro_input.html |
| 57 |
//! |
| 58 |
//! # Unstable features |
| 59 |
//! |
| 60 |
//! The default feature set of proc-macro2 tracks the most recent stable |
| 61 |
//! compiler API. Functionality in `proc_macro` that is not yet stable is not |
| 62 |
//! exposed by proc-macro2 by default. |
| 63 |
//! |
| 64 |
//! To opt into the additional APIs available in the most recent nightly |
| 65 |
//! compiler, the `procmacro2_semver_exempt` config flag must be passed to |
| 66 |
//! rustc. We will polyfill those nightly-only APIs back to Rust 1.68.0. As |
| 67 |
//! these are unstable APIs that track the nightly compiler, minor versions of |
| 68 |
//! proc-macro2 may make breaking changes to them at any time. |
| 69 |
//! |
| 70 |
//! ```sh |
| 71 |
//! RUSTFLAGS='--cfg procmacro2_semver_exempt' cargo build |
| 72 |
//! ``` |
| 73 |
//! |
| 74 |
//! Note that this must not only be done for your crate, but for any crate that |
| 75 |
//! depends on your crate. This infectious nature is intentional, as it serves |
| 76 |
//! as a reminder that you are outside of the normal semver guarantees. |
| 77 |
//! |
| 78 |
//! Semver exempt methods are marked as such in the proc-macro2 documentation. |
| 79 |
//! |
| 80 |
//! # Thread-Safety |
| 81 |
//! |
| 82 |
//! Most types in this crate are `!Sync` because the underlying compiler |
| 83 |
//! types make use of thread-local memory, meaning they cannot be accessed from |
| 84 |
//! a different thread. |
| 85 |
|
| 86 |
// Proc-macro2 types in rustdoc of other crates get linked to here. |
| 87 |
#![doc(html_root_url = "https://docs.rs/proc-macro2/1.0.104")] |
| 88 |
#![cfg_attr(any(proc_macro_span, super_unstable), feature(proc_macro_span))] |
| 89 |
#![cfg_attr(super_unstable, feature(proc_macro_def_site))] |
| 90 |
#![cfg_attr(docsrs, feature(doc_cfg))] |
| 91 |
#![deny(unsafe_op_in_unsafe_fn)] |
| 92 |
#![allow( |
| 93 |
clippy::cast_lossless, |
| 94 |
clippy::cast_possible_truncation, |
| 95 |
clippy::checked_conversions, |
| 96 |
clippy::doc_markdown, |
| 97 |
clippy::elidable_lifetime_names, |
| 98 |
clippy::incompatible_msrv, |
| 99 |
clippy::items_after_statements, |
| 100 |
clippy::iter_without_into_iter, |
| 101 |
clippy::let_underscore_untyped, |
| 102 |
clippy::manual_assert, |
| 103 |
clippy::manual_range_contains, |
| 104 |
clippy::missing_panics_doc, |
| 105 |
clippy::missing_safety_doc, |
| 106 |
clippy::must_use_candidate, |
| 107 |
clippy::needless_doctest_main, |
| 108 |
clippy::needless_lifetimes, |
| 109 |
clippy::new_without_default, |
| 110 |
clippy::return_self_not_must_use, |
| 111 |
clippy::shadow_unrelated, |
| 112 |
clippy::trivially_copy_pass_by_ref, |
| 113 |
clippy::uninlined_format_args, |
| 114 |
clippy::unnecessary_wraps, |
| 115 |
clippy::unused_self, |
| 116 |
clippy::used_underscore_binding, |
| 117 |
clippy::vec_init_then_push |
| 118 |
)] |
| 119 |
#![allow(unknown_lints, mismatched_lifetime_syntaxes)] |
| 120 |
|
| 121 |
#[cfg(all(procmacro2_semver_exempt, wrap_proc_macro, not(super_unstable)))] |
| 122 |
compile_error! {"\ |
| 123 |
Something is not right. If you've tried to turn on \ |
| 124 |
procmacro2_semver_exempt, you need to ensure that it \ |
| 125 |
is turned on for the compilation of the proc-macro2 \ |
| 126 |
build script as well. |
| 127 |
"} |
| 128 |
|
| 129 |
#[cfg(all( |
| 130 |
procmacro2_nightly_testing, |
| 131 |
feature = "proc-macro", |
| 132 |
not(proc_macro_span) |
| 133 |
))] |
| 134 |
compile_error! {"\ |
| 135 |
Build script probe failed to compile. |
| 136 |
"} |
| 137 |
|
| 138 |
extern crate alloc; |
| 139 |
|
| 140 |
#[cfg(feature = "proc-macro")] |
| 141 |
extern crate proc_macro; |
| 142 |
|
| 143 |
mod marker; |
| 144 |
mod parse; |
| 145 |
mod probe; |
| 146 |
mod rcvec; |
| 147 |
|
| 148 |
#[cfg(wrap_proc_macro)] |
| 149 |
mod detection; |
| 150 |
|
| 151 |
// Public for proc_macro2::fallback::force() and unforce(), but those are quite |
| 152 |
// a niche use case so we omit it from rustdoc. |
| 153 |
#[doc(hidden)] |
| 154 |
pub mod fallback; |
| 155 |
|
| 156 |
pub mod extra; |
| 157 |
|
| 158 |
#[cfg(not(wrap_proc_macro))] |
| 159 |
use crate::fallback as imp; |
| 160 |
#[path = "wrapper.rs"] |
| 161 |
#[cfg(wrap_proc_macro)] |
| 162 |
mod imp; |
| 163 |
|
| 164 |
#[cfg(span_locations)] |
| 165 |
mod location; |
| 166 |
|
| 167 |
#[cfg(procmacro2_semver_exempt)] |
| 168 |
mod num; |
| 169 |
#[cfg(procmacro2_semver_exempt)] |
| 170 |
#[allow(dead_code)] |
| 171 |
mod rustc_literal_escaper; |
| 172 |
|
| 173 |
use crate::extra::DelimSpan; |
| 174 |
use crate::marker::{ProcMacroAutoTraits, MARKER}; |
| 175 |
#[cfg(procmacro2_semver_exempt)] |
| 176 |
use crate::rustc_literal_escaper::MixedUnit; |
| 177 |
use core::cmp::Ordering; |
| 178 |
use core::fmt::{self, Debug, Display}; |
| 179 |
use core::hash::{Hash, Hasher}; |
| 180 |
#[cfg(span_locations)] |
| 181 |
use core::ops::Range; |
| 182 |
use core::ops::RangeBounds; |
| 183 |
use core::str::FromStr; |
| 184 |
use std::error::Error; |
| 185 |
use std::ffi::CStr; |
| 186 |
#[cfg(span_locations)] |
| 187 |
use std::path::PathBuf; |
| 188 |
|
| 189 |
#[cfg(span_locations)] |
| 190 |
#[cfg_attr(docsrs, doc(cfg(feature = "span-locations")))] |
| 191 |
pub use crate::location::LineColumn; |
| 192 |
|
| 193 |
#[cfg(procmacro2_semver_exempt)] |
| 194 |
#[cfg_attr(docsrs, doc(cfg(procmacro2_semver_exempt)))] |
| 195 |
pub use crate::rustc_literal_escaper::EscapeError; |
| 196 |
|
| 197 |
/// An abstract stream of tokens, or more concretely a sequence of token trees. |
| 198 |
/// |
| 199 |
/// This type provides interfaces for iterating over token trees and for |
| 200 |
/// collecting token trees into one stream. |
| 201 |
/// |
| 202 |
/// Token stream is both the input and output of `#[proc_macro]`, |
| 203 |
/// `#[proc_macro_attribute]` and `#[proc_macro_derive]` definitions. |
| 204 |
#[derive(Clone)] |
| 205 |
pub struct TokenStream { |
| 206 |
inner: imp::TokenStream, |
| 207 |
_marker: ProcMacroAutoTraits, |
| 208 |
} |
| 209 |
|
| 210 |
/// Error returned from `TokenStream::from_str`. |
| 211 |
pub struct LexError { |
| 212 |
inner: imp::LexError, |
| 213 |
_marker: ProcMacroAutoTraits, |
| 214 |
} |
| 215 |
|
| 216 |
impl TokenStream { |
| 217 |
fn _new(inner: imp::TokenStream) -> Self { |
| 218 |
TokenStream { |
| 219 |
inner, |
| 220 |
_marker: MARKER, |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
fn _new_fallback(inner: fallback::TokenStream) -> Self { |
| 225 |
TokenStream { |
| 226 |
inner: imp::TokenStream::from(inner), |
| 227 |
_marker: MARKER, |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
/// Returns an empty `TokenStream` containing no token trees. |
| 232 |
pub fn new() -> Self { |
| 233 |
TokenStream::_new(imp::TokenStream::new()) |
| 234 |
} |
| 235 |
|
| 236 |
/// Checks if this `TokenStream` is empty. |
| 237 |
pub fn is_empty(&self) -> bool { |
| 238 |
self.inner.is_empty() |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
/// `TokenStream::default()` returns an empty stream, |
| 243 |
/// i.e. this is equivalent with `TokenStream::new()`. |
| 244 |
impl Default for TokenStream { |
| 245 |
fn default() -> Self { |
| 246 |
TokenStream::new() |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
/// Attempts to break the string into tokens and parse those tokens into a token |
| 251 |
/// stream. |
| 252 |
/// |
| 253 |
/// May fail for a number of reasons, for example, if the string contains |
| 254 |
/// unbalanced delimiters or characters not existing in the language. |
| 255 |
/// |
| 256 |
/// NOTE: Some errors may cause panics instead of returning `LexError`. We |
| 257 |
/// reserve the right to change these errors into `LexError`s later. |
| 258 |
impl FromStr for TokenStream { |
| 259 |
type Err = LexError; |
| 260 |
|
| 261 |
fn from_str(src: &str) -> Result<TokenStream, LexError> { |
| 262 |
match imp::TokenStream::from_str_checked(src) { |
| 263 |
Ok(tokens) => Ok(TokenStream::_new(tokens)), |
| 264 |
Err(lex) => Err(LexError { |
| 265 |
inner: lex, |
| 266 |
_marker: MARKER, |
| 267 |
}), |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
#[cfg(feature = "proc-macro")] |
| 273 |
#[cfg_attr(docsrs, doc(cfg(feature = "proc-macro")))] |
| 274 |
impl From<proc_macro::TokenStream> for TokenStream { |
| 275 |
fn from(inner: proc_macro::TokenStream) -> Self { |
| 276 |
TokenStream::_new(imp::TokenStream::from(inner)) |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
#[cfg(feature = "proc-macro")] |
| 281 |
#[cfg_attr(docsrs, doc(cfg(feature = "proc-macro")))] |
| 282 |
impl From<TokenStream> for proc_macro::TokenStream { |
| 283 |
fn from(inner: TokenStream) -> Self { |
| 284 |
proc_macro::TokenStream::from(inner.inner) |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
impl From<TokenTree> for TokenStream { |
| 289 |
fn from(token: TokenTree) -> Self { |
| 290 |
TokenStream::_new(imp::TokenStream::from(token)) |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
impl Extend<TokenTree> for TokenStream { |
| 295 |
fn extend<I: IntoIterator<Item = TokenTree>>(&mut self, tokens: I) { |
| 296 |
self.inner.extend(tokens); |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
impl Extend<TokenStream> for TokenStream { |
| 301 |
fn extend<I: IntoIterator<Item = TokenStream>>(&mut self, streams: I) { |
| 302 |
self.inner |
| 303 |
.extend(streams.into_iter().map(|stream| stream.inner)); |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
impl Extend<Group> for TokenStream { |
| 308 |
fn extend<I: IntoIterator<Item = Group>>(&mut self, tokens: I) { |
| 309 |
self.inner.extend(tokens.into_iter().map(TokenTree::Group)); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
impl Extend<Ident> for TokenStream { |
| 314 |
fn extend<I: IntoIterator<Item = Ident>>(&mut self, tokens: I) { |
| 315 |
self.inner.extend(tokens.into_iter().map(TokenTree::Ident)); |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
impl Extend<Punct> for TokenStream { |
| 320 |
fn extend<I: IntoIterator<Item = Punct>>(&mut self, tokens: I) { |
| 321 |
self.inner.extend(tokens.into_iter().map(TokenTree::Punct)); |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
impl Extend<Literal> for TokenStream { |
| 326 |
fn extend<I: IntoIterator<Item = Literal>>(&mut self, tokens: I) { |
| 327 |
self.inner |
| 328 |
.extend(tokens.into_iter().map(TokenTree::Literal)); |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
/// Collects a number of token trees into a single stream. |
| 333 |
impl FromIterator<TokenTree> for TokenStream { |
| 334 |
fn from_iter<I: IntoIterator<Item = TokenTree>>(tokens: I) -> Self { |
| 335 |
TokenStream::_new(tokens.into_iter().collect()) |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
impl FromIterator<TokenStream> for TokenStream { |
| 340 |
fn from_iter<I: IntoIterator<Item = TokenStream>>(streams: I) -> Self { |
| 341 |
TokenStream::_new(streams.into_iter().map(|i| i.inner).collect()) |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
/// Prints the token stream as a string that is supposed to be losslessly |
| 346 |
/// convertible back into the same token stream (modulo spans), except for |
| 347 |
/// possibly `TokenTree::Group`s with `Delimiter::None` delimiters and negative |
| 348 |
/// numeric literals. |
| 349 |
impl Display for TokenStream { |
| 350 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 351 |
Display::fmt(&self.inner, f) |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
/// Prints token in a form convenient for debugging. |
| 356 |
impl Debug for TokenStream { |
| 357 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 358 |
Debug::fmt(&self.inner, f) |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
impl LexError { |
| 363 |
pub fn span(&self) -> Span { |
| 364 |
Span::_new(self.inner.span()) |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
impl Debug for LexError { |
| 369 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 370 |
Debug::fmt(&self.inner, f) |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
impl Display for LexError { |
| 375 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 376 |
Display::fmt(&self.inner, f) |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
impl Error for LexError {} |
| 381 |
|
| 382 |
/// A region of source code, along with macro expansion information. |
| 383 |
#[derive(Copy, Clone)] |
| 384 |
pub struct Span { |
| 385 |
inner: imp::Span, |
| 386 |
_marker: ProcMacroAutoTraits, |
| 387 |
} |
| 388 |
|
| 389 |
impl Span { |
| 390 |
fn _new(inner: imp::Span) -> Self { |
| 391 |
Span { |
| 392 |
inner, |
| 393 |
_marker: MARKER, |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
fn _new_fallback(inner: fallback::Span) -> Self { |
| 398 |
Span { |
| 399 |
inner: imp::Span::from(inner), |
| 400 |
_marker: MARKER, |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
/// The span of the invocation of the current procedural macro. |
| 405 |
/// |
| 406 |
/// Identifiers created with this span will be resolved as if they were |
| 407 |
/// written directly at the macro call location (call-site hygiene) and |
| 408 |
/// other code at the macro call site will be able to refer to them as well. |
| 409 |
pub fn call_site() -> Self { |
| 410 |
Span::_new(imp::Span::call_site()) |
| 411 |
} |
| 412 |
|
| 413 |
/// The span located at the invocation of the procedural macro, but with |
| 414 |
/// local variables, labels, and `$crate` resolved at the definition site |
| 415 |
/// of the macro. This is the same hygiene behavior as `macro_rules`. |
| 416 |
pub fn mixed_site() -> Self { |
| 417 |
Span::_new(imp::Span::mixed_site()) |
| 418 |
} |
| 419 |
|
| 420 |
/// A span that resolves at the macro definition site. |
| 421 |
/// |
| 422 |
/// This method is semver exempt and not exposed by default. |
| 423 |
#[cfg(procmacro2_semver_exempt)] |
| 424 |
#[cfg_attr(docsrs, doc(cfg(procmacro2_semver_exempt)))] |
| 425 |
pub fn def_site() -> Self { |
| 426 |
Span::_new(imp::Span::def_site()) |
| 427 |
} |
| 428 |
|
| 429 |
/// Creates a new span with the same line/column information as `self` but |
| 430 |
/// that resolves symbols as though it were at `other`. |
| 431 |
pub fn resolved_at(&self, other: Span) -> Span { |
| 432 |
Span::_new(self.inner.resolved_at(other.inner)) |
| 433 |
} |
| 434 |
|
| 435 |
/// Creates a new span with the same name resolution behavior as `self` but |
| 436 |
/// with the line/column information of `other`. |
| 437 |
pub fn located_at(&self, other: Span) -> Span { |
| 438 |
Span::_new(self.inner.located_at(other.inner)) |
| 439 |
} |
| 440 |
|
| 441 |
/// Convert `proc_macro2::Span` to `proc_macro::Span`. |
| 442 |
/// |
| 443 |
/// This method is available when building with a nightly compiler, or when |
| 444 |
/// building with rustc 1.29+ *without* semver exempt features. |
| 445 |
/// |
| 446 |
/// # Panics |
| 447 |
/// |
| 448 |
/// Panics if called from outside of a procedural macro. Unlike |
| 449 |
/// `proc_macro2::Span`, the `proc_macro::Span` type can only exist within |
| 450 |
/// the context of a procedural macro invocation. |
| 451 |
#[cfg(wrap_proc_macro)] |
| 452 |
pub fn unwrap(self) -> proc_macro::Span { |
| 453 |
self.inner.unwrap() |
| 454 |
} |
| 455 |
|
| 456 |
// Soft deprecated. Please use Span::unwrap. |
| 457 |
#[cfg(wrap_proc_macro)] |
| 458 |
#[doc(hidden)] |
| 459 |
pub fn unstable(self) -> proc_macro::Span { |
| 460 |
self.unwrap() |
| 461 |
} |
| 462 |
|
| 463 |
/// Returns the span's byte position range in the source file. |
| 464 |
/// |
| 465 |
/// This method requires the `"span-locations"` feature to be enabled. |
| 466 |
/// |
| 467 |
/// When executing in a procedural macro context, the returned range is only |
| 468 |
/// accurate if compiled with a nightly toolchain. The stable toolchain does |
| 469 |
/// not have this information available. When executing outside of a |
| 470 |
/// procedural macro, such as main.rs or build.rs, the byte range is always |
| 471 |
/// accurate regardless of toolchain. |
| 472 |
#[cfg(span_locations)] |
| 473 |
#[cfg_attr(docsrs, doc(cfg(feature = "span-locations")))] |
| 474 |
pub fn byte_range(&self) -> Range<usize> { |
| 475 |
self.inner.byte_range() |
| 476 |
} |
| 477 |
|
| 478 |
/// Get the starting line/column in the source file for this span. |
| 479 |
/// |
| 480 |
/// This method requires the `"span-locations"` feature to be enabled. |
| 481 |
/// |
| 482 |
/// When executing in a procedural macro context, the returned line/column |
| 483 |
/// are only meaningful if compiled with a nightly toolchain. The stable |
| 484 |
/// toolchain does not have this information available. When executing |
| 485 |
/// outside of a procedural macro, such as main.rs or build.rs, the |
| 486 |
/// line/column are always meaningful regardless of toolchain. |
| 487 |
#[cfg(span_locations)] |
| 488 |
#[cfg_attr(docsrs, doc(cfg(feature = "span-locations")))] |
| 489 |
pub fn start(&self) -> LineColumn { |
| 490 |
self.inner.start() |
| 491 |
} |
| 492 |
|
| 493 |
/// Get the ending line/column in the source file for this span. |
| 494 |
/// |
| 495 |
/// This method requires the `"span-locations"` feature to be enabled. |
| 496 |
/// |
| 497 |
/// When executing in a procedural macro context, the returned line/column |
| 498 |
/// are only meaningful if compiled with a nightly toolchain. The stable |
| 499 |
/// toolchain does not have this information available. When executing |
| 500 |
/// outside of a procedural macro, such as main.rs or build.rs, the |
| 501 |
/// line/column are always meaningful regardless of toolchain. |
| 502 |
#[cfg(span_locations)] |
| 503 |
#[cfg_attr(docsrs, doc(cfg(feature = "span-locations")))] |
| 504 |
pub fn end(&self) -> LineColumn { |
| 505 |
self.inner.end() |
| 506 |
} |
| 507 |
|
| 508 |
/// The path to the source file in which this span occurs, for display |
| 509 |
/// purposes. |
| 510 |
/// |
| 511 |
/// This might not correspond to a valid file system path. It might be |
| 512 |
/// remapped, or might be an artificial path such as `"<macro expansion>"`. |
| 513 |
#[cfg(span_locations)] |
| 514 |
#[cfg_attr(docsrs, doc(cfg(feature = "span-locations")))] |
| 515 |
pub fn file(&self) -> String { |
| 516 |
self.inner.file() |
| 517 |
} |
| 518 |
|
| 519 |
/// The path to the source file in which this span occurs on disk. |
| 520 |
/// |
| 521 |
/// This is the actual path on disk. It is unaffected by path remapping. |
| 522 |
/// |
| 523 |
/// This path should not be embedded in the output of the macro; prefer |
| 524 |
/// `file()` instead. |
| 525 |
#[cfg(span_locations)] |
| 526 |
#[cfg_attr(docsrs, doc(cfg(feature = "span-locations")))] |
| 527 |
pub fn local_file(&self) -> Option<PathBuf> { |
| 528 |
self.inner.local_file() |
| 529 |
} |
| 530 |
|
| 531 |
/// Create a new span encompassing `self` and `other`. |
| 532 |
/// |
| 533 |
/// Returns `None` if `self` and `other` are from different files. |
| 534 |
/// |
| 535 |
/// Warning: the underlying [`proc_macro::Span::join`] method is |
| 536 |
/// nightly-only. When called from within a procedural macro not using a |
| 537 |
/// nightly compiler, this method will always return `None`. |
| 538 |
pub fn join(&self, other: Span) -> Option<Span> { |
| 539 |
self.inner.join(other.inner).map(Span::_new) |
| 540 |
} |
| 541 |
|
| 542 |
/// Compares two spans to see if they're equal. |
| 543 |
/// |
| 544 |
/// This method is semver exempt and not exposed by default. |
| 545 |
#[cfg(procmacro2_semver_exempt)] |
| 546 |
#[cfg_attr(docsrs, doc(cfg(procmacro2_semver_exempt)))] |
| 547 |
pub fn eq(&self, other: &Span) -> bool { |
| 548 |
self.inner.eq(&other.inner) |
| 549 |
} |
| 550 |
|
| 551 |
/// Returns the source text behind a span. This preserves the original |
| 552 |
/// source code, including spaces and comments. It only returns a result if |
| 553 |
/// the span corresponds to real source code. |
| 554 |
/// |
| 555 |
/// Note: The observable result of a macro should only rely on the tokens |
| 556 |
/// and not on this source text. The result of this function is a best |
| 557 |
/// effort to be used for diagnostics only. |
| 558 |
pub fn source_text(&self) -> Option<String> { |
| 559 |
self.inner.source_text() |
| 560 |
} |
| 561 |
} |
| 562 |
|
| 563 |
/// Prints a span in a form convenient for debugging. |
| 564 |
impl Debug for Span { |
| 565 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 566 |
Debug::fmt(&self.inner, f) |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
/// A single token or a delimited sequence of token trees (e.g. `[1, (), ..]`). |
| 571 |
#[derive(Clone)] |
| 572 |
pub enum TokenTree { |
| 573 |
/// A token stream surrounded by bracket delimiters. |
| 574 |
Group(Group), |
| 575 |
/// An identifier. |
| 576 |
Ident(Ident), |
| 577 |
/// A single punctuation character (`+`, `,`, `$`, etc.). |
| 578 |
Punct(Punct), |
| 579 |
/// A literal character (`'a'`), string (`"hello"`), number (`2.3`), etc. |
| 580 |
Literal(Literal), |
| 581 |
} |
| 582 |
|
| 583 |
impl TokenTree { |
| 584 |
/// Returns the span of this tree, delegating to the `span` method of |
| 585 |
/// the contained token or a delimited stream. |
| 586 |
pub fn span(&self) -> Span { |
| 587 |
match self { |
| 588 |
TokenTree::Group(t) => t.span(), |
| 589 |
TokenTree::Ident(t) => t.span(), |
| 590 |
TokenTree::Punct(t) => t.span(), |
| 591 |
TokenTree::Literal(t) => t.span(), |
| 592 |
} |
| 593 |
} |
| 594 |
|
| 595 |
/// Configures the span for *only this token*. |
| 596 |
/// |
| 597 |
/// Note that if this token is a `Group` then this method will not configure |
| 598 |
/// the span of each of the internal tokens, this will simply delegate to |
| 599 |
/// the `set_span` method of each variant. |
| 600 |
pub fn set_span(&mut self, span: Span) { |
| 601 |
match self { |
| 602 |
TokenTree::Group(t) => t.set_span(span), |
| 603 |
TokenTree::Ident(t) => t.set_span(span), |
| 604 |
TokenTree::Punct(t) => t.set_span(span), |
| 605 |
TokenTree::Literal(t) => t.set_span(span), |
| 606 |
} |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
impl From<Group> for TokenTree { |
| 611 |
fn from(g: Group) -> Self { |
| 612 |
TokenTree::Group(g) |
| 613 |
} |
| 614 |
} |
| 615 |
|
| 616 |
impl From<Ident> for TokenTree { |
| 617 |
fn from(g: Ident) -> Self { |
| 618 |
TokenTree::Ident(g) |
| 619 |
} |
| 620 |
} |
| 621 |
|
| 622 |
impl From<Punct> for TokenTree { |
| 623 |
fn from(g: Punct) -> Self { |
| 624 |
TokenTree::Punct(g) |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
impl From<Literal> for TokenTree { |
| 629 |
fn from(g: Literal) -> Self { |
| 630 |
TokenTree::Literal(g) |
| 631 |
} |
| 632 |
} |
| 633 |
|
| 634 |
/// Prints the token tree as a string that is supposed to be losslessly |
| 635 |
/// convertible back into the same token tree (modulo spans), except for |
| 636 |
/// possibly `TokenTree::Group`s with `Delimiter::None` delimiters and negative |
| 637 |
/// numeric literals. |
| 638 |
impl Display for TokenTree { |
| 639 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 640 |
match self { |
| 641 |
TokenTree::Group(t) => Display::fmt(t, f), |
| 642 |
TokenTree::Ident(t) => Display::fmt(t, f), |
| 643 |
TokenTree::Punct(t) => Display::fmt(t, f), |
| 644 |
TokenTree::Literal(t) => Display::fmt(t, f), |
| 645 |
} |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
/// Prints token tree in a form convenient for debugging. |
| 650 |
impl Debug for TokenTree { |
| 651 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 652 |
// Each of these has the name in the struct type in the derived debug, |
| 653 |
// so don't bother with an extra layer of indirection |
| 654 |
match self { |
| 655 |
TokenTree::Group(t) => Debug::fmt(t, f), |
| 656 |
TokenTree::Ident(t) => { |
| 657 |
let mut debug = f.debug_struct("Ident"); |
| 658 |
debug.field("sym", &format_args!("{}", t)); |
| 659 |
imp::debug_span_field_if_nontrivial(&mut debug, t.span().inner); |
| 660 |
debug.finish() |
| 661 |
} |
| 662 |
TokenTree::Punct(t) => Debug::fmt(t, f), |
| 663 |
TokenTree::Literal(t) => Debug::fmt(t, f), |
| 664 |
} |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
/// A delimited token stream. |
| 669 |
/// |
| 670 |
/// A `Group` internally contains a `TokenStream` which is surrounded by |
| 671 |
/// `Delimiter`s. |
| 672 |
#[derive(Clone)] |
| 673 |
pub struct Group { |
| 674 |
inner: imp::Group, |
| 675 |
} |
| 676 |
|
| 677 |
/// Describes how a sequence of token trees is delimited. |
| 678 |
#[derive(Copy, Clone, Debug, Eq, PartialEq)] |
| 679 |
pub enum Delimiter { |
| 680 |
/// `( ... )` |
| 681 |
Parenthesis, |
| 682 |
/// `{ ... }` |
| 683 |
Brace, |
| 684 |
/// `[ ... ]` |
| 685 |
Bracket, |
| 686 |
/// `∅ ... ∅` |
| 687 |
/// |
| 688 |
/// An invisible delimiter, that may, for example, appear around tokens |
| 689 |
/// coming from a "macro variable" `$var`. It is important to preserve |
| 690 |
/// operator priorities in cases like `$var * 3` where `$var` is `1 + 2`. |
| 691 |
/// Invisible delimiters may not survive roundtrip of a token stream through |
| 692 |
/// a string. |
| 693 |
/// |
| 694 |
/// <div class="warning"> |
| 695 |
/// |
| 696 |
/// Note: rustc currently can ignore the grouping of tokens delimited by `None` in the output |
| 697 |
/// of a proc_macro. Only `None`-delimited groups created by a macro_rules macro in the input |
| 698 |
/// of a proc_macro macro are preserved, and only in very specific circumstances. |
| 699 |
/// Any `None`-delimited groups (re)created by a proc_macro will therefore not preserve |
| 700 |
/// operator priorities as indicated above. The other `Delimiter` variants should be used |
| 701 |
/// instead in this context. This is a rustc bug. For details, see |
| 702 |
/// [rust-lang/rust#67062](https://github.com/rust-lang/rust/issues/67062). |
| 703 |
/// |
| 704 |
/// </div> |
| 705 |
None, |
| 706 |
} |
| 707 |
|
| 708 |
impl Group { |
| 709 |
fn _new(inner: imp::Group) -> Self { |
| 710 |
Group { inner } |
| 711 |
} |
| 712 |
|
| 713 |
fn _new_fallback(inner: fallback::Group) -> Self { |
| 714 |
Group { |
| 715 |
inner: imp::Group::from(inner), |
| 716 |
} |
| 717 |
} |
| 718 |
|
| 719 |
/// Creates a new `Group` with the given delimiter and token stream. |
| 720 |
/// |
| 721 |
/// This constructor will set the span for this group to |
| 722 |
/// `Span::call_site()`. To change the span you can use the `set_span` |
| 723 |
/// method below. |
| 724 |
pub fn new(delimiter: Delimiter, stream: TokenStream) -> Self { |
| 725 |
Group { |
| 726 |
inner: imp::Group::new(delimiter, stream.inner), |
| 727 |
} |
| 728 |
} |
| 729 |
|
| 730 |
/// Returns the punctuation used as the delimiter for this group: a set of |
| 731 |
/// parentheses, square brackets, or curly braces. |
| 732 |
pub fn delimiter(&self) -> Delimiter { |
| 733 |
self.inner.delimiter() |
| 734 |
} |
| 735 |
|
| 736 |
/// Returns the `TokenStream` of tokens that are delimited in this `Group`. |
| 737 |
/// |
| 738 |
/// Note that the returned token stream does not include the delimiter |
| 739 |
/// returned above. |
| 740 |
pub fn stream(&self) -> TokenStream { |
| 741 |
TokenStream::_new(self.inner.stream()) |
| 742 |
} |
| 743 |
|
| 744 |
/// Returns the span for the delimiters of this token stream, spanning the |
| 745 |
/// entire `Group`. |
| 746 |
/// |
| 747 |
/// ```text |
| 748 |
/// pub fn span(&self) -> Span { |
| 749 |
/// ^^^^^^^ |
| 750 |
/// ``` |
| 751 |
pub fn span(&self) -> Span { |
| 752 |
Span::_new(self.inner.span()) |
| 753 |
} |
| 754 |
|
| 755 |
/// Returns the span pointing to the opening delimiter of this group. |
| 756 |
/// |
| 757 |
/// ```text |
| 758 |
/// pub fn span_open(&self) -> Span { |
| 759 |
/// ^ |
| 760 |
/// ``` |
| 761 |
pub fn span_open(&self) -> Span { |
| 762 |
Span::_new(self.inner.span_open()) |
| 763 |
} |
| 764 |
|
| 765 |
/// Returns the span pointing to the closing delimiter of this group. |
| 766 |
/// |
| 767 |
/// ```text |
| 768 |
/// pub fn span_close(&self) -> Span { |
| 769 |
/// ^ |
| 770 |
/// ``` |
| 771 |
pub fn span_close(&self) -> Span { |
| 772 |
Span::_new(self.inner.span_close()) |
| 773 |
} |
| 774 |
|
| 775 |
/// Returns an object that holds this group's `span_open()` and |
| 776 |
/// `span_close()` together (in a more compact representation than holding |
| 777 |
/// those 2 spans individually). |
| 778 |
pub fn delim_span(&self) -> DelimSpan { |
| 779 |
DelimSpan::new(&self.inner) |
| 780 |
} |
| 781 |
|
| 782 |
/// Configures the span for this `Group`'s delimiters, but not its internal |
| 783 |
/// tokens. |
| 784 |
/// |
| 785 |
/// This method will **not** set the span of all the internal tokens spanned |
| 786 |
/// by this group, but rather it will only set the span of the delimiter |
| 787 |
/// tokens at the level of the `Group`. |
| 788 |
pub fn set_span(&mut self, span: Span) { |
| 789 |
self.inner.set_span(span.inner); |
| 790 |
} |
| 791 |
} |
| 792 |
|
| 793 |
/// Prints the group as a string that should be losslessly convertible back |
| 794 |
/// into the same group (modulo spans), except for possibly `TokenTree::Group`s |
| 795 |
/// with `Delimiter::None` delimiters. |
| 796 |
impl Display for Group { |
| 797 |
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 798 |
Display::fmt(&self.inner, formatter) |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
impl Debug for Group { |
| 803 |
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 804 |
Debug::fmt(&self.inner, formatter) |
| 805 |
} |
| 806 |
} |
| 807 |
|
| 808 |
/// A `Punct` is a single punctuation character like `+`, `-` or `#`. |
| 809 |
/// |
| 810 |
/// Multicharacter operators like `+=` are represented as two instances of |
| 811 |
/// `Punct` with different forms of `Spacing` returned. |
| 812 |
#[derive(Clone)] |
| 813 |
pub struct Punct { |
| 814 |
ch: char, |
| 815 |
spacing: Spacing, |
| 816 |
span: Span, |
| 817 |
} |
| 818 |
|
| 819 |
/// Whether a `Punct` is followed immediately by another `Punct` or followed by |
| 820 |
/// another token or whitespace. |
| 821 |
#[derive(Copy, Clone, Debug, Eq, PartialEq)] |
| 822 |
pub enum Spacing { |
| 823 |
/// E.g. `+` is `Alone` in `+ =`, `+ident` or `+()`. |
| 824 |
Alone, |
| 825 |
/// E.g. `+` is `Joint` in `+=` or `'` is `Joint` in `'#`. |
| 826 |
/// |
| 827 |
/// Additionally, single quote `'` can join with identifiers to form |
| 828 |
/// lifetimes `'ident`. |
| 829 |
Joint, |
| 830 |
} |
| 831 |
|
| 832 |
impl Punct { |
| 833 |
/// Creates a new `Punct` from the given character and spacing. |
| 834 |
/// |
| 835 |
/// The `ch` argument must be a valid punctuation character permitted by the |
| 836 |
/// language, otherwise the function will panic. |
| 837 |
/// |
| 838 |
/// The returned `Punct` will have the default span of `Span::call_site()` |
| 839 |
/// which can be further configured with the `set_span` method below. |
| 840 |
pub fn new(ch: char, spacing: Spacing) -> Self { |
| 841 |
if let '!' | '#' | '$' | '%' | '&' | '\'' | '*' | '+' | ',' | '-' | '.' | '/' | ':' | ';' |
| 842 |
| '<' | '=' | '>' | '?' | '@' | '^' | '|' | '~' = ch |
| 843 |
{ |
| 844 |
Punct { |
| 845 |
ch, |
| 846 |
spacing, |
| 847 |
span: Span::call_site(), |
| 848 |
} |
| 849 |
} else { |
| 850 |
panic!("unsupported proc macro punctuation character {:?}", ch); |
| 851 |
} |
| 852 |
} |
| 853 |
|
| 854 |
/// Returns the value of this punctuation character as `char`. |
| 855 |
pub fn as_char(&self) -> char { |
| 856 |
self.ch |
| 857 |
} |
| 858 |
|
| 859 |
/// Returns the spacing of this punctuation character, indicating whether |
| 860 |
/// it's immediately followed by another `Punct` in the token stream, so |
| 861 |
/// they can potentially be combined into a multicharacter operator |
| 862 |
/// (`Joint`), or it's followed by some other token or whitespace (`Alone`) |
| 863 |
/// so the operator has certainly ended. |
| 864 |
pub fn spacing(&self) -> Spacing { |
| 865 |
self.spacing |
| 866 |
} |
| 867 |
|
| 868 |
/// Returns the span for this punctuation character. |
| 869 |
pub fn span(&self) -> Span { |
| 870 |
self.span |
| 871 |
} |
| 872 |
|
| 873 |
/// Configure the span for this punctuation character. |
| 874 |
pub fn set_span(&mut self, span: Span) { |
| 875 |
self.span = span; |
| 876 |
} |
| 877 |
} |
| 878 |
|
| 879 |
/// Prints the punctuation character as a string that should be losslessly |
| 880 |
/// convertible back into the same character. |
| 881 |
impl Display for Punct { |
| 882 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 883 |
Display::fmt(&self.ch, f) |
| 884 |
} |
| 885 |
} |
| 886 |
|
| 887 |
impl Debug for Punct { |
| 888 |
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
| 889 |
let mut debug = fmt.debug_struct("Punct"); |
| 890 |
debug.field("char", &self.ch); |
| 891 |
debug.field("spacing", &self.spacing); |
| 892 |
imp::debug_span_field_if_nontrivial(&mut debug, self.span.inner); |
| 893 |
debug.finish() |
| 894 |
} |
| 895 |
} |
| 896 |
|
| 897 |
/// A word of Rust code, which may be a keyword or legal variable name. |
| 898 |
/// |
| 899 |
/// An identifier consists of at least one Unicode code point, the first of |
| 900 |
/// which has the XID_Start property and the rest of which have the XID_Continue |
| 901 |
/// property. |
| 902 |
/// |
| 903 |
/// - The empty string is not an identifier. Use `Option<Ident>`. |
| 904 |
/// - A lifetime is not an identifier. Use `syn::Lifetime` instead. |
| 905 |
/// |
| 906 |
/// An identifier constructed with `Ident::new` is permitted to be a Rust |
| 907 |
/// keyword, though parsing one through its [`Parse`] implementation rejects |
| 908 |
/// Rust keywords. Use `input.call(Ident::parse_any)` when parsing to match the |
| 909 |
/// behaviour of `Ident::new`. |
| 910 |
/// |
| 911 |
/// [`Parse`]: https://docs.rs/syn/2.0/syn/parse/trait.Parse.html |
| 912 |
/// |
| 913 |
/// # Examples |
| 914 |
/// |
| 915 |
/// A new ident can be created from a string using the `Ident::new` function. |
| 916 |
/// A span must be provided explicitly which governs the name resolution |
| 917 |
/// behavior of the resulting identifier. |
| 918 |
/// |
| 919 |
/// ``` |
| 920 |
/// use proc_macro2::{Ident, Span}; |
| 921 |
/// |
| 922 |
/// fn main() { |
| 923 |
/// let call_ident = Ident::new("calligraphy", Span::call_site()); |
| 924 |
/// |
| 925 |
/// println!("{}", call_ident); |
| 926 |
/// } |
| 927 |
/// ``` |
| 928 |
/// |
| 929 |
/// An ident can be interpolated into a token stream using the `quote!` macro. |
| 930 |
/// |
| 931 |
/// ``` |
| 932 |
/// use proc_macro2::{Ident, Span}; |
| 933 |
/// use quote::quote; |
| 934 |
/// |
| 935 |
/// fn main() { |
| 936 |
/// let ident = Ident::new("demo", Span::call_site()); |
| 937 |
/// |
| 938 |
/// // Create a variable binding whose name is this ident. |
| 939 |
/// let expanded = quote! { let #ident = 10; }; |
| 940 |
/// |
| 941 |
/// // Create a variable binding with a slightly different name. |
| 942 |
/// let temp_ident = Ident::new(&format!("new_{}", ident), Span::call_site()); |
| 943 |
/// let expanded = quote! { let #temp_ident = 10; }; |
| 944 |
/// } |
| 945 |
/// ``` |
| 946 |
/// |
| 947 |
/// A string representation of the ident is available through the `to_string()` |
| 948 |
/// method. |
| 949 |
/// |
| 950 |
/// ``` |
| 951 |
/// # use proc_macro2::{Ident, Span}; |
| 952 |
/// # |
| 953 |
/// # let ident = Ident::new("another_identifier", Span::call_site()); |
| 954 |
/// # |
| 955 |
/// // Examine the ident as a string. |
| 956 |
/// let ident_string = ident.to_string(); |
| 957 |
/// if ident_string.len() > 60 { |
| 958 |
/// println!("Very long identifier: {}", ident_string) |
| 959 |
/// } |
| 960 |
/// ``` |
| 961 |
#[derive(Clone)] |
| 962 |
pub struct Ident { |
| 963 |
inner: imp::Ident, |
| 964 |
_marker: ProcMacroAutoTraits, |
| 965 |
} |
| 966 |
|
| 967 |
impl Ident { |
| 968 |
fn _new(inner: imp::Ident) -> Self { |
| 969 |
Ident { |
| 970 |
inner, |
| 971 |
_marker: MARKER, |
| 972 |
} |
| 973 |
} |
| 974 |
|
| 975 |
fn _new_fallback(inner: fallback::Ident) -> Self { |
| 976 |
Ident { |
| 977 |
inner: imp::Ident::from(inner), |
| 978 |
_marker: MARKER, |
| 979 |
} |
| 980 |
} |
| 981 |
|
| 982 |
/// Creates a new `Ident` with the given `string` as well as the specified |
| 983 |
/// `span`. |
| 984 |
/// |
| 985 |
/// The `string` argument must be a valid identifier permitted by the |
| 986 |
/// language, otherwise the function will panic. |
| 987 |
/// |
| 988 |
/// Note that `span`, currently in rustc, configures the hygiene information |
| 989 |
/// for this identifier. |
| 990 |
/// |
| 991 |
/// As of this time `Span::call_site()` explicitly opts-in to "call-site" |
| 992 |
/// hygiene meaning that identifiers created with this span will be resolved |
| 993 |
/// as if they were written directly at the location of the macro call, and |
| 994 |
/// other code at the macro call site will be able to refer to them as well. |
| 995 |
/// |
| 996 |
/// Later spans like `Span::def_site()` will allow to opt-in to |
| 997 |
/// "definition-site" hygiene meaning that identifiers created with this |
| 998 |
/// span will be resolved at the location of the macro definition and other |
| 999 |
/// code at the macro call site will not be able to refer to them. |
| 1000 |
/// |
| 1001 |
/// Due to the current importance of hygiene this constructor, unlike other |
| 1002 |
/// tokens, requires a `Span` to be specified at construction. |
| 1003 |
/// |
| 1004 |
/// # Panics |
| 1005 |
/// |
| 1006 |
/// Panics if the input string is neither a keyword nor a legal variable |
| 1007 |
/// name. If you are not sure whether the string contains an identifier and |
| 1008 |
/// need to handle an error case, use |
| 1009 |
/// <a href="https://docs.rs/syn/2.0/syn/fn.parse_str.html"><code |
| 1010 |
/// style="padding-right:0;">syn::parse_str</code></a><code |
| 1011 |
/// style="padding-left:0;">::<Ident></code> |
| 1012 |
/// rather than `Ident::new`. |
| 1013 |
#[track_caller] |
| 1014 |
pub fn new(string: &str, span: Span) -> Self { |
| 1015 |
Ident::_new(imp::Ident::new_checked(string, span.inner)) |
| 1016 |
} |
| 1017 |
|
| 1018 |
/// Same as `Ident::new`, but creates a raw identifier (`r#ident`). The |
| 1019 |
/// `string` argument must be a valid identifier permitted by the language |
| 1020 |
/// (including keywords, e.g. `fn`). Keywords which are usable in path |
| 1021 |
/// segments (e.g. `self`, `super`) are not supported, and will cause a |
| 1022 |
/// panic. |
| 1023 |
#[track_caller] |
| 1024 |
pub fn new_raw(string: &str, span: Span) -> Self { |
| 1025 |
Ident::_new(imp::Ident::new_raw_checked(string, span.inner)) |
| 1026 |
} |
| 1027 |
|
| 1028 |
/// Returns the span of this `Ident`. |
| 1029 |
pub fn span(&self) -> Span { |
| 1030 |
Span::_new(self.inner.span()) |
| 1031 |
} |
| 1032 |
|
| 1033 |
/// Configures the span of this `Ident`, possibly changing its hygiene |
| 1034 |
/// context. |
| 1035 |
pub fn set_span(&mut self, span: Span) { |
| 1036 |
self.inner.set_span(span.inner); |
| 1037 |
} |
| 1038 |
} |
| 1039 |
|
| 1040 |
impl PartialEq for Ident { |
| 1041 |
fn eq(&self, other: &Ident) -> bool { |
| 1042 |
self.inner == other.inner |
| 1043 |
} |
| 1044 |
} |
| 1045 |
|
| 1046 |
impl<T> PartialEq<T> for Ident |
| 1047 |
where |
| 1048 |
T: ?Sized + AsRef<str>, |
| 1049 |
{ |
| 1050 |
fn eq(&self, other: &T) -> bool { |
| 1051 |
self.inner == other |
| 1052 |
} |
| 1053 |
} |
| 1054 |
|
| 1055 |
impl Eq for Ident {} |
| 1056 |
|
| 1057 |
impl PartialOrd for Ident { |
| 1058 |
fn partial_cmp(&self, other: &Ident) -> Option<Ordering> { |
| 1059 |
Some(self.cmp(other)) |
| 1060 |
} |
| 1061 |
} |
| 1062 |
|
| 1063 |
impl Ord for Ident { |
| 1064 |
fn cmp(&self, other: &Ident) -> Ordering { |
| 1065 |
self.to_string().cmp(&other.to_string()) |
| 1066 |
} |
| 1067 |
} |
| 1068 |
|
| 1069 |
impl Hash for Ident { |
| 1070 |
fn hash<H: Hasher>(&self, hasher: &mut H) { |
| 1071 |
self.to_string().hash(hasher); |
| 1072 |
} |
| 1073 |
} |
| 1074 |
|
| 1075 |
/// Prints the identifier as a string that should be losslessly convertible back |
| 1076 |
/// into the same identifier. |
| 1077 |
impl Display for Ident { |
| 1078 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 1079 |
Display::fmt(&self.inner, f) |
| 1080 |
} |
| 1081 |
} |
| 1082 |
|
| 1083 |
impl Debug for Ident { |
| 1084 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 1085 |
Debug::fmt(&self.inner, f) |
| 1086 |
} |
| 1087 |
} |
| 1088 |
|
| 1089 |
/// A literal string (`"hello"`), byte string (`b"hello"`), character (`'a'`), |
| 1090 |
/// byte character (`b'a'`), an integer or floating point number with or without |
| 1091 |
/// a suffix (`1`, `1u8`, `2.3`, `2.3f32`). |
| 1092 |
/// |
| 1093 |
/// Boolean literals like `true` and `false` do not belong here, they are |
| 1094 |
/// `Ident`s. |
| 1095 |
#[derive(Clone)] |
| 1096 |
pub struct Literal { |
| 1097 |
inner: imp::Literal, |
| 1098 |
_marker: ProcMacroAutoTraits, |
| 1099 |
} |
| 1100 |
|
| 1101 |
macro_rules! suffixed_int_literals { |
| 1102 |
($($name:ident => $kind:ident,)*) => ($( |
| 1103 |
/// Creates a new suffixed integer literal with the specified value. |
| 1104 |
/// |
| 1105 |
/// This function will create an integer like `1u32` where the integer |
| 1106 |
/// value specified is the first part of the token and the integral is |
| 1107 |
/// also suffixed at the end. Literals created from negative numbers may |
| 1108 |
/// not survive roundtrips through `TokenStream` or strings and may be |
| 1109 |
/// broken into two tokens (`-` and positive literal). |
| 1110 |
/// |
| 1111 |
/// Literals created through this method have the `Span::call_site()` |
| 1112 |
/// span by default, which can be configured with the `set_span` method |
| 1113 |
/// below. |
| 1114 |
pub fn $name(n: $kind) -> Literal { |
| 1115 |
Literal::_new(imp::Literal::$name(n)) |
| 1116 |
} |
| 1117 |
)*) |
| 1118 |
} |
| 1119 |
|
| 1120 |
macro_rules! unsuffixed_int_literals { |
| 1121 |
($($name:ident => $kind:ident,)*) => ($( |
| 1122 |
/// Creates a new unsuffixed integer literal with the specified value. |
| 1123 |
/// |
| 1124 |
/// This function will create an integer like `1` where the integer |
| 1125 |
/// value specified is the first part of the token. No suffix is |
| 1126 |
/// specified on this token, meaning that invocations like |
| 1127 |
/// `Literal::i8_unsuffixed(1)` are equivalent to |
| 1128 |
/// `Literal::u32_unsuffixed(1)`. Literals created from negative numbers |
| 1129 |
/// may not survive roundtrips through `TokenStream` or strings and may |
| 1130 |
/// be broken into two tokens (`-` and positive literal). |
| 1131 |
/// |
| 1132 |
/// Literals created through this method have the `Span::call_site()` |
| 1133 |
/// span by default, which can be configured with the `set_span` method |
| 1134 |
/// below. |
| 1135 |
pub fn $name(n: $kind) -> Literal { |
| 1136 |
Literal::_new(imp::Literal::$name(n)) |
| 1137 |
} |
| 1138 |
)*) |
| 1139 |
} |
| 1140 |
|
| 1141 |
impl Literal { |
| 1142 |
fn _new(inner: imp::Literal) -> Self { |
| 1143 |
Literal { |
| 1144 |
inner, |
| 1145 |
_marker: MARKER, |
| 1146 |
} |
| 1147 |
} |
| 1148 |
|
| 1149 |
fn _new_fallback(inner: fallback::Literal) -> Self { |
| 1150 |
Literal { |
| 1151 |
inner: imp::Literal::from(inner), |
| 1152 |
_marker: MARKER, |
| 1153 |
} |
| 1154 |
} |
| 1155 |
|
| 1156 |
suffixed_int_literals! { |
| 1157 |
u8_suffixed => u8, |
| 1158 |
u16_suffixed => u16, |
| 1159 |
u32_suffixed => u32, |
| 1160 |
u64_suffixed => u64, |
| 1161 |
u128_suffixed => u128, |
| 1162 |
usize_suffixed => usize, |
| 1163 |
i8_suffixed => i8, |
| 1164 |
i16_suffixed => i16, |
| 1165 |
i32_suffixed => i32, |
| 1166 |
i64_suffixed => i64, |
| 1167 |
i128_suffixed => i128, |
| 1168 |
isize_suffixed => isize, |
| 1169 |
} |
| 1170 |
|
| 1171 |
unsuffixed_int_literals! { |
| 1172 |
u8_unsuffixed => u8, |
| 1173 |
u16_unsuffixed => u16, |
| 1174 |
u32_unsuffixed => u32, |
| 1175 |
u64_unsuffixed => u64, |
| 1176 |
u128_unsuffixed => u128, |
| 1177 |
usize_unsuffixed => usize, |
| 1178 |
i8_unsuffixed => i8, |
| 1179 |
i16_unsuffixed => i16, |
| 1180 |
i32_unsuffixed => i32, |
| 1181 |
i64_unsuffixed => i64, |
| 1182 |
i128_unsuffixed => i128, |
| 1183 |
isize_unsuffixed => isize, |
| 1184 |
} |
| 1185 |
|
| 1186 |
/// Creates a new unsuffixed floating-point literal. |
| 1187 |
/// |
| 1188 |
/// This constructor is similar to those like `Literal::i8_unsuffixed` where |
| 1189 |
/// the float's value is emitted directly into the token but no suffix is |
| 1190 |
/// used, so it may be inferred to be a `f64` later in the compiler. |
| 1191 |
/// Literals created from negative numbers may not survive round-trips |
| 1192 |
/// through `TokenStream` or strings and may be broken into two tokens (`-` |
| 1193 |
/// and positive literal). |
| 1194 |
/// |
| 1195 |
/// # Panics |
| 1196 |
/// |
| 1197 |
/// This function requires that the specified float is finite, for example |
| 1198 |
/// if it is infinity or NaN this function will panic. |
| 1199 |
pub fn f64_unsuffixed(f: f64) -> Literal { |
| 1200 |
assert!(f.is_finite()); |
| 1201 |
Literal::_new(imp::Literal::f64_unsuffixed(f)) |
| 1202 |
} |
| 1203 |
|
| 1204 |
/// Creates a new suffixed floating-point literal. |
| 1205 |
/// |
| 1206 |
/// This constructor will create a literal like `1.0f64` where the value |
| 1207 |
/// specified is the preceding part of the token and `f64` is the suffix of |
| 1208 |
/// the token. This token will always be inferred to be an `f64` in the |
| 1209 |
/// compiler. Literals created from negative numbers may not survive |
| 1210 |
/// round-trips through `TokenStream` or strings and may be broken into two |
| 1211 |
/// tokens (`-` and positive literal). |
| 1212 |
/// |
| 1213 |
/// # Panics |
| 1214 |
/// |
| 1215 |
/// This function requires that the specified float is finite, for example |
| 1216 |
/// if it is infinity or NaN this function will panic. |
| 1217 |
pub fn f64_suffixed(f: f64) -> Literal { |
| 1218 |
assert!(f.is_finite()); |
| 1219 |
Literal::_new(imp::Literal::f64_suffixed(f)) |
| 1220 |
} |
| 1221 |
|
| 1222 |
/// Creates a new unsuffixed floating-point literal. |
| 1223 |
/// |
| 1224 |
/// This constructor is similar to those like `Literal::i8_unsuffixed` where |
| 1225 |
/// the float's value is emitted directly into the token but no suffix is |
| 1226 |
/// used, so it may be inferred to be a `f64` later in the compiler. |
| 1227 |
/// Literals created from negative numbers may not survive round-trips |
| 1228 |
/// through `TokenStream` or strings and may be broken into two tokens (`-` |
| 1229 |
/// and positive literal). |
| 1230 |
/// |
| 1231 |
/// # Panics |
| 1232 |
/// |
| 1233 |
/// This function requires that the specified float is finite, for example |
| 1234 |
/// if it is infinity or NaN this function will panic. |
| 1235 |
pub fn f32_unsuffixed(f: f32) -> Literal { |
| 1236 |
assert!(f.is_finite()); |
| 1237 |
Literal::_new(imp::Literal::f32_unsuffixed(f)) |
| 1238 |
} |
| 1239 |
|
| 1240 |
/// Creates a new suffixed floating-point literal. |
| 1241 |
/// |
| 1242 |
/// This constructor will create a literal like `1.0f32` where the value |
| 1243 |
/// specified is the preceding part of the token and `f32` is the suffix of |
| 1244 |
/// the token. This token will always be inferred to be an `f32` in the |
| 1245 |
/// compiler. Literals created from negative numbers may not survive |
| 1246 |
/// round-trips through `TokenStream` or strings and may be broken into two |
| 1247 |
/// tokens (`-` and positive literal). |
| 1248 |
/// |
| 1249 |
/// # Panics |
| 1250 |
/// |
| 1251 |
/// This function requires that the specified float is finite, for example |
| 1252 |
/// if it is infinity or NaN this function will panic. |
| 1253 |
pub fn f32_suffixed(f: f32) -> Literal { |
| 1254 |
assert!(f.is_finite()); |
| 1255 |
Literal::_new(imp::Literal::f32_suffixed(f)) |
| 1256 |
} |
| 1257 |
|
| 1258 |
/// String literal. |
| 1259 |
pub fn string(string: &str) -> Literal { |
| 1260 |
Literal::_new(imp::Literal::string(string)) |
| 1261 |
} |
| 1262 |
|
| 1263 |
/// Character literal. |
| 1264 |
pub fn character(ch: char) -> Literal { |
| 1265 |
Literal::_new(imp::Literal::character(ch)) |
| 1266 |
} |
| 1267 |
|
| 1268 |
/// Byte character literal. |
| 1269 |
pub fn byte_character(byte: u8) -> Literal { |
| 1270 |
Literal::_new(imp::Literal::byte_character(byte)) |
| 1271 |
} |
| 1272 |
|
| 1273 |
/// Byte string literal. |
| 1274 |
pub fn byte_string(bytes: &[u8]) -> Literal { |
| 1275 |
Literal::_new(imp::Literal::byte_string(bytes)) |
| 1276 |
} |
| 1277 |
|
| 1278 |
/// C string literal. |
| 1279 |
pub fn c_string(string: &CStr) -> Literal { |
| 1280 |
Literal::_new(imp::Literal::c_string(string)) |
| 1281 |
} |
| 1282 |
|
| 1283 |
/// Returns the span encompassing this literal. |
| 1284 |
pub fn span(&self) -> Span { |
| 1285 |
Span::_new(self.inner.span()) |
| 1286 |
} |
| 1287 |
|
| 1288 |
/// Configures the span associated for this literal. |
| 1289 |
pub fn set_span(&mut self, span: Span) { |
| 1290 |
self.inner.set_span(span.inner); |
| 1291 |
} |
| 1292 |
|
| 1293 |
/// Returns a `Span` that is a subset of `self.span()` containing only |
| 1294 |
/// the source bytes in range `range`. Returns `None` if the would-be |
| 1295 |
/// trimmed span is outside the bounds of `self`. |
| 1296 |
/// |
| 1297 |
/// Warning: the underlying [`proc_macro::Literal::subspan`] method is |
| 1298 |
/// nightly-only. When called from within a procedural macro not using a |
| 1299 |
/// nightly compiler, this method will always return `None`. |
| 1300 |
pub fn subspan<R: RangeBounds<usize>>(&self, range: R) -> Option<Span> { |
| 1301 |
self.inner.subspan(range).map(Span::_new) |
| 1302 |
} |
| 1303 |
|
| 1304 |
/// Returns the unescaped string value if this is a string literal. |
| 1305 |
#[cfg(procmacro2_semver_exempt)] |
| 1306 |
pub fn str_value(&self) -> Result<String, ConversionErrorKind> { |
| 1307 |
let repr = self.to_string(); |
| 1308 |
|
| 1309 |
if repr.starts_with('"') && repr[1..].ends_with('"') { |
| 1310 |
let quoted = &repr[1..repr.len() - 1]; |
| 1311 |
let mut value = String::with_capacity(quoted.len()); |
| 1312 |
let mut error = None; |
| 1313 |
rustc_literal_escaper::unescape_str(quoted, |_range, res| match res { |
| 1314 |
Ok(ch) => value.push(ch), |
| 1315 |
Err(err) => { |
| 1316 |
if err.is_fatal() { |
| 1317 |
error = Some(ConversionErrorKind::FailedToUnescape(err)); |
| 1318 |
} |
| 1319 |
} |
| 1320 |
}); |
| 1321 |
return match error { |
| 1322 |
Some(error) => Err(error), |
| 1323 |
None => Ok(value), |
| 1324 |
}; |
| 1325 |
} |
| 1326 |
|
| 1327 |
if repr.starts_with('r') { |
| 1328 |
if let Some(raw) = get_raw(&repr[1..]) { |
| 1329 |
return Ok(raw.to_owned()); |
| 1330 |
} |
| 1331 |
} |
| 1332 |
|
| 1333 |
Err(ConversionErrorKind::InvalidLiteralKind) |
| 1334 |
} |
| 1335 |
|
| 1336 |
/// Returns the unescaped string value (including nul terminator) if this is |
| 1337 |
/// a c-string literal. |
| 1338 |
#[cfg(procmacro2_semver_exempt)] |
| 1339 |
pub fn cstr_value(&self) -> Result<Vec<u8>, ConversionErrorKind> { |
| 1340 |
let repr = self.to_string(); |
| 1341 |
|
| 1342 |
if repr.starts_with("c\"") && repr[2..].ends_with('"') { |
| 1343 |
let quoted = &repr[2..repr.len() - 1]; |
| 1344 |
let mut value = Vec::with_capacity(quoted.len()); |
| 1345 |
let mut error = None; |
| 1346 |
rustc_literal_escaper::unescape_c_str(quoted, |_range, res| match res { |
| 1347 |
Ok(MixedUnit::Char(ch)) => { |
| 1348 |
value.extend_from_slice(ch.get().encode_utf8(&mut [0; 4]).as_bytes()); |
| 1349 |
} |
| 1350 |
Ok(MixedUnit::HighByte(byte)) => value.push(byte.get()), |
| 1351 |
Err(err) => { |
| 1352 |
if err.is_fatal() { |
| 1353 |
error = Some(ConversionErrorKind::FailedToUnescape(err)); |
| 1354 |
} |
| 1355 |
} |
| 1356 |
}); |
| 1357 |
return match error { |
| 1358 |
Some(error) => Err(error), |
| 1359 |
None => { |
| 1360 |
value.push(b'\0'); |
| 1361 |
Ok(value) |
| 1362 |
} |
| 1363 |
}; |
| 1364 |
} |
| 1365 |
|
| 1366 |
if repr.starts_with("cr") { |
| 1367 |
if let Some(raw) = get_raw(&repr[2..]) { |
| 1368 |
let mut value = Vec::with_capacity(raw.len() + 1); |
| 1369 |
value.extend_from_slice(raw.as_bytes()); |
| 1370 |
value.push(b'\0'); |
| 1371 |
return Ok(value); |
| 1372 |
} |
| 1373 |
} |
| 1374 |
|
| 1375 |
Err(ConversionErrorKind::InvalidLiteralKind) |
| 1376 |
} |
| 1377 |
|
| 1378 |
/// Returns the unescaped string value if this is a byte string literal. |
| 1379 |
#[cfg(procmacro2_semver_exempt)] |
| 1380 |
pub fn byte_str_value(&self) -> Result<Vec<u8>, ConversionErrorKind> { |
| 1381 |
let repr = self.to_string(); |
| 1382 |
|
| 1383 |
if repr.starts_with("b\"") && repr[2..].ends_with('"') { |
| 1384 |
let quoted = &repr[2..repr.len() - 1]; |
| 1385 |
let mut value = Vec::with_capacity(quoted.len()); |
| 1386 |
let mut error = None; |
| 1387 |
rustc_literal_escaper::unescape_byte_str(quoted, |_range, res| match res { |
| 1388 |
Ok(byte) => value.push(byte), |
| 1389 |
Err(err) => { |
| 1390 |
if err.is_fatal() { |
| 1391 |
error = Some(ConversionErrorKind::FailedToUnescape(err)); |
| 1392 |
} |
| 1393 |
} |
| 1394 |
}); |
| 1395 |
return match error { |
| 1396 |
Some(error) => Err(error), |
| 1397 |
None => Ok(value), |
| 1398 |
}; |
| 1399 |
} |
| 1400 |
|
| 1401 |
if repr.starts_with("br") { |
| 1402 |
if let Some(raw) = get_raw(&repr[2..]) { |
| 1403 |
return Ok(raw.as_bytes().to_owned()); |
| 1404 |
} |
| 1405 |
} |
| 1406 |
|
| 1407 |
Err(ConversionErrorKind::InvalidLiteralKind) |
| 1408 |
} |
| 1409 |
|
| 1410 |
// Intended for the `quote!` macro to use when constructing a proc-macro2 |
| 1411 |
// token out of a macro_rules $:literal token, which is already known to be |
| 1412 |
// a valid literal. This avoids reparsing/validating the literal's string |
| 1413 |
// representation. This is not public API other than for quote. |
| 1414 |
#[doc(hidden)] |
| 1415 |
pub unsafe fn from_str_unchecked(repr: &str) -> Self { |
| 1416 |
Literal::_new(unsafe { imp::Literal::from_str_unchecked(repr) }) |
| 1417 |
} |
| 1418 |
} |
| 1419 |
|
| 1420 |
impl FromStr for Literal { |
| 1421 |
type Err = LexError; |
| 1422 |
|
| 1423 |
fn from_str(repr: &str) -> Result<Self, LexError> { |
| 1424 |
match imp::Literal::from_str_checked(repr) { |
| 1425 |
Ok(lit) => Ok(Literal::_new(lit)), |
| 1426 |
Err(lex) => Err(LexError { |
| 1427 |
inner: lex, |
| 1428 |
_marker: MARKER, |
| 1429 |
}), |
| 1430 |
} |
| 1431 |
} |
| 1432 |
} |
| 1433 |
|
| 1434 |
impl Debug for Literal { |
| 1435 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 1436 |
Debug::fmt(&self.inner, f) |
| 1437 |
} |
| 1438 |
} |
| 1439 |
|
| 1440 |
impl Display for Literal { |
| 1441 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 1442 |
Display::fmt(&self.inner, f) |
| 1443 |
} |
| 1444 |
} |
| 1445 |
|
| 1446 |
/// Error when retrieving a string literal's unescaped value. |
| 1447 |
#[cfg(procmacro2_semver_exempt)] |
| 1448 |
#[derive(Debug, PartialEq, Eq)] |
| 1449 |
pub enum ConversionErrorKind { |
| 1450 |
/// The literal is of the right string kind, but its contents are malformed |
| 1451 |
/// in a way that cannot be unescaped to a value. |
| 1452 |
FailedToUnescape(EscapeError), |
| 1453 |
/// The literal is not of the string kind whose value was requested, for |
| 1454 |
/// example byte string vs UTF-8 string. |
| 1455 |
InvalidLiteralKind, |
| 1456 |
} |
| 1457 |
|
| 1458 |
// ###"..."### -> ... |
| 1459 |
#[cfg(procmacro2_semver_exempt)] |
| 1460 |
fn get_raw(repr: &str) -> Option<&str> { |
| 1461 |
let pounds = repr.len() - repr.trim_start_matches('#').len(); |
| 1462 |
if repr.len() >= pounds + 1 + 1 + pounds |
| 1463 |
&& repr[pounds..].starts_with('"') |
| 1464 |
&& repr.trim_end_matches('#').len() + pounds == repr.len() |
| 1465 |
&& repr[..repr.len() - pounds].ends_with('"') |
| 1466 |
{ |
| 1467 |
Some(&repr[pounds + 1..repr.len() - pounds - 1]) |
| 1468 |
} else { |
| 1469 |
None |
| 1470 |
} |
| 1471 |
} |
| 1472 |
|
| 1473 |
/// Public implementation details for the `TokenStream` type, such as iterators. |
| 1474 |
pub mod token_stream { |
| 1475 |
use crate::marker::{ProcMacroAutoTraits, MARKER}; |
| 1476 |
use crate::{imp, TokenTree}; |
| 1477 |
use core::fmt::{self, Debug}; |
| 1478 |
|
| 1479 |
pub use crate::TokenStream; |
| 1480 |
|
| 1481 |
/// An iterator over `TokenStream`'s `TokenTree`s. |
| 1482 |
/// |
| 1483 |
/// The iteration is "shallow", e.g. the iterator doesn't recurse into |
| 1484 |
/// delimited groups, and returns whole groups as token trees. |
| 1485 |
#[derive(Clone)] |
| 1486 |
pub struct IntoIter { |
| 1487 |
inner: imp::TokenTreeIter, |
| 1488 |
_marker: ProcMacroAutoTraits, |
| 1489 |
} |
| 1490 |
|
| 1491 |
impl Iterator for IntoIter { |
| 1492 |
type Item = TokenTree; |
| 1493 |
|
| 1494 |
fn next(&mut self) -> Option<TokenTree> { |
| 1495 |
self.inner.next() |
| 1496 |
} |
| 1497 |
|
| 1498 |
fn size_hint(&self) -> (usize, Option<usize>) { |
| 1499 |
self.inner.size_hint() |
| 1500 |
} |
| 1501 |
} |
| 1502 |
|
| 1503 |
impl Debug for IntoIter { |
| 1504 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 1505 |
f.write_str("TokenStream ")?; |
| 1506 |
f.debug_list().entries(self.clone()).finish() |
| 1507 |
} |
| 1508 |
} |
| 1509 |
|
| 1510 |
impl IntoIterator for TokenStream { |
| 1511 |
type Item = TokenTree; |
| 1512 |
type IntoIter = IntoIter; |
| 1513 |
|
| 1514 |
fn into_iter(self) -> IntoIter { |
| 1515 |
IntoIter { |
| 1516 |
inner: self.inner.into_iter(), |
| 1517 |
_marker: MARKER, |
| 1518 |
} |
| 1519 |
} |
| 1520 |
} |
| 1521 |
} |