bee_block/payload/milestone/
index.rs

1// Copyright 2020-2021 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4use core::ops::{Add, Sub};
5
6use derive_more::{Deref, From};
7
8/// A wrapper around a `u32` that represents a milestone index.
9#[repr(transparent)]
10#[derive(Debug, Clone, Copy, Default, Eq, Hash, Ord, PartialEq, PartialOrd, From, Deref, packable::Packable)]
11#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
12pub struct MilestoneIndex(pub u32);
13
14impl MilestoneIndex {
15    /// Creates a new `MilestoneIndex`.
16    pub fn new(value: u32) -> Self {
17        Self(value)
18    }
19}
20
21impl core::fmt::Display for MilestoneIndex {
22    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
23        write!(f, "{}", self.0)
24    }
25}
26
27impl Add for MilestoneIndex {
28    type Output = Self;
29
30    fn add(self, other: Self) -> Self {
31        Self(*self + *other)
32    }
33}
34
35impl Add<u32> for MilestoneIndex {
36    type Output = Self;
37
38    fn add(self, other: u32) -> Self {
39        Self(*self + other)
40    }
41}
42
43impl Sub for MilestoneIndex {
44    type Output = Self;
45
46    fn sub(self, other: Self) -> Self {
47        Self(*self - *other)
48    }
49}
50
51impl Sub<u32> for MilestoneIndex {
52    type Output = Self;
53
54    fn sub(self, other: u32) -> Self {
55        Self(*self - other)
56    }
57}