dynamic_proxy_types/
lib.rs

1use std::any::{Any, TypeId};
2use std::ops::Deref;
3
4/// Contains invocation arguments, including function signature and arguments
5pub struct InvocationInfo<'a> {
6    /// Gets the name of the invoked function
7    pub func_name: &'a str,
8    /// Array containig the name of input arguments
9    pub arg_names: &'a[&'a str],
10    /// Array containing the values of the arguments as boxed dynamic value.
11    pub arg_values: &'a [Box<dyn Any>],
12    /// Type id of the function result
13    pub return_type: TypeId,
14    /// The interceptor must set this value if the function has result
15    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}