Skip to content
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

refactor: use serde_repr to cut some boilerplate #7147

Merged
merged 2 commits into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions core/primitives-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ bs58 = "0.4"
derive_more = "0.99.3"
num-rational = { version = "0.3.1", features = ["serde"]}
serde = { version = "1", features = ["derive"] }
serde_repr = { version = "0.1.8" }
strum = { version = "0.24", features = ["derive"] }
sha2 = "0.10"
deepsize = { version = "0.2.0", optional = true }
Expand Down
94 changes: 22 additions & 72 deletions core/primitives-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,17 @@ fn wasmer2_stack_limit_default() -> i32 {
/// `0` or `1`. We could have used a `bool` instead, but there's a chance that
/// our current impl isn't perfect either and would need further tweaks in the
/// future.
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
#[derive(
Debug,
Clone,
Copy,
Hash,
PartialEq,
Eq,
serde_repr::Serialize_repr,
serde_repr::Deserialize_repr,
)]
#[repr(u8)]
pub enum StackLimiterVersion {
/// Old, buggy version, don't use it unless specifically to support old protocol version.
V0,
Expand All @@ -121,44 +131,19 @@ impl StackLimiterVersion {
fn v0() -> StackLimiterVersion {
StackLimiterVersion::V0
}
fn repr(self) -> u32 {
match self {
StackLimiterVersion::V0 => 0,
StackLimiterVersion::V1 => 1,
}
}
fn from_repr(repr: u32) -> Option<StackLimiterVersion> {
let res = match repr {
0 => StackLimiterVersion::V0,
1 => StackLimiterVersion::V1,
_ => return None,
};
Some(res)
}
}

impl Serialize for StackLimiterVersion {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.repr().serialize(serializer)
}
}

impl<'de> Deserialize<'de> for StackLimiterVersion {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
u32::deserialize(deserializer).and_then(|repr| {
StackLimiterVersion::from_repr(repr)
.ok_or_else(|| serde::de::Error::custom("invalid stack_limiter_version"))
})
}
}

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
#[derive(
Debug,
Clone,
Copy,
Hash,
PartialEq,
Eq,
serde_repr::Serialize_repr,
serde_repr::Deserialize_repr,
)]
#[repr(u8)]
pub enum AccountIdValidityRulesVersion {
/// Skip account ID validation according to legacy rules.
V0,
Expand All @@ -170,41 +155,6 @@ impl AccountIdValidityRulesVersion {
fn v0() -> AccountIdValidityRulesVersion {
AccountIdValidityRulesVersion::V0
}
fn repr(self) -> u32 {
match self {
AccountIdValidityRulesVersion::V0 => 0,
AccountIdValidityRulesVersion::V1 => 1,
}
}
fn from_repr(repr: u32) -> Option<AccountIdValidityRulesVersion> {
let res = match repr {
0 => AccountIdValidityRulesVersion::V0,
1 => AccountIdValidityRulesVersion::V1,
_ => return None,
};
Some(res)
}
}

impl Serialize for AccountIdValidityRulesVersion {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.repr().serialize(serializer)
}
}

impl<'de> Deserialize<'de> for AccountIdValidityRulesVersion {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
u32::deserialize(deserializer).and_then(|repr| {
AccountIdValidityRulesVersion::from_repr(repr)
.ok_or_else(|| serde::de::Error::custom("invalid account_id_validity_rules"))
})
}
}

impl VMConfig {
Expand Down