for anyone maybe trying something similar:
Just guess all the different types when moving from Python to Rust - seems to work for my case and you can probably come up with something prettier (haven't found a match style expression for that yet)
#[derive(Debug)]
pub struct Parameter {
value: Box<dyn Any + 'static + Send + Sync>,
clone_fn: fn(&Box<dyn Any + 'static + Send + Sync>) -> Box<dyn Any + 'static + Send + Sync>,
}
impl Parameter {
pub fn new<T: 'static + Any + Clone + Send + Sync>(t: T) -> Self {
Parameter {
value: Box::new(t),
clone_fn: |x| {
let x = x.downcast_ref::<T>().unwrap();
Box::new(x.clone())
},
}
}
pub fn downcast<T: 'static + Any + Send + Sync>(&self) -> Option<&T> {
self.value.downcast_ref::<T>()
}
pub fn downcast_mut<T: 'static + Any + Send + Sync>(&mut self) -> Option<&mut T> {
self.value.downcast_mut::<T>()
}
}
impl<'py> FromPyObject<'py> for Parameter {
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
if ob.is_instance_of::<PyFloat>() {
let value: f64 = ob.extract()?;
return Ok(Parameter::new(value));
} else if ob.is_instance_of::<PyString>() {
let value: String = ob.extract()?;
return Ok(Parameter::new(value));
} else if ob.is_instance_of::<PyInt>() {
let value: i64 = ob.extract()?;
return Ok(Parameter::new(value));
} else {
return Err(pyo3::exceptions::PyTypeError::new_err(
"Unsupported type for Parameter",
));
}
}
}