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

BufWriter: Provide into_raw_parts #79705

Merged
merged 5 commits into from
Jan 19, 2021
Merged

Conversation

ijackson
Copy link
Contributor

@ijackson ijackson commented Dec 4, 2020

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

@rust-highfive
Copy link
Collaborator

r? @dtolnay

(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 Dec 4, 2020
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>
@m-ou-se m-ou-se assigned m-ou-se and unassigned dtolnay Dec 4, 2020
@m-ou-se m-ou-se added A-io Area: std::io, std::fs, std::net and std::path T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Dec 4, 2020
ijackson and others added 3 commits December 12, 2020 12:34
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
Co-authored-by: Ivan Tham <pickfire@riseup.net>
@m-ou-se
Copy link
Member

m-ou-se commented Dec 30, 2020

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!

@m-ou-se m-ou-se added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 30, 2020
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
@ijackson

This comment has been minimized.

@ijackson
Copy link
Contributor Author

ijackson commented Jan 4, 2021

@rustbot modify labels -S-waiting-on-author +S-waiting-on-review

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jan 4, 2021
@m-ou-se
Copy link
Member

m-ou-se commented Jan 19, 2021

@bors r+

@bors
Copy link
Contributor

bors commented Jan 19, 2021

📌 Commit dea6d6c has been approved by m-ou-se

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 19, 2021
@bors
Copy link
Contributor

bors commented Jan 19, 2021

⌛ Testing commit dea6d6c with merge 049587609826dd4af26d187ed4be5bb464d6f3d2...

@rust-log-analyzer
Copy link
Collaborator

The job i686-gnu failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
Some tests failed in compiletest suite=rustdoc mode=rustdoc host=i686-unknown-linux-gnu target=i686-unknown-linux-gnu

---- [rustdoc] rustdoc/issue-80893.rs stdout ----

error: rustdoc failed!
status: signal: 13
command: "/checkout/obj/build/i686-unknown-linux-gnu/stage2/bin/rustdoc" "-L" "/checkout/obj/build/i686-unknown-linux-gnu/stage2/lib/rustlib/i686-unknown-linux-gnu/lib" "-L" "/checkout/obj/build/i686-unknown-linux-gnu/test/rustdoc/issue-80893/auxiliary" "-o" "/checkout/obj/build/i686-unknown-linux-gnu/test/rustdoc/issue-80893" "/checkout/src/test/rustdoc/issue-80893.rs" "--test" "-Z" "unstable-options" "--test-builder" "true"
------------------------------------------

running 1 test

---
test result: FAILED. 409 passed; 1 failed; 3 ignored; 0 measured; 0 filtered out; finished in 24.65s



command did not execute successfully: "/checkout/obj/build/i686-unknown-linux-gnu/stage0-tools-bin/compiletest" "--compile-lib-path" "/checkout/obj/build/i686-unknown-linux-gnu/stage2/lib" "--run-lib-path" "/checkout/obj/build/i686-unknown-linux-gnu/stage2/lib/rustlib/i686-unknown-linux-gnu/lib" "--rustc-path" "/checkout/obj/build/i686-unknown-linux-gnu/stage2/bin/rustc" "--rustdoc-path" "/checkout/obj/build/i686-unknown-linux-gnu/stage2/bin/rustdoc" "--src-base" "/checkout/src/test/rustdoc" "--build-base" "/checkout/obj/build/i686-unknown-linux-gnu/test/rustdoc" "--stage-id" "stage2-i686-unknown-linux-gnu" "--suite" "rustdoc" "--mode" "rustdoc" "--target" "i686-unknown-linux-gnu" "--host" "i686-unknown-linux-gnu" "--llvm-filecheck" "/checkout/obj/build/i686-unknown-linux-gnu/llvm/build/bin/FileCheck" "--host-rustcflags" "-Crpath -O -Cdebuginfo=0 -Zunstable-options  -Lnative=/checkout/obj/build/i686-unknown-linux-gnu/native/rust-test-helpers" "--target-rustcflags" "-Crpath -O -Cdebuginfo=0 -Zunstable-options  -Lnative=/checkout/obj/build/i686-unknown-linux-gnu/native/rust-test-helpers" "--docck-python" "/usr/bin/python3" "--lldb-python" "/usr/bin/python3" "--gdb" "/usr/bin/gdb" "--llvm-version" "11.0.1-rust-1.51.0-nightly" "--llvm-components" "aarch64 aarch64asmparser aarch64codegen aarch64desc aarch64disassembler aarch64info aarch64utils aggressiveinstcombine all all-targets analysis arm armasmparser armcodegen armdesc armdisassembler arminfo armutils asmparser asmprinter avr avrasmparser avrcodegen avrdesc avrdisassembler avrinfo binaryformat bitreader bitstreamreader bitwriter cfguard codegen core coroutines coverage debuginfocodeview debuginfodwarf debuginfogsym debuginfomsf debuginfopdb demangle dlltooldriver dwarflinker engine executionengine extensions frontendopenmp fuzzmutate globalisel gtest gtest_main hexagon hexagonasmparser hexagoncodegen hexagondesc hexagondisassembler hexagoninfo instcombine instrumentation interpreter ipo irreader jitlink libdriver lineeditor linker lto mc mca mcdisassembler mcjit mcparser mips mipsasmparser mipscodegen mipsdesc mipsdisassembler mipsinfo mirparser msp430 msp430asmparser msp430codegen msp430desc msp430disassembler msp430info native nativecodegen nvptx nvptxcodegen nvptxdesc nvptxinfo objcarcopts object objectyaml option orcerror orcjit passes powerpc powerpcasmparser powerpccodegen powerpcdesc powerpcdisassembler powerpcinfo profiledata remarks riscv riscvasmparser riscvcodegen riscvdesc riscvdisassembler riscvinfo riscvutils runtimedyld scalaropts selectiondag sparc sparcasmparser sparccodegen sparcdesc sparcdisassembler sparcinfo support symbolize systemz systemzasmparser systemzcodegen systemzdesc systemzdisassembler systemzinfo tablegen target testingsupport textapi transformutils vectorize webassembly webassemblyasmparser webassemblycodegen webassemblydesc webassemblydisassembler webassemblyinfo windowsmanifest x86 x86asmparser x86codegen x86desc x86disassembler x86info xray" "--cc" "" "--cxx" "" "--cflags" "" "--adb-path" "adb" "--adb-test-dir" "/data/tmp/work" "--android-cross-path" "" "--color" "always"


failed to run: /checkout/obj/build/bootstrap/debug/bootstrap --stage 2 test --exclude src/bootstrap --exclude src/test/rustdoc-js --exclude src/tools/error_index_generator --exclude src/tools/linkchecker
Build completed unsuccessfully in 0:26:37

@bors
Copy link
Contributor

bors commented Jan 19, 2021

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jan 19, 2021
@m-ou-se
Copy link
Member

m-ou-se commented Jan 19, 2021

Weird. Same thing happened on another PR yesterday.

@bors retry

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 19, 2021
@ijackson
Copy link
Contributor Author

ijackson commented Jan 19, 2021 via email

@bors
Copy link
Contributor

bors commented Jan 19, 2021

⌛ Testing commit dea6d6c with merge cf04ae5...

@bors
Copy link
Contributor

bors commented Jan 19, 2021

☀️ Test successful - checks-actions
Approved by: m-ou-se
Pushing cf04ae5 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jan 19, 2021
@bors bors merged commit cf04ae5 into rust-lang:master Jan 19, 2021
@rustbot rustbot added this to the 1.51.0 milestone Jan 19, 2021
@bors bors mentioned this pull request Jan 19, 2021
@jyn514 jyn514 mentioned this pull request Jan 19, 2021
@ijackson ijackson deleted the bufwriter-disassemble branch August 24, 2021 17:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-io Area: std::io, std::fs, std::net and std::path merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

None yet

8 participants