rust mopa2

mopa = 0.2

 

use mopa;
use std::fmt::Debug;
use std::fmt::Display;
use std::any::TypeId;

fn is_string<T: ?Sized + mopa::Any>(_s: &T) -> bool {
    TypeId::of::<String>() == TypeId::of::<T>()
}

fn is_int<T: ?Sized + mopa::Any>(_s: &T) -> bool {
    TypeId::of::<i64>() == TypeId::of::<T>()
}

fn is_float<T: ?Sized + mopa::Any>(_s: &T) -> bool {
    TypeId::of::<f64>() == TypeId::of::<T>()
}

fn is_str<T: ?Sized + mopa::Any>(_s: &T) -> bool {
    TypeId::of::<&str>() == TypeId::of::<T>()
}

trait N<'a>: mopa::Any+Debug+Display{}

impl<'a> N<'a> for i64 {}
impl<'a> N<'a> for f64 {}
impl<'a> N<'a> for String {}
impl N<'static> for &'static str {}

fn dosm<'a>(c: impl N<'a>){
    println!("{}", c);
    if is_int(&c){
        println!("yes, int");
    }
    if is_float(&c){
        println!("float, it is,");
    }
    if is_string(&c){
        println!("it is string");
    }
    if is_str(&c){
        println!("it is str");
    }
}

fn main() {
    let a = String::from("hello");
    let a1 = "A";
    dosm(a1);
}

  

posted @ 2022-07-25 18:38  CrossPython  阅读(13)  评论(0编辑  收藏  举报