inconceivable/lib.rs
1/*
2 * Copyright (c) 2020 William Cody Laeder
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 *
22 */
23#![cfg_attr(not(feature = "std"), no_std)]
24
25/// inconceivable is a macro which closely parallels `std::unreachable`, or `std::panic`.
26///
27/// The primary difference is that when this crate is
28/// configured with the `ub_inconceivable` option it will emit
29/// the `core::hint::unreachable_unchecked` to hint
30/// for the compiler to understand a condition should
31/// never occur.
32///
33/// Generally compilers assume UB won't happen. This macro
34/// offers the "best of both worlds", it provides a solid
35/// way of asserting/testing behavior in debug builds, and
36/// no cost in properly configured release builds.
37#[macro_export]
38macro_rules! inconceivable {
39 () => {
40 {
41 #[cfg(all(feature = "ub_inconceivable", feature = "RUSTC_VERSION_GE_1_27", not(feature = "std")))]
42 {
43 unsafe{ core::hint::unreachable_unchecked() }
44 }
45
46 #[cfg(all(feature = "ub_inconceivable", feature = "RUSTC_VERSION_GE_1_27", feature = "std"))]
47 {
48 unsafe{ std::hint::unreachable_unchecked() }
49 }
50
51
52 #[cfg(not(all(feature = "ub_inconceivable", feature = "RUSTC_VERSION_GE_1_27")))]
53 {
54 unreachable!()
55 }
56 }
57 };
58 ($msg: expr) => {
59 {
60 #[cfg(all(feature = "ub_inconceivable", feature = "RUSTC_VERSION_GE_1_27", not(feature = "std")))]
61 {
62 unsafe{ core::hint::unreachable_unchecked() }
63 }
64
65 #[cfg(all(feature = "ub_inconceivable", feature = "RUSTC_VERSION_GE_1_27", feature = "std"))]
66 {
67 unsafe{ std::hint::unreachable_unchecked() }
68 }
69
70
71 #[cfg(not(all(feature = "ub_inconceivable", feature = "RUSTC_VERSION_GE_1_27")))]
72 {
73 unreachable!($msg)
74 }
75 }
76 };
77 ($msg: expr,) => {
78 {
79 #[cfg(all(feature = "ub_inconceivable", feature = "RUSTC_VERSION_GE_1_27", not(feature = "std")))]
80 {
81 unsafe{ core::hint::unreachable_unchecked() }
82 }
83
84 #[cfg(all(feature = "ub_inconceivable", feature = "RUSTC_VERSION_GE_1_27", feature = "std"))]
85 {
86 unsafe{ std::hint::unreachable_unchecked() }
87 }
88
89
90 #[cfg(not(all(feature = "ub_inconceivable", feature = "RUSTC_VERSION_GE_1_27")))]
91 {
92 unreachable!($msg)
93 }
94 }
95 };
96 ($fmt: expr, $($arg:tt)*) => {
97 {
98 #[cfg(all(feature = "ub_inconceivable", feature = "RUSTC_VERSION_GE_1_27", not(feature = "std")))]
99 {
100 unsafe{ core::hint::unreachable_unchecked() }
101 }
102
103 #[cfg(all(feature = "ub_inconceivable", feature = "RUSTC_VERSION_GE_1_27", feature = "std"))]
104 {
105 unsafe{ std::hint::unreachable_unchecked() }
106 }
107
108 #[cfg(not(all(feature = "ub_inconceivable", feature = "RUSTC_VERSION_GE_1_27")))]
109 {
110 unreachable!($fmt, $($arg)*)
111 }
112 }
113 };
114}
115
116#[cfg(test)]
117mod test {
118
119 #[cfg(not(feature = "ub_inconceivable"))]
120 #[test]
121 #[should_panic]
122 fn test_expansion() {
123 match 10usize {
124 1 => {}
125 _ => inconceivable!(),
126 };
127 }
128}