cosmwasm_std/
never.rs

1/// Never can never be instantiated. This can be used in places
2/// where we want to ensure that no error is returned, such as
3/// the `ibc_packet_receive` entry point.
4///
5/// In contrast to `Empty`, this does not have a JSON schema
6/// and cannot be used for message and query types.
7///
8/// Once the ! type is stable, this is not needed anymore.
9/// 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/// ```
28pub enum Never {}
29
30// The Debug implementation is needed to allow the use of `Result::unwrap`.
31impl core::fmt::Debug for Never {
32    fn fmt(&self, _f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
33        // Unreachable because no instance of Never can exist
34        match *self {}
35    }
36}
37
38// The Display implementation is needed to fulfill the ToString requirement of
39// entry point errors: `Result<IbcReceiveResponse<C>, E>` with `E: ToString`.
40impl core::fmt::Display for Never {
41    fn fmt(&self, _f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
42        // Unreachable because no instance of Never can exist
43        match *self {}
44    }
45}