Skip to content

Commit

Permalink
derive traits instead of implementing them (#1403)
Browse files Browse the repository at this point in the history
  • Loading branch information
z2trillion authored Aug 29, 2024
1 parent e319bee commit 902040e
Showing 1 changed file with 4 additions and 84 deletions.
88 changes: 4 additions & 84 deletions bus-mapping/src/circuit_input_builder/execution.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
//! Execution step related module.

use std::{
marker::PhantomData,
ops::{Add, Mul, Neg},
};
use std::ops::{Add, Mul, Neg};

use crate::{
circuit_input_builder::CallContext,
Expand All @@ -27,6 +24,7 @@ use halo2_proofs::{
},
plonk::Expression,
};
use strum_macros::EnumIter;

/// An execution step of the EVM.
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -206,14 +204,15 @@ impl ExecState {
}

/// Defines the various source/destination types for a copy event.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, EnumIter, Default)]
pub enum CopyDataType {
/// When we need to pad the Copy rows of the circuit up to a certain maximum
/// with rows that are not "useful".
Padding,
/// When the source for the copy event is the bytecode table.
Bytecode,
/// When the source/destination for the copy event is memory.
#[default]
Memory,
/// When the source for the copy event is tx's calldata.
TxCalldata,
Expand All @@ -235,79 +234,6 @@ impl CopyDataType {
/// How many bits are necessary to represent a copy data type.
pub const N_BITS: usize = 3usize;
}
const NUM_COPY_DATA_TYPES: usize = 8usize;
pub struct CopyDataTypeIter {
idx: usize,
back_idx: usize,
marker: PhantomData<()>,
}
impl CopyDataTypeIter {
fn get(&self, idx: usize) -> Option<CopyDataType> {
match idx {
0usize => Some(CopyDataType::Padding),
1usize => Some(CopyDataType::Bytecode),
2usize => Some(CopyDataType::Memory),
3usize => Some(CopyDataType::TxCalldata),
4usize => Some(CopyDataType::TxLog),
5usize => Some(CopyDataType::RlcAcc),
6usize => Some(CopyDataType::AccessListAddresses),
7usize => Some(CopyDataType::AccessListStorageKeys),
_ => None,
}
}
}
impl strum::IntoEnumIterator for CopyDataType {
type Iterator = CopyDataTypeIter;
fn iter() -> CopyDataTypeIter {
CopyDataTypeIter {
idx: 0,
back_idx: 0,
marker: PhantomData,
}
}
}
impl Iterator for CopyDataTypeIter {
type Item = CopyDataType;
fn next(&mut self) -> Option<<Self as Iterator>::Item> {
#[allow(clippy::iter_nth_zero)]
self.nth(0)
}
fn size_hint(&self) -> (usize, Option<usize>) {
let t = if self.idx + self.back_idx >= NUM_COPY_DATA_TYPES {
0
} else {
NUM_COPY_DATA_TYPES - self.idx - self.back_idx
};
(t, Some(t))
}
fn nth(&mut self, n: usize) -> Option<<Self as Iterator>::Item> {
let idx = self.idx + n + 1;
if idx + self.back_idx > NUM_COPY_DATA_TYPES {
self.idx = NUM_COPY_DATA_TYPES;
None
} else {
self.idx = idx;
self.get(idx - 1)
}
}
}
impl ExactSizeIterator for CopyDataTypeIter {
fn len(&self) -> usize {
self.size_hint().0
}
}
impl DoubleEndedIterator for CopyDataTypeIter {
fn next_back(&mut self) -> Option<<Self as Iterator>::Item> {
let back_idx = self.back_idx + 1;
if self.idx + back_idx > NUM_COPY_DATA_TYPES {
self.back_idx = NUM_COPY_DATA_TYPES;
None
} else {
self.back_idx = back_idx;
self.get(NUM_COPY_DATA_TYPES - self.back_idx)
}
}
}

impl From<CopyDataType> for usize {
fn from(t: CopyDataType) -> Self {
Expand Down Expand Up @@ -339,12 +265,6 @@ impl From<&CopyDataType> for u64 {
}
}

impl Default for CopyDataType {
fn default() -> Self {
Self::Memory
}
}

impl_expr!(CopyDataType, u64::from);

/// Defines a single copy step in a copy event. This type is unified over the
Expand Down

0 comments on commit 902040e

Please sign in to comment.