我想在不创建对象的情况下检查类型是否实现了特征。但它不编译。请参阅代码中的注释。那么我应该怎么做才能实现我的目标?
#![feature(specialization)]
struct T1;
struct T2;
trait A {}
impl A for T1 {}
trait Get_Static<TraitType> {
fn has_trait() -> bool ;
}
default impl<TraitType, T> Get_Static<TraitType> for T
{
fn has_trait() -> bool { false }
}
impl<TraitType, T> Get_Static<TraitType> for T where T:TraitType
{
fn has_trait() -> bool { true }
}//Compiler complains TraitType is not a trait but type parameter
fn main() {
if <T1 as Get_Static>::<A>::has_trait() {println!("{}", true)} else {println!("{}", false)}
if <T2 as Get_Static>::<A>::has_trait() {println!("{}", true)} else {println!("{}", false)}
//This is surely wrong syntax but I don't know the right syntax
}
最佳答案
警告:此解决方案不再适用于最近的 nightly (2022-01),并且不知道何时停止工作。
感谢 Stefan 抚平了最后的皱纹。
<T2 as Get_Static>::<A>::has_trait()
//This is surely wrong syntax but I don't know the right syntax
这会尝试调用:
- 特征相关函数,
- 针对特定类型实现。
语法是<Type as Trait>::associated_function()
.在这种情况下,Type
是T1
, Trait
是Get_Static<A>
所以这应该是:
<T2 as Get_Static<A>>::has_trait()
impl<TraitType, T> Get_Static<TraitType> for T where T:TraitType { fn has_trait() -> bool { true } } //Compiler complains TraitType is not a trait but type parameter
不可能直接表明TraitType
应该是 trait
, 然而 Unsize
标记可用于检查是否 T: Unsize<TraitType>
这足以满足我们的目的。
这需要 3 个更改:
- 启用夜间功能
#![feature(unsize)]
作为Unsize
标记不稳定, - 允许
Get_Static
通用参数为?Sized
,因为特征未定大小, - 使用
T: Unsize<TraitType>
作为实现中的约束。
总而言之,这意味着:
#![feature(specialization)]
#![feature(unsize)]
trait GetStatic<TraitType: ?Sized> {
fn has_trait() -> bool ;
}
default impl<TraitType: ?Sized, T> GetStatic<TraitType> for T {
fn has_trait() -> bool { false }
}
impl<TraitType: ?Sized, T> GetStatic<TraitType> for T
where
T: std::marker::Unsize<TraitType>
{
fn has_trait() -> bool { true }
}
然后用作:
struct T1;
struct T2;
trait A {}
impl A for T1 {}
fn main() {
println!("{}", <T1 as GetStatic<A>>::has_trait());
println!("{}", <T2 as GetStatic<A>>::has_trait());
}
关于rust - 如何检查泛型类型是否实现了泛型特征?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47709129/