rust - 有没有办法为可能包含任何数字元素的元组实现特征?

标签 rust

<分区>

我有这样的特质:

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

关于rust - 有没有办法为可能包含任何数字元素的元组实现特征?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56697029/

相关文章:

rust - 将 Result 转换为自定义 Fail impl 的惯用方法是什么?

rust - 使用 Serde 反序列化对象时,有没有办法省略包装器/根对象?

linux - Path::new(many-subdirs) 对于 Linux 和 Windows 是否足够好?

rust - 从包含无效字节的字符串中高效删除前缀

rust - 在 Rust 中迭代 Vec 的备用元素的最佳方法是什么?

c - 从 C 移植学习 Rust

arrays - 数组向量的内存布局是什么?

module - 如何从特定模块创建所有装饰函数的向量?

rust - "struct field is never used",但 RAII 需要

traits - 当 trait 和 type 都不在这个 crate 中时提供一个实现