dynamic_proxy_types/
lib.rs1use std::any::{Any, TypeId};
2use std::ops::Deref;
3
4pub struct InvocationInfo<'a> {
6 pub func_name: &'a str,
8 pub arg_names: &'a[&'a str],
10 pub arg_values: &'a [Box<dyn Any>],
12 pub return_type: TypeId,
14 pub return_value: Option<Box<dyn Any>>
16}
17
18unsafe impl Send for InvocationInfo<'_> {}
19
20pub trait DynamicProxy {
21 fn call(&self, invocation: &mut InvocationInfo);
22}
23
24pub trait AsyncDynamicProxy {
25 fn call_async(&self, invocation: &mut InvocationInfo)
26 -> impl std::future::Future<Output = ()> + Send;
27}
28
29
30impl<'a> InvocationInfo<'a> {
31
32 pub fn set_return_value<T: 'static>(&mut self, val: T) {
33 let result: Box<dyn Any> = Box::new(val);
34 self.return_value = Some(result);
35 }
36
37 pub fn get_arg_value<T: 'static>(&self, index: usize) -> &T {
38 self.arg_values[index].downcast_ref::<T>().unwrap()
39 }
40
41 pub fn get_arg_type(&self, index: usize) -> TypeId {
42 self.arg_values[index].deref().type_id()
43 }
44}