#[macro_export]
macro_rules! impl_from {
(<$($generic:ident),+> in $($rest:tt)+) => {
$crate::impl_from!(@impl [$($generic),+] $($rest)+);
};
(<const $($rest:tt)+) => {
$crate::__impl_more_parse_generics!(@parse impl_from [] [] const $($rest)+);
};
(<$generic:ident, $($rest:tt)+) => {
$crate::__impl_more_parse_generics!(@parse impl_from [] [] $generic, $($rest)+);
};
(@impl [$($generic:tt)*] $from:ty => $this:ty $(,)?) => {
impl <$($generic)*> ::core::convert::From<$from> for $this {
fn from(from: $from) -> Self {
Self(from)
}
}
};
(@impl [$($generic:tt)*] $from:ty => $this:ty : $field:ident in $($wrapper:tt)+) => {
$crate::impl_from!(@wrap [$($generic)*] $from => $this : $field in $($wrapper)+);
};
(@impl [$($generic:tt)*] $from:ty => $this:ty : $field:ident $(,)?) => {
impl <$($generic)*> ::core::convert::From<$from> for $this {
fn from(from: $from) -> Self {
Self { $field: from }
}
}
};
(@impl [$($generic:tt)*] $from:ty => $($target:tt)+) => {
$crate::impl_from!(@tuple [$($generic)*] [$from] [] $($target)+);
};
(@tuple [$($generic:tt)*] [$from:ty] [$($this:tt)+] in $($wrapper:tt)+) => {
$crate::impl_from!(@wrap [$($generic)*] $from => $($this)+ : 0 in $($wrapper)+);
};
(@tuple [$($generic:tt)*] [$from:ty] [$($this:tt)*] $next:tt $($rest:tt)+) => {
$crate::impl_from!(@tuple [$($generic)*] [$from] [$($this)* $next] $($rest)+);
};
(@wrap [$($generic:tt)*] $from:ty => $this:ty : $field:tt in $($wrapper:tt)+) => {
impl <$($generic)*> ::core::convert::From<$from> for $this {
fn from(value: $from) -> Self {
Self { $field: $crate::impl_from!(@new value; $($wrapper)+) }
}
}
};
(@new $value:ident; $($segment:ident)::+ $(,)?) => { $($segment)::+::new($value) };
(@new $value:ident; ::$($segment:ident)::+ $(,)?) => { ::$($segment)::+::new($value) };
(@new $value:ident; $wrapper:ty $(,)?) => { <$wrapper>::new($value) };
($from:ty => $($target:tt)+) => {
$crate::impl_from!(@impl [] $from => $($target)+);
};
}
#[macro_export]
macro_rules! impl_from_for_primitive {
($from:ty => $this:ty $(,)?) => {
impl ::core::convert::From<$from> for $this {
fn from(from: $from) -> $this {
<$this as ::core::convert::From<_>>::from(from.0)
}
}
};
}
#[macro_export]
macro_rules! impl_newtype_from_into {
($newtype:ty [<=>] $inner:ty $(,)?) => {
impl ::core::convert::From<$inner> for $newtype {
fn from(from: $inner) -> $newtype {
Self(from)
}
}
impl ::core::convert::From<$newtype> for $inner {
fn from(from: $newtype) -> $inner {
from.0
}
}
};
}
#[macro_export]
macro_rules! impl_into {
(<$($generic:ident),+> in $($rest:tt)+) => {
$crate::impl_into!(@impl [$($generic),+] $($rest)+);
};
(<const $($rest:tt)+) => {
$crate::__impl_more_parse_generics!(@parse impl_into [] [] const $($rest)+);
};
(<$generic:ident, $($rest:tt)+) => {
$crate::__impl_more_parse_generics!(@parse impl_into [] [] $generic, $($rest)+);
};
(@impl [$($generic:tt)+] $this:ty => $inner:ty : $field:ident) => {
impl <$($generic)+> ::core::convert::Into<$inner> for $this {
fn into(self) -> $inner {
self.$field
}
}
};
(@impl [$($generic:tt)+] $this:ty => $inner:ty) => {
impl <$($generic)+> ::core::convert::Into<$inner> for $this {
fn into(self) -> $inner {
self.0
}
}
};
($this:ty => $inner:ty) => {
impl ::core::convert::Into<$inner> for $this {
fn into(self) -> $inner {
self.0
}
}
};
($this:ty => $inner:ty : $field:ident) => {
impl ::core::convert::Into<$inner> for $this {
fn into(self) -> $inner {
self.$field
}
}
};
}
#[cfg(test)]
mod tests {
#![allow(clippy::from_over_into)]
use alloc::rc::Rc;
#[test]
fn reference_counted_fields() {
struct Local {
rc: Rc<u32>,
}
impl_from!(u32 => Local : rc in Rc);
impl_from!(Rc<u32> => Local : rc);
struct Shared(alloc::sync::Arc<u32>);
impl_from!(u32 => Shared in alloc::sync::Arc);
struct Named {
arc: u32,
}
impl_from!(u32 => Named : arc);
let local = Local::from(42).rc;
assert_eq!(*local, 42);
assert!(Rc::ptr_eq(&local, &Local::from(local.clone()).rc));
assert_eq!(*Shared::from(7).0, 7);
assert_eq!(Named::from(9).arc, 9);
}
#[test]
fn newtype() {
struct Foo(usize);
impl_from!(usize => Foo);
impl_into!(Foo => usize);
static_assertions::assert_impl_all!(Foo: From<usize>, Into<usize>);
let foo = Foo::from(42);
assert_eq!(foo.0, 42);
}
#[test]
fn newtype_primitive() {
struct Foo(usize);
impl_from!(usize => Foo);
impl_from_for_primitive!(Foo => usize);
static_assertions::assert_impl_all!(Foo: From<usize>);
static_assertions::assert_impl_all!(usize: From<Foo>);
let foo = Foo::from(42);
assert_eq!(foo.0, 42);
}
#[test]
fn newtype_generic() {
struct Foo<T>(Rc<T>);
impl_from!(<T> in Rc<T> => Foo<T>);
impl_into!(<T> in Foo<T> => Rc<T>);
let foo = Foo::from(Rc::new(42_usize));
assert_eq!(*foo.0, 42);
}
#[test]
fn named_field() {
struct Foo {
inner: usize,
}
impl_from!(usize => Foo : inner);
impl_into!(Foo => usize : inner);
static_assertions::assert_impl_all!(Foo: From<usize>, Into<usize>);
let foo = Foo::from(42);
assert_eq!(foo.inner, 42);
struct MultiFoo {
small: u8,
big: u64,
}
impl_into!(MultiFoo => u8 : small);
impl_into!(MultiFoo => u64 : big);
static_assertions::assert_impl_all!(MultiFoo: Into<u8>, Into<u64>);
let foo = MultiFoo { small: 4, big: 42 };
assert_eq!(foo.small, 4);
assert_eq!(foo.big, 42);
}
#[test]
fn named_field_generic() {
struct Foo<T> {
inner: Rc<T>,
}
impl_from!(<T> in Rc<T> => Foo<T> : inner);
impl_into!(<T> in Foo<T> => Rc<T> : inner);
let foo = Foo::from(Rc::new(42_usize));
assert_eq!(*foo.inner, 42);
}
}