1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
use std::ops::{Index, IndexMut};
// Struct to represent data related to removed/minus and added/plus lines
// which can be indexed with PlusMinusIndex::{Minus, Plus}.
#[derive(Debug, PartialEq, Eq)]
pub struct PlusMinus<T> {
pub minus: T,
pub plus: T,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PlusMinusIndex {
Minus,
Plus,
}
pub use PlusMinusIndex::*;
impl<T> Index<PlusMinusIndex> for PlusMinus<T> {
type Output = T;
fn index(&self, side: PlusMinusIndex) -> &Self::Output {
match side {
Minus => &self.minus,
Plus => &self.plus,
}
}
}
impl<T> IndexMut<PlusMinusIndex> for PlusMinus<T> {
fn index_mut(&mut self, side: PlusMinusIndex) -> &mut Self::Output {
match side {
Minus => &mut self.minus,
Plus => &mut self.plus,
}
}
}
impl<T> PlusMinus<T> {
pub fn new(minus: T, plus: T) -> Self {
PlusMinus { minus, plus }
}
}
impl<T: Default> Default for PlusMinus<T> {
fn default() -> Self {
Self {
minus: T::default(),
plus: T::default(),
}
}
}
|