You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 and Add<&T> so that generic code can be written without unnecessary cloning.
The text was updated successfully, but these errors were encountered:
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.
It's good practice for both
T
and&T
to implement e.g.Add<T>
andAdd<&T>
, even ifT: Copy
. Primitive types already do this, butVec2
etc. currently don't.This is important in generic contexts such as
struct Foo<T> { x: T }
.T
might not beCopy
, so to avoid unnecessary cloning, you canimpl<T> Add<&Foo<T>> for Foo<T> where T: Add<&T>
. However, this excludesFoo<Vec2>
becauseVec2: !Add<&Vec2>
.The following paragraph from the
std::ops
documentation is relevant:The text was updated successfully, but these errors were encountered: