vecmate/
lib.rs

1//! [![GitHub last commit](https://img.shields.io/github/last-commit/ztry8/vecmate)](https://github.com/ztry8/vecmate/)
2//! [![crates.io](https://img.shields.io/crates/v/vecmate)](https://crates.io/crates/vecmate)
3//! [![docs.rs](https://img.shields.io/docsrs/vecmate)](https://docs.rs/vecmate)
4//! [![License](https://img.shields.io/github/license/ztry8/vecmate)](https://github.com/ztry8/vecmate/blob/main/LICENSE)
5//! ## Lightweight, zero-dependency, type-agnostic library for vector math.   
6//! ```rust
7//! let mut position = consts::f32::ZERO;
8//! let target = vec2(10.0, 5.0);
9//! let speed = 2.0;
10//!
11//! let direction = (target - position).normalize();
12//! position += direction * speed;
13//!
14//! println!("Moving towards {target}");
15//! println!("New position: {position}");
16//! ```
17
18use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
19
20mod tests;
21
22/// Module for working with two-dimensional vectors
23pub mod vec2;
24
25/// Trait that defines that the type is a number
26pub trait Number<T>:
27    Clone
28    + Copy
29    + PartialEq
30    + Sized
31    + Add<Output = T>
32    + Sub<Output = T>
33    + Mul<Output = T>
34    + Div<Output = T>
35    + Neg
36    + AddAssign
37    + SubAssign
38    + MulAssign
39    + DivAssign
40{
41}
42
43impl<
44    T: Clone
45        + Copy
46        + PartialEq
47        + Sized
48        + Add<Output = T>
49        + Sub<Output = T>
50        + Mul<Output = T>
51        + Div<Output = T>
52        + Neg
53        + AddAssign
54        + SubAssign
55        + MulAssign
56        + DivAssign,
57> Number<T> for T
58{
59}
60
61/// Trait that specifies that the root of a number can be calculated
62pub trait Float<T>: Number<T> {
63    fn sqrt(self) -> T;
64}
65
66impl Float<f32> for f32 {
67    fn sqrt(self) -> f32 {
68        f32::sqrt(self)
69    }
70}
71
72impl Float<f64> for f64 {
73    fn sqrt(self) -> f64 {
74        f64::sqrt(self)
75    }
76}