#unwrap #try #let #else

inner

The inner! macro descends into an enum variant. It's more flexible than try!() and unwrap(), and it works with your enum, too!

1 unstable release

Uses old Rust 2015

0.1.1 Feb 5, 2016
0.1.0 Feb 2, 2016

#1870 in Rust patterns

Download history 3234/week @ 2023-10-22 2633/week @ 2023-10-29 2977/week @ 2023-11-05 2062/week @ 2023-11-12 2827/week @ 2023-11-19 2202/week @ 2023-11-26 2575/week @ 2023-12-03 1343/week @ 2023-12-10 3362/week @ 2023-12-17 879/week @ 2023-12-24 2286/week @ 2023-12-31 2075/week @ 2024-01-07 3274/week @ 2024-01-14 3108/week @ 2024-01-21 1794/week @ 2024-01-28 1323/week @ 2024-02-04

9,558 downloads per month
Used in 2 crates

Apache-2.0/MIT

15KB
233 lines

The inner! macro makes descending into an enum variant more ergonomic. The some! and ok! macros turns an enum into an Option or Result.

Crates.io

API Documentation

Helpful unwrap

The simplest case is almost like unwrap:

let x = Some(1);
let y: Result<_, ()> = Ok(2);
assert_eq!(inner!(x), 1);
assert_eq!(inner!(y), 2);

...but if you instead use it on a None or Err value:

let z = None;
let y = inner!(z);

...it will panic, with an error message that points you to a more helpful location than some line number inside libcore:

thread "test" panicked at "Unexpected value found inside "z"", src/lib.rs:23

Error handling

If panic isn't an option - and it usually isn't - just add an else clause:

let x: Result<String, i32> = Err(7);
let y = inner!(x, else { return });
// Since x is an Err, we'll never get here.
println!("The string length is: {}", y.len());

You can use the else clause to compute a default value, or use flow control (e g break, continue, or return).

Want access to what's inside the Err value in your else clause? No problem, just add a |variable| after else, like this:

let x: Result<String, i32> = Err(7);
let y = inner!(x, else |e| {
    assert_eq!(e, 7);
    (e + 2).to_string()
});
assert_eq!(&y, "9");

Note: This does not turn your else clause into a closure, so you can still use (e g) return the same way as before.

It works with your enums too

It does not work only with Option and Result. Just add an if clause:

enum Fruit {
    Apple(i32),
    Orange(i16),
}

let z = Fruit::Apple(15);
let y = inner!(z, if Fruit::Apple, else {
    println!("I wanted an apple and I didn't get one!");
    0
});
assert_eq!(y, 15);

You can skip the else clause to panic in case the enum is not the expected variant.

Note that in this case, the entire item (instead of the contents inside Err) is passed on to the else clause:

#[derive(Eq, PartialEq, Debug)]
enum Fruit {
    Apple(i32),
    Orange(i16),
}

let z = Fruit::Orange(15);
inner!(z, if Fruit::Apple, else |e| {
    assert_eq!(e, Fruit::Orange(15));
    return;
});

You can also turn your enum into a Option with the Some macro:

assert_eq!(some!(Fruit::Apple(15), if Fruit::Apple), Some(15));
assert_eq!(some!(Fruit::Orange(5), if Fruit::Apple), None);
assert_eq!(some!(Fruit::Orange(5), if Fruit::Apple, else |e| {Some(e + 2)}), Some(7));

Or into a Result with the ok!() macro:

assert_eq!(ok!(Fruit::Apple(15), if Fruit::Apple), Ok(15));
assert_eq!(ok!(Fruit::Orange(5), if Fruit::Apple), Err(Fruit::Orange(5)));

assert_eq!(ok!(Fruit::Orange(5), if Fruit::Apple, or |e| {e + 70}), Err(75));
assert_eq!(ok!(Fruit::Orange(5), if Fruit::Apple, else {Err(75)}), Err(75));

Notice that the ok!() macro has an optional or clause that encapsulates the expression in an Err, whereas the else clause gives you maximum flexibility to return either an Err or an Ok.

Another option is to implement this crate's IntoResult trait for your enum. Then you don't have to write an if clause to tell what enum variant you want to descend into, and you can choose more than one enum variant to be Ok:

enum Fruit {
    Apple(i32),
    Orange(i16),
    Rotten,
}

impl IntoResult<i32, ()> for Fruit {
    fn into_result(self) -> Result<i32, ()> {
        match self {
            Fruit::Apple(i) => Ok(i),
            Fruit::Orange(i) => Ok(i as i32),
            Fruit::Rotten => Err(()),
        }
    }
}

assert_eq!(9, inner!(Fruit::Apple(9)));

License

Apache2.0/MIT

No runtime deps