Skip to content

Commit

Permalink
fix(rust/cardano-blockchain-types): add more functionality to Slot (#…
Browse files Browse the repository at this point in the history
…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]>
  • Loading branch information
bkioshn authored Dec 30, 2024
1 parent fe48d5a commit 2f176b2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
3 changes: 2 additions & 1 deletion rust/cardano-blockchain-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
ed25519-dalek = "2.1.1"
serde = "1.0.210"
31 changes: 30 additions & 1 deletion rust/cardano-blockchain-types/src/slot.rs
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -33,3 +42,23 @@ impl From<Slot> for u64 {
val.0
}
}

impl MulAssign<u64> 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<Ordering> {
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))
}
}

0 comments on commit 2f176b2

Please sign in to comment.