-
Notifications
You must be signed in to change notification settings - Fork 768
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: How can I pass generic cutom classes to a method #696
Comments
Sorry, I'm not sure I understand the context completely... fn method<T: Trait>(&self, value: T) -> ... from Python.
enum Shapes {
Cuboid(CuboidImpl),
Sphere(SphareImpl),
}
#[pyclass]
struct Shape {
inner: Shapes,
}
#[pyclass]
struct Shape {
inner: Box<dyn ShapeTrait>,
}
#[pyclass]
struct BaseShape {
...
}
#[pyclass(extends=BaseShape)]
struct Cuboid {
...
} |
@kngwyu Thanks so much. I'm pretty sure I don't understand how to apply your options 1. and 2. to this situation, but I can confirm that option 3. Inheritance is exactly what I need. It also gets rid of the massive macro to build all the different shapes and generally simplifies the code. I had got used to rust 'not doing' inheritance in the python way that I didn't even try that. For the benefit of others this is a minimal example: import rpi3d
cone = rpi3d.Cone()
cone2 = rpi3d.Cone()
tetra = rpi3d.Tetra()
cone.add_child(tetra)
cone.add_child(cone2)
cone.print_children() in pyo3 rust #[pyclass(module="rpi3d")]
struct ShapeBase {
name: String,
children: Vec<String>,
}
#[pymethods]
impl ShapeBase {
fn add_child(&mut self, child: &ShapeBase) {
self.children.push(child.name.clone());
}
fn print_children(&self) {
println!("{:?}", self.children);
}
}
#[pyclass(extends=ShapeBase)]
struct Cone {}
#[pymethods]
impl Cone {
#[new]
fn new(obj: &PyRawObject) {
obj.init({ ShapeBase {
name: "Cone".to_string(),
children: vec![],
} });
}
}
#[pyclass(extends=ShapeBase)]
struct Tetra {}
#[pymethods]
impl Tetra {
#[new]
fn new(obj: &PyRawObject) {
obj.init({ ShapeBase {
name: "Tetra".to_string(),
children: vec![],
} });
}
}
#[pymodule]
fn rpi3d(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<ShapeBase>()?;
m.add_class::<Cone>()?;
m.add_class::<Tetra>()?;
Ok(())
} |
Background. I am trying to make a python wrapper for a rust version of a 3D graphics module https://github.com/paddywwoof/rust_pi3d see the pyo3_module directory. (Eventually to replace pi3d)
The original python code had
So all the different shapes are really just wrappers that create a different set of vertices etc of a Shape. This means that they can be used like
I am struggling to make a generic method like this in pyo3. If you look here there is a test version using
#[args(child="*")]
. But I'm struggling to check what type I have as an argument.Is there a much better way to do this?
The text was updated successfully, but these errors were encountered: