Skip to content

Commit

Permalink
Add polonius_try! macro
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhenrymantilla committed May 9, 2022
1 parent 999af4d commit a94e2ad
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ name = "polonius-the-crab"
authors = [
"Daniel Henry-Mantilla <daniel.henry.mantilla@gmail.com>",
]
version = "0.2.0" # Keep in sync
version = "0.2.1" # Keep in sync
edition = "2021"
rust-version = "1.56.0"

Expand Down
51 changes: 51 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod prelude {
polonius_continue,
polonius_loop,
polonius_return,
polonius_try,
};
}

Expand Down Expand Up @@ -73,6 +74,8 @@ where

/// Convenient entry-point to this crate's logic.
///
/// - See the [top-level docs][crate] for more info.
///
/// ## Usage
///
/** ```rust
Expand Down Expand Up @@ -184,6 +187,54 @@ macro_rules! exit_polonius {( $($e:expr $(,)?)? ) => (
)
)}

/// Perform the `?` operation (on `Result`s). See [`polonius!`] for more info.
///
/// ## Example
///
/** ```rust
use {
::polonius_the_crab::prelude::*,
::std::collections::HashMap,
};
enum Error { /* … */ }
fn fallible_operation (value: &'_ i32)
-> Result<(), Error>
{
# Ok(())
// …
}
fn get_or_insert (
mut map: &'_ mut HashMap<i32, i32>,
) -> Result<&'_ i32, Error>
{
polonius!(|map| -> Result<&'polonius i32, Error> {
if let Some(value) = map.get(&22) {
// fallible_operation(value)?;
polonius_try!(fallible_operation(value));
polonius_return!(Ok(value));
}
});
map.insert(22, 42);
Ok(&map[&22])
}
``` */
#[macro_export]
macro_rules! polonius_try {( $e:expr $(,)? ) => (
match $e {
| $crate::::core::result::Result::Ok(it) => it,
| $crate::::core::result::Result::Err(err) => {
$crate::polonius_return!(
$crate::::core::result::Result::Err(
$crate::::core::convert::From::from(err),
)
)
},
}
)}

/// Convenience support for the `loop { … polonius!(…) }` pattern.
///
/// ### Example
Expand Down

0 comments on commit a94e2ad

Please sign in to comment.