Skip to content

Commit

Permalink
implement Add/Sub for position
Browse files Browse the repository at this point in the history
being able to add/subtract positions is very handy when writing rendering code
  • Loading branch information
pascalkuthe committed Nov 22, 2023
1 parent 556f14c commit 5931a46
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion helix-core/src/position.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 }
Expand Down

0 comments on commit 5931a46

Please sign in to comment.