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

use checked write in LineWriter example #51628

Merged
merged 2 commits into from
Jul 19, 2018
Merged

Conversation

euclio
Copy link
Contributor

@euclio euclio commented Jun 19, 2018

The example was wrong because it didn't check the return value of
write(), and it didn't flush the buffer before comparing the contents
of the file.

Fixes #51621.

@rust-highfive
Copy link
Collaborator

r? @withoutboats

(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 Jun 19, 2018
/// for &byte in road_not_taken.iter() {
/// file.write(&[byte]).unwrap();
/// }
/// file.write_all(road_not_taken)?;
Copy link
Member

Choose a reason for hiding this comment

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

The example is explicitly writing each byte individually to note how the buffering provided by LineWriter helps things. If we were going to write the whole message out at once, there's no reason to use the LineWriter at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's still line buffering behind the scenes, so I don't think that it's totally wrong. I'm certainly open to suggestions on how to improve the entire example, though. Maybe we could write some bytes, and demonstrate that they aren't written to disk until a newline is written?

Copy link
Member

Choose a reason for hiding this comment

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

That seems reasonable.

@pietroalbini pietroalbini 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 Jun 25, 2018
@TimNN
Copy link
Contributor

TimNN commented Jul 3, 2018

Ping from triage, @withoutboats : This PR needs your review!

@euclio
Copy link
Contributor Author

euclio commented Jul 3, 2018

This isn't ready for review, I plan to rewrite the example per sfackler's review.

The original example didn't check the return value of `write()`, didn't
flush the writer, and didn't properly demonstrate the buffering.

Fixes rust-lang#51621.
@euclio
Copy link
Contributor Author

euclio commented Jul 8, 2018

@sfackler @withoutboats This is ready for another review.

///
/// // No bytes are written until a newline is encountered (or
/// // the internal buffer is filled).
/// assert_eq!(fs::read_to_string("poem.txt")?.as_bytes(), b"");
Copy link
Member

Choose a reason for hiding this comment

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

I think fs::read_to_string(...)?.as_bytes() can just be fs::read(...)?

https://doc.rust-lang.org/nightly/std/fs/fn.read.html

Copy link
Member

Choose a reason for hiding this comment

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

Alternatively this can be assert_eq!(fs::read_to_string("poem.txt")?, "").

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@TimNN TimNN 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 Jul 17, 2018
@TimNN
Copy link
Contributor

TimNN commented Jul 17, 2018

Ping from triage, @withoutboats: This PR is waiting for your review.

Copy link
Member

@frewsxcv frewsxcv left a comment

Choose a reason for hiding this comment

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

This looks great, thanks for this 🎉

@frewsxcv
Copy link
Member

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Jul 18, 2018

📌 Commit c12a757 has been approved by frewsxcv

@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 Jul 18, 2018
kennytm added a commit to kennytm/rust that referenced this pull request Jul 18, 2018
use checked write in `LineWriter` example

The example was wrong because it didn't check the return value of
`write()`, and it didn't flush the buffer before comparing the contents
of the file.

Fixes rust-lang#51621.
bors added a commit that referenced this pull request Jul 19, 2018
Rollup of 13 pull requests

Successful merges:

 - #51628 (use checked write in `LineWriter` example)
 - #52116 (Handle array manually in str case conversion methods)
 - #52218 (Amend option.take examples)
 - #52418 (Do not use desugared ident when suggesting adding a type)
 - #52439 (Revert some changes from #51917 to fix custom libdir)
 - #52455 (Fix doc comment: use `?` instead of `.unwrap()`)
 - #52458 (rustc: Fix a suggestion for the `proc_macro` feature)
 - #52464 (Allow clippy to be installed with make install)
 - #52472 (rustc: Enable `use_extern_macros` in 2018 edition)
 - #52477 (Clarify short-circuiting behvaior of Iterator::zip.)
 - #52480 (Cleanup #24958)
 - #52487 (Don't build twice the sanitizers on Linux)
 - #52510 (rustdoc: remove FIXME about macro redirects)

Failed merges:

r? @ghost
@bors bors merged commit c12a757 into rust-lang:master Jul 19, 2018
@euclio euclio deleted the line-writer branch February 7, 2019 18:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants