Reputation: 414
I've been writing a fairly simple raster image trait system, but I was also wanting to extend it to work for comma-separated values, and it seems more natural to allow the trait for both pixels (which would have the Copy
trait), and String
(which does not).
However, overloading for both causes conflicts. So how would one go about writing a trait which accepts both Copy
able types and Clone
able types?
Currently the working definition looks like
impl<T:Grid2d,V:Copy> drawable for Line2d{
fn stroke(&self,out:&mut T);
}
This works fine for pixels which are basically integers.
Upvotes: 2
Views: 70
Reputation: 65937
Copy
is a subtrait of Clone
(i.e. all Copy
types are also Clone
), so you could have a single impl
with a bound on Clone
and it will also accept all Copy
types.
impl<T: Grid2d, V: Clone> Drawable for Line2d {
fn stroke(&self, out: &mut T);
}
You'll have to explicitly call .clone()
to get a copy of the value, but for Copy
types, this .clone()
call should be very cheap (and in release builds, it's likely to be inlined).
Upvotes: 4