Skip to main content

Crate try_v2

Crate try_v2 

Source
Expand description

Provides a derive macro for Try (try_trait_v2)

§Requires:

  • RUSTC_BOOTSTRAP = 1 (or nightly)
  • #![feature(never_type)]
  • #![feature(try_trait_v2)]

§Current Limitations on the annotated type:

  • must be an enum
  • must have one generic type
  • the first and only generic type must be the Output type (produced when not short circuiting)
  • the output variant (does not short-circuit) must be the first variant
  • other (short-circuiting) variants can have at most one unnamed field

§Example Usage:

#![feature(never_type)]
#![feature(try_trait_v2)]
use try_v2::Try;

#[derive(Try)]
enum TestResult<T> {
    Ok(T),
    TestsFailed,
    OtherError(String)
}

fn run_tests() -> TestResult<()> {
    TestResult::OtherError("oops!".to_string())?; // <- Function short-circuits here ...
    TestResult::TestsFailed?;
    TestResult::Ok(())
}

assert!(matches!(run_tests(), TestResult::OtherError(msg) if msg == "oops!"))

Derive Macros§

Try
Derives try_trait_v2