-
Notifications
You must be signed in to change notification settings - Fork 190
fix: reduce repetitive code in enum match blocks #6453
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
49a6a88
bd4fddc
1882931
ea9a27d
f060b73
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,15 +2,17 @@ | |
| // SPDX-License-Identifier: Apache-2.0, MIT | ||
|
|
||
| use super::super::convert::{from_address_v3_to_v2, from_address_v4_to_v2}; | ||
| use crate::shim; | ||
| use crate::shim::{self, address::Address}; | ||
| use serde::Serialize; | ||
| use spire_enum::prelude::delegated_enum; | ||
|
|
||
| /// Account actor method. | ||
| pub type Method = fil_actor_account_state::v8::Method; | ||
|
|
||
| /// Account actor state. | ||
| #[derive(Serialize, Debug)] | ||
| #[serde(untagged)] | ||
| #[delegated_enum(impl_conversions)] | ||
| pub enum State { | ||
| V8(fil_actor_account_state::v8::State), | ||
| V9(fil_actor_account_state::v9::State), | ||
|
|
@@ -25,19 +27,8 @@ pub enum State { | |
| } | ||
|
|
||
| impl State { | ||
| pub fn pubkey_address(&self) -> fvm_shared2::address::Address { | ||
| match self { | ||
| State::V8(st) => st.address, | ||
| State::V9(st) => st.address, | ||
| State::V10(st) => from_address_v3_to_v2(st.address), | ||
| State::V11(st) => from_address_v3_to_v2(st.address), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does it do it now? What's the error message in case of a fiasco?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I don't quite get it. This method has no error propogation.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't, but it panics with a certain string. pub fn from_address_v3_to_v2(addr: AddressV3) -> AddressV2 {
AddressV2::from_bytes(&addr.to_bytes())
.expect("Couldn't convert between FVM3 and FVM2 addresses.")
}Given the conversion is not trivial (one has to call
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No conversion happens here, plz note that the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah okay, I missed the signature change. LGTM. |
||
| State::V12(st) => from_address_v4_to_v2(st.address), | ||
| State::V13(st) => from_address_v4_to_v2(st.address), | ||
| State::V14(st) => from_address_v4_to_v2(st.address), | ||
| State::V15(st) => from_address_v4_to_v2(st.address), | ||
| State::V16(st) => from_address_v4_to_v2(st.address), | ||
| State::V17(st) => from_address_v4_to_v2(st.address), | ||
| } | ||
| pub fn pubkey_address(&self) -> Address { | ||
| delegate_state!(self.address.into()) | ||
| } | ||
|
|
||
| pub fn default_latest_version(address: fvm_shared4::address::Address) -> Self { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to my understanding, we can avoid the declarative macro via
#[delegate_impl], no?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tried
#[delegate_impl]but it does not compile. According to the doc,#[delegate_impl]is used for trait impls.https://docs.rs/spire_enum/1.0.0/spire_enum/index.html#2-delegate_impl-inherenttrait-impl-attribute-macro
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, indeed. I wonder if
VMshould've been a trait. Something to consider in the future, but outside of the scope of this PR.