gpt4 book ai didi

rust - 是否可以为由所有实现特征的类型组成的任何元组自动实现特征?

转载 作者:行者123 更新时间:2023-11-29 08:10:45 26 4
gpt4 key购买 nike

假设我有一个

trait Happy {}

我可以为我可能想要的任何结构实现 Happy,例如:

struct Dog;
struct Cat;
struct Alligator;

impl Happy for Dog {}
impl Happy for Cat {}
impl Happy for Alligator {}

现在,我想自动impl 我的Happy 特性,无论元组是由所有实现Happy 特性的类型组成的。直觉上,所有快乐的元组也是快乐的。

有可能做这样的事情吗?例如,我可以简单地将 Happy 的实现扩展到两个 Happy 类型的任何元组:

impl <T, Q> Happy for (T, Q) where T: Happy, Q: Happy {}

因此,编译完美:

fn f(_: impl Happy) {
}

fn main() {
f((Dog{}, Alligator{}));
}

但是我如何将其推广到任何长度的任何元组呢?就我的理解而言,我们在 Rust 中没有可变参数泛型。有解决方法吗?

最佳答案

we don't have variadic generics in Rust.

正确。

Is there a workaround?

你使用宏:

trait Happy {}

macro_rules! tuple_impls {
( $head:ident, $( $tail:ident, )* ) => {
impl<$head, $( $tail ),*> Happy for ($head, $( $tail ),*)
where
$head: Happy,
$( $tail: Happy ),*
{
// interesting delegation here, as needed
}

tuple_impls!($( $tail, )*);
};

() => {};
}

tuple_impls!(A, B, C, D, E, F, G, H, I, J,);

现在编译:

fn example<T: Happy>() {}

fn call<A: Happy, B: Happy>() {
example::<(A, B)>();
}

这通常不被视为大问题,因为长元组基本上不可读,如果确实需要,您总是可以嵌套元组。

另见:

关于rust - 是否可以为由所有实现特征的类型组成的任何元组自动实现特征?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55553281/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com