This commit is contained in:
Joscha 2024-03-02 23:36:21 +01:00
parent 14c9dac398
commit 0487bdb465
3 changed files with 93 additions and 4 deletions

View file

@ -1,3 +1,3 @@
pub fn greet(who: &str) {
println!("Hello {who}!");
}
pub use crate::vec2::Vec2;
mod vec2;

View file

@ -0,0 +1,87 @@
use std::{
fmt,
ops::{Add, Mul, Neg, Sub},
};
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Vec2 {
pub x: i32,
pub y: i32,
}
impl Vec2 {
pub const ZERO: Self = Self::new(0, 0);
pub const NORTH: Self = Self::new(0, -1);
pub const SOUTH: Self = Self::new(0, 1);
pub const WEST: Self = Self::new(-1, 0);
pub const EAST: Self = Self::new(1, 0);
pub const fn new(x: i32, y: i32) -> Self {
Self { x, y }
}
pub fn to(self, other: Self) -> Self {
other - self
}
}
impl fmt::Debug for Vec2 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Vec2").field(&self.x).field(&self.y).finish()
}
}
impl Neg for Vec2 {
type Output = Self;
fn neg(self) -> Self::Output {
Self {
x: -self.x,
y: -self.y,
}
}
}
impl Add for Vec2 {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self {
x: self.x + rhs.x,
y: self.y + rhs.y,
}
}
}
impl Sub for Vec2 {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
Self {
x: self.x - rhs.x,
y: self.y - rhs.y,
}
}
}
impl Mul<i32> for Vec2 {
type Output = Self;
fn mul(self, rhs: i32) -> Self::Output {
Self {
x: self.x * rhs,
y: self.y * rhs,
}
}
}
impl Mul for Vec2 {
type Output = Self;
fn mul(self, rhs: Self) -> Self::Output {
Self {
x: self.x * rhs.x,
y: self.y * rhs.y,
}
}
}

View file

@ -1,3 +1,5 @@
use showbits_common::Vec2;
fn main() {
showbits_common::greet("world");
println!("{:?}", Vec2::EAST);
}