'How to check whether a generic type implements a generic trait?
I want to check whether a type implements a trait without creating an object. But it doesn't compile. See the comments in code . So what should I do to get my goal?
#![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
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
