我应该什么时候实现 std::convert::From 与 std::convert::Into? [英] When should I implement std::convert::From vs std::convert::Into?
问题描述
我看到 std::convert::Into
有任何实现 <的实现代码>std::convert::From:
impl<T, U> Into<U> for T
where
U: From<T>,
在 Rust 1.0 标准库中,From
有很多实现,而 Into
只在 3 个地方实现.这使得实现 From
看起来应该是默认的.我确信有时我想实现 Into
而不是 From
,但我没有看到它们.
In the Rust 1.0 standard library, there are many implementations of From
while Into
is only implemented in 3 places. This makes it seem like implementing From
should be the default. I'm certain that there are times I would want to implement Into
and not From
, but I'm not seeing them.
推荐答案
TL;DR:更喜欢实现 From
.
TL;DR: prefer implementing From
.
有趣的是,原始 RFC 关于std::convert
trait 建议采用相反的全面实现:
Interestingly, the original RFC about the std::convert
traits suggested the opposite blanket implementation:
impl<T, U> From<T> for U
where
T: Into<U>
但是在实现它的 PR 上,改为相反:
But on the PR implementing it, it was changed to the opposite:
添加 From
=>Into
实现,这使得可以在不违反一致性的情况下在两个方向上添加转换.例如,我们现在有 From<[T]>对于 Vec<T>
where T: Clone
,这会产生相应的 Into
朝另一个方向前进——尽管这两种类型生活在不同的板条箱.
Added
From
=>Into
implementation, which makes it possible to add conversions in both directions without running afoul of coherence. For example, we now haveFrom<[T]> for Vec<T>
whereT: Clone
, which yields the correspondingInto
going in the other direction -- despite the fact that the two types live in different crates.
我也相信这解决了一些关于实现 From
而不是 Into
I also believe this addresses a few concerns about things implementing From
instead of Into
这个最后一刻的变化反映了 From
和 Into
基本上是等价的.From
被选为首选,因为它不受类型参数与本地类型"的限制.观点.
This last-moment change reflects that From
and Into
are basically equivalent. From
was chosen as the preferred one as it was less restrictive from the "type parameter vs. local type" point of view.
Rust 1.41 之前.0,不可能制作 impl<'a, T>进入<Foo>对于 &'a [T]
,而 impl<'a, T>来自<&'a [T]>for Foo
是可能的.
Before Rust 1.41.0, it wasn't possible to make a impl<'a, T> Into<Foo> for &'a [T]
, while impl<'a, T> From<&'a [T]> for Foo
was possible.
第一次尝试引发E0210
:
error[E0210]: type parameter `T` must be used as the type parameter for some local type (e.g., `MyStruct<T>`)
--> x.rs:3:10
|
3 | impl<'a, T> Into<Foo> for &'a [T] {
| ^ type parameter `T` must be used as the type parameter for some local type
|
= note: only traits defined in the current crate can be implemented for a type parameter
在 Rust 1.14 之前的标准库中,只有两个例子实现了 Into
而不是 From
:
In the standard library prior to Rust 1.14, there were only two examples of implementing Into
and not From
:
impl Into
>对于字符串 impl Into
对于 PathBuf
我认为这些是他们接口逻辑的反映.OsString
实现了 From
和 From
,因为它们是您想要从中构建 OsString
的自然事物.
I think these are the reflexion of the logic of their interfaces. OsString
implements From<String>
and From<T> where T: AsRef<OsStr>
, because they are the natural things you'll want to build a OsString
from.
然而,PathBuf
仍然实现了Into
作为其From
实现的逆向操作,但这个逻辑属于PathBuf
,而不是 OsString
.
However, PathBuf
still implements Into<OsString>
as the reverse operation of its From<OsString>
implementation, but this logic belongs to PathBuf
, not OsString
.
这篇关于我应该什么时候实现 std::convert::From 与 std::convert::Into?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!