gfxmath-vec2
A simple 2D math library.
Vec2
There are multiple ways we can create a Vec2 object.
use Vec2;
// Standard 'new' semantics.
let v = new;
// Using macro 'vec2!'
use vec2;
let v: = vec2!;
// Using 'into' from a tuple.
let v: = .into;
// Using 'into' from a borrowed tuple
let t = ;
let v: = .into;
We can also create tuples from Vec2 objects.
use Vec2;
// With owned Vec2 object
let v = new;
let t: = v.into;
// With borrowed Vec2 object
let v = new;
let t: = .into;
Operators
Common mathematical operators are implemented for Vec2.
+
,+=
-
,-=
*
,*=
/
,/=
// Reminder: Rust automatically infers floats to `f64` and integers to `i64`.
use Vec2;
let v1 = new;
let v2 = new;
let res = v1 + v2;
assert_eq!;
assert_eq!;
Known Limitations
Left-Hand Primitives
One caveat is that operators where the Left-Hand side is a primitive has limited support. This is due to restrictions for trait implementations. impl <T> Add<Vec2<T>> for T
is illegal within Rust syntax (due to the trailing T
) because the implementation must be for a known type for non-local traits and types. Since Add
is from the core
package and T
(the type to be implemented for) is not derived from a local trait, this is not possible.
At the time of this writing, primitives that work on the Left-Hand Side for common operators are f32
, f64
, i32
, and i64
.
use Vec2;
// Works
let lhs: f32 = 4.0;
let rhs = new;
let res = lhs + rhs;
// NOT SUPPORTED!!
let lhs: u32 = 4;
let rhs = new;
let res = lhs + rhs;
Hash Implementation
Currently, Hash implementations are limited for the following types:
f32
f64
i32
i64
u32
u64
Showcase
License
Apache 2.0