Skip to content

Commit

Permalink
Add a Literal::from_str_unchecked constructor for quote
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Dec 27, 2021
1 parent 7b91e66 commit 4ca7537
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,10 @@ impl Literal {
}
}

pub(crate) unsafe fn from_str_unchecked(repr: &str) -> Self {
Literal::_new(repr.to_owned())
}

suffixed_numbers! {
u8_suffixed => u8,
u16_suffixed => u16,
Expand Down
9 changes: 9 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1232,6 +1232,15 @@ impl Literal {
pub fn subspan<R: RangeBounds<usize>>(&self, range: R) -> Option<Span> {
self.inner.subspan(range).map(Span::_new)
}

// Intended for the `quote!` macro to use when constructing a proc-macro2
// token out of a macro_rules $:literal token, which is already known to be
// a valid literal. This avoids reparsing/validating the literal's string
// representation. This is not public API other than for quote.
#[doc(hidden)]
pub unsafe fn from_str_unchecked(repr: &str) -> Self {
Literal::_new(imp::Literal::from_str_unchecked(repr))
}
}

impl FromStr for Literal {
Expand Down
8 changes: 8 additions & 0 deletions src/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,14 @@ macro_rules! unsuffixed_integers {
}

impl Literal {
pub unsafe fn from_str_unchecked(repr: &str) -> Self {
if inside_proc_macro() {
Literal::Compiler(repr.parse().expect("invalid literal"))
} else {
Literal::Fallback(fallback::Literal::from_str_unchecked(repr))
}
}

suffixed_numbers! {
u8_suffixed => u8,
u16_suffixed => u16,
Expand Down

0 comments on commit 4ca7537

Please sign in to comment.