glam-rs
glam-rs copied to clipboard
Implement std::ops traits on references
It's good practice for both T and &T to implement e.g. Add<T> and Add<&T>, even if T: Copy. Primitive types already do this, but Vec2 etc. currently don't.
This is important in generic contexts such as struct Foo<T> { x: T }. T might not be Copy, so to avoid unnecessary cloning, you can impl<T> Add<&Foo<T>> for Foo<T> where T: Add<&T>. However, this excludes Foo<Vec2> because Vec2: !Add<&Vec2>.
The following paragraph from the std::ops documentation is relevant:
Many of the operators take their operands by value. In non-generic contexts involving built-in types, this is usually not a problem. However, using these operators in generic code, requires some attention if values have to be reused as opposed to letting the operators consume them. One option is to occasionally use clone. Another option is to rely on the types involved providing additional operator implementations for references. For example, for a user-defined type T which is supposed to support addition, it is probably a good idea to have both T and &T implement the traits Add<T> and Add<&T> so that generic code can be written without unnecessary cloning.
This would be very much appreciated. I'm often working with references to vectors throughout my codebase, and this is a common point of friction. I don't want to have to dereference my vectors just to include them in operations.