1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
use std::any::{Any, TypeId};
use std::ops::Deref;

pub struct InvocationInfo<'a> {
    pub func_name: &'a str,
    pub arg_names: &'a[&'a str],
    pub arg_values: &'a [Box<dyn Any>],
    pub return_type: TypeId,
    pub return_value: Option<Box<dyn Any>>
}

pub trait DynamicProxy {
    fn call(&self, invocation: &mut InvocationInfo);
}

impl<'a> InvocationInfo<'a> {
    pub fn set_return_value<T: 'static>(&mut self, val: T) {
        let result: Box<dyn Any> = Box::new(val);
        self.return_value = Some(result);
    }
    
    pub fn get_arg_value<T: 'static>(&self, index: usize) -> &T {
        self.arg_values[index].downcast_ref::<T>().unwrap()
    }
    
    pub fn get_arg_type(&self, index: usize) -> TypeId {
        self.arg_values[index].deref().type_id()
    }
}