Skip to content

Commit 36c0fb5

Browse files
Add new byte_value and char_value methods to proc_macro::Literal
1 parent 88ad3d4 commit 36c0fb5

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

library/proc_macro/src/lib.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ use std::{error, fmt};
5454
pub use diagnostic::{Diagnostic, Level, MultiSpan};
5555
#[unstable(feature = "proc_macro_value", issue = "136652")]
5656
pub use rustc_literal_escaper::EscapeError;
57-
use rustc_literal_escaper::{MixedUnit, unescape_byte_str, unescape_c_str, unescape_str};
57+
use rustc_literal_escaper::{
58+
MixedUnit, unescape_byte, unescape_byte_str, unescape_c_str, unescape_char, unescape_str,
59+
};
5860
#[unstable(feature = "proc_macro_totokens", issue = "130977")]
5961
pub use to_tokens::ToTokens;
6062

@@ -1443,6 +1445,28 @@ impl Literal {
14431445
})
14441446
}
14451447

1448+
/// Returns the unescaped char value if the current literal is a char.
1449+
#[unstable(feature = "proc_macro_value", issue = "136652")]
1450+
pub fn byte_value(&self) -> Result<u8, ConversionErrorKind> {
1451+
self.0.symbol.with(|symbol| match self.0.kind {
1452+
bridge::LitKind::Char => {
1453+
unescape_byte(symbol).map_err(ConversionErrorKind::FailedToUnescape)
1454+
}
1455+
_ => Err(ConversionErrorKind::InvalidLiteralKind),
1456+
})
1457+
}
1458+
1459+
/// Returns the unescaped char value if the current literal is a char.
1460+
#[unstable(feature = "proc_macro_value", issue = "136652")]
1461+
pub fn char_value(&self) -> Result<char, ConversionErrorKind> {
1462+
self.0.symbol.with(|symbol| match self.0.kind {
1463+
bridge::LitKind::Char => {
1464+
unescape_char(symbol).map_err(ConversionErrorKind::FailedToUnescape)
1465+
}
1466+
_ => Err(ConversionErrorKind::InvalidLiteralKind),
1467+
})
1468+
}
1469+
14461470
/// Returns the unescaped string value if the current literal is a string or a string literal.
14471471
#[unstable(feature = "proc_macro_value", issue = "136652")]
14481472
pub fn str_value(&self) -> Result<String, ConversionErrorKind> {

0 commit comments

Comments
 (0)