rust - 在 Rust 中派生实例时的多个 impl 候选者

标签 rust

在 Rust 1.7 中尝试使用 rustc-serialize 对包含两个 Stringstruct 进行 (d)e(n) 编码时,编译器提示有太多 impl 候选项,不知道该选择哪一个。

代码:

#[derive(RustcDecodable, RustcEncodable, Debug)]
struct gonet {
    ip: String,
    mask: String,
}

投诉:

src/bin/measurer.rs:18:5: 18:15 error: multiple applicable items in scope [E0034]
src/bin/measurer.rs:18     ip: String,
                           ^~~~~~~~~~
src/bin/measurer.rs:16:26: 16:40 note: in this expansion of #[derive_RustcEncodable] (defined in src/bin/measurer.rs)
src/bin/measurer.rs:18:5: 18:15 help: run `rustc --explain E0034` to see a detailed explanation
src/bin/measurer.rs:18:5: 18:15 note: candidate #1 is defined in an impl of the trait `rustc_serialize::serialize::Encodable` for the type `str`
src/bin/measurer.rs:18     ip: String,
                           ^~~~~~~~~~
src/bin/measurer.rs:16:26: 16:40 note: in this expansion of #[derive_RustcEncodable] (defined in src/bin/measurer.rs)
src/bin/measurer.rs:18:5: 18:15 note: candidate #2 is defined in an impl of the trait `rustc_serialize::serialize::Encodable` for the type `collections::string::String`
src/bin/measurer.rs:18     ip: String,
                           ^~~~~~~~~~
src/bin/measurer.rs:16:26: 16:40 note: in this expansion of #[derive_RustcEncodable] (defined in src/bin/measurer.rs)
src/bin/measurer.rs:18:5: 18:15 note: candidate #3 is defined in an impl of the trait `rustc_serialize::serialize::Encodable` for the type `&_`
src/bin/measurer.rs:18     ip: String,
                           ^~~~~~~~~~
src/bin/measurer.rs:16:26: 16:40 note: in this expansion of #[derive_RustcEncodable] (defined in src/bin/measurer.rs)
src/bin/measurer.rs:18:5: 18:15 note: candidate #4 is defined in an impl of the trait `radix_trie::keys::TrieKey` for the type `_`
src/bin/measurer.rs:18     ip: String,
                           ^~~~~~~~~~
src/bin/measurer.rs:16:26: 16:40 note: in this expansion of #[derive_RustcEncodable] (defined in src/bin/measurer.rs)
src/bin/measurer.rs:19:5: 19:17 error: multiple applicable items in scope [E0034]
src/bin/measurer.rs:19     mask: String,
                           ^~~~~~~~~~~~
src/bin/measurer.rs:16:26: 16:40 note: in this expansion of #[derive_RustcEncodable] (defined in src/bin/measurer.rs)
src/bin/measurer.rs:19:5: 19:17 help: run `rustc --explain E0034` to see a detailed explanation
src/bin/measurer.rs:19:5: 19:17 note: candidate #1 is defined in an impl of the trait `rustc_serialize::serialize::Encodable` for the type `str`
src/bin/measurer.rs:19     mask: String,
                           ^~~~~~~~~~~~
src/bin/measurer.rs:16:26: 16:40 note: in this expansion of #[derive_RustcEncodable] (defined in src/bin/measurer.rs)
src/bin/measurer.rs:19:5: 19:17 note: candidate #2 is defined in an impl of the trait `rustc_serialize::serialize::Encodable` for the type `collections::string::String`
src/bin/measurer.rs:19     mask: String,
                           ^~~~~~~~~~~~
src/bin/measurer.rs:16:26: 16:40 note: in this expansion of #[derive_RustcEncodable] (defined in src/bin/measurer.rs)
src/bin/measurer.rs:19:5: 19:17 note: candidate #3 is defined in an impl of the trait `rustc_serialize::serialize::Encodable` for the type `&_`
src/bin/measurer.rs:19     mask: String,
                           ^~~~~~~~~~~~
src/bin/measurer.rs:16:26: 16:40 note: in this expansion of #[derive_RustcEncodable] (defined in src/bin/measurer.rs)
src/bin/measurer.rs:19:5: 19:17 note: candidate #4 is defined in an impl of the trait `radix_trie::keys::TrieKey` for the type `_`
src/bin/measurer.rs:19     mask: String,
                           ^~~~~~~~~~~~
src/bin/measurer.rs:16:26: 16:40 note: in this expansion of #[derive_RustcEncodable] (defined in src/bin/measurer.rs)

我的代码也使用了TrieKey来自 radix_tree crate 的特征,它也有一个 encode() 方法。

我希望在范围内同时使用这两个特征,但它们会发生冲突,所以我如何指定 String 的哪个特征用于派生(如果可能的话) ?

不是 useing TrieKey 有效,但我想要一个允许我使用 两者的解决方案。

最佳答案

正如原帖评论下所说,这实际上是bug .现在的修复是 available .

关于rust - 在 Rust 中派生实例时的多个 impl 候选者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36565194/

相关文章:

rust - 你能写一个宏来调用 Rust 中的 default() 运算符吗?

reference - 了解 Vec<T> 的调试实现

parameters - 有什么方法可以使采用解构数组的闭包?

generics - 如何避免将具体结构更改为通用结构所产生的链式 react ?

rust - 在 Rust 中别名闭包类型,但编译器要求生命周期说明符

rust - len() 和 count() 有什么区别?

rust - 使用动态类型对象实现 `tokio::io::AsyncWrite`

rust - 匹配字符串上的类型不匹配

embedded - 在没有 stdlib 的情况下使用带有 cargo 的集合

rust - 如果在Rust book 20.3中将发送Terminate消息和thread.join()放在一个循环中,为什​​么会出现死锁?