hyperlight_common/func/
param_type.rs1use alloc::string::String;
5use alloc::vec;
6use alloc::vec::Vec;
7
8use super::error::Error;
9use super::utils::for_each_tuple;
10use crate::flatbuffer_wrappers::function_types::{ParameterType, ParameterValue};
11
12pub trait SupportedParameterType: Sized + Clone + Send + Sync + 'static {
18 const TYPE: ParameterType;
20
21 fn into_value(self) -> ParameterValue;
24 fn from_value(value: ParameterValue) -> Result<Self, Error>;
26}
27
28macro_rules! for_each_param_type {
30 ($macro:ident) => {
31 $macro!(String, String);
32 $macro!(i32, Int);
33 $macro!(u32, UInt);
34 $macro!(i64, Long);
35 $macro!(u64, ULong);
36 $macro!(f32, Float);
37 $macro!(f64, Double);
38 $macro!(bool, Bool);
39 $macro!(Vec<u8>, VecBytes);
40 };
41}
42
43macro_rules! impl_supported_param_type {
44 ($type:ty, $enum:ident) => {
45 impl SupportedParameterType for $type {
46 const TYPE: ParameterType = ParameterType::$enum;
47
48 fn into_value(self) -> ParameterValue {
49 ParameterValue::$enum(self)
50 }
51
52 fn from_value(value: ParameterValue) -> Result<Self, Error> {
53 match value {
54 ParameterValue::$enum(i) => Ok(i),
55 other => Err(Error::ParameterValueConversionFailure(
56 other.clone(),
57 stringify!($type),
58 )),
59 }
60 }
61 }
62 };
63}
64
65for_each_param_type!(impl_supported_param_type);
66
67pub trait ParameterTuple: Sized + Clone + Send + Sync + 'static {
69 const SIZE: usize;
71
72 const TYPE: &[ParameterType];
74
75 fn into_value(self) -> Vec<ParameterValue>;
78
79 fn from_value(value: Vec<ParameterValue>) -> Result<Self, Error>;
81}
82
83impl<T: SupportedParameterType> ParameterTuple for T {
84 const SIZE: usize = 1;
85
86 const TYPE: &[ParameterType] = &[T::TYPE];
87
88 fn into_value(self) -> Vec<ParameterValue> {
89 vec![self.into_value()]
90 }
91
92 fn from_value(value: Vec<ParameterValue>) -> Result<Self, Error> {
93 match <[ParameterValue; 1]>::try_from(value) {
94 Ok([val]) => Ok(T::from_value(val)?),
95 Err(value) => Err(Error::UnexpectedNoOfArguments(value.len(), 1)),
96 }
97 }
98}
99
100macro_rules! impl_param_tuple {
101 ([$N:expr] ($($name:ident: $param:ident),*)) => {
102 impl<$($param: SupportedParameterType),*> ParameterTuple for ($($param,)*) {
103 const SIZE: usize = $N;
104
105 const TYPE: &[ParameterType] = &[
106 $($param::TYPE),*
107 ];
108
109 fn into_value(self) -> Vec<ParameterValue> {
110 let ($($name,)*) = self;
111 vec![$($name.into_value()),*]
112 }
113
114 fn from_value(value: Vec<ParameterValue>) -> Result<Self, Error> {
115 match <[ParameterValue; $N]>::try_from(value) {
116 Ok([$($name,)*]) => Ok(($($param::from_value($name)?,)*)),
117 Err(value) => Err(Error::UnexpectedNoOfArguments(value.len(), $N))
118 }
119 }
120 }
121 };
122}
123
124for_each_tuple!(impl_param_tuple);