Skip to content

Commit ffe19b9

Browse files
authored
chore: more derives, less manual boilerplate (#6434)
1 parent ab5f82f commit ffe19b9

File tree

13 files changed

+49
-284
lines changed

13 files changed

+49
-284
lines changed

src/blocks/header.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright 2019-2026 ChainSafe Systems
22
// SPDX-License-Identifier: Apache-2.0, MIT
33

4-
use std::ops::Deref;
54
use std::sync::{
65
OnceLock,
76
atomic::{AtomicBool, Ordering},
@@ -227,8 +226,9 @@ impl GetSize for RawBlockHeader {
227226

228227
/// A [`RawBlockHeader`] which caches calls to [`RawBlockHeader::cid`] and [`RawBlockHeader::verify_signature_against`]
229228
#[cfg_attr(test, derive(Default))]
230-
#[derive(Debug, GetSize)]
229+
#[derive(Debug, GetSize, derive_more::Deref)]
231230
pub struct CachingBlockHeader {
231+
#[deref]
232232
uncached: RawBlockHeader,
233233
#[get_size(ignore)]
234234
cid: OnceLock<Cid>,
@@ -257,14 +257,6 @@ impl Clone for CachingBlockHeader {
257257
}
258258
}
259259

260-
impl Deref for CachingBlockHeader {
261-
type Target = RawBlockHeader;
262-
263-
fn deref(&self) -> &Self::Target {
264-
&self.uncached
265-
}
266-
}
267-
268260
impl From<RawBlockHeader> for CachingBlockHeader {
269261
fn from(value: RawBlockHeader) -> Self {
270262
Self::new(value)

src/db/car/any.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ use std::io::{Error, ErrorKind, Read, Result};
2121
use std::path::{Path, PathBuf};
2222
use std::sync::Arc;
2323

24+
#[derive(derive_more::From)]
2425
pub enum AnyCar<ReaderT> {
2526
Plain(super::PlainCar<ReaderT>),
2627
Forest(super::ForestCar<ReaderT>),
28+
#[from(skip)]
2729
Memory(super::PlainCar<Vec<u8>>),
2830
}
2931

@@ -166,18 +168,6 @@ where
166168
}
167169
}
168170

169-
impl<ReaderT> From<super::ForestCar<ReaderT>> for AnyCar<ReaderT> {
170-
fn from(car: super::ForestCar<ReaderT>) -> Self {
171-
Self::Forest(car)
172-
}
173-
}
174-
175-
impl<ReaderT> From<super::PlainCar<ReaderT>> for AnyCar<ReaderT> {
176-
fn from(car: super::PlainCar<ReaderT>) -> Self {
177-
Self::Plain(car)
178-
}
179-
}
180-
181171
#[cfg(test)]
182172
mod tests {
183173
use super::*;

src/message/chain_message.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use serde::{Deserialize, Serialize};
1111

1212
/// `Enum` to encapsulate signed and unsigned messages. Useful when working with
1313
/// both types
14-
#[derive(Clone, Debug, Serialize, Deserialize, Hash, Eq, PartialEq, GetSize)]
14+
#[derive(Clone, Debug, Serialize, Deserialize, Hash, Eq, PartialEq, GetSize, derive_more::From)]
1515
#[serde(untagged)]
1616
pub enum ChainMessage {
1717
Unsigned(Message),
@@ -131,15 +131,3 @@ impl MessageTrait for ChainMessage {
131131
}
132132
}
133133
}
134-
135-
impl From<Message> for ChainMessage {
136-
fn from(value: Message) -> Self {
137-
Self::Unsigned(value)
138-
}
139-
}
140-
141-
impl From<SignedMessage> for ChainMessage {
142-
fn from(value: SignedMessage) -> Self {
143-
Self::Signed(value)
144-
}
145-
}

src/networks/network_name.rs

Lines changed: 4 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -11,68 +11,12 @@
1111
/// The network name as defined in the genesis block.
1212
/// This is not necessarily the same as the network name that the node is
1313
/// currently on. This is used by `libp2p` layer and the message pool.
14+
#[derive(derive_more::Display, derive_more::From, derive_more::Into, derive_more::AsRef)]
15+
#[from(String, &str)]
1416
pub struct GenesisNetworkName(String);
1517

16-
impl AsRef<str> for GenesisNetworkName {
17-
fn as_ref(&self) -> &str {
18-
&self.0
19-
}
20-
}
21-
22-
impl std::fmt::Display for GenesisNetworkName {
23-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24-
write!(f, "{}", self.0)
25-
}
26-
}
27-
28-
impl From<&str> for GenesisNetworkName {
29-
fn from(name: &str) -> Self {
30-
Self(name.to_owned())
31-
}
32-
}
33-
34-
impl From<String> for GenesisNetworkName {
35-
fn from(name: String) -> Self {
36-
Self(name)
37-
}
38-
}
39-
40-
impl From<GenesisNetworkName> for String {
41-
fn from(name: GenesisNetworkName) -> Self {
42-
name.0
43-
}
44-
}
45-
4618
/// The network name as defined by the state of the node.
4719
/// This is the network name that the node is currently on.
20+
#[derive(derive_more::Display, derive_more::From, derive_more::Into, derive_more::AsRef)]
21+
#[from(String, &str)]
4822
pub struct StateNetworkName(String);
49-
50-
impl AsRef<str> for StateNetworkName {
51-
fn as_ref(&self) -> &str {
52-
&self.0
53-
}
54-
}
55-
56-
impl std::fmt::Display for StateNetworkName {
57-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
58-
write!(f, "{}", self.0)
59-
}
60-
}
61-
62-
impl From<&str> for StateNetworkName {
63-
fn from(name: &str) -> Self {
64-
Self(name.to_owned())
65-
}
66-
}
67-
68-
impl From<String> for StateNetworkName {
69-
fn from(name: String) -> Self {
70-
Self(name)
71-
}
72-
}
73-
74-
impl From<StateNetworkName> for String {
75-
fn from(name: StateNetworkName) -> Self {
76-
name.0
77-
}
78-
}

src/rpc/methods/eth/types.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ pub struct EthTopicSpec(pub Vec<EthHashList>);
464464

465465
/// Represents an [`EthAddress`] or a collection of thereof. This allows the caller to either use,
466466
/// e.g., `0x1234...` or `["0x1234...", "0x5678..."]` as the address parameter.
467-
#[derive(PartialEq, Serialize, Deserialize, Debug, Clone, JsonSchema)]
467+
#[derive(PartialEq, Serialize, Deserialize, Debug, Clone, JsonSchema, derive_more::From)]
468468
#[serde(untagged)]
469469
pub enum EthAddressList {
470470
List(Vec<EthAddress>),
@@ -477,18 +477,6 @@ impl Default for EthAddressList {
477477
}
478478
}
479479

480-
impl From<EthAddress> for EthAddressList {
481-
fn from(addr: EthAddress) -> Self {
482-
EthAddressList::Single(addr)
483-
}
484-
}
485-
486-
impl From<Vec<EthAddress>> for EthAddressList {
487-
fn from(addrs: Vec<EthAddress>) -> Self {
488-
EthAddressList::List(addrs)
489-
}
490-
}
491-
492480
impl Deref for EthAddressList {
493481
type Target = [EthAddress];
494482

src/shim/address.rs

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ mod network_guard_impl {
122122
Deserialize,
123123
derive_more::Deref,
124124
derive_more::DerefMut,
125+
derive_more::From,
126+
derive_more::Into,
125127
)]
126128
#[serde(transparent)]
127129
#[cfg_attr(test, derive(derive_quickcheck_arbitrary::Arbitrary))]
@@ -284,6 +286,8 @@ impl GetSize for Address {
284286
Serialize,
285287
Deserialize,
286288
displaydoc::Display,
289+
derive_more::From,
290+
derive_more::Into,
287291
)]
288292
#[serde(transparent)]
289293
#[displaydoc("{0}")]
@@ -303,18 +307,6 @@ impl FromStr for StrictAddress {
303307
// identical and able to do a conversion, otherwise it is a logic error and
304308
// Forest should not continue so there is no point in `TryFrom`.
305309

306-
impl From<Address> for StrictAddress {
307-
fn from(other: Address) -> Self {
308-
StrictAddress(other)
309-
}
310-
}
311-
312-
impl From<StrictAddress> for Address {
313-
fn from(other: StrictAddress) -> Self {
314-
other.0
315-
}
316-
}
317-
318310
impl From<StrictAddress> for Address_v3 {
319311
fn from(other: StrictAddress) -> Self {
320312
other.0.into()
@@ -327,24 +319,12 @@ impl From<StrictAddress> for Address_v4 {
327319
}
328320
}
329321

330-
impl From<Address_v4> for Address {
331-
fn from(other: Address_v4) -> Self {
332-
Address(other)
333-
}
334-
}
335-
336322
impl From<&Address_v4> for Address {
337323
fn from(other: &Address_v4) -> Self {
338324
Address(*other)
339325
}
340326
}
341327

342-
impl From<Address> for Address_v4 {
343-
fn from(addr: Address) -> Self {
344-
addr.0
345-
}
346-
}
347-
348328
impl From<&Address> for Address_v4 {
349329
fn from(other: &Address) -> Self {
350330
other.0

src/shim/bigint.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,7 @@ use serde::{Deserialize, Serialize};
1414
Deserialize,
1515
derive_more::Deref,
1616
derive_more::DerefMut,
17+
derive_more::From,
1718
)]
1819
#[serde(transparent)]
1920
pub struct BigInt(#[serde(with = "bigint_ser")] num_bigint::BigInt);
20-
21-
impl From<num_bigint::BigInt> for BigInt {
22-
fn from(other: num_bigint::BigInt) -> Self {
23-
BigInt(other)
24-
}
25-
}

src/shim/econ.rs

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use num_traits::{One, Signed, Zero};
1313
use serde::{Deserialize, Serialize};
1414
use static_assertions::const_assert_eq;
1515
use std::{
16-
fmt,
1716
ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, Rem, Sub, SubAssign},
1817
sync::LazyLock,
1918
};
@@ -37,16 +36,14 @@ pub static TOTAL_FILECOIN: LazyLock<TokenAmount> =
3736
Default,
3837
derive_more::Deref,
3938
derive_more::DerefMut,
39+
derive_more::Debug,
40+
derive_more::Display,
41+
derive_more::From,
42+
derive_more::Into,
4043
)]
4144
#[serde(transparent)]
4245
pub struct TokenAmount(TokenAmount_latest);
4346

44-
impl fmt::Debug for TokenAmount {
45-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46-
fmt::Debug::fmt(&self.0, f)
47-
}
48-
}
49-
5047
impl GetSize for TokenAmount {
5148
fn get_heap_size(&self) -> usize {
5249
big_int_heap_size_helper(self.0.atto())
@@ -111,13 +108,6 @@ impl Neg for &TokenAmount {
111108
}
112109
}
113110

114-
impl std::fmt::Display for TokenAmount {
115-
// This trait requires `fmt` with this exact signature.
116-
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
117-
self.0.fmt(f)
118-
}
119-
}
120-
121111
impl TokenAmount {
122112
/// The logical number of decimal places of a token unit.
123113
pub const DECIMALS: usize = TokenAmount_latest::DECIMALS;
@@ -218,12 +208,6 @@ impl From<TokenAmount_v3> for TokenAmount {
218208
}
219209
}
220210

221-
impl From<TokenAmount_v4> for TokenAmount {
222-
fn from(other: TokenAmount_v4) -> Self {
223-
TokenAmount(other)
224-
}
225-
}
226-
227211
impl From<&TokenAmount_v4> for TokenAmount {
228212
fn from(other: &TokenAmount_v4) -> Self {
229213
other.clone().into()
@@ -254,12 +238,6 @@ impl From<&TokenAmount> for TokenAmount_v3 {
254238
}
255239
}
256240

257-
impl From<TokenAmount> for TokenAmount_v4 {
258-
fn from(other: TokenAmount) -> Self {
259-
other.0
260-
}
261-
}
262-
263241
impl From<&TokenAmount> for TokenAmount_v4 {
264242
fn from(other: &TokenAmount) -> Self {
265243
other.0.clone()

src/shim/error.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,18 @@ use std::fmt;
2424
/// assert_eq!(shim_from_v2, fvm2_success.into());
2525
/// assert_eq!(shim_from_v3, fvm3_success.into());
2626
/// ```
27-
#[derive(PartialEq, Eq, Debug, Clone, Copy, Serialize, Deserialize, JsonSchema)]
27+
#[derive(
28+
PartialEq,
29+
Eq,
30+
Debug,
31+
Clone,
32+
Copy,
33+
Serialize,
34+
Deserialize,
35+
JsonSchema,
36+
derive_more::From,
37+
derive_more::Into,
38+
)]
2839
pub struct ExitCode(#[schemars(with = "u32")] ExitCodeV4);
2940

3041
impl PartialOrd for ExitCode {
@@ -115,12 +126,6 @@ impl From<u32> for ExitCode {
115126
}
116127
}
117128

118-
impl From<ExitCodeV4> for ExitCode {
119-
fn from(value: ExitCodeV4) -> Self {
120-
Self(value)
121-
}
122-
}
123-
124129
impl From<ExitCodeV3> for ExitCode {
125130
fn from(value: ExitCodeV3) -> Self {
126131
value.value().into()
@@ -144,9 +149,3 @@ impl From<ExitCode> for ExitCodeV3 {
144149
Self::new(value.0.value())
145150
}
146151
}
147-
148-
impl From<ExitCode> for ExitCodeV4 {
149-
fn from(value: ExitCode) -> Self {
150-
value.0
151-
}
152-
}

0 commit comments

Comments
 (0)