implement Add/Sub for position
being able to add/subtract positions is very handy when writing rendering code
This commit is contained in:
parent
08ee8b9443
commit
22dfad605a
1 changed files with 37 additions and 1 deletions
|
@ -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…
Add table
Reference in a new issue