-
Notifications
You must be signed in to change notification settings - Fork 13k
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
BufWriter: Provide into_raw_parts #79705
Conversation
r? @dtolnay (rust-highfive has picked a reviewer for you, use r? to override) |
If something goes wrong, one might want to unpeel the layers of nested Writers to perform recovery actions on the underlying writer, or reuse its resources. `into_inner` can be used for this when the inner writer is still working. But when the inner writer is broken, and returning errors, `into_inner` simply gives you the error from flush, and the same `Bufwriter` back again. Here I provide the necessary function, which I have chosen to call `into_raw_parts`. I had to do something with `panicked`. Returning it to the caller as a boolean seemed rather bare. Throwing the buffered data away in this situation also seems unfriendly: maybe the programmer knows something about the underlying writer and can recover somehow. So I went for a custom Error. This may be overkill, but it does have the nice property that a caller who actually wants to look at the buffered data, rather than simply extracting the inner writer, will be told by the type system if they forget to handle the panicked case. If a caller doesn't need the buffer, it can just be discarded. That WriterPanicked is a newtype around Vec<u8> means that hopefully the layouts of the Ok and Err variants can be very similar, with just a boolean discriminant. So this custom error type should compile down to nearly no code. Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
997670a
to
3817631
Compare
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
Co-authored-by: Ivan Tham <pickfire@riseup.net>
I spend some time thinking about this, and I agree the API you propose makes more sense than the alternatives. We'll just have to get some experience with this new feature to find out if it works well in practice. Can you open a tracking issue for this? Thanks! |
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
This comment has been minimized.
This comment has been minimized.
@rustbot modify labels -S-waiting-on-author +S-waiting-on-review |
@bors r+ |
📌 Commit dea6d6c has been approved by |
⌛ Testing commit dea6d6c with merge 049587609826dd4af26d187ed4be5bb464d6f3d2... |
The job Click to see the possible cause of the failure (guessed by this bot)
|
💔 Test failed - checks-actions |
Weird. Same thing happened on another PR yesterday. @bors retry |
Mara Bos writes ("Re: [rust-lang/rust] BufWriter: Provide into_raw_parts (#79705)"):
Weird. Same thing happened on another PR yesterday.
13 is SIGPIPE. That is odd because Rust's startup code (IMO
unjustifiably) catches SIGPIPE. See #62569, #46016.
The test case for #80893 tests rustdoc `-test-builder`, passing
`true`. Perhaps that makes rustdoc much faster so we lose some race.
…--
Ian Jackson <ijackson@chiark.greenend.org.uk> These opinions are my own.
Pronouns: they/he. If I emailed you from @fyvzl.net or @evade.org.uk,
that is a private address which bypasses my fierce spamfilter.
|
☀️ Test successful - checks-actions |
If something goes wrong, one might want to unpeel the layers of nested
Writers to perform recovery actions on the underlying writer, or reuse
its resources.
into_inner
can be used for this when the inner writer is stillworking. But when the inner writer is broken, and returning errors,
into_inner
simply gives you the error from flush, and the sameBufwriter
back again.Here I provide the necessary function, which I have chosen to call
into_raw_parts
.I had to do something with
panicked
. Returning it to the caller asa boolean seemed rather bare. Throwing the buffered data away in this
situation also seems unfriendly: maybe the programmer knows something
about the underlying writer and can recover somehow.
So I went for a custom Error. This may be overkill, but it does have
the nice property that a caller who actually wants to look at the
buffered data, rather than simply extracting the inner writer, will be
told by the type system if they forget to handle the panicked case.
If a caller doesn't need the buffer, it can just be discarded. That
WriterPanicked is a newtype around Vec means that hopefully the
layouts of the Ok and Err variants can be very similar, with just a
boolean discriminant. So this custom error type should compile down
to nearly no code.
If this general idea is felt appropriate, I will open a tracking issue, etc.