Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement From<TryReserveError> for io::Error #96505

Closed
wants to merge 3 commits into from

Conversation

nvzqz
Copy link
Contributor

@nvzqz nvzqz commented Apr 28, 2022

The try_reserve methods may be called when allocating for I/O.

The `try_reserve` methods may be called when allocating for I/O.
@rust-highfive
Copy link
Collaborator

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with r? rust-lang/libs-api @rustbot label +T-libs-api -T-libs to request review from a libs-api team reviewer. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Apr 28, 2022
@rust-highfive
Copy link
Collaborator

r? @m-ou-se

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 28, 2022
@nvzqz
Copy link
Contributor Author

nvzqz commented Apr 28, 2022

r? rust-lang/libs-api @rustbot label +T-libs-api -T-libs

@rustbot rustbot added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Apr 28, 2022
@rust-highfive rust-highfive assigned dtolnay and unassigned m-ou-se Apr 28, 2022
@rustbot rustbot removed the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Apr 28, 2022
@m-ou-se
Copy link
Member

m-ou-se commented Apr 28, 2022

I feel somewhat similar about this as I did about #96104, although mapping TryReserveError to io::ErrorKind::OutOfMemory seems more obvious and universally correct.

I wonder if CapacityOverflow should map to OutOfMemory as well, instead of InvalidInput. CapacityOverflow usually means you're trying to allocate even more than could possibly be allocated, which arguably still fits within the 'out of memory' category.

TryReserveErrorKind::AllocError { .. } => ErrorKind::OutOfMemory,
};

Error::new(kind, error)
Copy link
Member

Choose a reason for hiding this comment

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

Note that this involves a double allocation, effectively a Box<Box<dyn io::Error>>.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't realize io::Error::new was a double-allocation! I wonder if thin dyn box would solve that.

Copy link
Member

Choose a reason for hiding this comment

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

Even worse: if you pass it a string literal ( Error::new(kind, "...")) you get a triple allocation (and a strcpy/memcpy) as the inner box will contain a String.

ThinBox can save one allocation here. I think that'd be a simple change to make, but there might be some non-obvious pitfall.

Copy link
Member

Choose a reason for hiding this comment

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

See #83352

@nvzqz
Copy link
Contributor Author

nvzqz commented Apr 28, 2022

I agree with yours and @dtolnay's points in #96104. I think io::Error gets too often used as a catch-all and thus the value exposed at such a generic point (From impl) may be too ambiguous.

Copy link
Member

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

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

Since you agree with #96104 (comment), the same point definitely applies to this impl too so I'll close the PR. Thanks anyway!

@dtolnay dtolnay closed this May 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants