I have a struct that mostly encapsulates a vector:
struct Group<S> {
elements: Vec<S>
}
I also have a simple trait which is also implemented for other structs:
trait Solid {
fn intersect(&self, ray: f32) -> f32;
}
I want to implement Solid
for Group
, but I want to be able to use Group
both for lists of the same implementation of Solid
and for lists of mixed implementations of Solid
. Basically I want to use both Group<Box<Solid>>
and Group<Sphere>
(Sphere
implements Solid
).
Currently I am using something like this:
impl Solid for Group<Box<Solid>> {
fn intersect(&self, ray: f32) -> f32 {
//do stuff
}
}
impl<S: Solid> Solid for Group<S> {
fn intersect(&self, ray: f32) -> f32 {
//do the same stuff, code copy-pasted from previous impl
}
}
This works, but having line-for-line the same code twice can't be the idiomatic solution. I must be missing something obvious?
In my case I measure a notable performance difference between both trait implementations, so always using Group<Box<Solid>>
isn't a great option.
Implement your trait for all Box<S>
where S
implements your trait. Then you can delegate to the existing implementation:
impl<S: Solid + ?Sized> Solid for Box<S> {
fn intersect(&self, ray: f32) -> f32 {
(**self).intersect(ray)
// Some people prefer this less-ambiguous form
// S::intersect(self, ray)
}
}
You'll also find that it can be useful to do the same for references:
impl<S: Solid + ?Sized> Solid for &'_ S {
fn intersect(&self, ray: f32) -> f32 {
(**self).intersect(ray)
// Some people prefer this less-ambiguous form
// S::intersect(self, ray)
}
}
All together:
trait Solid {
fn intersect(&self, ray: f32) -> f32;
}
impl<S: Solid + ?Sized> Solid for Box<S> {
fn intersect(&self, ray: f32) -> f32 {
(**self).intersect(ray)
// S::intersect(self, ray)
}
}
impl<S: Solid + ?Sized> Solid for &'_ S {
fn intersect(&self, ray: f32) -> f32 {
(**self).intersect(ray)
// S::intersect(self, ray)
}
}
struct Group<S>(Vec<S>);
impl<S: Solid> Solid for Group<S> {
fn intersect(&self, _ray: f32) -> f32 {
42.42
}
}
struct Point;
impl Solid for Point {
fn intersect(&self, _ray: f32) -> f32 {
100.
}
}
fn main() {
let direct = Group(vec![Point]);
let boxed = Group(vec![Box::new(Point)]);
let pt = Point;
let reference = Group(vec![&pt]);
let mixed: Group<Box<dyn Solid>> = Group(vec![
Box::new(direct),
Box::new(boxed),
Box::new(Point),
Box::new(reference),
]);
mixed.intersect(1.0);
}
The ?Sized
bound allows the S
to not have a size known at compile time. Importantly, this allows you to pass in trait objects such as Box<dyn Solid>
or &dyn Solid
as the type Solid
does not have a known size.
See also:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With