Skip to content

Add public constructors for SizeError and ValidityError - #3678

Open
barunaniket wants to merge 4 commits into
google:mainfrom
barunaniket:size-error-constructor
Open

barunaniket wants to merge 4 commits into
google:mainfrom
barunaniket:size-error-constructor

Conversation

@barunaniket

Copy link
Copy Markdown

Summary

Callers who know, out-of-band, that a conversion would fail due to incorrect source size or invalid source data currently have no way to construct a SizeError or ValidityError directly, and must instead perform a contrived failing conversion just to obtain one (see #3666).

This makes SizeError::new and ValidityError::new public so these errors can be constructed directly and forwarded via the existing From impls into ConvertError, TryReadError, etc.

AlignmentError is intentionally left as-is: its constructor is unsafe because of a soundness invariant (Dst's alignment requirement must be greater than one, relied on elsewhere via unreachable_unchecked), so a safe public constructor would need a checked design (e.g. panicking or Option-returning) rather than a simple visibility change — that seems like a separate decision for maintainers to weigh in on.

Fixes #3666

Test plan

  • cargo test --doc — new doc examples for both constructors compile and pass
  • cargo test --lib — full library test suite passes (112 tests)
  • cargo test --doc (full suite) — 128 doc tests pass
  • cargo clippy — no new warnings in error.rs
  • cargo build --no-default-features — no_std build succeeds
  • rustfmt +nightly --check on the changed file — no diff introduced by this change

Callers who know, out-of-band, that a conversion would fail due to
incorrect source size or invalid source data currently have no way to
construct a SizeError or ValidityError directly, and must instead
perform a contrived failing conversion just to obtain one (see google#3666).

Make SizeError::new and ValidityError::new public so these errors can
be constructed directly and forwarded via the existing From impls into
ConvertError, TryReadError, etc.

Fixes google#3666
@google-cla

google-cla Bot commented Sep 12, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@codecov-commenter

codecov-commenter commented Sep 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.96%. Comparing base (218f771) to head (ad8d743).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3678      +/-   ##
==========================================
+ Coverage   91.90%   91.96%   +0.05%     
==========================================
  Files          20       20              
  Lines        6175     6183       +8     
==========================================
+ Hits         5675     5686      +11     
+ Misses        500      497       -3     

☔ 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.

Adding rustdoc to SizeError::new shifted every line below it in
error.rs down by 16, including the ValidityError struct definition
whose TryFromBytes bound is cited by rustc in trybuild UI test
snapshots (try_transmute[.mut/.ref].{msrv,stable,nightly}.stderr).
Update the embedded line number from 590 to 606 to match.
The previous fix updated the '--> src/error.rs:590:45' pointer line but
missed the quoted source excerpt line ('590 | pub struct ValidityError...')
that rustc also embeds in the same diagnostic. Update it to 606 as well.
@barunaniket

Copy link
Copy Markdown
Author

@joshlf This is ready for review whenever you get a chance — CI is green. Let me know if you'd like any changes.

This branch has not been deployed

No deployments
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.

Add a way to construct Size Errors, etc

2 participants