zbus_names/
utils.rs
1macro_rules! impl_str_basic {
2 ($type:ty) => {
3 impl zvariant::Basic for $type {
4 const SIGNATURE_CHAR: char = <zvariant::Str<'_>>::SIGNATURE_CHAR;
5 const SIGNATURE_STR: &'static str = <zvariant::Str<'_>>::SIGNATURE_STR;
6
7 fn alignment(format: zvariant::serialized::Format) -> usize {
8 <zvariant::Str<'_>>::alignment(format)
9 }
10 }
11 };
12}
13
14macro_rules! impl_try_from {
15 (ty: $type:ty, owned_ty: $owned_type:ty, validate_fn: $validate_fn:ident, try_from: [$($from:ty),*],) => {
16 $(
17 impl<'s> TryFrom<$from> for $type {
18 type Error = Error;
19
20 fn try_from(value: $from) -> Result<Self> {
21 let value = Str::from(value);
22 $validate_fn(value.as_str())?;
23 Ok(Self(value))
24 }
25 }
26
27 impl<'s> TryFrom<$from> for $owned_type {
28 type Error = Error;
29
30 fn try_from(value: $from) -> Result<Self> {
31 Ok(Self::from(<$type>::try_from(value)?))
32 }
33 }
34 )*
35 };
36}
37
38pub(crate) use impl_str_basic;
39pub(crate) use impl_try_from;