简体   繁体   English

Rust 中的“一揽子实现”是什么?

[英]What are "Blanket Implementations" in Rust?

When looking through the documentation of Rust structs, I often come across a section with the title "Blanket Implementations" .在查看 Rust 结构的文档时,我经常遇到标题为“Blanket Implementations”的部分。 I have heard that it can be used to implement a trait for all types or all types that match some condition but I am not sure why this is needed.我听说它可以用来为所有类型或匹配某些条件的所有类型实现特征,但我不确定为什么需要这样做。

So what really are Blanket Implementations and why are they useful in Rust?那么究竟什么是一揽子实现,为什么它们在 Rust 中有用?

A blanket implementation is any implementation of a trait on a generic parameter:一揽子实现是对通用参数的特征的任何实现:

impl<T> Trait for T

They usually also have where clauses involved since it is very hard to do anything useful to an unconstrained T .它们通常还涉及where子句,因为很难对不受约束的T做任何有用的事情。 This does not cover things like impl<T> Trait for Vec<T> , which is a generic implementation but not a blanket implementation.这不包括impl<T> Trait for Vec<T>类的东西,这是一个通用实现,但不是一揽子实现。

They are documented separately since they are applied without any particularity and may or may not be relevant to the type you're looking at.它们被单独记录,因为它们的应用没有任何特殊性,并且可能与您正在查看的类型相关,也可能不相关。 Whereas for the normal "Trait Implementations" section, all those traits at least had some thought for the specific type in mind (usually).而对于普通的“特征实现”部分,所有这些特征至少对特定类型有一些想法(通常)。

They are patently useful since it implements the trait for anything , in the entire ecosystem, If something satisfies the constraints.它们显然很有用,因为它在整个生态系统中实现了任何事物的特征,如果某些事物满足约束。 then it is able to take advantage of the implementation without needing to implement it themselves.那么它就能够利用实现而无需自己实现它。 You do not need to do anything to "opt-in" besides bringing the trait into scope.除了将特征纳入范围之外,您不需要做任何事情来“选择加入”。

Some notable ones:一些值得注意的:

  • From<T> is implemented for all T (the identity implementation) From<T>为所有T实现(身份实现)
  • Into<U> is implemented for all T where T: From<U> (the reflexive implementation that allows you to call .into() when a matching From implementation exists) Into<U>为所有T实现,其中T: From<U> (当存在匹配的From实现时,允许您调用.into()的反身实现)
  • Any is implemented for all T where T: 'static Any对所有T实施,其中T: 'static

They are also needed to implement "trait aliases" where you create a trait that is constrained over multiple other traits (handy for reducing boilerplate and needed for multi-trait trait objects).还需要它们来实现“特征别名”,您可以在其中创建一个受多个其他特征约束的特征(便于减少样板文件并需要多特征特征对象)。 You use a blanket implementation that the trait is implemented for any type satisfying the super-traits:您使用一个完整的实现,该特征是为满足超特征的任何类型实现的:

trait MyCoolAsyncTrait: AsyncRead + AsyncWrite + AsyncSeek + 'static {}

impl<T> MyCoolAsyncTrait for T 
where
    T: AsyncRead + AsyncWrite + AsyncSeek + 'static
{ }

Be careful when adding them to your types though.不过,在将它们添加到您的类型时要小心。 Because of their extensive scope, they can easily conflict with other trait implementations that may be desirable.由于其广泛的 scope,它们很容易与可能需要的其他特征实现发生冲突。 You can only define one blanket implementation per type ( even if the constraints are non-overlapping ).您只能为每种类型定义一个全面实现( 即使约束不重叠)。

See also:也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM