haar_lib/macros/
convert.rs

1//! `impl_from!`, `impl_try_from!`
2
3/// [`From`]を実装する。
4#[macro_export]
5#[doc(hidden)]
6macro_rules! impl_from {
7    ($(#[$meta:meta])* $({$($t:tt)*})? $from:ty => $into:ty, $f:expr) => {
8        impl<$($($t)*)?> From<$from> for $into {
9            $(#[$meta])*
10            fn from(value: $from) -> Self {
11                $f(value)
12            }
13        }
14    };
15}
16
17/// [`TryFrom`]を実装する。
18#[macro_export]
19#[doc(hidden)]
20macro_rules! impl_try_from {
21    ($(#[$meta:meta])* $({$($t:tt)*})? $from:ty => $into:ty, type Error = $error:ty, $f:expr) => {
22        impl<$($($t)*)?> TryFrom<$from> for $into {
23            type Error = $error;
24            $(#[$meta])*
25            fn try_from(value: $from) -> Result<Self, Self::Error> {
26                $f(value)
27            }
28        }
29    };
30}