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
use crate::ty;
/// The alignment of a D-Bus type, as a runtime value.
///
/// D-Bus values are padded to the alignment of their type, which for the static
/// writers in this crate is derived from a [`ty::Marker`]. Code which works with
/// types that are only known at runtime needs to pass it explicitly instead.
///
/// # Examples
///
/// ```
/// use tokio_dbus::{ty, Alignment};
///
/// assert_eq!(Alignment::of::<u8>(), Alignment::BYTE);
/// assert_eq!(Alignment::of::<ty::Str>(), Alignment::U32);
/// assert_eq!(Alignment::of::<(u8, u8)>(), Alignment::U64);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum Alignment {
/// Alignment of one byte, used by `y`, `g` and `v`.
BYTE,
/// Alignment of two bytes, used by `n` and `q`.
U16,
/// Alignment of four bytes, used by `b`, `i`, `u`, `h`, `s`, `o` and by the
/// length prefix of an array.
U32,
/// Alignment of eight bytes, used by `x`, `t`, `d`, structs and dict
/// entries.
U64,
}
impl Alignment {
/// The alignment of the type described by the [`ty::Marker`] `T`.
///
/// # Examples
///
/// ```
/// use tokio_dbus::{ty, Alignment};
///
/// assert_eq!(Alignment::of::<u64>(), Alignment::U64);
/// assert_eq!(Alignment::of::<ty::Variant>(), Alignment::BYTE);
/// ```
pub const fn of<T>() -> Self
where
T: ty::Aligned,
{
match align_of::<T::Alignment>() {
1 => Alignment::BYTE,
2 => Alignment::U16,
4 => Alignment::U32,
_ => Alignment::U64,
}
}
/// The alignment as a number of bytes, which is always a power of two.
///
/// # Examples
///
/// ```
/// use tokio_dbus::Alignment;
///
/// assert_eq!(Alignment::BYTE.in_bytes(), 1);
/// assert_eq!(Alignment::U64.in_bytes(), 8);
/// ```
pub const fn in_bytes(self) -> usize {
match self {
Alignment::BYTE => 1,
Alignment::U16 => 2,
Alignment::U32 => 4,
Alignment::U64 => 8,
}
}
}