implement Add/Sub for position

being able to add/subtract positions is very handy when writing rendering code
pull/6417/head
Pascal Kuthe 1 year ago
parent 08ee8b9443
commit 22dfad605a
No known key found for this signature in database
GPG Key ID: D715E8655AE166A6

@ -1,4 +1,8 @@
use std::{borrow::Cow, cmp::Ordering}; use std::{
borrow::Cow,
cmp::Ordering,
ops::{Add, AddAssign, Sub, SubAssign},
};
use crate::{ use crate::{
chars::char_is_line_ending, chars::char_is_line_ending,
@ -16,6 +20,38 @@ pub struct Position {
pub col: usize, 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 { impl Position {
pub const fn new(row: usize, col: usize) -> Self { pub const fn new(row: usize, col: usize) -> Self {
Self { row, col } Self { row, col }

Loading…
Cancel
Save