1use std::slice::SliceIndex;
2
3use crate::{Error, Result};
4
5pub const ARRAY_SIGNATURE_CHAR: char = 'a';
7pub const ARRAY_SIGNATURE_STR: &str = "a";
9pub(crate) const ARRAY_ALIGNMENT_DBUS: usize = 4;
10pub const STRUCT_SIG_START_CHAR: char = '(';
12pub const STRUCT_SIG_END_CHAR: char = ')';
14pub const STRUCT_SIG_START_STR: &str = "(";
17pub const STRUCT_SIG_END_STR: &str = ")";
20pub(crate) const STRUCT_ALIGNMENT_DBUS: usize = 8;
21pub const DICT_ENTRY_SIG_START_CHAR: char = '{';
23pub const DICT_ENTRY_SIG_END_CHAR: char = '}';
25pub const DICT_ENTRY_SIG_START_STR: &str = "{";
28pub const DICT_ENTRY_SIG_END_STR: &str = "}";
31pub(crate) const DICT_ENTRY_ALIGNMENT_DBUS: usize = 8;
32pub const VARIANT_SIGNATURE_CHAR: char = 'v';
34pub const VARIANT_SIGNATURE_STR: &str = "v";
36#[cfg(feature = "gvariant")]
37pub(crate) const VARIANT_ALIGNMENT_GVARIANT: usize = 8;
38#[cfg(feature = "gvariant")]
41#[deprecated(
42 since = "5.15.0",
43 note = "GVariant support is deprecated and will be removed in zvariant 6.0. Use the \
44 `zgvariant` crate instead."
45)]
46pub const MAYBE_SIGNATURE_CHAR: char = 'm';
47#[cfg(feature = "gvariant")]
50#[deprecated(
51 since = "5.15.0",
52 note = "GVariant support is deprecated and will be removed in zvariant 6.0. Use the \
53 `zgvariant` crate instead."
54)]
55pub const MAYBE_SIGNATURE_STR: &str = "m";
56
57#[doc(hidden)]
67pub fn padding_for_n_bytes(value: usize, align: usize) -> usize {
68 assert!(
69 align > 0 && align.is_power_of_two(),
70 "`align` must be a positive power of two"
71 );
72 let len_rounded_up = value.wrapping_add(align).wrapping_sub(1) & !align.wrapping_sub(1);
73
74 len_rounded_up.wrapping_sub(value)
75}
76
77pub(crate) fn usize_to_u32(value: usize) -> u32 {
78 assert!(value <= (u32::MAX as usize), "{value} too large for `u32`",);
79
80 value as u32
81}
82
83pub(crate) fn usize_to_u8(value: usize) -> u8 {
84 assert!(value <= (u8::MAX as usize), "{value} too large for `u8`",);
85
86 value as u8
87}
88
89pub(crate) fn subslice<I, T>(input: &[T], index: I) -> Result<&I::Output>
91where
92 I: SliceIndex<[T]>,
93{
94 input.get(index).ok_or(Error::OutOfBounds)
95}