block_seq_no.rs

Overview

This file defines a strongly-typed wrapper around a 32-bit unsigned integer to represent a block sequence number within a blockchain or distributed ledger context. The BlockSeqNo struct encapsulates the sequence number, providing type safety and a set of utility methods and trait implementations to facilitate safe arithmetic operations, comparisons, and conversions. This abstraction helps prevent misuse of raw integers when dealing with block sequence numbers and ensures consistent handling of block sequencing logic across the application.

Structure and Components

BlockSeqNo Struct

Functions and Methods

next_seq_no

pub fn next_seq_no(seq_no: BlockSeqNo) -> BlockSeqNo
let current = BlockSeqNo::from(42);
let next = next_seq_no(current);
assert_eq!(u32::from(next), 43);

BlockSeqNo::saturating_sub

pub fn saturating_sub(self, other: u32) -> Self
let seq = BlockSeqNo::from(10);
let result = seq.saturating_sub(15);
assert_eq!(u32::from(result), 0); // Saturates at zero, no negative values

Trait Implementations

These trait implementations enable idiomatic usage of BlockSeqNo with Rust's standard library features and arithmetic operators.

Design Considerations

Interaction with Other Parts of the System

Visual Diagram

classDiagram
class BlockSeqNo {
-u32
+saturating_sub()
+from(u32)
+into<u32>()
+Display
+Debug
+Add<u32>
+Sub<BlockSeqNo>
+Rem<u32>
}
BlockSeqNo : +Copy
BlockSeqNo : +Clone
BlockSeqNo : +Eq
BlockSeqNo : +PartialEq
BlockSeqNo : +Hash
BlockSeqNo : +Serialize
BlockSeqNo : +Deserialize
BlockSeqNo : +Default
BlockSeqNo : +PartialOrd
BlockSeqNo : +Ord
class Functions {
+next_seq_no(BlockSeqNo) BlockSeqNo
}
Functions --> BlockSeqNo : uses

This diagram illustrates the BlockSeqNo struct with its key methods and implemented traits, as well as the standalone next_seq_no function that operates on it.


For further information about serialization and trait implementations, see serde serialization. For details on operator overloading in Rust, refer to Operator Overloading. The concept of type safety wrappers is discussed under Newtype Pattern.