是否可以为函数的泛型类型创建一个具有特征边界的类型别名? [英] Is it possible to create a type alias that has trait bounds on a generic type for a function?

查看:33
本文介绍了是否可以为函数的泛型类型创建一个具有特征边界的类型别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码:

pub type Foo<T: Read> = fn(bar: T);

产生 错误 E0122(在较新版本的Rust,这只是一个警告):

yields error E0122 (in newer versions of Rust, it is only a warning):

已尝试向类型别名添加通用约束.这个约束完全被忽略.为了向后兼容,Rust仍然允许此警告.考虑以下示例:

An attempt was made to add a generic constraint to a type alias. This constraint is entirely ignored. For backwards compatibility, Rust still allows this with a warning. Consider the example below:

trait Foo {}

type MyType<R: Foo> = (R, ());

fn main() {
    let t: MyType<u32>;
}

我们可以声明一个 MyType 类型的变量,尽管事实上u32 没有实现 Foo.因此,应避免使用与类型别名一致的通用约束.

We're able to declare a variable of type MyType<u32>, despite the fact that u32 does not implement Foo. As a result, one should avoid using generic constraints in concert with type aliases.

是否可以创建一个包含对函数指针的特征要求的类型别名?显然,编译器告诉我没有类型,但不知道是否还有其他我没有想到的函数选项.

Is it possible to create a type alias that contains trait requirements on a function pointer? Obviously the compiler is telling me no for types, but didn't know if there was another option for functions that I wasn't thinking of.

推荐答案

目前看来不可能,也没有解决方法.

At this time, it does not seem to be possible and no workarounds exist.

这篇关于是否可以为函数的泛型类型创建一个具有特征边界的类型别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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