我有这样的特质:
trait Foo {
fn do_something(self) -> Self;
}
我想为一个元组实现这个特性,它可能有任意数量的元素实现这个特性。
impl<T> Foo for (T, T) where T: Foo {
fn do_something(self) -> Self {
let (t0, t1) = self;
(t0.do_something(), t1.do_something())
}
}
impl<T> Foo for (T, T, T) where T: Foo {
fn do_something(self) -> Self {
let (t0, t1, t2) = self;
(t0.do_something(), t1.do_something(), t2.do_something())
}
}
// and so on...
注意:我不确定您是否应该这样做,但这里有一个方法。 (我觉得很老套,这可能是因为我不知道如何制作更好的宏。)
齐次元组 (T, T)
您描述的方式:
impl<T> Foo for (T, T) where T: Foo
在这里,整个元组必须是同构的(即(MyType, MyType2).do_something()
将不工作,因为单态化)。
这会引发一个标志,因为元组适用于异构数据。
如果仅实现一个同质元组的特征仍然是你想要的,我们可以实现一个宏的方式 the standard library does to implement traits for varied length tuples , 进行了一些修改。 (单击 src
右侧的 impl
查看其来源。)
macro_rules! replace_expr {
($_t:tt $sub:ty) => {$sub};
}
macro_rules! tuple_impls {
( $( $name:ident )+ ) => {
impl<T: Foo> Foo for ($(replace_expr!(($name) T),)+)
{
fn do_something(self) -> Self {
let ($($name,)+) = self;
($($name.do_something(),)+)
}
}
};
}
tuple_impls! { A }
tuple_impls! { A B }
tuple_impls! { A B C }
tuple_impls! { A B C D }
tuple_impls! { A B C D E }
tuple_impls! { A B C D E F }
tuple_impls! { A B C D E F G }
tuple_impls! { A B C D E F G H }
tuple_impls! { A B C D E F G H I }
tuple_impls! { A B C D E F G H I J }
tuple_impls! { A B C D E F G H I J K }
tuple_impls! { A B C D E F G H I J K L }
Playground
异构元组(T1, T2)
如果您同意 (MyType, MyType2).do_something()
工作(两者都实现了 Foo
特性),你可以试试这个更简单的宏:
macro_rules! tuple_impls {
( $( $name:ident )+ ) => {
impl<$($name: Foo),+> Foo for ($($name,)+)
{
fn do_something(self) -> Self {
let ($($name,)+) = self;
($($name.do_something(),)+)
}
}
};
}
tuple_impls! { A }
tuple_impls! { A B }
tuple_impls! { A B C }
tuple_impls! { A B C D }
tuple_impls! { A B C D E }
tuple_impls! { A B C D E F }
tuple_impls! { A B C D E F G }
tuple_impls! { A B C D E F G H }
tuple_impls! { A B C D E F G H I }
tuple_impls! { A B C D E F G H I J }
tuple_impls! { A B C D E F G H I J K }
tuple_impls! { A B C D E F G H I J K L }
Playground