I'm writing a rust library that I hope will let users easily create plugins for a program that exposes a C API.
One of the things that this API allows for is creating classes with named methods
with various arguments.
I end up writing trampolines that I give the the API that then calls back into my code, does some casting, and calls a method. Something like the below.
pub struct A;
impl A {
pub fn x(&mut self, v: i64) {
println!("x {}", v);
}
pub fn y(&mut self, v: i64) {
println!("y {}", v);
}
}
pub unsafe extern "C" fn x_tramp(o: *mut A, v: i64) {
let o = &mut *o;
o.x(v);
}
pub unsafe extern "C" fn y_tramp(o: *mut A, v: i64) {
let o = &mut *o;
o.y(v);
}
While I know I can write macros to write these trampolines for me, I'm wondering if there is a way to use generics to make reduce the need for macros (and not require users of the library to generate trampolines)?
Is there a way to encode call method y on o with v
in a type so I can make a generic trampoline that executes that?
Since a closure has a unique type, can we do something with that and not a specific one for each of the methods x
and y
?
Something like..
pub unsafe extern "C" fn int_tramp<T, F: Fn(&mut T, i64) + Default>(o: *mut T, v: i64) {
let f: F = Default::default();
let o = &mut *o;
f(o, v);
}
Any ideas?