Skip to main content

tephra_types/
position.rs

1use std::{fmt, ops};
2
3#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
4pub struct Position(pub(crate) u64);
5
6impl Position {
7    pub const ZERO: Position = Position(0);
8
9    pub fn new(n: u64) -> Self {
10        Position(n)
11    }
12
13    pub fn get(self) -> u64 {
14        self.0
15    }
16
17    pub fn next(self) -> Position {
18        Position(self.0 + 1)
19    }
20
21    pub fn offset_from(self, base: Position) -> u64 {
22        self - base
23    }
24}
25
26impl From<u64> for Position {
27    fn from(n: u64) -> Self {
28        Position(n)
29    }
30}
31
32impl ops::Add<Position> for Position {
33    type Output = u64;
34
35    fn add(self, rhs: Position) -> Self::Output {
36        self.0.add(rhs.0)
37    }
38}
39
40impl ops::Add<u64> for Position {
41    type Output = u64;
42
43    fn add(self, rhs: u64) -> Self::Output {
44        self.0.add(rhs)
45    }
46}
47
48impl ops::Add<Position> for u64 {
49    type Output = u64;
50
51    fn add(self, rhs: Position) -> Self::Output {
52        self.add(rhs.0)
53    }
54}
55
56impl ops::Sub<Position> for Position {
57    type Output = u64;
58
59    fn sub(self, rhs: Position) -> Self::Output {
60        self.0.sub(rhs.0)
61    }
62}
63
64impl ops::Sub<u64> for Position {
65    type Output = u64;
66
67    fn sub(self, rhs: u64) -> Self::Output {
68        self.0.sub(rhs)
69    }
70}
71
72impl ops::Sub<Position> for u64 {
73    type Output = u64;
74
75    fn sub(self, rhs: Position) -> Self::Output {
76        self.sub(rhs.0)
77    }
78}
79
80impl fmt::Display for Position {
81    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
82        self.0.fmt(f)
83    }
84}