somelse 0.1.0-rc.1

extract an Option::Some value or diverge on None
Documentation
  • Coverage
  • 100%
    2 out of 2 items documented2 out of 2 items with examples
  • Size
  • Source code size: 22.2 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 69.4 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 1s Average build duration of successful builds.
  • all releases: 1s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • awill1988/somelse
    1 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • awill1988

somelse!

Keep the Some. Diverge on None.

somelse! is a dependency-free, no_std macro that extracts an Option::Some payload as an expression and requires None to diverge from the current path.

Crates.io Documentation CI License

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 input runs once. A Some becomes the value of the macro. The else expression matches None and must leave the current path through return, break, continue, panic, or another never-returning expression.

Why an expression

Rust's let-else keeps the inner value in the surrounding scope and requires the failure branch to diverge. It is a statement, so it cannot be placed directly inside another expression or function argument.

A match works in those positions but repeats the Some and None structure:

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 the same control flow in expression position:

use somelse::somelse;

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

Prefer let-else when a statement is sufficient and match when multiple branches need meaningful behavior. This macro exists only for concise extraction where expression placement and caller-selected divergence are both useful.

The name

The name describes the complete domain: Some + else = somelse!.

Contributor checks

Read CONTRIBUTING.md before proposing a change.

The conventional commit linter is written in Rust. Activate the tracked hooks:

git config core.hooksPath .githooks

Run local validation:

cargo test
cargo fmt --all -- --check
cargo fmt --manifest-path tools/commit_check/Cargo.toml -- --check
cargo fmt --manifest-path tests/fixtures/downstream/Cargo.toml -- --check
cargo clippy --all-targets --all-features -- -D warnings
cargo clippy --manifest-path tools/commit_check/Cargo.toml --all-targets -- -D warnings
cargo clippy --manifest-path tests/fixtures/downstream/Cargo.toml --all-targets -- -D warnings
python3 -m unittest discover -s scripts -p 'test_*.py'
cargo test --manifest-path tools/commit_check/Cargo.toml
cargo check --manifest-path tests/fixtures/downstream/Cargo.toml
cargo publish --dry-run

License

Dual-licensed under MIT or Apache 2.0.