-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Add conversions from io:ErrorKind
to io::Error
#37037
Conversation
r? @sfackler (rust_highfive has picked a reviewer for you, use r? to override) |
6b252bc
to
43decfc
Compare
io::Error's layout was designed to mimimize its size since it is so frequently passed around. I would be somewhat worried about blowing it up from 16 bytes to 40. If the message is cut out we should be okay size wise though. |
Cutting the message out would make us lose information, though, which is why it seems best not to do that; though I'm not sure how often the messages carry meaningful information with them not already included in the ErrorKind. |
I think I'd personally want to hear a very compelling story around why it's crucial to have custom messages and not allocate at the same time before blowing up io::Error's size. The use case described in the related issue wants to avoid allocation because the errors are being generated very frequently, in which case a user isn't going to be looking at the message almost by definition. |
I'll push code sometime soon removing the Cow from the non-allocating repr variant; I agree that it's unlikely an accompanying message would be useful for the case this is intended to be used for. On another note, can someone explain/help me out to see why Travis is failing and how to fix it? I didn't change anything inside Vec, but I see "std/vec/struct.Vec.html:2447: id is not unique: |
Dunno what's going on with the travis errors. Seems like it's probably a false alarm? |
I've pushed (force) a couple times already, and it's repeated itself a couple times (same error), so I feel like it might not be.... though I agree it feels spurious. |
No reason to have a String or Cow in a non-heap variant. Needs a different solution, for example there is room for an application-specific error identifier (number) in addition to ErrorKind, but it's not going to be easy to use. |
I agree with @sfackler that the representation here was chosen pretty precisely to ensure an efficient runtime representation, and we'd likely want some strong numbers to change it much. In the current iteration, though, I'm not sure I understand what the benefit is over just |
Adding a u64 or another integer/number type to the new variant will also work (no increase in size); but I'm not sure how useful that is, since it will be library specific and difficult to differentiate between (library A and B could use the same error kind and attached integer for different meanings, which would get confusing). |
@rfcbot fcp close With the particulars around the representation chosen here I personally feel that just being able to construct from a |
Team member @alexcrichton has proposed to close this. The next step is review by the rest of the tagged teams: No concerns currently listed. Once these reviewers reach consensus, this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
@alexcrichton The issue (#36658) which spurred this PR suggests that heap-allocating errors is significant. Can you document your thoughts on how crates/libstd should be creating stack-allocated errors for the common case (e.g. Is the intent that crates would use the |
I was going to suggest that we could provide a conversion from However, perhaps we could offer a conversion from |
@Mark-Simulacrum Unfortunately we still don't have numbers one way or another (beyond microbenchmarks), so the perf wins are still somewhat heresay. Crates can always create a non-allocating version like the standard library does, which is to use @aturon that's essentially what this PR does, adding |
@alexcrichton Right, but I'm saying we could do it without adding another variant. In your comment you mentioned "the particulars of the representation" so I was thinking you were concerned about the representation change. I also disagree about the prominence of |
Oh we can add new variants so long as they don't inflate the size of the payload. The current PR would suffice for that. I erroneously remembered this as having a new |
@alexcrichton OK -- so can you elaborate a bit more on your concern here? Having a platform-agnostic, lightweight way to go from an |
I'm moderately in favor of adding this as well. The extra variant shouldn't cause any size blowup and I can see the allocation cost mattering in certain rare-ish situations. |
I would find it a little odd that we can construct an error with so little information. An error created with only an |
I believe the point is that some IO Errors (and ErrorKinds) are usually not intended for presentation to the user; e.g. WouldBlock seems to be mainly a program-internal error message, not something the user would care about. I can add to the documentation comment (did I write one?) to include that this is intended for library/application-internal error construction that will not be exposed to the end user. |
Ok, given that information, let's go the other way instead: @rfc fcp cancel |
@rfcbot fcp cancel |
@alexcrichton proposal cancelled. |
@rfcbot fcp merge |
Just so it's clear, this shouldn't be merged yet--documentation needs to be added. I don't really want to type it out prior to a decision wrt to merging, though. |
Can we land this thing? This PR has been open for 23 days. It really doesn't seem necessary to wait around for another week. |
Team member @alexcrichton has proposed to merge this. The next step is review by the rest of the tagged teams: No concerns currently listed. Once these reviewers reach consensus, this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
Two things that would need to be discussed:
|
@sfackler remember that "FCP" for PRs is insta-merge, no need to wait for a week (just need to wait for eyes). I personally feel this should be called |
At least judging from every other T-libs PR, it'll take at least a week to get everyone to check their boxes.
|
I'd actually also be fine with just: impl From<ErrorKind> for Error {
// ...
} |
👍 for |
|
io:ErrorKind
to io::Error
SGTM |
e8ed76e
to
35b5a1a
Compare
|
||
/// Intended for use for errors not exposed to the user, where allocating onto | ||
/// the heap (for normal construction via Error::new) is too costly. | ||
#[stable(feature = "io_error_from_errorkind", since = "1.13.0")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if this is the right tag here.
Implemented the I'm not sure if the documentation comment is sufficiently emphatic about not exposing this "raw" container to users, please let me know if you have better text for it. |
35b5a1a
to
e1acbbe
Compare
Implement From<ErrorKind> for io::Error, intended for use with errors that should never be exposed to the user.
e1acbbe
to
99234bb
Compare
Looks good to me! |
🔔 This is now entering its final comment period, as per the review above. 🔔 psst @alexcrichton, I wasn't able to add the |
@bors: r+ |
📌 Commit 99234bb has been approved by |
⌛ Testing commit 99234bb with merge d2bc30b... |
Add conversions from `io:ErrorKind` to `io::Error` Filing to help with discussion around the possibility of doing this. Current changes are clearly backwards incompatible, but I think adding a new function (with a bikeshed on naming) like `Error::new_str` should be possible (or some other way of specializing the string error message case) to fix #36658.
Thanks for making this happen @Mark-Simulacrum |
Filing to help with discussion around the possibility of doing this.
Current changes are clearly backwards incompatible, but I think adding a new function (with a bikeshed on naming) like
Error::new_str
should be possible (or some other way of specializing the string error message case) to fix #36658.