Skip to content

Commit 2f176b2

Browse files
authored
fix(rust/cardano-blockchain-types): add more functionality to Slot (#124)
* fix(cardano-blockchain-types): add more trait to slot Signed-off-by: bkioshn <[email protected]> * fix(cardano-blockchain-types): add serde serialize to slot Signed-off-by: bkioshn <[email protected]> * fix(cardano-blockchain-types): format Signed-off-by: bkioshn <[email protected]> * fix(cardano-blockchain-types): use sat_mul for slot Signed-off-by: bkioshn <[email protected]> --------- Signed-off-by: bkioshn <[email protected]>
1 parent fe48d5a commit 2f176b2

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

rust/cardano-blockchain-types/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ dashmap = "6.1.0"
3333
blake2b_simd = "1.0.2"
3434
minicbor = { version = "0.25.1", features = ["alloc"] }
3535
num-traits = "0.2.19"
36-
ed25519-dalek = "2.1.1"
36+
ed25519-dalek = "2.1.1"
37+
serde = "1.0.210"

rust/cardano-blockchain-types/src/slot.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
//! Block Slot
2+
3+
use std::{
4+
cmp::Ordering,
5+
ops::{MulAssign, Sub},
6+
};
7+
8+
use serde::Serialize;
9+
210
use crate::conversion::from_saturating;
311

4-
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
12+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Default, Serialize)]
13+
514
/// Slot on the blockchain, typically one slot equals one second. However chain
615
/// parameters can alter how long a slot is.
716
pub struct Slot(u64);
@@ -33,3 +42,23 @@ impl From<Slot> for u64 {
3342
val.0
3443
}
3544
}
45+
46+
impl MulAssign<u64> for Slot {
47+
fn mul_assign(&mut self, rhs: u64) {
48+
self.0 = self.0.saturating_mul(rhs);
49+
}
50+
}
51+
52+
impl PartialOrd for Slot {
53+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
54+
self.0.partial_cmp(&other.0)
55+
}
56+
}
57+
58+
impl Sub for Slot {
59+
type Output = Slot;
60+
61+
fn sub(self, rhs: Slot) -> Self::Output {
62+
Slot(self.0.saturating_sub(rhs.0))
63+
}
64+
}

0 commit comments

Comments
 (0)