Debian Codegraph @

debian/rust-vendor/defmt/src/export/traits.rs

raw
1 #[allow(unused_imports)]
2 use crate as defmt;
3 use crate::{Format, Formatter, Str};
4
5 pub trait Truncate<U> {
6 fn truncate(self) -> U;
7 }
8
9 macro_rules! impl_truncate {
10 ($($from:ty => $into:ty),*) => {
11 $(impl Truncate<$into> for $from {
12 fn truncate(self) -> $into {
13 self as $into
14 }
15 })*
16 };
17 }
18
19 // We implement `Truncate<X> for X` so that the macro can unconditionally use it,
20 // even if no truncation is performed.
21 impl_truncate!(
22 u8 => u8,
23 u16 => u8,
24 u32 => u8,
25 u64 => u8,
26 u128 => u8,
27 u16 => u16,
28 u32 => u16,
29 u64 => u16,
30 u128 => u16,
31 u32 => u32,
32 u64 => u32,
33 u128 => u32,
34 u64 => u64,
35 u128 => u64,
36 u128 => u128
37 );
38
39 #[derive(Debug, Copy, Clone, Eq, PartialEq)]
40 pub struct NoneError;
41
42 impl Format for NoneError {
43 fn format(&self, _fmt: Formatter) {
44 unreachable!();
45 }
46
47 fn _format_tag() -> Str {
48 defmt_macros::internp!("Unwrap of a None option value")
49 }
50
51 fn _format_data(&self) {}
52 }
53
54 /// Transform `self` into a `Result`
55 ///
56 /// # Call sites
57 /// * [`defmt::unwrap!`]
58 pub trait IntoResult {
59 type Ok;
60 type Error;
61 fn into_result(self) -> Result<Self::Ok, Self::Error>;
62 }
63
64 impl<T> IntoResult for Option<T> {
65 type Ok = T;
66 type Error = NoneError;
67
68 #[inline]
69 fn into_result(self) -> Result<T, NoneError> {
70 self.ok_or(NoneError)
71 }
72 }
73
74 impl<T, E> IntoResult for Result<T, E> {
75 type Ok = T;
76 type Error = E;
77
78 #[inline]
79 fn into_result(self) -> Self {
80 self
81 }
82 }