VEC或迭代量与标量的通用性状边界
请考虑以下示例:
use std::str::FromStr;
enum Thing {
One(String),
Many(Vec<String>)
}
trait FromThing where Self: Sized {
fn from_thing(thing: Thing) -> Result<Self, String>;
}
impl<S: FromStr> FromThing for S {
fn from_thing(thing: Thing) -> Result<Self, String> {
match thing {
Thing::One(s) => s.parse().or_else(|_| Err("invalid value".into())),
_ => Err("expected One, got Many".into())
}
}
}
impl<S: FromStr> FromThing for Vec<S> {
fn from_thing(thing: Thing) -> Result<Self, String> {
todo!();
}
}
fn main() {
let thing = Thing::One("1".into());
let i: usize = FromThing::from_thing(thing).unwrap();
println!("{}", i);
}
如果我删除第二个IMPL块,则它会编译并运行良好。但是,它不允许我定义VEC版本,因为将来有可能在VEC上实施VEC版本。
搜索引擎的确切错误消息:
error[E0119]: conflicting implementations of trait `FromThing` for type `std::vec::Vec<_>`
--> src/main.rs:21:1
|
12 | impl<S: FromStr> FromThing for S {
| -------------------------------- first implementation here
...
21 | impl<S: FromStr> FromThing for Vec<S> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `std::vec::Vec<_>`
|
= note: upstream crates may add a new impl of trait `std::str::FromStr` for type `std::vec::Vec<_>` in future versions
我可以简单地使用夜间auto_traits
和pastic_impls
来解决此问题,但我宁愿不要每晚使用。我可以添加这些限制,以使它们相互排斥?如果不是,除了手动(或通过宏)实现从STR实现的所有标量类型的特征外,是否有其他设计模式或方法中的解决方案?
如果解决方案仅排除向量,则可以,但是最好以某种方式区分标量和集合(或迭代)(或迭代)。但是我知道这不太可能。
希望我错过了一些明显的东西。 :)
Consider the following example:
use std::str::FromStr;
enum Thing {
One(String),
Many(Vec<String>)
}
trait FromThing where Self: Sized {
fn from_thing(thing: Thing) -> Result<Self, String>;
}
impl<S: FromStr> FromThing for S {
fn from_thing(thing: Thing) -> Result<Self, String> {
match thing {
Thing::One(s) => s.parse().or_else(|_| Err("invalid value".into())),
_ => Err("expected One, got Many".into())
}
}
}
impl<S: FromStr> FromThing for Vec<S> {
fn from_thing(thing: Thing) -> Result<Self, String> {
todo!();
}
}
fn main() {
let thing = Thing::One("1".into());
let i: usize = FromThing::from_thing(thing).unwrap();
println!("{}", i);
}
If I remove the second impl block, it compiles and runs fine. However, it won't let me define the Vec version because of the possibility of FromStr being implementing on Vec at some point in the future.
The exact error message for search engines:
error[E0119]: conflicting implementations of trait `FromThing` for type `std::vec::Vec<_>`
--> src/main.rs:21:1
|
12 | impl<S: FromStr> FromThing for S {
| -------------------------------- first implementation here
...
21 | impl<S: FromStr> FromThing for Vec<S> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `std::vec::Vec<_>`
|
= note: upstream crates may add a new impl of trait `std::str::FromStr` for type `std::vec::Vec<_>` in future versions
I can resolve this problem rather simply using nightly auto_traits
and negative_impls
but I would prefer to not use nightly. Is there a constraint I can add to these impls to make them mutually exclusive? If not, is there a solution to this problem in another design pattern or methodology, aside from manually (or via macros) implementing the trait for all scalar types that implement FromStr?
It is okay if the solution only excludes vectors, but it would be best if it could somehow differentiate between Scalars and Collections (or Iterables). But I understand that is unlikely.
Hopefully I'm missing something obvious. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论