1
Fork 0

impl FromStr for proc_macro::Literal

This commit is contained in:
David Tolnay 2021-04-29 12:08:35 -07:00
parent 965bce4834
commit 34585cb678
No known key found for this signature in database
GPG key ID: F9BA143B95FF6D82
3 changed files with 70 additions and 3 deletions

View file

@ -91,6 +91,12 @@ pub struct LexError {
_inner: (),
}
impl LexError {
fn new() -> Self {
LexError { _inner: () }
}
}
#[stable(feature = "proc_macro_lexerror_impls", since = "1.44.0")]
impl fmt::Display for LexError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@ -1171,6 +1177,28 @@ impl Literal {
}
}
/// Parse a single literal from its stringified representation.
///
/// In order to parse successfully, the input string must not contain anything
/// but the literal token. Specifically, it must not contain whitespace or
/// comments in addition to the literal.
///
/// The resulting literal token will have a `Span::call_site()` span.
///
/// NOTE: some errors may cause panics instead of returning `LexError`. We
/// reserve the right to change these errors into `LexError`s later.
#[stable(feature = "proc_macro_literal_parse", since = "1.54.0")]
impl FromStr for Literal {
type Err = LexError;
fn from_str(src: &str) -> Result<Self, LexError> {
match bridge::client::Literal::from_str(src) {
Ok(literal) => Ok(Literal(literal)),
Err(()) => Err(LexError::new()),
}
}
}
// N.B., the bridge only provides `to_string`, implement `fmt::Display`
// based on it (the reverse of the usual relationship between the two).
#[stable(feature = "proc_macro_lib", since = "1.15.0")]