| 1 | //! Types which can be used to tune the behavior of `BufReader` and `BufWriter`. |
| 2 | //! |
| 3 | //! Some simple policies are provided for your convenience. You may prefer to create your own |
| 4 | //! types and implement the traits for them instead. |
| 5 | |
| 6 | use superrust-analyzer cargo buffer-redux 1.1.0 crate/::Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer#; |
| 7 | |
| 8 | /// Flag for `ReaderPolicy` methods to signal whether or not `BufReader` should read into |
| 9 | /// the buffer. |
| 10 | /// |
| 11 | /// See `do_read!()` for a shorthand. |
| 12 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 13 | pub struct DoReadrust-analyzer cargo buffer-redux 1.1.0 policy/DoRead#(pub bool); |
| 14 | |
| 15 | /// Shorthand for `return DoRead(bool)` or `return DoRead(true)` (empty invocation) |
| 16 | #[macro_export] |
| 17 | macro_rules! do_readrust-analyzer cargo buffer-redux 1.1.0 policy/do_read! ( |
| 18 | ($val:expr) => ( return $crate::policy::DoRead($val) ); |
| 19 | () => ( do_read!(true); ) |
| 20 | ); |
| 21 | |
| 22 | /// Default policy for both `BufReader` and `BufWriter` that reproduces the behaviors of their |
| 23 | /// `std::io` counterparts: |
| 24 | /// |
| 25 | /// * `BufReader`: only reads when the buffer is empty, does not resize or move data. |
| 26 | /// * `BufWriter`: only flushes the buffer when there is not enough room for an incoming write. |
| 27 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)] |
| 28 | pub struct StdPolicyrust-analyzer cargo buffer-redux 1.1.0 policy/StdPolicy#; |
| 29 | |
| 30 | /// Trait that governs `BufReader`'s behavior. |
| 31 | pub trait ReaderPolicyrust-analyzer cargo buffer-redux 1.1.0 policy/ReaderPolicy# { |
| 32 | /// Consulted before attempting to read into the buffer. |
| 33 | /// |
| 34 | /// Return `DoRead(true)` to issue a read into the buffer before reading data out of it, |
| 35 | /// or `DoRead(false)` to read from the buffer as it is, even if it's empty. |
| 36 | /// `do_read!()` is provided as a shorthand. |
| 37 | /// |
| 38 | /// If there is no room in the buffer after this method is called, |
| 39 | /// the buffer will not be read into (so if the buffer is full but you want more data |
| 40 | /// you should call `.make_room()` or reserve more space). If there *is* room, `BufReader` will |
| 41 | /// attempt to read into the buffer. If successful (`Ok(x)` where `x > 0` is returned), this |
| 42 | /// method will be consulted again for another read attempt. |
| 43 | /// |
| 44 | /// By default, this implements `std::io::BufReader`'s behavior: only read into the buffer if |
| 45 | /// it is empty. |
| 46 | /// |
| 47 | /// ### Note |
| 48 | /// If the read will ignore the buffer entirely (if the buffer is empty and the amount to be |
| 49 | /// read matches or exceeds its capacity) or if `BufReader::read_into_buf()` was called to force |
| 50 | /// a read into the buffer manually, this method will not be called. |
| 51 | fn before_readrust-analyzer cargo buffer-redux 1.1.0 policy/ReaderPolicy#before_read().(&mut self, buffer: &mut Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer#) -> DoReadrust-analyzer cargo buffer-redux 1.1.0 policy/DoRead# { |
| 52 | DoReadrust-analyzer cargo buffer-redux 1.1.0 policy/DoRead#(buffer.is_emptyrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]is_empty().()) |
| 53 | } |
| 54 | |
| 55 | /// Called after bytes are consumed from the buffer. |
| 56 | /// |
| 57 | /// Supplies the true amount consumed if the amount passed to `BufReader::consume` |
| 58 | /// was in excess. |
| 59 | /// |
| 60 | /// This is a no-op by default. |
| 61 | fn after_consumerust-analyzer cargo buffer-redux 1.1.0 policy/ReaderPolicy#after_consume().(&mut self, _buffer: &mut Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer#, _amt: usize) {} |
| 62 | } |
| 63 | |
| 64 | /// Behavior of `std::io::BufReader`: the buffer will only be read into if it is empty. |
| 65 | impl ReaderPolicyrust-analyzer cargo buffer-redux 1.1.0 policy/ReaderPolicy# for StdPolicyrust-analyzer cargo buffer-redux 1.1.0 policy/StdPolicy# {} |
| 66 | |
| 67 | /// A policy for [`BufReader`](::BufReader) which ensures there is at least the given number of |
| 68 | /// bytes in the buffer, failing this only if the reader is at EOF. |
| 69 | /// |
| 70 | /// If the minimum buffer length is greater than the buffer capacity, it will be resized. |
| 71 | /// |
| 72 | /// ### Example |
| 73 | /// ```rust |
| 74 | /// use buffer_redux::BufReader; |
| 75 | /// use buffer_redux::policy::MinBuffered; |
| 76 | /// use std::io::{BufRead, Cursor}; |
| 77 | /// |
| 78 | /// let data = (1 .. 16).collect::<Vec<u8>>(); |
| 79 | /// |
| 80 | /// // normally you should use `BufReader::new()` or give a capacity of several KiB or more |
| 81 | /// let mut reader = BufReader::with_capacity(8, Cursor::new(data)) |
| 82 | /// // always at least 4 bytes in the buffer (or until the source is empty) |
| 83 | /// .set_policy(MinBuffered(4)); // always at least 4 bytes in the buffer |
| 84 | /// |
| 85 | /// // first buffer fill, same as `std::io::BufReader` |
| 86 | /// assert_eq!(reader.fill_buf().unwrap(), &[1, 2, 3, 4, 5, 6, 7, 8]); |
| 87 | /// reader.consume(3); |
| 88 | /// |
| 89 | /// // enough data in the buffer, another read isn't done yet |
| 90 | /// assert_eq!(reader.fill_buf().unwrap(), &[4, 5, 6, 7, 8]); |
| 91 | /// reader.consume(4); |
| 92 | /// |
| 93 | /// // `std::io::BufReader` would return `&[8]` |
| 94 | /// assert_eq!(reader.fill_buf().unwrap(), &[8, 9, 10, 11, 12, 13, 14, 15]); |
| 95 | /// reader.consume(5); |
| 96 | /// |
| 97 | /// // no data left in the reader |
| 98 | /// assert_eq!(reader.fill_buf().unwrap(), &[13, 14, 15]); |
| 99 | /// ``` |
| 100 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 101 | pub struct MinBufferedrust-analyzer cargo buffer-redux 1.1.0 policy/MinBuffered#(pub usize); |
| 102 | |
| 103 | impl MinBufferedrust-analyzer cargo buffer-redux 1.1.0 policy/MinBuffered# { |
| 104 | /// Set the number of bytes to ensure are in the buffer. |
| 105 | pub fn set_minrust-analyzer cargo buffer-redux 1.1.0 policy/impl#[MinBuffered]set_min().(&mut self, min: usize) { |
| 106 | self.0rust-analyzer cargo buffer-redux 1.1.0 policy/MinBuffered#0. = min; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | impl ReaderPolicyrust-analyzer cargo buffer-redux 1.1.0 policy/ReaderPolicy# for MinBufferedrust-analyzer cargo buffer-redux 1.1.0 policy/MinBuffered# { |
| 111 | fn before_readrust-analyzer cargo buffer-redux 1.1.0 policy/impl#[MinBuffered][ReaderPolicy]before_read().(&mut self, buffer: &mut Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer#) -> DoReadrust-analyzer cargo buffer-redux 1.1.0 policy/DoRead# { |
| 112 | // do nothing if we have enough data |
| 113 | if buffer.lenrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().self.0rust-analyzer cargo buffer-redux 1.1.0 policy/MinBuffered#0. { |
| 114 | do_readrust-analyzer cargo buffer-redux 1.1.0 policy/do_read!!(false) |
| 115 | } |
| 116 | |
| 117 | let cap = buffer.capacityrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]capacity().(); |
| 118 | |
| 119 | // if there's enough room but some of it's stuck after the head |
| 120 | if buffer.usable_spacerust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]usable_space().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().self.0rust-analyzer cargo buffer-redux 1.1.0 policy/MinBuffered#0. && buffer.free_spacerust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]free_space().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().self.0rust-analyzer cargo buffer-redux 1.1.0 policy/MinBuffered#0. { |
| 121 | buffer.make_roomrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]make_room().(); |
| 122 | } else if cap rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().self.0rust-analyzer cargo buffer-redux 1.1.0 policy/MinBuffered#0. { |
| 123 | buffer.reserverust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]reserve().(self.0rust-analyzer cargo buffer-redux 1.1.0 policy/MinBuffered#0. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().cap); |
| 124 | } |
| 125 | |
| 126 | DoReadrust-analyzer cargo buffer-redux 1.1.0 policy/DoRead#(true) |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /// Flag for `WriterPolicy` methods to tell `BufWriter` how many bytes to flush to the |
| 131 | /// underlying reader. |
| 132 | /// |
| 133 | /// See `flush_amt!()` for a shorthand. |
| 134 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 135 | pub struct FlushAmtrust-analyzer cargo buffer-redux 1.1.0 policy/FlushAmt#(pub usize); |
| 136 | |
| 137 | /// Shorthand for `return FlushAmt(n)` or `return FlushAmt(0)` (empty invocation) |
| 138 | #[macro_export] |
| 139 | macro_rules! flush_amtrust-analyzer cargo buffer-redux 1.1.0 policy/flush_amt! ( |
| 140 | ($n:expr) => ( return $crate::policy::FlushAmt($n); ); |
| 141 | () => ( flush_amt!(0); ) |
| 142 | ); |
| 143 | |
| 144 | /// A trait which tells `BufWriter` when to flush. |
| 145 | pub trait WriterPolicyrust-analyzer cargo buffer-redux 1.1.0 policy/WriterPolicy# { |
| 146 | /// Return `FlushAmt(n > 0)` if the buffer should be flushed before reading into it. |
| 147 | /// If the returned amount is 0 or greater than the amount of buffered data, no flush is |
| 148 | /// performed. |
| 149 | /// |
| 150 | /// The buffer is provided, as well as `incoming` which is |
| 151 | /// the size of the buffer that will be written to the `BufWriter`. |
| 152 | /// |
| 153 | /// By default, flushes the buffer if the usable space is smaller than the incoming write. |
| 154 | fn before_writerust-analyzer cargo buffer-redux 1.1.0 policy/WriterPolicy#before_write().(&mut self, buf: &mut Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer#, incoming: usize) -> FlushAmtrust-analyzer cargo buffer-redux 1.1.0 policy/FlushAmt# { |
| 155 | FlushAmtrust-analyzer cargo buffer-redux 1.1.0 policy/FlushAmt#(if incoming rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().buf.usable_spacerust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]usable_space().() { |
| 156 | buf.lenrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]len().() |
| 157 | } else { |
| 158 | 0 |
| 159 | }) |
| 160 | } |
| 161 | |
| 162 | /// Return `true` if the buffer should be flushed after reading into it. |
| 163 | /// |
| 164 | /// `buf` references the updated buffer after the read. |
| 165 | /// |
| 166 | /// Default impl is a no-op. |
| 167 | fn after_writerust-analyzer cargo buffer-redux 1.1.0 policy/WriterPolicy#after_write().(&mut self, _buf: &Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer#) -> FlushAmtrust-analyzer cargo buffer-redux 1.1.0 policy/FlushAmt# { |
| 168 | FlushAmtrust-analyzer cargo buffer-redux 1.1.0 policy/FlushAmt#(0) |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /// Default behavior of `std::io::BufWriter`: flush before a read into the buffer |
| 173 | /// only if the incoming data is larger than the buffer's writable space. |
| 174 | impl WriterPolicyrust-analyzer cargo buffer-redux 1.1.0 policy/WriterPolicy# for StdPolicyrust-analyzer cargo buffer-redux 1.1.0 policy/StdPolicy# {} |
| 175 | |
| 176 | /// Flush the buffer if it contains at least the given number of bytes. |
| 177 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)] |
| 178 | pub struct FlushAtLeastrust-analyzer cargo buffer-redux 1.1.0 policy/FlushAtLeast#(pub usize); |
| 179 | |
| 180 | impl WriterPolicyrust-analyzer cargo buffer-redux 1.1.0 policy/WriterPolicy# for FlushAtLeastrust-analyzer cargo buffer-redux 1.1.0 policy/FlushAtLeast# { |
| 181 | fn before_writerust-analyzer cargo buffer-redux 1.1.0 policy/impl#[FlushAtLeast][WriterPolicy]before_write().(&mut self, buf: &mut Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer#, incoming: usize) -> FlushAmtrust-analyzer cargo buffer-redux 1.1.0 policy/FlushAmt# { |
| 182 | ensure_capacityrust-analyzer cargo buffer-redux 1.1.0 policy/ensure_capacity().(buf, self.0rust-analyzer cargo buffer-redux 1.1.0 policy/FlushAtLeast#0.); |
| 183 | FlushAmtrust-analyzer cargo buffer-redux 1.1.0 policy/FlushAmt#(if incoming rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().buf.usable_spacerust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]usable_space().() { |
| 184 | buf.lenrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]len().() |
| 185 | } else { |
| 186 | 0 |
| 187 | }) |
| 188 | } |
| 189 | |
| 190 | fn after_writerust-analyzer cargo buffer-redux 1.1.0 policy/impl#[FlushAtLeast][WriterPolicy]after_write().(&mut self, buf: &Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer#) -> FlushAmtrust-analyzer cargo buffer-redux 1.1.0 policy/FlushAmt# { |
| 191 | FlushAmtrust-analyzer cargo buffer-redux 1.1.0 policy/FlushAmt#(::stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::maxrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/max().(buf.lenrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]len().(), self.0rust-analyzer cargo buffer-redux 1.1.0 policy/FlushAtLeast#0.)) |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /// Only ever flush exactly the given number of bytes, until the writer is empty. |
| 196 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)] |
| 197 | pub struct FlushExactrust-analyzer cargo buffer-redux 1.1.0 policy/FlushExact#(pub usize); |
| 198 | |
| 199 | impl WriterPolicyrust-analyzer cargo buffer-redux 1.1.0 policy/WriterPolicy# for FlushExactrust-analyzer cargo buffer-redux 1.1.0 policy/FlushExact# { |
| 200 | /// Flushes the buffer if there is not enough room to fit `incoming` bytes, |
| 201 | /// but only when the buffer contains at least `self.0` bytes. |
| 202 | /// |
| 203 | /// Otherwise, calls [`Buffer::make_room()`](::Buffer::make_room) |
| 204 | fn before_writerust-analyzer cargo buffer-redux 1.1.0 policy/impl#[FlushExact][WriterPolicy]before_write().(&mut self, buf: &mut Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer#, incoming: usize) -> FlushAmtrust-analyzer cargo buffer-redux 1.1.0 policy/FlushAmt# { |
| 205 | ensure_capacityrust-analyzer cargo buffer-redux 1.1.0 policy/ensure_capacity().(buf, self.0rust-analyzer cargo buffer-redux 1.1.0 policy/FlushExact#0.); |
| 206 | |
| 207 | // don't have enough room to fit the additional bytes but we can't flush, |
| 208 | // then make room for (at least some of) the incoming bytes. |
| 209 | if incoming rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().buf.usable_spacerust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]usable_space().() && buf.lenrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().self.0rust-analyzer cargo buffer-redux 1.1.0 policy/FlushExact#0. { |
| 210 | buf.make_roomrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]make_room().(); |
| 211 | } |
| 212 | |
| 213 | FlushAmtrust-analyzer cargo buffer-redux 1.1.0 policy/FlushAmt#(self.0rust-analyzer cargo buffer-redux 1.1.0 policy/FlushExact#0.) |
| 214 | } |
| 215 | |
| 216 | /// Flushes the given amount if possible, nothing otherwise. |
| 217 | fn after_writerust-analyzer cargo buffer-redux 1.1.0 policy/impl#[FlushExact][WriterPolicy]after_write().(&mut self, _buf: &Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer#) -> FlushAmtrust-analyzer cargo buffer-redux 1.1.0 policy/FlushAmt# { |
| 218 | FlushAmtrust-analyzer cargo buffer-redux 1.1.0 policy/FlushAmt#(self.0rust-analyzer cargo buffer-redux 1.1.0 policy/FlushExact#0.) |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | /// Flush the buffer if it contains the given byte. |
| 223 | /// |
| 224 | /// Only scans the buffer after reading. Searches from the end first. |
| 225 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)] |
| 226 | pub struct FlushOnrust-analyzer cargo buffer-redux 1.1.0 policy/FlushOn#(pub u8); |
| 227 | |
| 228 | impl WriterPolicyrust-analyzer cargo buffer-redux 1.1.0 policy/WriterPolicy# for FlushOnrust-analyzer cargo buffer-redux 1.1.0 policy/FlushOn# { |
| 229 | fn after_writerust-analyzer cargo buffer-redux 1.1.0 policy/impl#[FlushOn][WriterPolicy]after_write().(&mut self, buf: &Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer#) -> FlushAmtrust-analyzer cargo buffer-redux 1.1.0 policy/FlushAmt# { |
| 230 | // include the delimiter in the flush |
| 231 | FlushAmtrust-analyzer cargo buffer-redux 1.1.0 policy/FlushAmt#(memchrrust-analyzer cargo memchr 2.7.6 crate/::memrchrrust-analyzer cargo memchr 2.7.6 memchr/memrchr().(self.0rust-analyzer cargo buffer-redux 1.1.0 policy/FlushOn#0., buf.bufrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]buf().()).map_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map_or().(0, |n| n rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1)) |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | /// Flush the buffer if it contains a newline (`\n`). |
| 236 | /// |
| 237 | /// Equivalent to `FlushOn(b'\n')`. |
| 238 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)] |
| 239 | pub struct FlushOnNewlinerust-analyzer cargo buffer-redux 1.1.0 policy/FlushOnNewline#; |
| 240 | |
| 241 | impl WriterPolicyrust-analyzer cargo buffer-redux 1.1.0 policy/WriterPolicy# for FlushOnNewlinerust-analyzer cargo buffer-redux 1.1.0 policy/FlushOnNewline# { |
| 242 | fn after_writerust-analyzer cargo buffer-redux 1.1.0 policy/impl#[FlushOnNewline][WriterPolicy]after_write().(&mut self, buf: &Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer#) -> FlushAmtrust-analyzer cargo buffer-redux 1.1.0 policy/FlushAmt# { |
| 243 | FlushAmtrust-analyzer cargo buffer-redux 1.1.0 policy/FlushAmt#(memchrrust-analyzer cargo memchr 2.7.6 crate/::memrchrrust-analyzer cargo memchr 2.7.6 memchr/memrchr().(b'\n', buf.bufrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]buf().()).map_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map_or().(0, |n| n rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1)) |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | fn ensure_capacityrust-analyzer cargo buffer-redux 1.1.0 policy/ensure_capacity().(buf: &mut Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer#, min_cap: usize) { |
| 248 | let cap = buf.capacityrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]capacity().(); |
| 249 | |
| 250 | if cap rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().min_cap { |
| 251 | buf.reserverust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]reserve().(min_cap rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().cap); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | #[cfg(test)] |
| 256 | mod testrust-analyzer cargo buffer-redux 1.1.0 policy/test/ { |
| 257 | use craterust-analyzer cargo buffer-redux 1.1.0 crate/::policyrust-analyzer cargo buffer-redux 1.1.0 policy/::*; |
| 258 | use craterust-analyzer cargo buffer-redux 1.1.0 crate/::{BufReaderrust-analyzer cargo buffer-redux 1.1.0 BufReader#, BufWriterrust-analyzer cargo buffer-redux 1.1.0 BufWriter#}; |
| 259 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{BufReadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/BufRead#, Cursorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/cursor/Cursor#, Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#}; |
| 260 | |
| 261 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 262 | fn test_min_bufferedrust-analyzer cargo buffer-redux 1.1.0 policy/test/test_min_buffered().() { |
| 263 | let min_buffered = 4; |
| 264 | let data = (0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#20).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>>(); |
| 265 | // create a reader with 0 capacity |
| 266 | let mut reader = |
| 267 | BufReaderrust-analyzer cargo buffer-redux 1.1.0 BufReader#::with_capacityrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R>`]with_capacity().(0, Cursorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/cursor/Cursor#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/cursor/impl#[`Cursor<T>`]new().(data)).set_policyrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`]set_policy().(MinBufferedrust-analyzer cargo buffer-redux 1.1.0 policy/MinBuffered#(min_buffered)); |
| 268 | |
| 269 | // policy reserves the required space in the buffer |
| 270 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(reader.fill_bufrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`][BufRead]fill_buf().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), &[0, 1, 2, 3][rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()...]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().); |
| 271 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(reader.capacityrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`]capacity().(), min_buffered); |
| 272 | |
| 273 | // double the size now that the buffer's full |
| 274 | reader.reserverust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`]reserve().(min_buffered); |
| 275 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(reader.capacityrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`]capacity().(), min_buffered *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). 2); |
| 276 | |
| 277 | // we haven't consumed anything, the reader should have the same data |
| 278 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(reader.fill_bufrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`][BufRead]fill_buf().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), &[0, 1, 2, 3]); |
| 279 | reader.consumerust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`][BufRead]consume().(2); |
| 280 | // policy read more data, `std::io::BufReader` doesn't do that |
| 281 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(reader.fill_bufrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`][BufRead]fill_buf().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), &[2, 3, 4, 5, 6, 7]); |
| 282 | reader.consumerust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`][BufRead]consume().(4); |
| 283 | // policy made room and read more |
| 284 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(reader.fill_bufrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`][BufRead]fill_buf().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), &[6, 7, 8, 9, 10, 11, 12, 13]); |
| 285 | reader.consumerust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`][BufRead]consume().(4); |
| 286 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(reader.fill_bufrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`][BufRead]fill_buf().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), &[10, 11, 12, 13]); |
| 287 | reader.consumerust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`][BufRead]consume().(2); |
| 288 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 289 | reader.fill_bufrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`][BufRead]fill_buf().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), |
| 290 | &[12, 13, 14, 15, 16, 17, 18, 19] |
| 291 | ); |
| 292 | reader.consumerust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`][BufRead]consume().(8); |
| 293 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(reader.fill_bufrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`][BufRead]fill_buf().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), &[]) |
| 294 | } |
| 295 | |
| 296 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 297 | fn test_flush_at_leastrust-analyzer cargo buffer-redux 1.1.0 policy/test/test_flush_at_least().() { |
| 298 | let flush_min = 4; |
| 299 | |
| 300 | let mut writer = BufWriterrust-analyzer cargo buffer-redux 1.1.0 BufWriter#::with_capacityrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W>`]with_capacity().(0, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![]).set_policyrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]set_policy().(FlushAtLeastrust-analyzer cargo buffer-redux 1.1.0 policy/FlushAtLeast#(flush_min)); |
| 301 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(writer.capacityrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]capacity().(), 0); |
| 302 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(writer.writerust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`][Write]write().(&[1]).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), 1); |
| 303 | // policy reserved space for writing |
| 304 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(writer.capacityrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]capacity().(), flush_min); |
| 305 | // one byte in buffer, we want to double our capacity |
| 306 | writer.reserverust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]reserve().(flush_min rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().2 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().1); |
| 307 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(writer.capacityrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]capacity().(), flush_min *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). 2); |
| 308 | |
| 309 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(writer.writerust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`][Write]write().(&[2, 3]).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), 2); |
| 310 | // no flush yet, only 3 bytes in buffer |
| 311 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().writer.get_refrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]get_ref().(), &[]); |
| 312 | |
| 313 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(writer.writerust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`][Write]write().(&[4, 5, 6]).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), 3); |
| 314 | // flushed all |
| 315 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().writer.get_refrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]get_ref().(), &[1, 2, 3, 4, 5, 6]); |
| 316 | |
| 317 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(writer.writerust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`][Write]write().(&[7, 8, 9]).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), 3); |
| 318 | // `.into_inner()` should flush always |
| 319 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(writer.into_innerrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]into_inner().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), &[1, 2, 3, 4, 5, 6, 7, 8, 9]); |
| 320 | } |
| 321 | |
| 322 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 323 | fn test_flush_exactrust-analyzer cargo buffer-redux 1.1.0 policy/test/test_flush_exact().() { |
| 324 | let flush_exact = 4; |
| 325 | |
| 326 | let mut writer = BufWriterrust-analyzer cargo buffer-redux 1.1.0 BufWriter#::with_capacityrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W>`]with_capacity().(0, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![]).set_policyrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]set_policy().(FlushExactrust-analyzer cargo buffer-redux 1.1.0 policy/FlushExact#(flush_exact)); |
| 327 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(writer.capacityrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]capacity().(), 0); |
| 328 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(writer.writerust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`][Write]write().(&[1]).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), 1); |
| 329 | // policy reserved space for writing |
| 330 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(writer.capacityrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]capacity().(), flush_exact); |
| 331 | // one byte in buffer, we want to double our capacity |
| 332 | writer.reserverust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]reserve().(flush_exact rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().2 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().1); |
| 333 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(writer.capacityrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]capacity().(), flush_exact *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). 2); |
| 334 | |
| 335 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(writer.writerust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`][Write]write().(&[2, 3]).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), 2); |
| 336 | // no flush yet, only 3 bytes in buffer |
| 337 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().writer.get_refrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]get_ref().(), &[]); |
| 338 | |
| 339 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(writer.writerust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`][Write]write().(&[4, 5, 6]).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), 3); |
| 340 | // flushed exactly 4 bytes |
| 341 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().writer.get_refrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]get_ref().(), &[1, 2, 3, 4]); |
| 342 | |
| 343 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(writer.writerust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`][Write]write().(&[7, 8, 9, 10]).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), 4); |
| 344 | // flushed another 4 bytes |
| 345 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().writer.get_refrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]get_ref().(), &[1, 2, 3, 4, 5, 6, 7, 8]); |
| 346 | // `.into_inner()` should flush always |
| 347 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 348 | writer.into_innerrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]into_inner().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), |
| 349 | &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
| 350 | ); |
| 351 | } |
| 352 | |
| 353 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 354 | fn test_flush_onrust-analyzer cargo buffer-redux 1.1.0 policy/test/test_flush_on().() { |
| 355 | let mut writer = BufWriterrust-analyzer cargo buffer-redux 1.1.0 BufWriter#::with_capacityrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W>`]with_capacity().(8, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![]).set_policyrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]set_policy().(FlushOnrust-analyzer cargo buffer-redux 1.1.0 policy/FlushOn#(0)); |
| 356 | |
| 357 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(writer.writerust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`][Write]write().(&[1, 2, 3]).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), 3); |
| 358 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().writer.get_refrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]get_ref().(), &[]); |
| 359 | |
| 360 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(writer.writerust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`][Write]write().(&[0, 4, 5]).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), 3); |
| 361 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().writer.get_refrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]get_ref().(), &[1, 2, 3, 0]); |
| 362 | |
| 363 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(writer.writerust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`][Write]write().(&[6, 7, 8, 9, 10, 11, 12]).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), 7); |
| 364 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().writer.get_refrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]get_ref().(), &[1, 2, 3, 0, 4, 5]); |
| 365 | |
| 366 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(writer.writerust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`][Write]write().(&[0]).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), 1); |
| 367 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 368 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().writer.get_refrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]get_ref().(), |
| 369 | &[1, 2, 3, 0, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0] |
| 370 | ); |
| 371 | } |
| 372 | |
| 373 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 374 | fn test_flush_on_newlinerust-analyzer cargo buffer-redux 1.1.0 policy/test/test_flush_on_newline().() { |
| 375 | let mut writer = BufWriterrust-analyzer cargo buffer-redux 1.1.0 BufWriter#::with_capacityrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W>`]with_capacity().(8, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![]).set_policyrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]set_policy().(FlushOnNewlinerust-analyzer cargo buffer-redux 1.1.0 policy/FlushOnNewline#); |
| 376 | |
| 377 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(writer.writerust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`][Write]write().(&[1, 2, 3]).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), 3); |
| 378 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().writer.get_refrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]get_ref().(), &[]); |
| 379 | |
| 380 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(writer.writerust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`][Write]write().(&[b'\n', 4, 5]).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), 3); |
| 381 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().writer.get_refrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]get_ref().(), &[1, 2, 3, b'\n']); |
| 382 | |
| 383 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(writer.writerust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`][Write]write().(&[6, 7, 8, 9, b'\n', 11, 12]).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), 7); |
| 384 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 385 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().writer.get_refrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]get_ref().(), |
| 386 | &[1, 2, 3, b'\n', 4, 5, 6, 7, 8, 9, b'\n'] |
| 387 | ); |
| 388 | |
| 389 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(writer.writerust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`][Write]write().(&[b'\n']).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), 1); |
| 390 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 391 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().writer.get_refrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]get_ref().(), |
| 392 | &[1, 2, 3, b'\n', 4, 5, 6, 7, 8, 9, b'\n', 11, 12, b'\n'] |
| 393 | ); |
| 394 | } |
| 395 | } |