implement Add/Sub for position

being able to add/subtract positions is very handy when writing rendering code
This commit is contained in:
Pascal Kuthe 2023-11-19 22:35:40 +01:00
parent 08ee8b9443
commit 22dfad605a
No known key found for this signature in database
GPG key ID: D715E8655AE166A6

View file

@ -1,4 +1,8 @@
use std::{borrow::Cow, cmp::Ordering};
use std::{
borrow::Cow,
cmp::Ordering,
ops::{Add, AddAssign, Sub, SubAssign},
};
use crate::{
chars::char_is_line_ending,
@ -16,6 +20,38 @@ pub struct Position {
pub col: usize,
}
impl AddAssign for Position {
fn add_assign(&mut self, rhs: Self) {
self.row += rhs.row;
self.col += rhs.col;
}
}
impl SubAssign for Position {
fn sub_assign(&mut self, rhs: Self) {
self.row -= rhs.row;
self.col -= rhs.col;
}
}
impl Sub for Position {
type Output = Position;
fn sub(mut self, rhs: Self) -> Self::Output {
self -= rhs;
self
}
}
impl Add for Position {
type Output = Position;
fn add(mut self, rhs: Self) -> Self::Output {
self += rhs;
self
}
}
impl Position {
pub const fn new(row: usize, col: usize) -> Self {
Self { row, col }