Skip to main content

Crate somelse

Crate somelse 

Source
Expand description

§somelse

A no_std declarative macro for extracting an Option<T> payload or diverging from the caller on None.

§The pattern

A match lets an absence handler diverge and return from the caller:

fn process(input: Option<i32>) -> Result<i32, &'static str> {
    let value = match input {
        Some(value) => value,
        None => return Err("missing value"),
    };
    Ok(value * 2)
}

somelse! keeps that behavior at the call site with less repeated structure:

use somelse::somelse;

fn process(input: Option<i32>) -> Result<i32, &'static str> {
    let value = somelse!(input, else => return Err("missing value"));
    Ok(value * 2)
}

The Some payload continues in the surrounding scope. The else expression handles None and must diverge. A handler can return, break, continue, panic, loop forever, or call another never-returning expression.

The input is evaluated exactly once. Because somelse! is an expression, it can be nested inside another expression or passed directly as a function argument. An expression containing .await works when the invocation is in an async context; the macro does not await implicitly.

Macros§

somelse
Extracts a Some payload and handles None through a diverging else expression.