Skip to content

Commit 32f308a

Browse files
committed
Add type-safe unwrap example
1 parent 9e822c4 commit 32f308a

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

packages/std/src/never.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,24 @@
77
///
88
/// Once the ! type is stable, this is not needed anymore.
99
/// See <https://github.com/rust-lang/rust/issues/35121>.
10+
///
11+
/// ## Examples
12+
///
13+
/// When using `Never` in a `Result`, we can unwrap in a type-safe way:
14+
///
15+
/// ```
16+
/// use cosmwasm_std::Never;
17+
///
18+
/// pub fn safe_unwrap<T>(res: Result<T, Never>) -> T {
19+
/// match res {
20+
/// Ok(value) => value,
21+
/// Err(err) => match err {},
22+
/// }
23+
/// }
24+
///
25+
/// let res: Result<i32, Never> = Ok(5);
26+
/// assert_eq!(safe_unwrap(res), 5);
27+
/// ```
1028
pub enum Never {}
1129

1230
// The Debug implementation is needed to allow the use of `Result::unwrap`.

0 commit comments

Comments
 (0)