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/// ```
28#[derive(cw_schema::Schemaifier)]
29pub enum Never {}
30
31// The Debug implementation is needed to allow the use of `Result::unwrap`.
32impl core::fmt::Debug for Never {
33 fn fmt(&self, _f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
34 // Unreachable because no instance of Never can exist
35 match *self {}
36 }
37}
38
39// The Display implementation is needed to fulfill the ToString requirement of
40// entry point errors: `Result<IbcReceiveResponse<C>, E>` with `E: ToString`.
41impl core::fmt::Display for Never {
42 fn fmt(&self, _f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
43 // Unreachable because no instance of Never can exist
44 match *self {}
45 }
46}