rust - Generating unique ID's for types at compile time -
//pseudo code struct classtypeid{ id: &'static uint } impl classtypeid{ fn get_type<t>(&mut self) -> &'static uint { let _id :&'static uint = self.id + 1; self.id = _id; _id } } let c = classtypeid{id:0}; c.get_type::<i32>(); // returns 1 c.get_type::<f32>(); // returns 2 c.get_type::<i32>(); // returns 1 c.get_type::<uint>(); // returns 3 so want generate unique id every type @ compile time. possible in rust?
i stole idea c++ library, looks this
typedef std::size_t typeid; template <typename tbase> class classtypeid { public: template <typename t> static typeid gettypeid() { static const typeid id = m_nexttypeid++; return id; } private: static typeid m_nexttypeid; }; template <typename tbase> typeid classtypeid<tbase>::m_nexttypeid = 0; }
std::any::typeid that:
use std::any::typeid; fn main() { let type_id = typeid::of::<isize>(); println!("{:?}", type_id); } outputs:
typeid { t: 4150853580804116396 }
Comments
Post a Comment