sel4_root_task/
termination.rs

1//
2// Copyright 2023, Colias Group, LLC
3//
4// SPDX-License-Identifier: BSD-2-Clause
5//
6
7use core::fmt;
8
9/// Trait for the return type of [`#[root_task]`](crate::root_task) main functions.
10pub trait Termination {
11    type Error: fmt::Debug;
12
13    fn report(self) -> Self::Error;
14}
15
16impl Termination for ! {
17    type Error = !;
18
19    fn report(self) -> Self::Error {
20        self
21    }
22}
23
24impl Termination for Never {
25    type Error = Never;
26
27    fn report(self) -> Self::Error {
28        self
29    }
30}
31
32impl<E: fmt::Debug> Termination for Result<!, E> {
33    type Error = E;
34
35    fn report(self) -> Self::Error {
36        match self {
37            #[allow(unreachable_patterns)]
38            Ok(absurdity) => match absurdity {},
39            Err(err) => err,
40        }
41    }
42}
43
44impl<E: fmt::Debug> Termination for Result<Never, E> {
45    type Error = E;
46
47    fn report(self) -> Self::Error {
48        match self {
49            #[allow(unreachable_patterns)]
50            Ok(absurdity) => match absurdity {},
51            Err(err) => err,
52        }
53    }
54}
55
56/// Stable alternative to `!`.
57///
58/// This type in uninhabited like `!`, but does not require the unstable `#[feature(never_type)]`.
59/// It implements [`Termination`], so it is useful in return types for
60/// [`#[root_task]`](crate::root_task) main functions.
61#[derive(Debug, Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)]
62pub enum Never {}
63
64impl fmt::Display for Never {
65    fn fmt(&self, _f: &mut fmt::Formatter) -> fmt::Result {
66        match *self {}
67    }
68}