zvariant/
basic.rs

1use crate::{serialized::Format, Signature, Type};
2
3/// Trait for basic types.
4///
5/// All basic types are also [`Type`] implementers.
6///
7/// [`Type`]: trait.Type.html
8/// [`Value`]: enum.Value.html
9pub trait Basic: Type {
10    /// The type signature, as a character.
11    const SIGNATURE_CHAR: char;
12    /// The type signature, as a string.
13    const SIGNATURE_STR: &'static str;
14
15    /// The required padding alignment for the given format.
16    ///
17    /// The default implementation covers all possible cases so you should never need to override
18    /// it.
19    fn alignment(format: Format) -> usize {
20        Self::SIGNATURE.alignment(format)
21    }
22}
23
24impl<B: ?Sized> Basic for &B
25where
26    B: Basic,
27{
28    const SIGNATURE_CHAR: char = B::SIGNATURE_CHAR;
29    const SIGNATURE_STR: &'static str = B::SIGNATURE_STR;
30}
31
32macro_rules! impl_type {
33    ($for:ty) => {
34        impl Type for $for {
35            const SIGNATURE: &'static Signature = {
36                match Self::SIGNATURE_CHAR {
37                    'y' => &Signature::U8,
38                    'b' => &Signature::Bool,
39                    'n' => &Signature::I16,
40                    'q' => &Signature::U16,
41                    'i' => &Signature::I32,
42                    'u' => &Signature::U32,
43                    'x' => &Signature::I64,
44                    't' => &Signature::U64,
45                    'd' => &Signature::F64,
46                    's' => &Signature::Str,
47                    'g' => &Signature::Signature,
48                    'o' => &Signature::ObjectPath,
49                    'v' => &Signature::Variant,
50                    #[cfg(unix)]
51                    'h' => &Signature::Fd,
52                    _ => unreachable!(),
53                }
54            };
55        }
56    };
57}
58
59impl Basic for u8 {
60    const SIGNATURE_CHAR: char = 'y';
61    const SIGNATURE_STR: &'static str = "y";
62}
63impl_type!(u8);
64
65impl Basic for std::num::NonZeroU8 {
66    const SIGNATURE_CHAR: char = u8::SIGNATURE_CHAR;
67    const SIGNATURE_STR: &'static str = u8::SIGNATURE_STR;
68}
69impl_type!(std::num::NonZeroU8);
70
71// No i8 type in D-Bus/GVariant, let's pretend it's i16
72impl Basic for i8 {
73    const SIGNATURE_CHAR: char = i16::SIGNATURE_CHAR;
74    const SIGNATURE_STR: &'static str = i16::SIGNATURE_STR;
75}
76impl_type!(i8);
77
78impl Basic for std::num::NonZeroI8 {
79    const SIGNATURE_CHAR: char = i8::SIGNATURE_CHAR;
80    const SIGNATURE_STR: &'static str = i8::SIGNATURE_STR;
81}
82impl_type!(std::num::NonZeroI8);
83
84impl Basic for bool {
85    const SIGNATURE_CHAR: char = 'b';
86    const SIGNATURE_STR: &'static str = "b";
87}
88impl_type!(bool);
89
90impl Basic for i16 {
91    const SIGNATURE_CHAR: char = 'n';
92    const SIGNATURE_STR: &'static str = "n";
93}
94impl_type!(i16);
95
96impl Basic for std::num::NonZeroI16 {
97    const SIGNATURE_CHAR: char = i16::SIGNATURE_CHAR;
98    const SIGNATURE_STR: &'static str = i16::SIGNATURE_STR;
99}
100impl_type!(std::num::NonZeroI16);
101
102impl Basic for u16 {
103    const SIGNATURE_CHAR: char = 'q';
104    const SIGNATURE_STR: &'static str = "q";
105}
106impl_type!(u16);
107
108impl Basic for std::num::NonZeroU16 {
109    const SIGNATURE_CHAR: char = u16::SIGNATURE_CHAR;
110    const SIGNATURE_STR: &'static str = u16::SIGNATURE_STR;
111}
112impl_type!(std::num::NonZeroU16);
113
114impl Basic for i32 {
115    const SIGNATURE_CHAR: char = 'i';
116    const SIGNATURE_STR: &'static str = "i";
117}
118impl_type!(i32);
119
120impl Basic for std::num::NonZeroI32 {
121    const SIGNATURE_CHAR: char = i32::SIGNATURE_CHAR;
122    const SIGNATURE_STR: &'static str = i32::SIGNATURE_STR;
123}
124impl_type!(std::num::NonZeroI32);
125
126impl Basic for u32 {
127    const SIGNATURE_CHAR: char = 'u';
128    const SIGNATURE_STR: &'static str = "u";
129}
130impl_type!(u32);
131
132impl Basic for std::num::NonZeroU32 {
133    const SIGNATURE_CHAR: char = u32::SIGNATURE_CHAR;
134    const SIGNATURE_STR: &'static str = u32::SIGNATURE_STR;
135}
136impl_type!(std::num::NonZeroU32);
137
138impl Basic for i64 {
139    const SIGNATURE_CHAR: char = 'x';
140    const SIGNATURE_STR: &'static str = "x";
141}
142impl_type!(i64);
143
144impl Basic for std::num::NonZeroI64 {
145    const SIGNATURE_CHAR: char = i64::SIGNATURE_CHAR;
146    const SIGNATURE_STR: &'static str = i64::SIGNATURE_STR;
147}
148impl_type!(std::num::NonZeroI64);
149
150impl Basic for u64 {
151    const SIGNATURE_CHAR: char = 't';
152    const SIGNATURE_STR: &'static str = "t";
153}
154impl_type!(u64);
155
156impl Basic for std::num::NonZeroU64 {
157    const SIGNATURE_CHAR: char = u64::SIGNATURE_CHAR;
158    const SIGNATURE_STR: &'static str = u64::SIGNATURE_STR;
159}
160impl_type!(std::num::NonZeroU64);
161
162// No f32 type in D-Bus/GVariant, let's pretend it's f64
163impl Basic for f32 {
164    const SIGNATURE_CHAR: char = f64::SIGNATURE_CHAR;
165    const SIGNATURE_STR: &'static str = f64::SIGNATURE_STR;
166}
167impl_type!(f32);
168
169impl Basic for f64 {
170    const SIGNATURE_CHAR: char = 'd';
171    const SIGNATURE_STR: &'static str = "d";
172}
173impl_type!(f64);
174
175impl Basic for str {
176    const SIGNATURE_CHAR: char = 's';
177    const SIGNATURE_STR: &'static str = "s";
178}
179impl_type!(str);
180
181impl Basic for String {
182    const SIGNATURE_CHAR: char = 's';
183    const SIGNATURE_STR: &'static str = "s";
184}
185impl_type!(String);
186
187impl Basic for char {
188    const SIGNATURE_CHAR: char = <&str>::SIGNATURE_CHAR;
189    const SIGNATURE_STR: &'static str = <&str>::SIGNATURE_STR;
190}
191impl_type!(char);