Skip to content

Commit a17468d

Browse files
committed
add RFC 841 to add back bufferless read_to_string/read_to_end methods
1 parent dab3e4b commit a17468d

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
- Feature Name: read_to_string_without_buffer
2+
- Start Date: 2015-03-13
3+
- RFC PR: https://github.com/rust-lang/rfcs/pull/970
4+
- Rust Issue:
5+
6+
# Summary
7+
8+
Add back `read_to_string` and `read_to_end` methods to the `Read` trait that
9+
don't take a buffer.
10+
11+
# Motivation
12+
13+
While the `fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<()>` and
14+
`fn read_to_string(&mut self, buf: &mut String) -> Result<()>` APIs are more
15+
efficient removing the APIs that don't require passing a buffer entirely comes
16+
at with convenience loss in some situations. In particular if one want's to
17+
implement a chaining API and doesn't care about efficiency.
18+
19+
Today we either have to write this
20+
21+
```rust
22+
fn get_last_commit () -> String {
23+
24+
let output = Command::new("git")
25+
.arg("rev-parse")
26+
.arg("HEAD")
27+
.output()
28+
.ok().expect("error invoking git rev-parse");
29+
30+
let encoded = String::from_utf8(output.stdout).ok().expect("error parsing output of git rev-parse");
31+
32+
encoded
33+
}
34+
```
35+
36+
Or this:
37+
38+
39+
```rust
40+
fn get_last_commit () -> String {
41+
42+
Command::new("git")
43+
.arg("rev-parse")
44+
.arg("HEAD")
45+
.output()
46+
.map(|output| {
47+
String::from_utf8(output.stdout).ok().expect("error reading into string")
48+
})
49+
.ok().expect("error invoking git rev-parse")
50+
}
51+
```
52+
53+
But we'd like to be able to just write
54+
55+
Or this:
56+
57+
58+
```rust
59+
fn get_last_commit () -> String {
60+
61+
Command::new("git")
62+
.arg("rev-parse")
63+
.arg("HEAD")
64+
.spawn()
65+
.ok().expect("error spawning process")
66+
.stdout.read_to_string()
67+
.ok().expect("error reading output")
68+
}
69+
```
70+
71+
This was possible before but since there is not such `read_to_string` API
72+
anymore, it's currently impossible.
73+
74+
75+
# Detailed design
76+
77+
Add back methods with following signature
78+
79+
`fn read_to_end(&mut self) -> Result<Vec<u8>>`
80+
81+
`fn read_to_string(&mut self) -> Result<String>`
82+
83+
# Drawbacks
84+
85+
Two more methods to maintain
86+
87+
# Alternatives
88+
89+
Don't do it and force users to use things like `map` for chaining
90+
91+
# Unresolved questions
92+
93+
None.

0 commit comments

Comments
 (0)