|
| 1 | +/******************************************************************************* |
| 2 | +* (c) 2021 Zondax GmbH |
| 3 | +* |
| 4 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +* you may not use this file except in compliance with the License. |
| 6 | +* You may obtain a copy of the License at |
| 7 | +* |
| 8 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +* |
| 10 | +* Unless required by applicable law or agreed to in writing, software |
| 11 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +* See the License for the specific language governing permissions and |
| 14 | +* limitations under the License. |
| 15 | +********************************************************************************/ |
| 16 | + |
| 17 | +pub trait PanicHandler: Sized { |
| 18 | + type Item; |
| 19 | + |
| 20 | + fn unwrap_handler(self) -> Self::Item; |
| 21 | + |
| 22 | + fn unwrap_expect(self, s: &str) -> Self::Item; |
| 23 | +} |
| 24 | + |
| 25 | +impl<T, E> PanicHandler for Result<T, E> { |
| 26 | + type Item = T; |
| 27 | + |
| 28 | + #[inline] |
| 29 | + fn unwrap_handler(self) -> Self::Item { |
| 30 | + match self { |
| 31 | + Ok(t) => t, |
| 32 | + Err(_) => unsafe { core::hint::unreachable_unchecked() }, |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + #[inline] |
| 37 | + fn unwrap_expect(self, _: &str) -> Self::Item { |
| 38 | + match self { |
| 39 | + Ok(t) => t, |
| 40 | + Err(_) => unsafe { core::hint::unreachable_unchecked() }, |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +impl<T> PanicHandler for Option<T> { |
| 46 | + type Item = T; |
| 47 | + |
| 48 | + #[inline] |
| 49 | + fn unwrap_handler(self) -> Self::Item { |
| 50 | + match self { |
| 51 | + Some(t) => t, |
| 52 | + None => unsafe { core::hint::unreachable_unchecked() }, |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + #[inline] |
| 57 | + fn unwrap_expect(self, _: &str) -> Self::Item { |
| 58 | + match self { |
| 59 | + Some(t) => t, |
| 60 | + None => unsafe { core::hint::unreachable_unchecked() }, |
| 61 | + } |
| 62 | + } |
| 63 | +} |
0 commit comments