Skip to content

Commit b1b1a2c

Browse files
authored
fix: reduce repetitive code in enum match blocks (#6453)
1 parent 26757f5 commit b1b1a2c

File tree

8 files changed

+173
-168
lines changed

8 files changed

+173
-168
lines changed

Cargo.lock

Lines changed: 126 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ similar = "2"
196196
slotmap = "1"
197197
smallvec = "1"
198198
smart-default = "0.7"
199+
spire_enum = "1"
199200
stacker = "0.1"
200201
static_assertions = "1"
201202
statrs = "0.18"

src/interpreter/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,5 @@ where
4646

4747
let acc_st = account::State::load(store, act.code, act.state)?;
4848

49-
Ok(acc_st.pubkey_address().into())
49+
Ok(acc_st.pubkey_address())
5050
}

src/interpreter/vm.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ use fvm4::{
5353
},
5454
};
5555
use num::Zero;
56+
use spire_enum::prelude::delegated_enum;
5657
use std::time::{Duration, Instant};
5758

5859
pub(in crate::interpreter) type ForestMachineV2<DB> =
@@ -139,6 +140,7 @@ impl BlockMessages {
139140

140141
/// Interpreter which handles execution of state transitioning messages and
141142
/// returns receipts from the VM execution.
143+
#[delegated_enum(impl_conversions)]
142144
pub enum VM<DB: Blockstore + Send + Sync + 'static> {
143145
VM2(ForestExecutorV2<DB>),
144146
VM3(ForestExecutorV3<DB>),
@@ -272,11 +274,7 @@ where
272274

273275
/// Flush stores in VM and return state root.
274276
pub fn flush(&mut self) -> anyhow::Result<Cid> {
275-
match self {
276-
VM::VM2(fvm_executor) => Ok(fvm_executor.flush()?),
277-
VM::VM3(fvm_executor) => Ok(fvm_executor.flush()?),
278-
VM::VM4(fvm_executor) => Ok(fvm_executor.flush()?),
279-
}
277+
Ok(delegate_vm!(self.flush()?))
280278
}
281279

282280
/// Get actor state from an address. Will be resolved to ID address.

src/lotus_json/actors/states/account_state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl HasLotusJson for State {
2929

3030
fn into_lotus_json(self) -> Self::LotusJson {
3131
AccountStateLotusJson {
32-
address: self.pubkey_address().into(),
32+
address: self.pubkey_address(),
3333
}
3434
}
3535

src/shim/actors/builtin/account/mod.rs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22
// SPDX-License-Identifier: Apache-2.0, MIT
33

44
use super::super::convert::{from_address_v3_to_v2, from_address_v4_to_v2};
5-
use crate::shim;
5+
use crate::shim::{self, address::Address};
66
use serde::Serialize;
7+
use spire_enum::prelude::delegated_enum;
78

89
/// Account actor method.
910
pub type Method = fil_actor_account_state::v8::Method;
1011

1112
/// Account actor state.
1213
#[derive(Serialize, Debug)]
1314
#[serde(untagged)]
15+
#[delegated_enum(impl_conversions)]
1416
pub enum State {
1517
V8(fil_actor_account_state::v8::State),
1618
V9(fil_actor_account_state::v9::State),
@@ -25,19 +27,8 @@ pub enum State {
2527
}
2628

2729
impl State {
28-
pub fn pubkey_address(&self) -> fvm_shared2::address::Address {
29-
match self {
30-
State::V8(st) => st.address,
31-
State::V9(st) => st.address,
32-
State::V10(st) => from_address_v3_to_v2(st.address),
33-
State::V11(st) => from_address_v3_to_v2(st.address),
34-
State::V12(st) => from_address_v4_to_v2(st.address),
35-
State::V13(st) => from_address_v4_to_v2(st.address),
36-
State::V14(st) => from_address_v4_to_v2(st.address),
37-
State::V15(st) => from_address_v4_to_v2(st.address),
38-
State::V16(st) => from_address_v4_to_v2(st.address),
39-
State::V17(st) => from_address_v4_to_v2(st.address),
40-
}
30+
pub fn pubkey_address(&self) -> Address {
31+
delegate_state!(self.address.into())
4132
}
4233

4334
pub fn default_latest_version(address: fvm_shared4::address::Address) -> Self {

0 commit comments

Comments
 (0)