diff --git a/rust/cardano-blockchain-types/Cargo.toml b/rust/cardano-blockchain-types/Cargo.toml index 6c95c7b02c..788bdcfb66 100644 --- a/rust/cardano-blockchain-types/Cargo.toml +++ b/rust/cardano-blockchain-types/Cargo.toml @@ -33,4 +33,5 @@ dashmap = "6.1.0" blake2b_simd = "1.0.2" minicbor = { version = "0.25.1", features = ["alloc"] } num-traits = "0.2.19" -ed25519-dalek = "2.1.1" \ No newline at end of file +ed25519-dalek = "2.1.1" +serde = "1.0.210" diff --git a/rust/cardano-blockchain-types/src/slot.rs b/rust/cardano-blockchain-types/src/slot.rs index 04225cf94e..d72c5d298d 100644 --- a/rust/cardano-blockchain-types/src/slot.rs +++ b/rust/cardano-blockchain-types/src/slot.rs @@ -1,7 +1,16 @@ //! Block Slot + +use std::{ + cmp::Ordering, + ops::{MulAssign, Sub}, +}; + +use serde::Serialize; + use crate::conversion::from_saturating; -#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] +#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Default, Serialize)] + /// Slot on the blockchain, typically one slot equals one second. However chain /// parameters can alter how long a slot is. pub struct Slot(u64); @@ -33,3 +42,23 @@ impl From for u64 { val.0 } } + +impl MulAssign for Slot { + fn mul_assign(&mut self, rhs: u64) { + self.0 = self.0.saturating_mul(rhs); + } +} + +impl PartialOrd for Slot { + fn partial_cmp(&self, other: &Self) -> Option { + self.0.partial_cmp(&other.0) + } +} + +impl Sub for Slot { + type Output = Slot; + + fn sub(self, rhs: Slot) -> Self::Output { + Slot(self.0.saturating_sub(rhs.0)) + } +}