Skip to content

Commit 4ba58d1

Browse files
committed
impl FromStr for proc_macro::Literal
1 parent 191c2b0 commit 4ba58d1

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

proc_macro/src/bridge/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ macro_rules! with_api {
107107
Literal {
108108
fn drop($self: $S::Literal);
109109
fn clone($self: &$S::Literal) -> $S::Literal;
110+
fn from_str(s: &str) -> Result<$S::Literal, ()>;
110111
fn debug_kind($self: &$S::Literal) -> String;
111112
fn symbol($self: &$S::Literal) -> String;
112113
fn suffix($self: &$S::Literal) -> Option<String>;
@@ -315,6 +316,19 @@ impl<T: Unmark> Unmark for Option<T> {
315316
}
316317
}
317318

319+
impl<T: Mark, E: Mark> Mark for Result<T, E> {
320+
type Unmarked = Result<T::Unmarked, E::Unmarked>;
321+
fn mark(unmarked: Self::Unmarked) -> Self {
322+
unmarked.map(T::mark).map_err(E::mark)
323+
}
324+
}
325+
impl<T: Unmark, E: Unmark> Unmark for Result<T, E> {
326+
type Unmarked = Result<T::Unmarked, E::Unmarked>;
327+
fn unmark(self) -> Self::Unmarked {
328+
self.map(T::unmark).map_err(E::unmark)
329+
}
330+
}
331+
318332
macro_rules! mark_noop {
319333
($($ty:ty),* $(,)?) => {
320334
$(

proc_macro/src/lib.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ pub struct LexError {
9191
_inner: (),
9292
}
9393

94+
impl LexError {
95+
fn new() -> Self {
96+
LexError { _inner: () }
97+
}
98+
}
99+
94100
#[stable(feature = "proc_macro_lexerror_impls", since = "1.44.0")]
95101
impl fmt::Display for LexError {
96102
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -1171,6 +1177,28 @@ impl Literal {
11711177
}
11721178
}
11731179

1180+
/// Parse a single literal from its stringified representation.
1181+
///
1182+
/// In order to parse successfully, the input string must not contain anything
1183+
/// but the literal token. Specifically, it must not contain whitespace or
1184+
/// comments in addition to the literal.
1185+
///
1186+
/// The resulting literal token will have a `Span::call_site()` span.
1187+
///
1188+
/// NOTE: some errors may cause panics instead of returning `LexError`. We
1189+
/// reserve the right to change these errors into `LexError`s later.
1190+
#[stable(feature = "proc_macro_literal_parse", since = "1.54.0")]
1191+
impl FromStr for Literal {
1192+
type Err = LexError;
1193+
1194+
fn from_str(src: &str) -> Result<Self, LexError> {
1195+
match bridge::client::Literal::from_str(src) {
1196+
Ok(literal) => Ok(Literal(literal)),
1197+
Err(()) => Err(LexError::new()),
1198+
}
1199+
}
1200+
}
1201+
11741202
// N.B., the bridge only provides `to_string`, implement `fmt::Display`
11751203
// based on it (the reverse of the usual relationship between the two).
11761204
#[stable(feature = "proc_macro_lib", since = "1.15.0")]

0 commit comments

Comments
 (0)