diff options
author | David Tolnay <dtolnay@gmail.com> | 2019-09-21 00:54:38 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-09-21 00:54:38 -0700 |
commit | 8df91b87e0b6d1dac6a3ea4af5044491c6193a8f (patch) | |
tree | 2af7dddeef45222911ac769083d7427b79d022ec | |
parent | cf62a7bac077a1755d254f5bfca6f097e56efe84 (diff) | |
parent | a1a22fc8cf075cfed532359923bfec682641183e (diff) | |
download | proc-macro2-8df91b87e0b6d1dac6a3ea4af5044491c6193a8f.tar.gz |
Merge pull request #195 from SergioBenitez/master
Add 'Literal::subspan()'
-rw-r--r-- | src/fallback.rs | 5 | ||||
-rw-r--r-- | src/lib.rs | 14 | ||||
-rw-r--r-- | src/wrapper.rs | 11 |
3 files changed, 30 insertions, 0 deletions
diff --git a/src/fallback.rs b/src/fallback.rs index 29c0621..2a710be 100644 --- a/src/fallback.rs +++ b/src/fallback.rs @@ -9,6 +9,7 @@ use std::path::Path; use std::path::PathBuf; use std::str::FromStr; use std::vec; +use std::ops::RangeBounds; use crate::strnom::{block_comment, skip_whitespace, whitespace, word_break, Cursor, PResult}; use crate::{Delimiter, Punct, Spacing, TokenTree}; @@ -777,6 +778,10 @@ impl Literal { pub fn set_span(&mut self, span: Span) { self.span = span; } + + pub fn subspan<R: RangeBounds<usize>>(&self, _range: R) -> Option<Span> { + None + } } impl fmt::Display for Literal { @@ -94,6 +94,7 @@ use std::marker; use std::path::PathBuf; use std::rc::Rc; use std::str::FromStr; +use std::ops::RangeBounds; #[macro_use] mod strnom; @@ -1124,6 +1125,19 @@ impl Literal { pub fn set_span(&mut self, span: Span) { self.inner.set_span(span.inner); } + + /// Returns a `Span` that is a subset of `self.span()` containing only + /// the source bytes in range `range`. Returns `None` if the would-be + /// trimmed span is outside the bounds of `self`. + /// + /// Warning: the underlying [`proc_macro::Literal::subspan`] method is + /// nightly-only. When called from within a procedural macro not using a + /// nightly compiler, this method will always return `None`. + /// + /// [`proc_macro::Literal::subspan`]: https://doc.rust-lang.org/proc_macro/struct.Literal.html#method.subspan + pub fn subspan<R: RangeBounds<usize>>(&self, range: R) -> Option<Span> { + self.inner.subspan(range).map(Span::_new) + } } impl fmt::Debug for Literal { diff --git a/src/wrapper.rs b/src/wrapper.rs index 887e1e6..907f813 100644 --- a/src/wrapper.rs +++ b/src/wrapper.rs @@ -4,6 +4,7 @@ use std::panic::{self, PanicInfo}; #[cfg(super_unstable)] use std::path::PathBuf; use std::str::FromStr; +use std::ops::RangeBounds; use crate::{fallback, Delimiter, Punct, Spacing, TokenTree}; @@ -860,6 +861,16 @@ impl Literal { } } + pub fn subspan<R: RangeBounds<usize>>(&self, range: R) -> Option<Span> { + match self { + #[cfg(proc_macro_span)] + Literal::Compiler(lit) => lit.subspan(range).map(Span::Compiler), + #[cfg(not(proc_macro_span))] + Literal::Compiler(_lit) => None, + Literal::Fallback(lit) => lit.subspan(range).map(Span::Fallback), + } + } + fn unwrap_nightly(self) -> proc_macro::Literal { match self { Literal::Compiler(s) => s, |