| 1 | # buffer-re(a)dux |
| 2 | |
| 3 | [](https://crates.io/crates/buffer-redux) |
| 4 | [](https://crates.io/crates/buffer-redux) |
| 5 | [](https://crates.io/crates/buffer-redux) |
| 6 | |
| 7 | > Fork of the unmaintained [`buf_redux`](https://github.com/abonander/buf_redux). |
| 8 | |
| 9 | Drop-in replacements for buffered I/O types in `std::io`. |
| 10 | |
| 11 | These replacements retain the method names/signatures and implemented traits of their stdlib |
| 12 | counterparts, making replacement as simple as swapping the import of the type. |
| 13 | |
| 14 | ### More Direct Control |
| 15 | |
| 16 | All replacement types provide methods to: |
| 17 | * Increase the capacity of the buffer |
| 18 | * Get the number of available bytes as well as the total capacity of the buffer |
| 19 | * Consume the wrapper without losing data |
| 20 | |
| 21 | `BufReader` provides methods to: |
| 22 | * Access the buffer through an `&`-reference without performing I/O |
| 23 | * Force unconditional reads into the buffer |
| 24 | * Get a `Read` adapter which empties the buffer and then pulls from the inner reader directly |
| 25 | * Shuffle bytes down to the beginning of the buffer to make room for more reading |
| 26 | * Get inner reader and trimmed buffer with the remaining data |
| 27 | |
| 28 | `BufWriter` and `LineWriter` provide methods to: |
| 29 | * Flush the buffer and unwrap the inner writer unconditionally. |
| 30 | |
| 31 | ### More Sensible and Customizable Buffering Behavior |
| 32 | Tune the behavior of the buffer to your specific use-case using the types in the |
| 33 | `policy` module: |
| 34 | |
| 35 | * Refine `BufReader`'s behavior by implementing the `ReaderPolicy` trait or use |
| 36 | an existing implementation like `MinBuffered` to ensure the buffer always contains |
| 37 | a minimum number of bytes (until the underlying reader is empty). |
| 38 | |
| 39 | * Refine `BufWriter`'s behavior by implementing the `WriterPolicy` trait |
| 40 | or use an existing implementation like `FlushOn` to flush when a particular byte |
| 41 | appears in the buffer (used to implement `LineWriter`). |
| 42 | |
| 43 | |
| 44 | ## Usage |
| 45 | |
| 46 | #### [Documentation](http://docs.rs/buffer-redux/) |
| 47 | |
| 48 | `Cargo.toml`: |
| 49 | ```toml |
| 50 | [dependencies] |
| 51 | buffer-redux = "0.2" |
| 52 | ``` |
| 53 | And then simply swap the import of the types you want to replace: |
| 54 | |
| 55 | #### `BufReader`: |
| 56 | ``` |
| 57 | - use std::io::BufReader; |
| 58 | + use buffer_redux::BufReader; |
| 59 | ``` |
| 60 | #### `BufWriter`: |
| 61 | ``` |
| 62 | - use std::io::BufWriter; |
| 63 | + use buffer_redux::BufWriter; |
| 64 | ``` |
| 65 | |
| 66 | #### `LineWriter`: |
| 67 | ``` |
| 68 | - use std::io::LineWriter; |
| 69 | + use buffer_redux::LineWriter; |
| 70 | ``` |
| 71 | |
| 72 | ### Using `MinBuffered` |
| 73 | The new `policy::MinBuffered` reader-policy can be used to ensure that `BufReader` always has at least a |
| 74 | certain number of bytes in its buffer. This can be useful for parsing applications that require a |
| 75 | certain amount of lookahead. |
| 76 | |
| 77 | ```rust |
| 78 | use buffer_redux::BufReader; |
| 79 | use buffer_redux::policy::MinBuffered; |
| 80 | use std::io::{BufRead, Cursor}; |
| 81 | |
| 82 | let data = (1 .. 16).collect::<Vec<u8>>(); |
| 83 | |
| 84 | // normally you should use `BufReader::new()` or give a capacity of several KiB or more |
| 85 | let mut reader = BufReader::with_capacity(8, Cursor::new(data)) |
| 86 | // always at least 4 bytes in the buffer (or until the source is empty) |
| 87 | .set_policy(MinBuffered(4)); // always at least 4 bytes in the buffer |
| 88 | |
| 89 | // first buffer fill, same as `std::io::BufReader` |
| 90 | assert_eq!(reader.fill_buf().unwrap(), &[1, 2, 3, 4, 5, 6, 7, 8]); |
| 91 | reader.consume(3); |
| 92 | |
| 93 | // enough data in the buffer, another read isn't done yet |
| 94 | assert_eq!(reader.fill_buf().unwrap(), &[4, 5, 6, 7, 8]); |
| 95 | reader.consume(4); |
| 96 | |
| 97 | // `std::io::BufReader` would return `&[8]` |
| 98 | assert_eq!(reader.fill_buf().unwrap(), &[8, 9, 10, 11, 12, 13, 14, 15]); |
| 99 | reader.consume(5); |
| 100 | |
| 101 | // no data left in the reader |
| 102 | assert_eq!(reader.fill_buf().unwrap(), &[13, 14, 15]); |
| 103 | ``` |
| 104 | |
| 105 | ### Note: Making Room / Ringbuffers / `slice-deque` Feature |
| 106 | With policies like `MinBuffered`, that will read into the buffer and consume bytes from it without completely |
| 107 | emptying it, normal buffer handling can run out of room to read/write into as all the free space is at the |
| 108 | head of the buffer. If the amount of data in the buffer is small, you can call `.make_room()` on the buffered |
| 109 | type to make more room for reading. `MinBuffered` will do this automatically. |
| 110 | |
| 111 | Instead of this, with the `slice-deque` feature, you can instead have your buffered type allocate a *ringbuffer*, |
| 112 | simply by using the `::new_ringbuf()` or `::with_capacity_ringbuf()` constructors instead of |
| 113 | `::new()` or `with_capacity()`, respectively. With a ringbuffer, consuming/flushing bytes |
| 114 | from a buffer instantly makes room for more reading/writing at the end. |
| 115 | However, this has some caveats: |
| 116 | |
| 117 | * It is only available on target platforms with virtual memory support, namely fully fledged |
| 118 | OSes such as Windows and Unix-derivative platforms like Linux, OS X, BSD variants, etc. |
| 119 | |
| 120 | * The default capacity varies based on platform, and custom capacities are rounded up to a |
| 121 | multiple of their minimum size, typically the page size of the platform. |
| 122 | Windows' minimum size is comparably quite large (**64 KiB**) due to some legacy reasons, |
| 123 | so this may be less optimal than the default capacity for a normal buffer (8 KiB) for some |
| 124 | use-cases. |
| 125 | |
| 126 | * Due to the nature of the virtual-memory trick, the virtual address space the buffer |
| 127 | allocates will be double its capacity. This means that your program will *appear* to use more |
| 128 | memory than it would if it was using a normal buffer of the same capacity. The physical memory |
| 129 | usage will be the same in both cases, but if address space is at a premium in your application |
| 130 | (32-bit targets) then this may be a concern. |
| 131 | |
| 132 | It is up to you to decide if the benefits outweigh the costs. With a policy like `MinBuffered`, |
| 133 | it could significantly improve performance. |
| 134 | |
| 135 | ## License |
| 136 | |
| 137 | Licensed under either of |
| 138 | |
| 139 | * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) |
| 140 | * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) |
| 141 | |
| 142 | at your option. |
| 143 | |
| 144 | ### Contribution |
| 145 | |
| 146 | Unless you explicitly state otherwise, any contribution intentionally submitted |
| 147 | for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any |
| 148 | additional terms or conditions. |