| 1 | A `BufferedReader` is a super-powered `Read`er. |
| 2 | |
| 3 | Like the [`BufRead`] trait, the `BufferedReader` trait has an |
| 4 | internal buffer that is directly exposed to the user. This design |
| 5 | enables two performance optimizations. First, the use of an |
| 6 | internal buffer amortizes system calls. Second, exposing the |
| 7 | internal buffer allows the user to work with data in place, which |
| 8 | avoids another copy. |
| 9 | |
| 10 | The [`BufRead`] trait, however, has a significant limitation for |
| 11 | parsers: the user of a [`BufRead`] object can't control the amount |
| 12 | of buffering. This is essential for being able to conveniently |
| 13 | work with data in place, and being able to lookahead without |
| 14 | consuming data. The result is that either the sizing has to be |
| 15 | handled by the instantiator of the [`BufRead`] object---assuming |
| 16 | the [`BufRead`] object provides such a mechanism---which is a |
| 17 | layering violation, or the parser has to fallback to buffering if |
| 18 | the internal buffer is too small, which eliminates most of the |
| 19 | advantages of the [`BufRead`] abstraction. The `BufferedReader` |
| 20 | trait addresses this shortcoming by allowing the user to control |
| 21 | the size of the internal buffer. |
| 22 | |
| 23 | The `BufferedReader` trait also has some functionality, |
| 24 | specifically, a generic interface to work with a stack of |
| 25 | `BufferedReader` objects, that simplifies using multiple parsers |
| 26 | simultaneously. This is helpful when one parser deals with |
| 27 | framing (e.g., something like [HTTP's chunk transfer encoding]), |
| 28 | and another decodes the actual objects. It is also useful when |
| 29 | objects are nested. |
| 30 | |
| 31 | # Details |
| 32 | |
| 33 | Because the [`BufRead`] trait doesn't provide a mechanism for the |
| 34 | user to size the internal buffer, a parser can't generally be sure |
| 35 | that the internal buffer will be large enough to allow it to work |
| 36 | with all data in place. |
| 37 | |
| 38 | Using the standard [`BufRead`] implementation, [`BufReader`], the |
| 39 | instantiator can set the size of the internal buffer at creation |
| 40 | time. Unfortunately, this mechanism is ugly, and not always |
| 41 | adequate. First, the parser is typically not the instantiator. |
| 42 | Thus, the instantiator needs to know about the implementation |
| 43 | details of all of the parsers, which turns an implementation |
| 44 | detail into a cross-cutting concern. Second, when working with |
| 45 | dynamically sized data, the maximum amount of the data that needs |
| 46 | to be worked with in place may not be known apriori, or the |
| 47 | maximum amount may be significantly larger than the typical |
| 48 | amount. This leads to poorly sized buffers. |
| 49 | |
| 50 | Alternatively, the code that uses, but does not instantiate a |
| 51 | [`BufRead`] object, can be changed to stream the data, or to |
| 52 | fallback to reading the data into a local buffer if the internal |
| 53 | buffer is too small. Both of these approaches increase code |
| 54 | complexity, and the latter approach is contrary to the |
| 55 | [`BufRead`]'s goal of reducing unnecessary copying. |
| 56 | |
| 57 | The `BufferedReader` trait solves this problem by allowing the |
| 58 | user to dynamically (i.e., at read time, not open time) ensure |
| 59 | that the internal buffer has a certain amount of data. |
| 60 | |
| 61 | The ability to control the size of the internal buffer is also |
| 62 | essential to straightforward support for speculative lookahead. |
| 63 | The reason that speculative lookahead with a [`BufRead`] object is |
| 64 | difficult is that speculative lookahead is /speculative/, i.e., if |
| 65 | the parser backtracks, the data that was read must not be |
| 66 | consumed. Using a [`BufRead`] object, this is not possible if the |
| 67 | amount of lookahead is larger than the internal buffer. That is, |
| 68 | if the amount of lookahead data is larger than the [`BufRead`]'s |
| 69 | internal buffer, the parser first has to `BufRead::consume`() some |
| 70 | data to be able to examine more data. But, if the parser then |
| 71 | decides to backtrack, it has no way to return the unused data to |
| 72 | the [`BufRead`] object. This forces the parser to manage a buffer |
| 73 | of read, but unconsumed data, which significantly complicates the |
| 74 | code. |
| 75 | |
| 76 | The `BufferedReader` trait also simplifies working with a stack of |
| 77 | `BufferedReader`s in two ways. First, the `BufferedReader` trait |
| 78 | provides *generic* methods to access the underlying |
| 79 | `BufferedReader`. Thus, even when dealing with a trait object, it |
| 80 | is still possible to recover the underlying `BufferedReader`. |
| 81 | Second, the `BufferedReader` provides a mechanism to associate |
| 82 | generic state with each `BufferedReader` via a cookie. Although |
| 83 | it is possible to realize this functionality using a custom trait |
| 84 | that extends the `BufferedReader` trait and wraps existing |
| 85 | `BufferedReader` implementations, this approach eliminates a lot |
| 86 | of error-prone, boilerplate code. |
| 87 | |
| 88 | [`BufRead`]: https://doc.rust-lang.org/stable/std/io/trait.BufRead.html |
| 89 | [`BufReader`]: https://doc.rust-lang.org/stable/std/io/struct.BufReader.html |
| 90 | [HTTP's chunk transfer encoding]: https://en.wikipedia.org/wiki/Chunked_transfer_encoding |