musli_binary_common/
macros.rs

1/// Declare an error representation.
2#[macro_export]
3macro_rules! decl_message_repr {
4    ($vis:vis $ident:ident, $fallback:literal) => {
5        #[cfg(feature = "std")]
6        $vis struct $ident(Box<str>);
7
8        #[cfg(feature = "std")]
9        impl $ident {
10            $vis fn collect<T>(message: T) -> Self where T: core::fmt::Display {
11                Self(message.to_string().into())
12            }
13        }
14
15        #[cfg(feature = "std")]
16        impl core::fmt::Debug for $ident {
17            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
18                self.0.fmt(f)
19            }
20        }
21
22        #[cfg(feature = "std")]
23        impl core::fmt::Display for $ident {
24            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
25                self.0.fmt(f)
26            }
27        }
28
29        #[cfg(not(feature = "std"))]
30        $vis struct $ident;
31
32        #[cfg(not(feature = "std"))]
33        impl $ident {
34            $vis fn collect<T>(_: T) -> Self where T: core::fmt::Display {
35                Self
36            }
37        }
38
39        #[cfg(not(feature = "std"))]
40        impl core::fmt::Debug for $ident {
41            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
42                $fallback.fmt(f)
43            }
44        }
45
46        #[cfg(not(feature = "std"))]
47        impl core::fmt::Display for $ident {
48            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
49                $fallback.fmt(f)
50            }
51        }
52    }
53}