3 ms·
> there is a trait of Material that has a function on it, and then there are different "implementations" of Material with different implementations of that fn,
by physguy1123 9y ago
> there is a trait of Material that has a function on it, and then there are different "implementations" of Material with different implementations of that fn, and can have arbitrary data stored against them.
Do you mean like a class hierarchy+virtual functions, or just having allocated data of arbitrary length past the end of a struct?
If it's the former, C++ fares no better in this regard but it's pretty easy to do in Rust. You just have a cloner in the interface. If it's the latter, that's pretty nonstandard and Rust doesn't make that easy for good reason. Unless this raytracer is doing something wild the code should hopefully be refactorable into a more normal struct+trait layout.
- SCdF 9y agoimpl Material for Lambertain { ... fn box_clone(&self) -> Box<Material> { Box::new(Lambertian { ..self }) } } Holllllly shit I think that works. Why am I allowed to do this and not just derive Clone and Copy!?!
- physguy1123 9y agoCopy and clone have specific meanings and must resolve to the type of the class itself - basically copy or cloning a class of type A gets you another class of type A always. It's possible, from a language sense, to have an implementation of box that could directly clone the trait, but I don't believe it would have a reasonable api.