如何实现任何可变性的特征? [英] How to implement a trait for any mutability?

查看:138
本文介绍了如何实现任何可变性的特征?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可变性可以成为特征中的通用参数吗?我想实现一种类型的可变和不可变变体的特征,而不必复制并粘贴impl块.

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.


trait Foo<T> {…}

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

一厢情愿的伪代码:


trait Foo<T> {…}

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

推荐答案

可变性可以成为特征中的通用参数吗?

Can mutability be a generic parameter in traits?

不. ^ _ ^

这是此事的详细讨论(内部人 Reddit ).

Here's some detailed discussion on the matter (Internals, Reddit).

我认为,一般而言,人们认识到当前的状态并不理想,但目前也不是十分困难的.人们担心它的确切实施方式以及各种方法的合理性.有人怀疑,是否以及何时将它们添加到Rust中,更高种类的类型(HKT)是否可以解决问题.

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.

另请参阅:

这篇关于如何实现任何可变性的特征?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆