Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion library/proc_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -1443,6 +1445,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> {
Expand Down
Loading