I want to dispatch function according to types, see the example below:
struct Bytes;
impl Bytes {
fn get<T>(&mut self) -> T {
todo!()
}
fn do_something<T>(&mut self, value: T) {
todo!()
}
}
#[type_dispatch(i32, f64, u8)]
pub fn f1<T>(_type_id: TypeId, bytes: &mut Bytes) {
let value: T = bytes.get();
bytes.do_something(value);
}
pub fn f2(type_id: TypeId, bytes: &mut Bytes) {
if type_id == TypeId::of::<i32>() {
let value: i32 = bytes.get();
bytes.do_something(value);
} else if type_id == TypeId::of::<f64> {
let value: f64 = bytes.get();
bytes.do_something(value);
} else if type_id == TypeId::of::<u8>() {
let value: u8 = bytes.get();
bytes.do_something(value);
} else {
panic!("unknown type_id")
}
}
by using the proc_macro_attribute type_dispatch
, function f1
can be convert to function f2
.
All the occurrence of type T
will be replaced by the concrete types specified in the arguments of type_dispatch
.
Are there any crates that can do something like it? Or how can I write a proc_macro_attribute to do this?