miden_objects/block/
block_number.rs1use core::{fmt, ops::Add};
2
3use crate::{
4 Felt,
5 utils::serde::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable},
6};
7
8#[derive(Debug, Default, Eq, PartialEq, Copy, Clone, PartialOrd, Ord, Hash)]
15pub struct BlockNumber(u32);
16
17impl BlockNumber {
18 pub const EPOCH_LENGTH_EXPONENT: u8 = 16;
24
25 pub const GENESIS: Self = Self(0);
27
28 pub fn parent(self) -> Option<BlockNumber> {
30 self.checked_sub(1)
31 }
32
33 pub fn child(self) -> BlockNumber {
35 self + 1
36 }
37
38 pub const fn from_epoch(epoch: u16) -> BlockNumber {
40 BlockNumber((epoch as u32) << BlockNumber::EPOCH_LENGTH_EXPONENT)
41 }
42
43 pub const fn block_epoch(&self) -> u16 {
45 (self.0 >> BlockNumber::EPOCH_LENGTH_EXPONENT) as u16
46 }
47
48 pub fn as_u32(&self) -> u32 {
50 self.0
51 }
52
53 pub fn as_u64(&self) -> u64 {
55 self.0 as u64
56 }
57
58 pub fn as_usize(&self) -> usize {
60 self.0 as usize
61 }
62
63 pub fn checked_sub(&self, rhs: u32) -> Option<Self> {
65 self.0.checked_sub(rhs).map(Self)
66 }
67}
68
69impl Add<u32> for BlockNumber {
70 type Output = Self;
71
72 fn add(self, other: u32) -> Self::Output {
73 BlockNumber(self.0 + other)
74 }
75}
76
77impl Serializable for BlockNumber {
78 fn write_into<W: ByteWriter>(&self, target: &mut W) {
79 target.write_u32(self.0);
80 }
81
82 fn get_size_hint(&self) -> usize {
83 core::mem::size_of::<u32>()
84 }
85}
86
87impl Deserializable for BlockNumber {
88 fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
89 source.read::<u32>().map(BlockNumber::from)
90 }
91}
92
93impl From<BlockNumber> for Felt {
94 fn from(value: BlockNumber) -> Self {
95 Felt::from(value.as_u32())
96 }
97}
98
99impl From<u32> for BlockNumber {
100 fn from(value: u32) -> Self {
101 BlockNumber(value)
102 }
103}
104
105impl fmt::Display for BlockNumber {
106 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
107 write!(f, "{}", self.0)
108 }
109}