Skip to content
34 changes: 25 additions & 9 deletions crates/storage/src/merkle/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use alloc::{
Eq,
Hash,
enum_iterator::Sequence,
strum_macros::EnumCount,
strum_macros::IntoStaticStr,
)]
pub enum MerkleizedColumn<TC> {
Expand All @@ -31,6 +30,16 @@ pub enum MerkleizedColumn<TC> {
MerkleMetadataColumn,
}

impl<TC> strum::EnumCount for MerkleizedColumn<TC>
where
TC: strum::EnumCount + AsU32,
{
/// The total count of variants in the enum.
/// Since we have two columns for each table column and one for the merkle data,
/// we have to multiply the count of the table columns by 2 and add one for the merkle metadata.
const COUNT: usize = TC::COUNT * 2 + 1;
}

Comment thread
netrome marked this conversation as resolved.
/// The trait to convert the column to the `u32`.
pub trait AsU32 {
/// Returns the `u32` representation of the `Column`.
Expand All @@ -42,21 +51,28 @@ where
TC: strum::EnumCount + AsU32,
{
/// The total count of variants in the enum.
/// Since we have two columns for each table column and one for the merkle data,
/// we have to multiply the count of the table columns by 2 and add one for the merkle metadata.
pub const COUNT: usize = TC::COUNT * 2 + 1;
pub const COUNT: usize = <Self as strum::EnumCount>::COUNT;

/// The start of the merkle data columns.
pub const MERKLE_DATA_COLUMNS_START: u32 = u16::MAX as u32;

/// The merkle metadata column
// we set it to 0, since any future column updates will be added after this column
pub const MERKLE_METADATA_COLUMN: u32 = 0;

#[inline]
fn with_metadata_offset(column: u32) -> u32 {
column.wrapping_add(Self::MERKLE_METADATA_COLUMN + 1)
}

/// Returns the `u32` representation of the `Column`.
pub fn as_u32(&self) -> u32 {
match self {
Self::TableColumn(column) => column.as_u32(),
Self::MerkleDataColumn(column) => {
Self::MERKLE_DATA_COLUMNS_START.wrapping_add(column.as_u32())
}
Self::MerkleMetadataColumn => u32::MAX,
Self::TableColumn(column) => Self::with_metadata_offset(column.as_u32()),
Self::MerkleDataColumn(column) => Self::with_metadata_offset(
Self::MERKLE_DATA_COLUMNS_START.wrapping_add(column.as_u32()),
),
Self::MerkleMetadataColumn => Self::MERKLE_METADATA_COLUMN,
}
}
}
Expand Down
Loading