bodoni / svg

Composer and parser for SVG
Other
305 stars 44 forks source link

How to copy a child into another Element #61

Closed blackghost1987 closed 1 year ago

blackghost1987 commented 2 years ago

I'm trying to get a node from the children of an element, and copy it into another element, but I'm facing issues.

The output of get_children() is &Vec<Box<dyn Node>>, but the add / append functions will only accept actual Node implementors. I tried dereferencing to get the inner Node from the box, but then the compiler complaining about Sized not being implemented. Node is a trait object so it cannot be Sized, but an unsized variable cannot be used as a function parameter, so how could I solve this?

Can I convert Box somehow back to it's original type (e.g. Circle)? I thought Box erases the actual type, so I'm not sure how would this be possible. If I have prior knowledge of the type, can I "cast" it maybe somehow?

Looks like Element::append will just put it inside a Box again anyways, so maybe there should be another append-like function which accepts already boxed Nodes?

IvanUkhov commented 1 year ago

The following example might help:

https://github.com/bodoni/svg/blob/main/src/node/element/mod.rs#L322-L328