zbus 6.2.0

API for D-Bus communication and its wire format
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
use crate::wire::{Signature, Type, impl_type_with_repr};

/// Trait for basic types.
///
/// All basic types are also [`Type`] implementers.
///
/// [`Type`]: trait.Type.html
/// [`Variant`]: enum.Variant.html
pub trait Basic: Type {
    /// The type signature, as a character.
    const SIGNATURE_CHAR: char;
    /// The type signature, as a string.
    const SIGNATURE_STR: &'static str;

    /// The required padding alignment.
    ///
    /// The default implementation covers all possible cases so you should never need to override
    /// it.
    fn alignment() -> usize {
        Self::SIGNATURE.alignment_dbus()
    }
}

macro_rules! impl_type {
    ($for:ty) => {
        impl Type for $for {
            const SIGNATURE: &'static Signature = {
                match Self::SIGNATURE_CHAR {
                    'y' => &Signature::U8,
                    'b' => &Signature::Bool,
                    'n' => &Signature::I16,
                    'q' => &Signature::U16,
                    'i' => &Signature::I32,
                    'u' => &Signature::U32,
                    'x' => &Signature::I64,
                    't' => &Signature::U64,
                    'd' => &Signature::F64,
                    's' => &Signature::Str,
                    'g' => &Signature::Signature,
                    'o' => &Signature::ObjectPath,
                    'v' => &Signature::Variant,
                    #[cfg(unix)]
                    'h' => &Signature::Fd,
                    _ => unreachable!(),
                }
            };
        }
    };
}

impl Basic for u8 {
    const SIGNATURE_CHAR: char = 'y';
    const SIGNATURE_STR: &'static str = "y";
}
impl_type!(u8);

impl Basic for std::num::NonZeroU8 {
    const SIGNATURE_CHAR: char = u8::SIGNATURE_CHAR;
    const SIGNATURE_STR: &'static str = u8::SIGNATURE_STR;
}
impl_type!(std::num::NonZeroU8);

// No i8 type in D-Bus/GVariant, let's pretend it's i16
impl Basic for i8 {
    const SIGNATURE_CHAR: char = i16::SIGNATURE_CHAR;
    const SIGNATURE_STR: &'static str = i16::SIGNATURE_STR;
}
impl_type!(i8);

impl Basic for std::num::NonZeroI8 {
    const SIGNATURE_CHAR: char = i8::SIGNATURE_CHAR;
    const SIGNATURE_STR: &'static str = i8::SIGNATURE_STR;
}
impl_type!(std::num::NonZeroI8);

impl Basic for bool {
    const SIGNATURE_CHAR: char = 'b';
    const SIGNATURE_STR: &'static str = "b";
}
impl_type!(bool);

impl Basic for i16 {
    const SIGNATURE_CHAR: char = 'n';
    const SIGNATURE_STR: &'static str = "n";
}
impl_type!(i16);

impl Basic for std::num::NonZeroI16 {
    const SIGNATURE_CHAR: char = i16::SIGNATURE_CHAR;
    const SIGNATURE_STR: &'static str = i16::SIGNATURE_STR;
}
impl_type!(std::num::NonZeroI16);

impl Basic for u16 {
    const SIGNATURE_CHAR: char = 'q';
    const SIGNATURE_STR: &'static str = "q";
}
impl_type!(u16);

impl Basic for std::num::NonZeroU16 {
    const SIGNATURE_CHAR: char = u16::SIGNATURE_CHAR;
    const SIGNATURE_STR: &'static str = u16::SIGNATURE_STR;
}
impl_type!(std::num::NonZeroU16);

impl Basic for i32 {
    const SIGNATURE_CHAR: char = 'i';
    const SIGNATURE_STR: &'static str = "i";
}
impl_type!(i32);

impl Basic for std::num::NonZeroI32 {
    const SIGNATURE_CHAR: char = i32::SIGNATURE_CHAR;
    const SIGNATURE_STR: &'static str = i32::SIGNATURE_STR;
}
impl_type!(std::num::NonZeroI32);

impl Basic for u32 {
    const SIGNATURE_CHAR: char = 'u';
    const SIGNATURE_STR: &'static str = "u";
}
impl_type!(u32);

impl Basic for std::num::NonZeroU32 {
    const SIGNATURE_CHAR: char = u32::SIGNATURE_CHAR;
    const SIGNATURE_STR: &'static str = u32::SIGNATURE_STR;
}
impl_type!(std::num::NonZeroU32);

impl Basic for i64 {
    const SIGNATURE_CHAR: char = 'x';
    const SIGNATURE_STR: &'static str = "x";
}
impl_type!(i64);

impl Basic for std::num::NonZeroI64 {
    const SIGNATURE_CHAR: char = i64::SIGNATURE_CHAR;
    const SIGNATURE_STR: &'static str = i64::SIGNATURE_STR;
}
impl_type!(std::num::NonZeroI64);

impl Basic for u64 {
    const SIGNATURE_CHAR: char = 't';
    const SIGNATURE_STR: &'static str = "t";
}
impl_type!(u64);

impl Basic for std::num::NonZeroU64 {
    const SIGNATURE_CHAR: char = u64::SIGNATURE_CHAR;
    const SIGNATURE_STR: &'static str = u64::SIGNATURE_STR;
}
impl_type!(std::num::NonZeroU64);

// No f32 type in D-Bus/GVariant, let's pretend it's f64
impl Basic for f32 {
    const SIGNATURE_CHAR: char = f64::SIGNATURE_CHAR;
    const SIGNATURE_STR: &'static str = f64::SIGNATURE_STR;
}
impl_type!(f32);

impl Basic for f64 {
    const SIGNATURE_CHAR: char = 'd';
    const SIGNATURE_STR: &'static str = "d";
}
impl_type!(f64);

impl Basic for str {
    const SIGNATURE_CHAR: char = 's';
    const SIGNATURE_STR: &'static str = "s";
}
impl_type!(str);

impl Basic for String {
    const SIGNATURE_CHAR: char = 's';
    const SIGNATURE_STR: &'static str = "s";
}
impl_type!(String);

impl Basic for char {
    const SIGNATURE_CHAR: char = <&str>::SIGNATURE_CHAR;
    const SIGNATURE_STR: &'static str = <&str>::SIGNATURE_STR;
}
impl_type!(char);

////////////////////////////////////////////////////////////////////////////////

impl Basic for usize {
    const SIGNATURE_CHAR: char = <u64 as Basic>::SIGNATURE_CHAR;
    const SIGNATURE_STR: &'static str = <u64 as Basic>::SIGNATURE_STR;
}

impl_type_with_repr! {
    // usize is serialized as u64:
    // https://github.com/serde-rs/serde/blob/9b868ef831c95f50dd4bde51a7eb52e3b9ee265a/serde/src/ser/impls.rs#L28
    usize => u64 {
        usize {
            samples = [usize::MAX, usize::MIN],
            repr(n) = n as u64,
        }
    }
}

impl_type_with_repr! {
    // isize is serialized as i64:
    // https://github.com/serde-rs/serde/blob/9b868ef831c95f50dd4bde51a7eb52e3b9ee265a/serde/src/ser/impls.rs#L22
    isize => i64 {
        isize {
            samples = [isize::MAX, isize::MIN],
            repr(n) = n as i64,
        }
    }
}

impl Basic for isize {
    const SIGNATURE_CHAR: char = <i64 as Basic>::SIGNATURE_CHAR;
    const SIGNATURE_STR: &'static str = <i64 as Basic>::SIGNATURE_STR;
}

////////////////////////////////////////////////////////////////////////////////

macro_rules! impl_basic_for_deref {
    (
        $type:ty,
        <$($desc:tt)+
    ) => {
        impl <$($desc)+ {
            const SIGNATURE_CHAR: char = <$type>::SIGNATURE_CHAR;
            const SIGNATURE_STR: &'static str = <$type>::SIGNATURE_STR;
        }
    };
}

impl_basic_for_deref!(T, <T: ?Sized + Basic> Basic for &T);
impl_basic_for_deref!(T, <T: ?Sized + Basic> Basic for &mut T);
impl_basic_for_deref!(T, <T: ?Sized + Basic + ToOwned> Basic for std::borrow::Cow<'_,T>);
impl_basic_for_deref!(T, <T: ?Sized + Basic> Basic for std::sync::Arc<T>);
impl_basic_for_deref!(T, <T: ?Sized + Basic> Basic for std::sync::Weak<T>);
impl_basic_for_deref!(T, <T: ?Sized + Basic> Basic for std::sync::Mutex<T>);
impl_basic_for_deref!(T, <T: ?Sized + Basic> Basic for std::sync::RwLock<T>);
impl_basic_for_deref!(T, <T: ?Sized + Basic> Basic for std::boxed::Box<T>);
impl_basic_for_deref!(T, <T: ?Sized + Basic> Basic for std::rc::Rc<T>);
impl_basic_for_deref!(T, <T: ?Sized + Basic> Basic for std::rc::Weak<T>);
impl_basic_for_deref!(T, <T: ?Sized + Basic> Basic for std::cell::Cell<T>);
impl_basic_for_deref!(T, <T: ?Sized + Basic> Basic for std::cell::RefCell<T>);

////////////////////////////////////////////////////////////////////////////////

use std::{
    cmp::Reverse,
    num::{Saturating, Wrapping},
};

macro_rules! impl_basic_for_wrapper {
    ($($wrapper:ident<$T:ident>),+) => {
        $(
            impl<$T: Basic> Basic for $wrapper<$T> {
                const SIGNATURE_CHAR: char = <$T>::SIGNATURE_CHAR;
                const SIGNATURE_STR: &'static str = <$T>::SIGNATURE_STR;
            }
        )+
    };
}

impl_basic_for_wrapper!(Wrapping<T>, Saturating<T>, Reverse<T>);

////////////////////////////////////////////////////////////////////////////////

macro_rules! atomic_impl {
    ($($ty:ident $size:expr => $primitive:ident)*) => {
        $(
            #[cfg(target_has_atomic = $size)]
            impl Basic for $ty {
                const SIGNATURE_CHAR: char = <$primitive as Basic>::SIGNATURE_CHAR;
                const SIGNATURE_STR: &'static str = <$primitive as Basic>::SIGNATURE_STR;
            }
            impl_type!($ty);
        )*
    }
}

use std::sync::atomic::{
    AtomicBool, AtomicI8, AtomicI16, AtomicI32, AtomicIsize, AtomicU8, AtomicU16, AtomicU32,
    AtomicUsize,
};
#[cfg(target_has_atomic = "64")]
use std::sync::atomic::{AtomicI64, AtomicU64};

atomic_impl! {
    AtomicBool "8" => bool
    AtomicI8 "8" => i8
    AtomicI16 "16" => i16
    AtomicI32 "32" => i32
    AtomicIsize "ptr" => isize
    AtomicU8 "8" => u8
    AtomicU16 "16" => u16
    AtomicU32 "32" => u32
    AtomicUsize "ptr" => usize
}

#[cfg(target_has_atomic = "64")]
atomic_impl! {
    AtomicI64 "64" => i64
    AtomicU64 "64" => u64
}

////////////////////////////////////////////////////////////////////////////////

#[cfg(feature = "heapless")]
impl<const CAP: usize> Basic for heapless::String<CAP> {
    const SIGNATURE_CHAR: char = <&str as Basic>::SIGNATURE_CHAR;
    const SIGNATURE_STR: &'static str = <&str as Basic>::SIGNATURE_STR;
}

////////////////////////////////////////////////////////////////////////////////

macro_rules! impl_basic_with_repr {
    ($($ty:ty => $repr:ty),+ $(,)?) => {
        $(
            impl Basic for $ty {
                const SIGNATURE_CHAR: char = <$repr as Basic>::SIGNATURE_CHAR;
                const SIGNATURE_STR: &'static str = <$repr as Basic>::SIGNATURE_STR;
            }
        )+
    };
}

impl_basic_with_repr!(
    std::path::Path => str,
    std::path::PathBuf => str,
);

impl<T> Basic for [T; 0]
where
    T: Type,
{
    const SIGNATURE_CHAR: char = u8::SIGNATURE_CHAR;
    const SIGNATURE_STR: &'static str = u8::SIGNATURE_STR;
}

#[cfg(feature = "arrayvec")]
impl<const CAP: usize> Basic for arrayvec::ArrayString<CAP> {
    const SIGNATURE_CHAR: char = str::SIGNATURE_CHAR;
    const SIGNATURE_STR: &'static str = str::SIGNATURE_STR;
}

#[cfg(feature = "camino")]
impl_basic_with_repr!(
    camino::Utf8Path => str,
    camino::Utf8PathBuf => str,
);

#[cfg(feature = "url")]
impl_basic_with_repr!(url::Url => str);

#[cfg(feature = "time")]
impl_basic_with_repr!(
    time::Month => u8,
    time::Weekday => u8,
);

#[cfg(feature = "chrono")]
impl<Tz> Basic for chrono::DateTime<Tz>
where
    Tz: chrono::TimeZone,
{
    const SIGNATURE_CHAR: char = str::SIGNATURE_CHAR;
    const SIGNATURE_STR: &'static str = str::SIGNATURE_STR;
}

#[cfg(feature = "chrono")]
impl_basic_with_repr!(
    chrono::Month => str,
    chrono::NaiveDate => str,
    chrono::NaiveDateTime => str,
    chrono::NaiveTime => str,
    chrono::Weekday => str,
);

#[cfg(test)]
mod tests {
    use static_assertions::assert_impl_all;

    use super::Basic;

    assert_impl_all!([u8; 0]: Basic);
    assert_impl_all!(std::path::Path: Basic);
    assert_impl_all!(std::path::PathBuf: Basic);
    assert_impl_all!(crate::Optional<u8>: Basic);

    #[cfg(feature = "arrayvec")]
    assert_impl_all!(arrayvec::ArrayString<8>: Basic);

    #[cfg(feature = "camino")]
    assert_impl_all!(camino::Utf8Path: Basic);
    #[cfg(feature = "camino")]
    assert_impl_all!(camino::Utf8PathBuf: Basic);

    #[cfg(feature = "url")]
    assert_impl_all!(url::Url: Basic);

    #[cfg(feature = "time")]
    assert_impl_all!(time::Month: Basic);
    #[cfg(feature = "time")]
    assert_impl_all!(time::Weekday: Basic);

    #[cfg(feature = "chrono")]
    assert_impl_all!(chrono::DateTime<chrono::Utc>: Basic);
    #[cfg(feature = "chrono")]
    assert_impl_all!(chrono::Month: Basic);
    #[cfg(feature = "chrono")]
    assert_impl_all!(chrono::NaiveDate: Basic);
    #[cfg(feature = "chrono")]
    assert_impl_all!(chrono::NaiveDateTime: Basic);
    #[cfg(feature = "chrono")]
    assert_impl_all!(chrono::NaiveTime: Basic);
    #[cfg(feature = "chrono")]
    assert_impl_all!(chrono::Weekday: Basic);

    #[cfg(feature = "comms")]
    assert_impl_all!(crate::fdo::RequestNameFlags: Basic);
    #[cfg(feature = "comms")]
    assert_impl_all!(crate::message::Flags: Basic);
    #[cfg(feature = "proxy")]
    assert_impl_all!(crate::proxy::MethodFlags: Basic);
}