Auto merge of #151543 - GuillaumeGomez:more-proc-macro-literal-methods, r=Amanieu
Add new `byte_value` and `char_value` methods to `proc_macro::Literal` Part of https://github.com/rust-lang/rust/issues/136652. It adds two more methods to get unescaped `u8` and `char` from `proc_macro::Literal`. r? @Amanieu
This commit is contained in:
commit
8340622e14
1 changed files with 25 additions and 1 deletions
|
|
@ -54,7 +54,9 @@ use std::{error, fmt};
|
|||
pub use diagnostic::{Diagnostic, Level, MultiSpan};
|
||||
#[unstable(feature = "proc_macro_value", issue = "136652")]
|
||||
pub use rustc_literal_escaper::EscapeError;
|
||||
use rustc_literal_escaper::{MixedUnit, unescape_byte_str, unescape_c_str, unescape_str};
|
||||
use rustc_literal_escaper::{
|
||||
MixedUnit, unescape_byte, unescape_byte_str, unescape_c_str, unescape_char, unescape_str,
|
||||
};
|
||||
#[unstable(feature = "proc_macro_totokens", issue = "130977")]
|
||||
pub use to_tokens::ToTokens;
|
||||
|
||||
|
|
@ -1451,6 +1453,28 @@ impl Literal {
|
|||
})
|
||||
}
|
||||
|
||||
/// Returns the unescaped character value if the current literal is a byte character literal.
|
||||
#[unstable(feature = "proc_macro_value", issue = "136652")]
|
||||
pub fn byte_character_value(&self) -> Result<u8, ConversionErrorKind> {
|
||||
self.0.symbol.with(|symbol| match self.0.kind {
|
||||
bridge::LitKind::Char => {
|
||||
unescape_byte(symbol).map_err(ConversionErrorKind::FailedToUnescape)
|
||||
}
|
||||
_ => Err(ConversionErrorKind::InvalidLiteralKind),
|
||||
})
|
||||
}
|
||||
|
||||
/// Returns the unescaped character value if the current literal is a character literal.
|
||||
#[unstable(feature = "proc_macro_value", issue = "136652")]
|
||||
pub fn character_value(&self) -> Result<char, ConversionErrorKind> {
|
||||
self.0.symbol.with(|symbol| match self.0.kind {
|
||||
bridge::LitKind::Char => {
|
||||
unescape_char(symbol).map_err(ConversionErrorKind::FailedToUnescape)
|
||||
}
|
||||
_ => Err(ConversionErrorKind::InvalidLiteralKind),
|
||||
})
|
||||
}
|
||||
|
||||
/// Returns the unescaped string value if the current literal is a string or a string literal.
|
||||
#[unstable(feature = "proc_macro_value", issue = "136652")]
|
||||
pub fn str_value(&self) -> Result<String, ConversionErrorKind> {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue