• rust type


    use std::{any::{Any, TypeId}, fmt::Debug};
    use downcast_rs::Downcast;
    
    fn is_string<T: ?Sized + Any>(_s: &T) -> bool {
        TypeId::of::<String>() == TypeId::of::<T>()
    }
    
    fn is_int<T: ?Sized + Any>(_s: &T) -> bool {
        TypeId::of::<i64>() == TypeId::of::<T>()
    }
    
    fn is_float<T: ?Sized + Any>(_s: &T) -> bool {
        TypeId::of::<f64>() == TypeId::of::<T>()
    }
    
    // fn is_n_int(_s:Box<dyn K>) -> bool {
    //     let r = match _s {
    //         Option::<i64>(_) =>true,
    //         _ => false
    //     };
    //     r
    // }
    
    trait K:Downcast + Debug{
        // fn as_any<T>(&self)->T;
    }
    downcast_rs::impl_downcast!(K);
    
    impl K for i64{
    //     fn as_any<T>(&self)->T {
            
    //     }
    }
    
    impl K for f64{
        // fn as_any(&self)->Self{
        //     *self
        // }
    }
    
    // impl K for String{}
     
    fn tests(c:&mut Box<dyn K>)->bool{
        // println!("{:?}", &c);
        let b = *c.downcast_mut::<i64>().unwrap();
        // let cc = b;
        println!(">>>>>>>>>>>>>>>>>{:?}", b);
    
        // if is_float(&c){
        //     println!("it is float");
        // }
        // if is_string(&c){
        //     println!("it is strint");
        // }
        if is_int(&b){
            println!("it is int.");
        }else{
            println!("it is not int");
        }
        true
    }
    
    
    trait M:Any+Debug{}
    
    impl M for i64{}
    
    fn testbbb(c:&mut Box<dyn M>)->bool {
        let tmp = c.as_any_mut().downcast_mut::<i64>();
        println!("{:?}", &tmp);
    
        // if is_int(&tmp){
        //     println!("it is int");
        // }else{
        //     println!("it not int");
        // }
    
        true
    }
    
    
    fn main(){
        
        let mut a:Box<dyn K> = Box::new(5_i64);
        
        let b = tests(&mut a);
    
        let mut c:Box<dyn M> = Box::new(3_i64);
        let d = testbbb(&mut c);
        
        // let b:i64 = 5;
        // println!("{:?}", a.downcast_ref::<f64>());
    
        // let b = match a.downcast_ref::<f64>(){
        //     Some(x) => x,
        //     _ => None,
        // };
    
        // let c = tests(b);
        // let mut a = tests(5);
    }
    

      

  • 相关阅读:
    20220620
    解决vscode可以编译通过c++项目,但头文件有红色波浪线的问题
    FreeMarker变量输出
    Rime输入法中文标点符号配置
    解决大数据量join发生的数据倾斜问题
    炸裂函数的用法
    linux端口测试
    Centos7关闭默认防火墙firewall和启用iptables操作
    php简单处理手机号中间四位*代替
    esrally(elasticsearch压测工具)介绍(一)
  • 原文地址:https://www.cnblogs.com/pythonClub/p/16500711.html
Copyright © 2020-2023  润新知