proc_macro_c_str_literals: Implement Literal::c_string constructor

This commit is contained in:
novafacing
2024-01-16 13:27:58 -08:00
parent e64f8495e7
commit ee007ab187
6 changed files with 38 additions and 4 deletions

View File

@@ -45,6 +45,7 @@ mod diagnostic;
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
pub use diagnostic::{Diagnostic, Level, MultiSpan};
use std::ffi::CStr;
use std::ops::{Range, RangeBounds};
use std::path::PathBuf;
use std::str::FromStr;
@@ -1351,6 +1352,13 @@ impl Literal {
Literal::new(bridge::LitKind::ByteStr, &string, None)
}
/// C string literal.
#[unstable(feature = "proc_macro_c_str_literals", issue = "119750")]
pub fn c_string(string: &CStr) -> Literal {
let string = string.to_bytes().escape_ascii().to_string();
Literal::new(bridge::LitKind::CStr, &string, None)
}
/// Returns the span encompassing this literal.
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
pub fn span(&self) -> Span {