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

Add a std::io::read_to_string function #80217

Merged
merged 4 commits into from
Jan 14, 2021
Merged

Conversation

camelid
Copy link
Member

@camelid camelid commented Dec 20, 2020

I recognize that you're usually supposed to open an issue first, but the
implementation is very small so it's okay if this is closed and it was 'wasted
work' :)


The equivalent of std::fs::read_to_string, but generalized to all
Read impls.

As the documentation on std::io::read_to_string says, the advantage of
this function is that it means you don't have to create a variable first
and it provides more type safety since you can only get the buffer out
if there were no errors. If you use Read::read_to_string, you have to
remember to check whether the read succeeded because otherwise your
buffer will be empty.

It's friendlier to newcomers and better in most cases to use an explicit
return value instead of an out parameter.

@camelid camelid added A-io Area: `std::io`, `std::fs`, `std::net` and `std::path` C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Dec 20, 2020
@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 Dec 20, 2020
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

The equivalent of `std::fs::read_to_string`, but generalized to all
`Read` impls.

As the documentation on `std::io::read_to_string` says, the advantage of
this function is that it means you don't have to create a variable first
and it provides more type safety since you can only get the buffer out
if there were no errors. If you use `Read::read_to_string`, you have to
remember to check whether the read succeeded because otherwise your
buffer will be empty.

It's friendlier to newcomers and better in most cases to use an explicit
return value instead of an out parameter.
Copy link
Member

@m-ou-se m-ou-se left a comment

Choose a reason for hiding this comment

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

Thanks! Looks good!

One small comment:

library/std/src/io/mod.rs Outdated Show resolved Hide resolved
@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
@camelid camelid added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. 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-author Status: This is awaiting some action (such as code changes or more information) from the author. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 30, 2020
@camelid camelid removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jan 12, 2021
@camelid camelid added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 12, 2021
@danclive
Copy link
Contributor

We could add a note to the "Read::read_to_string" example to tell him that he can write such a "helper" function to help him Read a string. We don't need to add this function to the std.

@m-ou-se
Copy link
Member

m-ou-se commented Jan 13, 2021

Looks good, @camelid!

@bors r+

@bors
Copy link
Contributor

bors commented Jan 13, 2021

📌 Commit 7463292 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 13, 2021
@m-ou-se
Copy link
Member

m-ou-se commented Jan 13, 2021

We could add a note to the "Read::read_to_string" example to tell him that he can write such a "helper" function to help him Read a string.

Him? Who?

We don't need to add this function to the std.

I think it's a useful addition. If people are going to write this helper function anyway, why not provide it directly?

JohnTitor added a commit to JohnTitor/rust that referenced this pull request Jan 14, 2021
Add a `std::io::read_to_string` function

I recognize that you're usually supposed to open an issue first, but the
implementation is very small so it's okay if this is closed and it was 'wasted
work' :)

-----

The equivalent of `std::fs::read_to_string`, but generalized to all
`Read` impls.

As the documentation on `std::io::read_to_string` says, the advantage of
this function is that it means you don't have to create a variable first
and it provides more type safety since you can only get the buffer out
if there were no errors. If you use `Read::read_to_string`, you have to
remember to check whether the read succeeded because otherwise your
buffer will be empty.

It's friendlier to newcomers and better in most cases to use an explicit
return value instead of an out parameter.
@danclive
Copy link
Contributor

@m-ou-se As you said, this function is useful sometimes. However, I was wondering what functions could be added to the standard library. These functions can also be added, right?

fn read_i32_le(reader: &mut impl Read) -> Result<i32> {
    let mut buf = [0; 4];
    reader.read_exact(&mut buf)?;
    Ok(i32::from_le_bytes(buf))
}

fn read_i64_le(reader: &mut impl Read) -> Result<i64> {
    let mut buf = [0; 8];
    reader.read_exact(&mut buf)?;
    Ok(i64::from_le_bytes(buf))
}

...

JohnTitor added a commit to JohnTitor/rust that referenced this pull request Jan 14, 2021
Add a `std::io::read_to_string` function

I recognize that you're usually supposed to open an issue first, but the
implementation is very small so it's okay if this is closed and it was 'wasted
work' :)

-----

The equivalent of `std::fs::read_to_string`, but generalized to all
`Read` impls.

As the documentation on `std::io::read_to_string` says, the advantage of
this function is that it means you don't have to create a variable first
and it provides more type safety since you can only get the buffer out
if there were no errors. If you use `Read::read_to_string`, you have to
remember to check whether the read succeeded because otherwise your
buffer will be empty.

It's friendlier to newcomers and better in most cases to use an explicit
return value instead of an out parameter.
@lnicola
Copy link
Member

lnicola commented Jan 14, 2021

This seems like a good roll-up candidate.

EDIT: sorry, never mind, I didn't notice the PRs doing exactly that.

JohnTitor added a commit to JohnTitor/rust that referenced this pull request Jan 14, 2021
Add a `std::io::read_to_string` function

I recognize that you're usually supposed to open an issue first, but the
implementation is very small so it's okay if this is closed and it was 'wasted
work' :)

-----

The equivalent of `std::fs::read_to_string`, but generalized to all
`Read` impls.

As the documentation on `std::io::read_to_string` says, the advantage of
this function is that it means you don't have to create a variable first
and it provides more type safety since you can only get the buffer out
if there were no errors. If you use `Read::read_to_string`, you have to
remember to check whether the read succeeded because otherwise your
buffer will be empty.

It's friendlier to newcomers and better in most cases to use an explicit
return value instead of an out parameter.
m-ou-se added a commit to m-ou-se/rust that referenced this pull request Jan 14, 2021
Add a `std::io::read_to_string` function

I recognize that you're usually supposed to open an issue first, but the
implementation is very small so it's okay if this is closed and it was 'wasted
work' :)

-----

The equivalent of `std::fs::read_to_string`, but generalized to all
`Read` impls.

As the documentation on `std::io::read_to_string` says, the advantage of
this function is that it means you don't have to create a variable first
and it provides more type safety since you can only get the buffer out
if there were no errors. If you use `Read::read_to_string`, you have to
remember to check whether the read succeeded because otherwise your
buffer will be empty.

It's friendlier to newcomers and better in most cases to use an explicit
return value instead of an out parameter.
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 14, 2021
Rollup of 17 pull requests

Successful merges:

 - rust-lang#79982 (Add missing methods to unix ExitStatusExt)
 - rust-lang#80017 (Suggest `_` and `..` if a pattern has too few fields)
 - rust-lang#80169 (Recommend panic::resume_unwind instead of panicking.)
 - rust-lang#80217 (Add a `std::io::read_to_string` function)
 - rust-lang#80444 (Add as_ref and as_mut methods for Bound)
 - rust-lang#80567 (Add Iterator::intersperse_with)
 - rust-lang#80829 (Get rid of `DepConstructor`)
 - rust-lang#80895 (Fix handling of malicious Readers in read_to_end)
 - rust-lang#80966 (Deprecate atomic::spin_loop_hint in favour of hint::spin_loop)
 - rust-lang#80969 (Use better ICE message when no MIR is available)
 - rust-lang#80972 (Remove unstable deprecated Vec::remove_item)
 - rust-lang#80973 (Update books)
 - rust-lang#80980 (Fixed incorrect doc comment)
 - rust-lang#80981 (Fix -Cpasses=list and llvm version print with -vV)
 - rust-lang#80985 (Fix stabilisation version of slice_strip)
 - rust-lang#80990 (llvm: Remove the unused context from CreateDebugLocation)
 - rust-lang#80991 (Fix formatting specifiers doc links)

Failed merges:

 - rust-lang#80944 (Use Option::map_or instead of `.map(..).unwrap_or(..)`)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 9fc298c into rust-lang:master Jan 14, 2021
@rustbot rustbot added this to the 1.51.0 milestone Jan 14, 2021
@camelid camelid deleted the io-read_to_string branch January 15, 2021 00:02
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` C-feature-request Category: A feature request, i.e: not implemented / a PR. 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.

9 participants