So i'm writing a proc macro to be able to dynamically build some basic field & method reflection on values. First part I'm trying to handle is just store anonymized static function -> call it. The test below works, but having to cast everything outside of the structs methods is pretty terrible. Is there anyway to create generic method that takes ANY static method stores -> then I can cast it back? I keep getting compiler errors for 'unknown size of generic parameter' and can't seem to find a way to get the traits on the generics to work. Below an example of what I have and more the interface I'm looking for.
What works currently.
pub struct MethodInfo {
method: *const ()
}
impl MethodInfo {
fn new(method: *const ()) -> Self {
return Self { method };
}
fn get(&self) -> *const ()
{
return self.method;
}
}
#[test]
fn reflection_method_test() {
let a = Point::new(2.5f32,-1.5f32);
let b = Point::new(8.23f32,10.0f32);
let method = MethodInfo::new(Point::add_points as *const ());
let result = unsafe { std::mem::transmute::<*const (),fn(Point,Point)->Point>(method.get())(a,b) };
assert_eq!(result.x,10.73f32);
assert_eq!(result.y,8.5f32);
}
Interface that matches more of hat I'm looking for.
#[test]
fn reflection_method_test() {
let a = Point::new(2.5f32,-1.5f32);
let b = Point::new(8.23f32,10.0f32);
let method = MethodInfo::new(Point::add_points);
let result = method.get::<fn(Point,Point)->Point>()(a,b);
assert_eq!(result.x,10.73f32);
assert_eq!(result.y,8.5f32);
}