1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//! # 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:
//!
//! ```rust
//! 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:
//!
//! ```rust
//! 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.
/// Extracts a `Some` payload and handles `None` through a diverging `else`
/// expression.
///
/// Every `else` handler must diverge (`return`, `break`, `continue`, panic, or
/// invoke another non-returning expression).
///
/// A non-diverging handler is rejected:
///
/// ```compile_fail
/// use somelse::somelse;
///
/// let _: i32 = somelse!(None::<i32>, else => 0);
/// ```