Skip to content

Commit

Permalink
added block number support for basic id
Browse files Browse the repository at this point in the history
  • Loading branch information
antiyro committed Mar 1, 2024
1 parent abfc3b3 commit 7f81111
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,27 @@ pub trait Id: hash::Hash + PartialEq + Eq + PartialOrd + Ord + Debug + Copy {
#[derive(Hash, PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Copy, Default)]
pub struct BasicId(u64);

impl BasicId {
/// Constructor for creating a new `BasicId` from a `u64`.
pub fn new(id: u64) -> Self {
BasicId(id)
}

/// Converts the ID to a byte vector.
pub fn to_bytes(&self) -> Vec<u8> {
self.0.to_be_bytes().to_vec()
}
}

impl Id for BasicId {
fn to_bytes(&self) -> Vec<u8> {
self.0.to_be_bytes().to_vec()
self.to_bytes()
}
}

impl From<u64> for BasicId {
fn from(item: u64) -> Self {
BasicId::new(item)
}
}

Expand All @@ -36,12 +54,7 @@ impl BasicIdBuilder {

/// Create a new ID (unique).
pub fn new_id(&mut self) -> BasicId {
let id = BasicId(self.last_id);
self.last_id = self.last_id.checked_add(1).expect("Id overflow");
id
}

pub fn block_number(id: u64) -> BasicId {
BasicId(id)
BasicId::new(self.last_id - 1)
}
}

0 comments on commit 7f81111

Please sign in to comment.