Skip to content

Commit

Permalink
Spell out Result transformations using match
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Nov 21, 2024
1 parent 2581d80 commit a244808
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
23 changes: 13 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,13 @@ impl FromStr for TokenStream {
type Err = LexError;

fn from_str(src: &str) -> Result<TokenStream, LexError> {
let e = imp::TokenStream::from_str(src).map_err(|e| LexError {
inner: e,
_marker: MARKER,
})?;
Ok(TokenStream::_new(e))
match imp::TokenStream::from_str(src) {
Ok(tokens) => Ok(TokenStream::_new(tokens)),
Err(lex) => Err(LexError {
inner: lex,
_marker: MARKER,
}),
}
}
}

Expand Down Expand Up @@ -1307,12 +1309,13 @@ impl FromStr for Literal {
type Err = LexError;

fn from_str(repr: &str) -> Result<Self, LexError> {
imp::Literal::from_str(repr)
.map(Literal::_new)
.map_err(|inner| LexError {
inner,
match imp::Literal::from_str(repr) {
Ok(lit) => Ok(Literal::_new(lit)),
Err(lex) => Err(LexError {
inner: lex,
_marker: MARKER,
})
}),
}
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,10 @@ impl FromStr for TokenStream {

// Work around https://github.com/rust-lang/rust/issues/58736.
fn proc_macro_parse(src: &str) -> Result<proc_macro::TokenStream, LexError> {
let result =
panic::catch_unwind(|| proc_macro::TokenStream::from_str(src).map_err(LexError::Compiler));
result.unwrap_or_else(|_| Err(LexError::CompilerPanic))
match panic::catch_unwind(|| proc_macro::TokenStream::from_str(src)) {
Ok(result) => result.map_err(LexError::Compiler),
Err(_) => Err(LexError::CompilerPanic),
}
}

impl Display for TokenStream {
Expand Down

0 comments on commit a244808

Please sign in to comment.