Skip to content

doc: document best practices for traits as part of style guide - #1618

Merged
Wodann merged 7 commits into
mainfrom
doc/trait-best-practices
Aug 27, 2026
Merged

doc: document best practices for traits as part of style guide#1618
Wodann merged 7 commits into
mainfrom
doc/trait-best-practices

Conversation

@Wodann

@Wodann Wodann commented Aug 12, 2026

Copy link
Copy Markdown
Member

Adds a section to the book's style guide explaining best practices and rules for using traits that are implicit in the existing design of (most of) the multi-chain architecture.

@Wodann
Wodann requested a review from anaPerezGhiglia August 12, 2026 19:13
@Wodann Wodann self-assigned this Aug 12, 2026
@Wodann Wodann added the no changeset needed This PR doesn't require a changeset label Aug 12, 2026
@changeset-bot

changeset-bot Bot commented Aug 12, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 7a54f12

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@codecov

codecov Bot commented Aug 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.91%. Comparing base (9637a17) to head (7a54f12).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1618      +/-   ##
==========================================
- Coverage   79.91%   79.91%   -0.01%     
==========================================
  Files         461      461              
  Lines       80120    80120              
  Branches    80120    80120              
==========================================
- Hits        64025    64024       -1     
  Misses      13887    13887              
- Partials     2208     2209       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Wodann
Wodann had a problem deploying to github-action-benchmark August 12, 2026 19:42 — with GitHub Actions Failure
@Wodann
Wodann had a problem deploying to github-action-benchmark August 12, 2026 19:42 — with GitHub Actions Failure

@anaPerezGhiglia anaPerezGhiglia left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The CI is failing, and I left some suggestions that are worth considering before merging, but I'm approving it since is just a doc entry, to avoid blocking the merge


When defining traits, try to keep traits composable by only defining the generic types, associated types and/or functions needed for a specific use case.

Furthermore, avoid overconstraining traits by only requiring:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This sentence is not easy to read. Is not clear until you read both bulletpoints which way is intended, since it can be interpreted both ways:

  • you are overconstraining when you only require (which should be avoided)
  • you should avoid overconstraining by requiring only the listed elements

Perhaps change it to

Suggested change
Furthermore, avoid overconstraining traits by only requiring:
Furthermore, avoid overconstraining traits. Require only:

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Addressed by e1754e9

Comment on lines +269 to +273
When a user is calling a function or using a type with trait bounds, they will have to satisfy all the constraints of the trait bounds.
By using the least constraining option, you can reduce the number of constraints that need to be satisfied at usage sites; thus making the function more reusable and composable.

Moreover, this limits the number of obligations the trait solver has to discharge and—because well-formedness obligations feed region inference—the number of lifetime constraints that come with them.
Historically, we have run into problems where an unrelated trait bound resulted in an `outlives` requirement and the error blamed a lifetime that was in fact valid.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this is almost a copy-paste from "Defining traits" section. I think it can be unified. If you prefer keeping a Rationale subsection for each section, then I would suggest editing them so the reader can spot where the difference is

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Addressed by 14ad9e5

@anaPerezGhiglia

Copy link
Copy Markdown
Contributor

by the way, thanks for putting this together! 🙇

@Wodann
Wodann temporarily deployed to github-action-benchmark August 27, 2026 20:56 — with GitHub Actions Inactive
Wodann added 2 commits August 27, 2026 21:10
`prettier --check "**/*.md"` runs in CI with `proseWrap: "never"`, so
the hard-wrapped paragraphs and `*emphasis*` failed the lint.
"avoid overconstraining traits by only requiring" parsed two ways:
the list could read as the cause of overconstraining or as the
permitted set. Split it into an imperative and its object.
@Wodann
Wodann had a problem deploying to github-action-benchmark August 27, 2026 21:13 — with GitHub Actions Error
@Wodann
Wodann had a problem deploying to github-action-benchmark August 27, 2026 21:13 — with GitHub Actions Error
@Wodann
Wodann temporarily deployed to github-action-benchmark August 27, 2026 21:25 — with GitHub Actions Inactive
@Wodann
Wodann had a problem deploying to github-action-benchmark August 27, 2026 21:27 — with GitHub Actions Error
@Wodann
Wodann had a problem deploying to github-action-benchmark August 27, 2026 21:27 — with GitHub Actions Error
Wodann added 3 commits August 27, 2026 21:34
The rationales for defining traits and for using trait bounds argued
the same point in near-identical wording. Both rules now live under a
`Traits` heading that carries a single rationale, which also picks up
the details each copy stated alone.
Also renames the second, differently defined `FullChainSpec` example,
which collided with the first now that both sit under one heading.
In `trait A: B`, B is A's supertrait and A is B's subtrait. The guide
had the two reversed, including in the derive-ordering rule, whose own
example lists supertraits first.
@Wodann
Wodann force-pushed the doc/trait-best-practices branch from b38e44d to 7a54f12 Compare August 27, 2026 21:35
@Wodann
Wodann temporarily deployed to github-action-benchmark August 27, 2026 21:36 — with GitHub Actions Inactive
@Wodann
Wodann temporarily deployed to github-action-benchmark August 27, 2026 21:38 — with GitHub Actions Inactive
@Wodann
Wodann temporarily deployed to github-action-benchmark August 27, 2026 21:38 — with GitHub Actions Inactive
@Wodann
Wodann added this pull request to the merge queue Aug 27, 2026
Merged via the queue into main with commit 16771cd Aug 27, 2026
59 checks passed
@Wodann
Wodann deleted the doc/trait-best-practices branch August 27, 2026 21:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no changeset needed This PR doesn't require a changeset

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants