Skip to content

chore: derive more constructors#6438

Merged
LesnyRumcajs merged 1 commit intomainfrom
reduce-boilerplate-constructors
Jan 19, 2026
Merged

chore: derive more constructors#6438
LesnyRumcajs merged 1 commit intomainfrom
reduce-boilerplate-constructors

Conversation

@LesnyRumcajs
Copy link
Member

@LesnyRumcajs LesnyRumcajs commented Jan 19, 2026

Summary of changes

Changes introduced in this pull request:

  • use derive_more::Constructor to remove trivial c-tor boilerplate

Reference issue to close (if applicable)

Closes

Other information and links

Change checklist

  • I have performed a self-review of my own code,
  • I have made corresponding changes to the documentation. All new code adheres to the team's documentation standards,
  • I have added tests that prove my fix is effective or that my feature works (if possible),
  • I have made sure the CHANGELOG is up-to-date. All user-facing changes should be reflected in this document.

Summary by CodeRabbit

Release Notes

  • Refactor
    • Unified constructor implementations across 16 struct types by transitioning from explicit manual constructors to derive-based auto-generation. This improves code consistency and maintainability while maintaining complete backward compatibility with all existing public APIs.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 19, 2026

Walkthrough

This PR systematically replaces manual constructor implementations with automatically generated ones using derive_more::Constructor across 16 structs. The structs retain identical functionality while reducing boilerplate code through derivation macros.

Changes

Cohort / File(s) Summary
Block types
src/blocks/ticket.rs, src/blocks/vrf_proof.rs
Added derive_more::Constructor to Ticket and VRFProof; removed test-only manual constructors
Chain module
src/chain/snapshot_format.rs
Added derive_more::Constructor to FilecoinSnapshotMetadata; removed explicit new() constructor
Chain sync
src/chain_sync/metrics.rs, src/chain_sync/network_context.rs
Added derive_more::Constructor to Libp2pMessageKindLabel and SyncNetworkContext; removed manual new() implementations
Database
src/db/blockstore_with_read_cache.rs
Added derive_more::Constructor to BlockstoreWithReadCache; removed explicit constructor
Consensus & keystore
src/fil_cns/mod.rs, src/key_management/keystore.rs
Added derive_more::Constructor to FilecoinConsensus and KeyInfo; removed manual constructors
Message pool
src/message_pool/msgpool/provider.rs
Added derive_more::Constructor to MpoolRpcProvider; removed explicit new() implementation
Metrics & telemetry
src/metrics/mod.rs, src/networks/metrics.rs
Added derive_more::Constructor to KindLabel, NetworkHeightCollector, and NetworkVersionCollector; removed manual constructors
RPC endpoints
src/rpc/methods/eth/pubsub.rs, src/rpc/types/mod.rs
Added derive_more::Constructor to EthPubSub and MinerSectors; removed explicit constructors
Cryptography & state
src/shim/crypto.rs, src/state_manager/chain_rand.rs
Added derive_more::Constructor to Signature and ChainRand; removed manual new() implementations
Utilities
src/utils/version/mod.rs
Added derive_more::Constructor to VersionLabel; removed explicit constructor

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~15 minutes

Possibly related PRs

Suggested reviewers

  • hanabi1224
  • akaladarshi
🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'chore: derive more constructors' accurately describes the main objective of the changeset: replacing handwritten constructors with derive_more::Constructor macros across multiple structs.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Comment @coderabbitai help to get the list of available commands and usage tips.

@LesnyRumcajs LesnyRumcajs force-pushed the reduce-boilerplate-constructors branch from 0b9cf00 to 265861d Compare January 19, 2026 10:28
@LesnyRumcajs LesnyRumcajs marked this pull request as ready for review January 19, 2026 10:29
@LesnyRumcajs LesnyRumcajs requested a review from a team as a code owner January 19, 2026 10:29
@LesnyRumcajs LesnyRumcajs requested review from akaladarshi and hanabi1224 and removed request for a team January 19, 2026 10:29
Copy link
Collaborator

@akaladarshi akaladarshi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/metrics/mod.rs (1)

172-184: Derived constructor is not const fn — breaks const initialization.

The previous manual constructor was pub const fn new(...), which allowed its use in const contexts (lines 181, 183). The derive_more::Constructor macro generates a regular pub fn new(...), not a const fn. This will cause a compilation error for the const items in the values module.

🐛 Proposed fix: Keep manual const constructor or use direct initialization

Option 1: Restore the manual const constructor:

-#[derive(Clone, Debug, Hash, PartialEq, Eq, EncodeLabelSet, derive_more::Constructor)]
+#[derive(Clone, Debug, Hash, PartialEq, Eq, EncodeLabelSet)]
 pub struct KindLabel {
     kind: &'static str,
 }
+
+impl KindLabel {
+    pub const fn new(kind: &'static str) -> Self {
+        Self { kind }
+    }
+}

Option 2: Use direct struct initialization in values module:

 pub mod values {
     use super::KindLabel;
 
     /// `TipsetCache`.
-    pub const TIPSET: KindLabel = KindLabel::new("tipset");
+    pub const TIPSET: KindLabel = KindLabel { kind: "tipset" };
     /// tipset cache in state manager
-    pub const STATE_MANAGER_TIPSET: KindLabel = KindLabel::new("sm_tipset");
+    pub const STATE_MANAGER_TIPSET: KindLabel = KindLabel { kind: "sm_tipset" };
 }
🤖 Fix all issues with AI agents
In `@src/utils/version/mod.rs`:
- Around line 24-27: The public struct VersionLabel lacks documentation; add a
doc comment above the VersionLabel definition describing its purpose (e.g., that
it represents a Prometheus label for the Forest version metric) so the pub
struct VersionLabel is documented per guidelines, keeping the comment concise
and placed immediately before the derive/struct declaration.
🧹 Nitpick comments (2)
src/fil_cns/mod.rs (1)

60-68: Add a brief doc comment for FilecoinConsensus.
Line 61 defines a public struct without a /// doc comment; please add a short summary. As per coding guidelines, document public structs.

src/rpc/methods/eth/pubsub.rs (1)

72-75: Add a brief doc comment for EthPubSub.
Line 73 defines a public struct without a /// doc comment; please add a short summary. As per coding guidelines, document public structs.

Comment on lines +24 to 27
#[derive(Clone, Debug, Hash, PartialEq, Eq, EncodeLabelSet, derive_more::Constructor)]
pub struct VersionLabel {
version: &'static str,
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Search for const/static uses of VersionLabel::new
rg -n --type=rust 'const\s+.*VersionLabel::new'
rg -n --type=rust 'static\s+.*VersionLabel::new'
# Also search for any VersionLabel::new calls
rg -n --type=rust 'VersionLabel::new'

Repository: ChainSafe/forest

Length of output: 166


🏁 Script executed:

# Check the current state of the file
cat -n src/utils/version/mod.rs

Repository: ChainSafe/forest

Length of output: 2205


🏁 Script executed:

# Check derive_more version and constructor behavior
fd -e toml | xargs grep -l 'derive_more' | head -1 | xargs cat

Repository: ChainSafe/forest

Length of output: 11258


🌐 Web query:

derive_more v2 Constructor derive macro const fn

💡 Result:

Yes — derive_more v2's #[derive(Constructor)] generates a pub const fn new(...) constructor. It’s provided by the "constructor" feature; docs show the generated impl uses pub const fn new(...) -> Type { ... }. [1][2]

References:

  • Constructor docs (examples showing pub const fn new). [1]
  • derive_more changelog noting Constructor now generates const fn (1.0.0 / v2). [2]

Add doc comment for public struct.
The VersionLabel struct is public but lacks documentation. As per coding guidelines, document all public structs with doc comments.

/// Prometheus label for the Forest version metric.
#[derive(Clone, Debug, Hash, PartialEq, Eq, EncodeLabelSet, derive_more::Constructor)]
pub struct VersionLabel {
    version: &'static str,
}
🤖 Prompt for AI Agents
In `@src/utils/version/mod.rs` around lines 24 - 27, The public struct
VersionLabel lacks documentation; add a doc comment above the VersionLabel
definition describing its purpose (e.g., that it represents a Prometheus label
for the Forest version metric) so the pub struct VersionLabel is documented per
guidelines, keeping the comment concise and placed immediately before the
derive/struct declaration.

@codecov
Copy link

codecov bot commented Jan 19, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 58.76%. Comparing base (ffe19b9) to head (265861d).
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files
Files with missing lines Coverage Δ
src/blocks/ticket.rs 100.00% <ø> (ø)
src/blocks/vrf_proof.rs 33.33% <ø> (-16.67%) ⬇️
src/chain/snapshot_format.rs 42.85% <ø> (-13.67%) ⬇️
src/chain_sync/metrics.rs 0.00% <ø> (ø)
src/chain_sync/network_context.rs 30.27% <ø> (-2.02%) ⬇️
src/db/blockstore_with_read_cache.rs 93.90% <ø> (-0.48%) ⬇️
src/fil_cns/mod.rs 23.52% <ø> (+3.52%) ⬆️
src/key_management/keystore.rs 78.12% <ø> (-0.45%) ⬇️
src/message_pool/msgpool/provider.rs 34.88% <ø> (-4.25%) ⬇️
src/metrics/mod.rs 19.69% <ø> (+0.43%) ⬆️
... and 6 more

... and 7 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ffe19b9...265861d. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@LesnyRumcajs LesnyRumcajs added this pull request to the merge queue Jan 19, 2026
Merged via the queue into main with commit de93062 Jan 19, 2026
47 checks passed
@LesnyRumcajs LesnyRumcajs deleted the reduce-boilerplate-constructors branch January 19, 2026 11:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants