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); }