简体   繁体   English

如何实现任何可变性的特征?

[英]How to implement a trait for any mutability?

Can mutability be a generic parameter in traits? 可变性是特征中的通用参数吗? I'd like to implement a trait for a mutable and an immutable variant of a type without having to copy&paste the impl block. 我想为一个类型的可变和不可变变量实现一个特性,而不必复制和粘贴impl块。

trait Foo<T> {…}

impl<T> Foo for *const T {…}
impl<T> Foo for *mut T {…same thing again…}

Wishful pseudocode: 一厢情愿的伪代码:

trait Foo<T> {…}

impl<T, Mutability> Foo for *Mutability T {…}

Can mutability be a generic parameter in traits? 可变性是特征中的通用参数吗?

No. ^_^ 没有。^ _ ^

Here's some detailed discussion on the matter ( Internals , Reddit ). 这里有一些关于此事的详细讨论( InternalsReddit )。

I think in general people recognize that the current state is not ideal, but that it's not terribly constraining at the moment, either. 我认为一般人都认识到目前的状态并不理想,但目前也不是非常严格的限制。 There's concerns on exactly how it would be implemented and the soundness of various approaches. 人们关注的是如何实施以及各种方法的合理性。 Some people wonder if Higher-Kinded Types (HKTs) would solve the problem, if and when they are added to Rust. 有些人想知道更高级的类型(HKTs)是否可以解决问题,如果它们被添加到Rust中。

See also: 也可以看看:

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

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