Skip to content

Commit 8340622

Browse files
committed
Auto merge of rust-lang#151543 - GuillaumeGomez:more-proc-macro-literal-methods, r=Amanieu
Add new `byte_value` and `char_value` methods to `proc_macro::Literal` Part of rust-lang#136652. It adds two more methods to get unescaped `u8` and `char` from `proc_macro::Literal`. r? @Amanieu
2 parents b7fb220 + 55e3d22 commit 8340622

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

@@ -1451,6 +1453,28 @@ impl Literal {
14511453
})
14521454
}
14531455

1456+
/// Returns the unescaped character value if the current literal is a byte character literal.
1457+
#[unstable(feature = "proc_macro_value", issue = "136652")]
1458+
pub fn byte_character_value(&self) -> Result<u8, ConversionErrorKind> {
1459+
self.0.symbol.with(|symbol| match self.0.kind {
1460+
bridge::LitKind::Char => {
1461+
unescape_byte(symbol).map_err(ConversionErrorKind::FailedToUnescape)
1462+
}
1463+
_ => Err(ConversionErrorKind::InvalidLiteralKind),
1464+
})
1465+
}
1466+
1467+
/// Returns the unescaped character value if the current literal is a character literal.
1468+
#[unstable(feature = "proc_macro_value", issue = "136652")]
1469+
pub fn character_value(&self) -> Result<char, ConversionErrorKind> {
1470+
self.0.symbol.with(|symbol| match self.0.kind {
1471+
bridge::LitKind::Char => {
1472+
unescape_char(symbol).map_err(ConversionErrorKind::FailedToUnescape)
1473+
}
1474+
_ => Err(ConversionErrorKind::InvalidLiteralKind),
1475+
})
1476+
}
1477+
14541478
/// Returns the unescaped string value if the current literal is a string or a string literal.
14551479
#[unstable(feature = "proc_macro_value", issue = "136652")]
14561480
pub fn str_value(&self) -> Result<String, ConversionErrorKind> {

0 commit comments

Comments
 (0)