use std::slice::SliceIndex;
use crate::{Error, Result};
pub const ARRAY_SIGNATURE_CHAR: char = 'a';
pub const ARRAY_SIGNATURE_STR: &str = "a";
pub(crate) const ARRAY_ALIGNMENT_DBUS: usize = 4;
pub const STRUCT_SIG_START_CHAR: char = '(';
pub const STRUCT_SIG_END_CHAR: char = ')';
pub const STRUCT_SIG_START_STR: &str = "(";
pub const STRUCT_SIG_END_STR: &str = ")";
pub(crate) const STRUCT_ALIGNMENT_DBUS: usize = 8;
pub const DICT_ENTRY_SIG_START_CHAR: char = '{';
pub const DICT_ENTRY_SIG_END_CHAR: char = '}';
pub const DICT_ENTRY_SIG_START_STR: &str = "{";
pub const DICT_ENTRY_SIG_END_STR: &str = "}";
pub(crate) const DICT_ENTRY_ALIGNMENT_DBUS: usize = 8;
pub const VARIANT_SIGNATURE_CHAR: char = 'v';
pub const VARIANT_SIGNATURE_STR: &str = "v";
#[cfg(feature = "gvariant")]
pub(crate) const VARIANT_ALIGNMENT_GVARIANT: usize = 8;
#[cfg(feature = "gvariant")]
pub const MAYBE_SIGNATURE_CHAR: char = 'm';
#[cfg(feature = "gvariant")]
pub const MAYBE_SIGNATURE_STR: &str = "m";
#[doc(hidden)]
pub fn padding_for_n_bytes(value: usize, align: usize) -> usize {
assert!(
align > 0 && align.is_power_of_two(),
"`align` must be a positive power of two"
);
let len_rounded_up = value.wrapping_add(align).wrapping_sub(1) & !align.wrapping_sub(1);
len_rounded_up.wrapping_sub(value)
}
pub(crate) fn usize_to_u32(value: usize) -> u32 {
assert!(value <= (u32::MAX as usize), "{value} too large for `u32`",);
value as u32
}
pub(crate) fn usize_to_u8(value: usize) -> u8 {
assert!(value <= (u8::MAX as usize), "{value} too large for `u8`",);
value as u8
}
pub(crate) fn subslice<I, T>(input: &[T], index: I) -> Result<&I::Output>
where
I: SliceIndex<[T]>,
{
input.get(index).ok_or(Error::OutOfBounds)
}