Skip to main content

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 + Send + Sync>],
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 + 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}