[英]What is the idiomatic way to implement a trait for a large number of arrays in stable Rust?
Given my trait T
and a large set of std
/ core
arrays ( not slices ), How can I make implementations of T
for these arrays available to other crates on stable Rust?鉴于我的 trait T
和一大组std
/ core
数组(不是 slices ),我怎样才能使这些数组的T
实现可用于稳定的 Rust 上的其他板条箱?
From searching around, it seems my only options (that don't sidestep this problem) are to:通过四处搜索,似乎我唯一的选择(不回避这个问题)是:
Option 1 is not acceptable.选项 1 是不可接受的。 Option 2 leads to very long compile times (especially when the set of arrays exceeds 5000 types).选项 2 会导致很长的编译时间(尤其是当数组集超过 5000 种类型时)。 Hiding every single implementation behind its own feature, ie feature impl-t-for-array-N
conditionally compiles in an implementation of T
for array [U;N]
, does considerably lower compilation time.将每个单独的实现隐藏在它自己的特性后面,即特性impl-t-for-array-N
有条件地在T
的实现中编译为数组[U;N]
,确实大大降低了编译时间。 (Compilation times went from from tens of minutes to a couple of seconds). (编译时间从几十分钟变成了几秒钟)。 However, the delay caused by processing thousands of features is still noticeable.然而,处理数千个特征造成的延迟仍然很明显。
Is using features and manual implementations the most idiomatic way to handle this problem on stable rust, or is there a more idiomatic way I'm missing?使用功能和手动实现是在稳定 Rust 上处理这个问题的最惯用的方法,还是我缺少更惯用的方法?
Is using features and manual implementations the most idiomatic way to handle this problem on stable rust使用功能和手动实现是在稳定锈上处理这个问题的最惯用的方法
Yes.是的。 For example, check Debug trait and its sources - as you can see rust uses special macro to implement it for arrays with length <= 32例如,检查Debug trait 及其来源 - 正如你所看到的,rust 使用特殊的宏来为长度 <= 32 的数组实现它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.