pub use self::fields::Fields;
mod fields;
pub use self::r#unsized::Unsized;
mod r#unsized;
pub use self::marker::Marker;
pub(crate) mod marker;
pub use self::aligned::Aligned;
pub(crate) mod aligned;
use core::marker::PhantomData;
use crate::error::ErrorKind;
use crate::signature::SignatureBuilder;
use crate::{Body, Error, LoadArray, Result, SignatureError};
#[non_exhaustive]
pub struct Str;
impl_trait_unsized_marker!(Str, u32, str, STRING);
#[non_exhaustive]
pub struct Signature;
impl_trait_unsized_marker!(Signature, u8, crate::Signature, SIGNATURE);
#[non_exhaustive]
pub struct ObjectPath;
impl_trait_unsized_marker!(ObjectPath, u8, crate::ObjectPath, OBJECT_PATH);
pub struct Array<T>(PhantomData<T>);
impl<T> self::aligned::sealed::Sealed for Array<T> {}
impl<T> Aligned for Array<T> {
type Alignment = u32;
}
impl<T> self::marker::sealed::Sealed for Array<T> where T: Marker {}
impl<T> Marker for Array<T>
where
T: Marker,
{
type Return<'de> = LoadArray<'de, T>;
#[inline]
fn load_struct<'de>(buf: &mut Body<'de>) -> Result<Self::Return<'de>> {
buf.load_array::<T>()
}
#[inline]
fn write_signature(signature: &mut SignatureBuilder) -> Result<(), SignatureError> {
signature.open_array()?;
T::write_signature(signature)?;
signature.close_array();
Ok(())
}
}
pub struct Dict<K, V>(PhantomData<(K, V)>);
impl<K, V> self::aligned::sealed::Sealed for Dict<K, V> {}
impl<K, V> Aligned for Dict<K, V> {
type Alignment = u64;
}
impl<K, V> self::marker::sealed::Sealed for Dict<K, V>
where
K: Marker,
V: Marker,
{
}
impl<K, V> Marker for Dict<K, V>
where
K: Marker,
V: Marker,
{
type Return<'de> = (K::Return<'de>, V::Return<'de>);
#[inline]
fn load_struct<'de>(buf: &mut Body<'de>) -> Result<Self::Return<'de>> {
buf.align::<u64>()?;
Ok((K::load_struct(buf)?, V::load_struct(buf)?))
}
#[inline]
fn write_signature(signature: &mut SignatureBuilder) -> Result<(), SignatureError> {
signature.open_dict()?;
K::write_signature(signature)?;
V::write_signature(signature)?;
signature.close_dict()?;
Ok(())
}
}
#[non_exhaustive]
pub struct Bool;
impl self::aligned::sealed::Sealed for Bool {}
impl Aligned for Bool {
type Alignment = u32;
}
impl self::marker::sealed::Sealed for Bool {}
impl Marker for Bool {
type Return<'de> = bool;
#[inline]
fn load_struct<'de>(buf: &mut Body<'de>) -> Result<Self::Return<'de>> {
Ok(buf.load::<u32>()? != 0)
}
#[inline]
fn write_signature(signature: &mut SignatureBuilder) -> Result<(), SignatureError> {
if !signature.extend_from_signature(crate::Signature::BOOLEAN) {
return Err(SignatureError::too_long());
}
Ok(())
}
}
#[non_exhaustive]
pub struct Variant;
impl self::aligned::sealed::Sealed for Variant {}
impl Aligned for Variant {
type Alignment = u8;
}
impl self::marker::sealed::Sealed for Variant {}
impl Marker for Variant {
type Return<'de> = crate::Variant<'de>;
#[inline]
fn load_struct<'de>(buf: &mut Body<'de>) -> Result<Self::Return<'de>> {
let signature: &crate::Signature = buf.read()?;
let variant = match signature.as_bytes() {
b"b" => crate::Variant::Bool(buf.load::<u32>()? != 0),
b"y" => crate::Variant::U8(buf.load()?),
b"n" => crate::Variant::I16(buf.load()?),
b"q" => crate::Variant::U16(buf.load()?),
b"i" => crate::Variant::I32(buf.load()?),
b"u" => crate::Variant::U32(buf.load()?),
b"x" => crate::Variant::I64(buf.load()?),
b"t" => crate::Variant::U64(buf.load()?),
b"d" => crate::Variant::F64(buf.load()?),
b"s" => crate::Variant::String(buf.read()?),
b"o" => crate::Variant::ObjectPath(buf.read()?),
b"g" => crate::Variant::Signature(buf.read()?),
#[cfg(feature = "alloc")]
_ => {
return Err(Error::new(ErrorKind::UnsupportedVariant(signature.into())));
}
#[cfg(not(feature = "alloc"))]
_ => {
return Err(Error::new(ErrorKind::UnsupportedVariantNoAlloc));
}
};
Ok(variant)
}
#[inline]
fn write_signature(signature: &mut SignatureBuilder) -> Result<(), SignatureError> {
if !signature.extend_from_signature(crate::Signature::VARIANT) {
return Err(SignatureError::too_long());
}
Ok(())
}
}