zbus_names/
utils.rs

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
macro_rules! impl_try_from {
    (ty: $type:ty, owned_ty: $owned_type:ty, validate_fn: $validate_fn:ident, try_from: [$($from:ty),*],) => {
        $(
            impl<'s> TryFrom<$from> for $type {
                type Error = Error;

                fn try_from(value: $from) -> Result<Self> {
                    let value = Str::from(value);
                    $validate_fn(value.as_str())?;
                    Ok(Self(value))
                }
            }

            impl<'s> TryFrom<$from> for $owned_type {
                type Error = Error;

                fn try_from(value: $from) -> Result<Self> {
                    Ok(Self::from(<$type>::try_from(value)?))
                }
            }
        )*
    };
}

pub(crate) use impl_try_from;