在许多不同的类型上实现相同的特征也有类似的问题,这是6年前提出的here年前的问题.我也想知道是否有任何关于这方面的更新.

对于我的问题,假设

struct Foo { data: SomeType }

fn convert(num: u128) -> SomeType {
    //This function only works on u128 and processes it into some data in a unique way
    todo!();
}

//Now we want to create the Foo wrapper using the convert function
impl<T: Into<u128>> From<T> for Foo {
    fn from(other: T) -> Self {
        Foo{ data: convert(other.into()) }
    }
}

这可能是默认的操作方式.但是,如果我想要将特定类型列入黑名单,即使它们实现了Into<u128>,或者只想将一些类型列入白名单,该怎么办?

假设我们现在想使用i128,其中不存在<u128 as From<i128>>,但仍然可以使用as强制转换将其合理地转换为u128.下面的实现旨在处理所有原始整数类型,因为所有这些类型都可以使用as转换为u128.

//Now there is a problem!
impl<T> From<T> for Foo {
    fn from(other: T) -> Foo {
        Foo {data: convert(other as u128)}
    }
}

这个问题的出现是因为我们的实现与stdFrom<T> for T的一揽子实现之间的冲突.我们没有具体说明任何性状界限.

在这种情况下,您可以简单地使用numnum_traits箱中提供的有用特性来将T绑定到原始整数. 在不使用这种特征界限的情况下,有没有一个宏可以为语法体相同的不同类型的列表生成一组实现?

推荐答案

您可以使用宏来执行此操作:

macro_rules from_foo {
    // The first part is a pattern that describes the expected input:
    //  - `$(...),*` means repeat the contents 0 or more times, with comma separator
    //  - `$t: ty` introduces a variable called `$t` which is a type
    //  - `$(,)?` is just to allow an optional trailing comma
    ($($t: ty),* $(,)?) => {
        // In the output, anything inside the `$(...)*` gets repeated for every `$t` in the input
        $(
            impl From<$t> for Foo {
                fn from(other: $t) -> Foo {
                    Foo { data: convert(other as u128) }
                }
            }
        )*
    }
}

from_foo!(u128, i128, u64, i64, u32, i32);

请注意,这些实现仍将与您的T: Into<u128>实现冲突,因此您需要为非确认类型单独实现Foo,或者如果有几个类型具有相同的实现,则添加不同的宏.

Rust相关问答推荐

有没有方法处理rust中嵌套的ok_or()?

捕获Rust因C++异常而产生panic

如何在Rust中为具有多个数据持有者的enum变体编写文档 comments ?

为什么复印是豆荚的一个重要特征?

在Rust中赋值变量有运行时开销吗?

S,一般性状和联想型性状有什么不同?

重写Rust中的方法以使用`&;mut self`而不是`mut self`

在IntoIter上调用.by_ref().Take().rev()时会发生什么情况

零拷贝按步骤引用一段字节

如何创建一个可变的嵌套迭代器?

如何使用reqwest进行异步请求?

如何从borrow 的异步代码运行阻塞代码?

在Rust中实现Trie数据 struct 的更好方式

如何基于常量在Rust中跳过一个测试

Rust 中 Mutex<> 的深拷贝?

更好的方法来模式匹配带有 Rust 的窥视孔装配说明窗口?

预期的整数,找到 `&{integer}`

Rust - 在线程之间不安全地共享没有互斥量的可变数据

传递 Option<&mut T> 时何时需要 mut

在 Rust 中组合特征的不同方法是否等效?