|
| 1 | +- Feature Name: read_to_string_without_buffer |
| 2 | +- Start Date: 2015-03-13 |
| 3 | +- RFC PR: |
| 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 | +Or this: |
| 36 | + |
| 37 | + |
| 38 | +```rust |
| 39 | +fn get_last_commit () -> String { |
| 40 | + |
| 41 | + Command::new("git") |
| 42 | + .arg("rev-parse") |
| 43 | + .arg("HEAD") |
| 44 | + .output() |
| 45 | + .map(|output| { |
| 46 | + String::from_utf8(output.stdout).ok().expect("error reading into string") |
| 47 | + }) |
| 48 | + .ok().expect("error invoking git rev-parse") |
| 49 | +}``` |
| 50 | + |
| 51 | +But we'd like to be able to just write |
| 52 | + |
| 53 | +Or this: |
| 54 | + |
| 55 | + |
| 56 | +```rust |
| 57 | +fn get_last_commit () -> String { |
| 58 | + |
| 59 | + Command::new("git") |
| 60 | + .arg("rev-parse") |
| 61 | + .arg("HEAD") |
| 62 | + .spawn() |
| 63 | + .ok().expect("error spawning process") |
| 64 | + .stdout.read_to_string() |
| 65 | + .ok().expect("error reading output") |
| 66 | +}``` |
| 67 | + |
| 68 | +This was possible before but since there is not such `read_to_string` API |
| 69 | +anymore, it's currently impossible. |
| 70 | + |
| 71 | + |
| 72 | +# Detailed design |
| 73 | + |
| 74 | +Add back methods with following signature |
| 75 | + |
| 76 | +`fn read_to_end(&mut self) -> Result<Vec<u8>>` |
| 77 | +`fn read_to_string(&mut self) -> Result<String>` |
| 78 | + |
| 79 | +# Drawbacks |
| 80 | + |
| 81 | +Two more methods to maintain |
| 82 | + |
| 83 | +# Alternatives |
| 84 | + |
| 85 | +Don't do it and force users to use things like `map` for chaining |
| 86 | + |
| 87 | +# Unresolved questions |
| 88 | + |
| 89 | +None. |
0 commit comments