Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement more useful traits in Slot type #1595

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion substrate/primitives/consensus/slots/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use scale_info::TypeInfo;
use sp_timestamp::Timestamp;

/// Unit type wrapper that represents a slot.
#[derive(Debug, Encode, MaxEncodedLen, Decode, Eq, Clone, Copy, Default, Ord, TypeInfo)]
#[derive(Debug, Encode, MaxEncodedLen, Decode, Eq, Clone, Copy, Default, Ord, Hash, TypeInfo)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Slot(u64);

Expand All @@ -44,6 +44,26 @@ impl core::ops::Add for Slot {
}
}

impl core::ops::Sub for Slot {
type Output = Self;

fn sub(self, other: Self) -> Self {
Self(self.0 - other.0)
}
}

impl core::ops::AddAssign for Slot {
fn add_assign(&mut self, rhs: Self) {
self.0 += rhs.0
}
}

impl core::ops::SubAssign for Slot {
fn sub_assign(&mut self, rhs: Self) {
self.0 -= rhs.0
}
}

impl core::ops::Add<u64> for Slot {
type Output = Self;

Expand Down