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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
/// Implement [`From`] for a newtype struct.
///
/// The first argument is that of the newtype struct to create the impl for and the second is the
/// wrapped type.
///
/// # Examples
/// ```
/// use impl_more::impl_from;
/// struct Foo(String);
///
/// impl_from!(Foo, String);
/// let foo = Foo::from("bar".to_owned());
/// ```
#[macro_export]
macro_rules! impl_from {
($ty:ty, $inner:ty) => {
impl ::core::convert::From<$inner> for $ty {
fn from(inner: $inner) -> Self {
Self(inner)
}
}
};
}
/// Implement [`Into`] for a newtype struct.
///
/// The first argument is that of the newtype struct to create the impl for and the second is the
/// wrapped type.
///
/// # Examples
/// ```
/// use impl_more::impl_into;
/// struct Foo(String);
///
/// impl_into!(Foo, String);
///
/// let foo = Foo("bar".to_owned());
/// let foo_str: String = foo.into();
/// ```
#[macro_export]
macro_rules! impl_into {
($ty:ty, $inner:ty) => {
impl ::core::convert::Into<$inner> for $ty {
fn into(self) -> $inner {
self.0
}
}
};
}