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 + Send + Sync>],
12 pub return_type: TypeId,
14 pub return_value: Option<Box<dyn Any + Send + Sync>>
16}
17
18pub trait DynamicProxy {
19 fn call(&self, invocation: &mut InvocationInfo);
20}
21
22pub trait AsyncDynamicProxy {
23 fn call_async(&self, invocation: &mut InvocationInfo)
24 -> impl std::future::Future<Output = ()> + Send;
25}
26
27
28impl<'a> InvocationInfo<'a> {
29
30 pub fn set_return_value<T: 'static + Send + Sync>(&mut self, val: T) {
31 let result: Box<dyn Any + Send + Sync> = Box::new(val);
32 self.return_value = Some(result);
33 }
34
35 pub fn get_arg_value<T: 'static>(&self, index: usize) -> &T {
36 self.arg_values[index].downcast_ref::<T>().unwrap()
37 }
38
39 pub fn get_arg_type(&self, index: usize) -> TypeId {
40 self.arg_values[index].deref().type_id()
41 }
42}