Skip to content

Commit f8b1f59

Browse files
committed
Add CtOption::expect
Along the lines of the existing `CtOption::unwrap`, this adds a corresponding `CtOption::expect` which works like `Option::expect`: unwraps the value if it's some, or else panics with a provided error message. Using `expect` over `unwrap` is generally prefered from a Rust style perspective. This allows the caller to describe what invariant was violated when it's expected some `CtOption` is always some.
1 parent b4b070c commit f8b1f59

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,18 @@ impl<T> CtOption<T> {
605605
}
606606
}
607607

608+
/// Returns the contained value, consuming the `self` value.
609+
///
610+
/// # Panics
611+
///
612+
/// Panics if the value is none with a custom panic message provided by
613+
/// `msg`.
614+
pub fn expect(self, msg: &str) -> T {
615+
assert_eq!(self.is_some.unwrap_u8(), 1, "{}", msg);
616+
617+
self.value
618+
}
619+
608620
/// This returns the underlying value but panics if it
609621
/// is not `Some`.
610622
#[inline]

0 commit comments

Comments
 (0)