From f22c2aa4da53c093c9a7c856cdcc7843eb75b852 Mon Sep 17 00:00:00 2001 From: Hubert Bugaj Date: Fri, 16 Jan 2026 18:09:38 +0100 Subject: [PATCH] chore: more derives, less manual boilerplate --- src/blocks/header.rs | 12 ++----- src/db/car/any.rs | 14 ++------ src/message/chain_message.rs | 14 +------- src/networks/network_name.rs | 64 +++--------------------------------- src/rpc/methods/eth/types.rs | 14 +------- src/shim/address.rs | 28 +++------------- src/shim/bigint.rs | 7 +--- src/shim/econ.rs | 30 +++-------------- src/shim/error.rs | 25 +++++++------- src/shim/gas.rs | 41 +++-------------------- src/shim/piece.rs | 33 ++++--------------- src/shim/randomness.rs | 15 ++------- src/shim/version.rs | 36 +++----------------- 13 files changed, 49 insertions(+), 284 deletions(-) diff --git a/src/blocks/header.rs b/src/blocks/header.rs index aed943944874..136b6a0fc3c3 100644 --- a/src/blocks/header.rs +++ b/src/blocks/header.rs @@ -1,7 +1,6 @@ // Copyright 2019-2026 ChainSafe Systems // SPDX-License-Identifier: Apache-2.0, MIT -use std::ops::Deref; use std::sync::{ OnceLock, atomic::{AtomicBool, Ordering}, @@ -227,8 +226,9 @@ impl GetSize for RawBlockHeader { /// A [`RawBlockHeader`] which caches calls to [`RawBlockHeader::cid`] and [`RawBlockHeader::verify_signature_against`] #[cfg_attr(test, derive(Default))] -#[derive(Debug, GetSize)] +#[derive(Debug, GetSize, derive_more::Deref)] pub struct CachingBlockHeader { + #[deref] uncached: RawBlockHeader, #[get_size(ignore)] cid: OnceLock, @@ -257,14 +257,6 @@ impl Clone for CachingBlockHeader { } } -impl Deref for CachingBlockHeader { - type Target = RawBlockHeader; - - fn deref(&self) -> &Self::Target { - &self.uncached - } -} - impl From for CachingBlockHeader { fn from(value: RawBlockHeader) -> Self { Self::new(value) diff --git a/src/db/car/any.rs b/src/db/car/any.rs index d69d98680712..a3347a214f45 100644 --- a/src/db/car/any.rs +++ b/src/db/car/any.rs @@ -21,9 +21,11 @@ use std::io::{Error, ErrorKind, Read, Result}; use std::path::{Path, PathBuf}; use std::sync::Arc; +#[derive(derive_more::From)] pub enum AnyCar { Plain(super::PlainCar), Forest(super::ForestCar), + #[from(skip)] Memory(super::PlainCar>), } @@ -166,18 +168,6 @@ where } } -impl From> for AnyCar { - fn from(car: super::ForestCar) -> Self { - Self::Forest(car) - } -} - -impl From> for AnyCar { - fn from(car: super::PlainCar) -> Self { - Self::Plain(car) - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/src/message/chain_message.rs b/src/message/chain_message.rs index c14179df42d8..e7b81e271726 100644 --- a/src/message/chain_message.rs +++ b/src/message/chain_message.rs @@ -11,7 +11,7 @@ use serde::{Deserialize, Serialize}; /// `Enum` to encapsulate signed and unsigned messages. Useful when working with /// both types -#[derive(Clone, Debug, Serialize, Deserialize, Hash, Eq, PartialEq, GetSize)] +#[derive(Clone, Debug, Serialize, Deserialize, Hash, Eq, PartialEq, GetSize, derive_more::From)] #[serde(untagged)] pub enum ChainMessage { Unsigned(Message), @@ -131,15 +131,3 @@ impl MessageTrait for ChainMessage { } } } - -impl From for ChainMessage { - fn from(value: Message) -> Self { - Self::Unsigned(value) - } -} - -impl From for ChainMessage { - fn from(value: SignedMessage) -> Self { - Self::Signed(value) - } -} diff --git a/src/networks/network_name.rs b/src/networks/network_name.rs index b64360a9e337..95b69d807858 100644 --- a/src/networks/network_name.rs +++ b/src/networks/network_name.rs @@ -11,68 +11,12 @@ /// The network name as defined in the genesis block. /// This is not necessarily the same as the network name that the node is /// currently on. This is used by `libp2p` layer and the message pool. +#[derive(derive_more::Display, derive_more::From, derive_more::Into, derive_more::AsRef)] +#[from(String, &str)] pub struct GenesisNetworkName(String); -impl AsRef for GenesisNetworkName { - fn as_ref(&self) -> &str { - &self.0 - } -} - -impl std::fmt::Display for GenesisNetworkName { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.0) - } -} - -impl From<&str> for GenesisNetworkName { - fn from(name: &str) -> Self { - Self(name.to_owned()) - } -} - -impl From for GenesisNetworkName { - fn from(name: String) -> Self { - Self(name) - } -} - -impl From for String { - fn from(name: GenesisNetworkName) -> Self { - name.0 - } -} - /// The network name as defined by the state of the node. /// This is the network name that the node is currently on. +#[derive(derive_more::Display, derive_more::From, derive_more::Into, derive_more::AsRef)] +#[from(String, &str)] pub struct StateNetworkName(String); - -impl AsRef for StateNetworkName { - fn as_ref(&self) -> &str { - &self.0 - } -} - -impl std::fmt::Display for StateNetworkName { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.0) - } -} - -impl From<&str> for StateNetworkName { - fn from(name: &str) -> Self { - Self(name.to_owned()) - } -} - -impl From for StateNetworkName { - fn from(name: String) -> Self { - Self(name) - } -} - -impl From for String { - fn from(name: StateNetworkName) -> Self { - name.0 - } -} diff --git a/src/rpc/methods/eth/types.rs b/src/rpc/methods/eth/types.rs index 2b59f4634e72..6f7ceeb17cd7 100644 --- a/src/rpc/methods/eth/types.rs +++ b/src/rpc/methods/eth/types.rs @@ -464,7 +464,7 @@ pub struct EthTopicSpec(pub Vec); /// Represents an [`EthAddress`] or a collection of thereof. This allows the caller to either use, /// e.g., `0x1234...` or `["0x1234...", "0x5678..."]` as the address parameter. -#[derive(PartialEq, Serialize, Deserialize, Debug, Clone, JsonSchema)] +#[derive(PartialEq, Serialize, Deserialize, Debug, Clone, JsonSchema, derive_more::From)] #[serde(untagged)] pub enum EthAddressList { List(Vec), @@ -477,18 +477,6 @@ impl Default for EthAddressList { } } -impl From for EthAddressList { - fn from(addr: EthAddress) -> Self { - EthAddressList::Single(addr) - } -} - -impl From> for EthAddressList { - fn from(addrs: Vec) -> Self { - EthAddressList::List(addrs) - } -} - impl Deref for EthAddressList { type Target = [EthAddress]; diff --git a/src/shim/address.rs b/src/shim/address.rs index 6c695097a628..b5a53318cfe1 100644 --- a/src/shim/address.rs +++ b/src/shim/address.rs @@ -122,6 +122,8 @@ mod network_guard_impl { Deserialize, derive_more::Deref, derive_more::DerefMut, + derive_more::From, + derive_more::Into, )] #[serde(transparent)] #[cfg_attr(test, derive(derive_quickcheck_arbitrary::Arbitrary))] @@ -284,6 +286,8 @@ impl GetSize for Address { Serialize, Deserialize, displaydoc::Display, + derive_more::From, + derive_more::Into, )] #[serde(transparent)] #[displaydoc("{0}")] @@ -303,18 +307,6 @@ impl FromStr for StrictAddress { // identical and able to do a conversion, otherwise it is a logic error and // Forest should not continue so there is no point in `TryFrom`. -impl From
for StrictAddress { - fn from(other: Address) -> Self { - StrictAddress(other) - } -} - -impl From for Address { - fn from(other: StrictAddress) -> Self { - other.0 - } -} - impl From for Address_v3 { fn from(other: StrictAddress) -> Self { other.0.into() @@ -327,24 +319,12 @@ impl From for Address_v4 { } } -impl From for Address { - fn from(other: Address_v4) -> Self { - Address(other) - } -} - impl From<&Address_v4> for Address { fn from(other: &Address_v4) -> Self { Address(*other) } } -impl From
for Address_v4 { - fn from(addr: Address) -> Self { - addr.0 - } -} - impl From<&Address> for Address_v4 { fn from(other: &Address) -> Self { other.0 diff --git a/src/shim/bigint.rs b/src/shim/bigint.rs index 4c5faf31fb84..7fb587961a14 100644 --- a/src/shim/bigint.rs +++ b/src/shim/bigint.rs @@ -14,12 +14,7 @@ use serde::{Deserialize, Serialize}; Deserialize, derive_more::Deref, derive_more::DerefMut, + derive_more::From, )] #[serde(transparent)] pub struct BigInt(#[serde(with = "bigint_ser")] num_bigint::BigInt); - -impl From for BigInt { - fn from(other: num_bigint::BigInt) -> Self { - BigInt(other) - } -} diff --git a/src/shim/econ.rs b/src/shim/econ.rs index 6173fabc1ec2..1b5dd6d03999 100644 --- a/src/shim/econ.rs +++ b/src/shim/econ.rs @@ -13,7 +13,6 @@ use num_traits::{One, Signed, Zero}; use serde::{Deserialize, Serialize}; use static_assertions::const_assert_eq; use std::{ - fmt, ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, Rem, Sub, SubAssign}, sync::LazyLock, }; @@ -37,16 +36,14 @@ pub static TOTAL_FILECOIN: LazyLock = Default, derive_more::Deref, derive_more::DerefMut, + derive_more::Debug, + derive_more::Display, + derive_more::From, + derive_more::Into, )] #[serde(transparent)] pub struct TokenAmount(TokenAmount_latest); -impl fmt::Debug for TokenAmount { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Debug::fmt(&self.0, f) - } -} - impl GetSize for TokenAmount { fn get_heap_size(&self) -> usize { big_int_heap_size_helper(self.0.atto()) @@ -111,13 +108,6 @@ impl Neg for &TokenAmount { } } -impl std::fmt::Display for TokenAmount { - // This trait requires `fmt` with this exact signature. - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - self.0.fmt(f) - } -} - impl TokenAmount { /// The logical number of decimal places of a token unit. pub const DECIMALS: usize = TokenAmount_latest::DECIMALS; @@ -218,12 +208,6 @@ impl From for TokenAmount { } } -impl From for TokenAmount { - fn from(other: TokenAmount_v4) -> Self { - TokenAmount(other) - } -} - impl From<&TokenAmount_v4> for TokenAmount { fn from(other: &TokenAmount_v4) -> Self { other.clone().into() @@ -254,12 +238,6 @@ impl From<&TokenAmount> for TokenAmount_v3 { } } -impl From for TokenAmount_v4 { - fn from(other: TokenAmount) -> Self { - other.0 - } -} - impl From<&TokenAmount> for TokenAmount_v4 { fn from(other: &TokenAmount) -> Self { other.0.clone() diff --git a/src/shim/error.rs b/src/shim/error.rs index 981de5fe2c8b..c9c3ea9e51bf 100644 --- a/src/shim/error.rs +++ b/src/shim/error.rs @@ -24,7 +24,18 @@ use std::fmt; /// assert_eq!(shim_from_v2, fvm2_success.into()); /// assert_eq!(shim_from_v3, fvm3_success.into()); /// ``` -#[derive(PartialEq, Eq, Debug, Clone, Copy, Serialize, Deserialize, JsonSchema)] +#[derive( + PartialEq, + Eq, + Debug, + Clone, + Copy, + Serialize, + Deserialize, + JsonSchema, + derive_more::From, + derive_more::Into, +)] pub struct ExitCode(#[schemars(with = "u32")] ExitCodeV4); impl PartialOrd for ExitCode { @@ -115,12 +126,6 @@ impl From for ExitCode { } } -impl From for ExitCode { - fn from(value: ExitCodeV4) -> Self { - Self(value) - } -} - impl From for ExitCode { fn from(value: ExitCodeV3) -> Self { value.value().into() @@ -144,9 +149,3 @@ impl From for ExitCodeV3 { Self::new(value.0.value()) } } - -impl From for ExitCodeV4 { - fn from(value: ExitCode) -> Self { - value.0 - } -} diff --git a/src/shim/gas.rs b/src/shim/gas.rs index 3de5ee544976..87c706447eab 100644 --- a/src/shim/gas.rs +++ b/src/shim/gas.rs @@ -17,20 +17,19 @@ use fvm3::gas::{ pub use fvm3::gas::{GasCharge as GasChargeV3, GasTracker, PriceList as PriceListV3}; use fvm4::gas::price_list_by_network_version as price_list_by_network_version_v4; pub use fvm4::gas::{ - Gas as GasV4, GasCharge as GasChargeV4, GasDuration as GasDurationV4, - GasOutputs as GasOutputsV4, PriceList as PriceListV4, + Gas as GasV4, GasCharge as GasChargeV4, GasOutputs as GasOutputsV4, PriceList as PriceListV4, }; use crate::shim::econ::TokenAmount; use crate::shim::version::NetworkVersion; -#[derive(Hash, Eq, PartialEq, Ord, PartialOrd, Copy, Clone, Default)] +#[derive(Hash, Eq, PartialEq, Ord, PartialOrd, Copy, Clone, Default, derive_more::From)] pub struct Gas(Gas_latest); -#[derive(Clone, Default)] +#[derive(Clone, Default, derive_more::From, derive_more::Into)] pub struct GasDuration(GasDuration_latest); -#[derive(Clone, Default)] +#[derive(Clone, Default, derive_more::From, derive_more::Into)] pub struct GasOutputs(GasOutputs_latest); impl Debug for Gas { @@ -109,13 +108,7 @@ impl From for GasV4 { } } -impl From for Gas { - fn from(value: GasV4) -> Self { - Gas(value) - } -} - -#[derive(Debug, Clone)] +#[derive(Debug, Clone, derive_more::From, derive_more::Into)] pub struct GasCharge(GasCharge_latest); impl GasCharge { @@ -162,12 +155,6 @@ impl From for GasCharge { } } -impl From for GasCharge { - fn from(value: GasChargeV4) -> Self { - GasCharge(value) - } -} - impl From for GasChargeV2 { fn from(value: GasCharge) -> Self { Self { @@ -189,18 +176,6 @@ impl From for GasChargeV3 { } } -impl From for GasChargeV4 { - fn from(value: GasCharge) -> Self { - value.0 - } -} - -impl From for GasDuration { - fn from(value: GasDurationV4) -> Self { - GasDuration(value) - } -} - impl GasOutputs { pub fn compute( // In whole gas units. @@ -218,12 +193,6 @@ impl GasOutputs { } } -impl From for GasOutputs { - fn from(value: GasOutputsV4) -> Self { - GasOutputs(value) - } -} - pub enum PriceList { V2(&'static PriceListV2), V3(&'static PriceListV3), diff --git a/src/shim/piece.rs b/src/shim/piece.rs index 3eab6ab6c0ff..e6531655cf47 100644 --- a/src/shim/piece.rs +++ b/src/shim/piece.rs @@ -5,11 +5,12 @@ use super::fvm_shared_latest::piece as piece_latest; use cid::Cid; use fvm_shared2::piece as piece_v2; use fvm_shared3::piece as piece_v3; -use fvm_shared4::piece as piece_v4; use serde::{Deserialize, Serialize}; /// Piece information for part or a whole file. -#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug)] +#[derive( + Serialize, Deserialize, PartialEq, Eq, Clone, Debug, derive_more::From, derive_more::Into, +)] #[serde(transparent)] pub struct PieceInfo(piece_latest::PieceInfo); @@ -22,18 +23,6 @@ impl PieceInfo { } } -impl From for piece_v4::PieceInfo { - fn from(value: PieceInfo) -> Self { - value.0 - } -} - -impl From for PieceInfo { - fn from(value: piece_v4::PieceInfo) -> Self { - Self(value) - } -} - impl From for piece_v2::PieceInfo { fn from(value: PieceInfo) -> Self { Self { @@ -71,7 +60,9 @@ impl From for PieceInfo { } /// Size of a piece in bytes with padding. -#[derive(PartialEq, Debug, Eq, Clone, Copy, Serialize, Deserialize)] +#[derive( + PartialEq, Debug, Eq, Clone, Copy, Serialize, Deserialize, derive_more::From, derive_more::Into, +)] #[serde(transparent)] pub struct PaddedPieceSize(piece_latest::PaddedPieceSize); @@ -81,18 +72,6 @@ impl From for PaddedPieceSize { } } -impl From for piece_v4::PaddedPieceSize { - fn from(value: PaddedPieceSize) -> Self { - value.0 - } -} - -impl From for PaddedPieceSize { - fn from(value: piece_v4::PaddedPieceSize) -> Self { - Self(value) - } -} - impl From for piece_v3::PaddedPieceSize { fn from(value: PaddedPieceSize) -> Self { Self(value.0.0) diff --git a/src/shim/randomness.rs b/src/shim/randomness.rs index d5014ec128da..c040ab87fc45 100644 --- a/src/shim/randomness.rs +++ b/src/shim/randomness.rs @@ -4,7 +4,6 @@ use super::fvm_shared_latest::randomness::Randomness as Randomness_latest; use fvm_shared2::randomness::Randomness as Randomness_v2; use fvm_shared3::randomness::Randomness as Randomness_v3; -use fvm_shared4::randomness::Randomness as Randomness_v4; use serde::{Deserialize, Serialize}; /// Represents a shim over `Randomness` from `fvm_shared` with convenience @@ -39,6 +38,8 @@ use serde::{Deserialize, Serialize}; Serialize, derive_more::Deref, derive_more::DerefMut, + derive_more::From, + derive_more::Into, )] #[serde(transparent)] pub struct Randomness(Randomness_latest); @@ -49,12 +50,6 @@ impl Randomness { } } -impl From for Randomness { - fn from(other: Randomness_v4) -> Self { - Randomness(other) - } -} - impl From for Randomness { fn from(other: Randomness_v3) -> Self { Randomness(Randomness_latest(other.0)) @@ -67,12 +62,6 @@ impl From for Randomness { } } -impl From for Randomness_v4 { - fn from(other: Randomness) -> Self { - other.0 - } -} - impl From for Randomness_v3 { fn from(other: Randomness) -> Self { Self(other.0.0) diff --git a/src/shim/version.rs b/src/shim/version.rs index 9b6be56b3358..11be64705728 100644 --- a/src/shim/version.rs +++ b/src/shim/version.rs @@ -1,6 +1,5 @@ // Copyright 2019-2026 ChainSafe Systems // SPDX-License-Identifier: Apache-2.0, MIT -use std::fmt; use std::str::FromStr; use crate::lotus_json::lotus_json_with_self; @@ -42,7 +41,12 @@ use serde::{Deserialize, Serialize}; JsonSchema, derive_more::Deref, derive_more::DerefMut, + derive_more::From, + derive_more::Into, + derive_more::Display, )] +#[from(u32, NetworkVersion_v4)] +#[into(u32, NetworkVersion_v4)] #[repr(transparent)] #[serde(transparent)] pub struct NetworkVersion(#[schemars(with = "u32")] pub NetworkVersion_latest); @@ -68,18 +72,6 @@ define_network_versions!( 26, 27, 28, ); -impl From for NetworkVersion { - fn from(value: u32) -> Self { - NetworkVersion(NetworkVersion_latest::new(value)) - } -} - -impl From for u32 { - fn from(value: NetworkVersion) -> Self { - value.0.into() - } -} - impl From for NetworkVersion { fn from(value: NetworkVersion_v2) -> Self { NetworkVersion((value as u32).into()) @@ -92,12 +84,6 @@ impl From for NetworkVersion { } } -impl From for NetworkVersion { - fn from(value: NetworkVersion_v4) -> Self { - NetworkVersion(value) - } -} - impl From for NetworkVersion_v2 { fn from(other: NetworkVersion) -> NetworkVersion_v2 { u32::from(other.0).try_into().expect("Infallible") @@ -110,12 +96,6 @@ impl From for NetworkVersion_v3 { } } -impl From for NetworkVersion_v4 { - fn from(other: NetworkVersion) -> Self { - other.0 - } -} - impl FromStr for NetworkVersion { type Err = ::Err; @@ -125,12 +105,6 @@ impl FromStr for NetworkVersion { } } -impl fmt::Display for NetworkVersion { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.0.fmt(f) - } -} - #[cfg(test)] impl quickcheck::Arbitrary for NetworkVersion { fn arbitrary(g: &mut quickcheck::Gen) -> Self {