| 1 | // Original implementation Copyright 2013 The Rust Project Developers <https://github.com/rust-lang> |
| 2 | // Original source file: https://github.com/rust-lang/rust/blob/master/src/libstd/io/buffered.P |
| 3 | // Additions copyright 2016-2018 Austin Bonander <[email protected]> |
| 4 | |
| 5 | //! Drop-in replacements for buffered I/O types in `std::io`. |
| 6 | //! |
| 7 | //! These replacements retain the method names/signatures and implemented traits of their stdlib |
| 8 | //! counterparts, making replacement as simple as swapping the import of the type: |
| 9 | //! |
| 10 | //! #### `BufReader`: |
| 11 | //! ```notest |
| 12 | //! - use std::io::BufReader; |
| 13 | //! + use buffer_redux::BufReader; |
| 14 | //! ``` |
| 15 | //! #### `BufWriter`: |
| 16 | //! ```notest |
| 17 | //! - use std::io::BufWriter; |
| 18 | //! + use buffer_redux::BufWriter; |
| 19 | //! ``` |
| 20 | //! #### `LineWriter`: |
| 21 | //! ```notest |
| 22 | //! - use std::io::LineWriter; |
| 23 | //! + use buffer_redux::LineWriter; |
| 24 | //! ``` |
| 25 | //! |
| 26 | //! ### More Direct Control |
| 27 | //! All replacement types provide methods to: |
| 28 | //! |
| 29 | //! * Increase the capacity of the buffer |
| 30 | //! * Get the number of available bytes as well as the total capacity of the buffer |
| 31 | //! * Consume the wrapper without losing data |
| 32 | //! |
| 33 | //! `BufReader` provides methods to: |
| 34 | //! |
| 35 | //! * Access the buffer through an `&`-reference without performing I/O |
| 36 | //! * Force unconditional reads into the buffer |
| 37 | //! * Get a `Read` adapter which empties the buffer and then pulls from the inner reader directly |
| 38 | //! * Shuffle bytes down to the beginning of the buffer to make room for more reading |
| 39 | //! * Get inner reader and trimmed buffer with the remaining data |
| 40 | //! |
| 41 | //! `BufWriter` and `LineWriter` provides methods to: |
| 42 | //! |
| 43 | //! * Flush the buffer and unwrap the inner writer unconditionally. |
| 44 | //! * Get the inner writer and trimmed buffer with the unflushed data. |
| 45 | //! |
| 46 | //! ### More Sensible and Customizable Buffering Behavior |
| 47 | //! Tune the behavior of the buffer to your specific use-case using the types in the |
| 48 | //! [`policy` module]: |
| 49 | //! |
| 50 | //! * Refine `BufReader`'s behavior by implementing the [`ReaderPolicy` trait] or use |
| 51 | //! an existing implementation like [`MinBuffered`] to ensure the buffer always contains |
| 52 | //! a minimum number of bytes (until the underlying reader is empty). |
| 53 | //! |
| 54 | //! * Refine `BufWriter`'s behavior by implementing the [`WriterPolicy` trait] |
| 55 | //! or use an existing implementation like [`FlushOn`] to flush when a particular byte |
| 56 | //! appears in the buffer (used to implement [`LineWriter`]). |
| 57 | //! |
| 58 | //! [`policy` module]: policy |
| 59 | //! [`ReaderPolicy` trait]: policy::ReaderPolicy |
| 60 | //! [`MinBuffered`]: policy::MinBuffered |
| 61 | //! [`WriterPolicy`]: policy::WriterPolicy |
| 62 | //! [`FlushOn`]: policy::FlushOn |
| 63 | //! [`LineWriter`]: LineWriter |
| 64 | //! |
| 65 | //! ### Making Room |
| 66 | //! The buffered types of this crate and their `std::io` counterparts, by default, use `Box<[u8]>` |
| 67 | //! as their buffer types ([`Buffer`](Buffer) is included as well since it is used internally |
| 68 | //! by the other types in this crate). |
| 69 | //! |
| 70 | //! When one of these types inserts bytes into its buffer, via `BufRead::fill_buf()` (implicitly |
| 71 | //! called by `Read::read()`) in `BufReader`'s case or `Write::write()` in `BufWriter`'s case, |
| 72 | //! the entire buffer is provided to be read/written into and the number of bytes written is saved. |
| 73 | //! The read/written data then resides in the `[0 .. bytes_inserted]` slice of the buffer. |
| 74 | //! |
| 75 | //! When bytes are consumed from the buffer, via `BufRead::consume()` or `Write::flush()`, |
| 76 | //! the number of bytes consumed is added to the start of the slice such that the remaining |
| 77 | //! data resides in the `[bytes_consumed .. bytes_inserted]` slice of the buffer. |
| 78 | //! |
| 79 | //! The `std::io` buffered types, and their counterparts in this crate with their default policies, |
| 80 | //! don't have to deal with partially filled buffers as `BufReader` only reads when empty and |
| 81 | //! `BufWriter` only flushes when full. |
| 82 | //! |
| 83 | //! However, because the replacements in this crate are capable of reading on-demand and flushing |
| 84 | //! less than a full buffer, they can run out of room in their buffers to read/write data into even |
| 85 | //! though there is technically free space, because this free space is at the head of the buffer |
| 86 | //! where reading into it would cause the data in the buffer to become non-contiguous. |
| 87 | //! |
| 88 | //! This isn't technically a problem as the buffer could operate like `VecDeque` in `std` and return |
| 89 | //! both slices at once, but this would not fit all use-cases: the `Read::fill_buf()` interface only |
| 90 | //! allows one slice to be returned at a time so the older data would need to be completely consumed |
| 91 | //! before the newer data can be returned; `BufWriter` could support it as the `Write` interface |
| 92 | //! doesn't make an opinion on how the buffer works, but because the data would be non-contiguous |
| 93 | //! it would require two flushes to get it all, which could degrade performance. |
| 94 | //! |
| 95 | //! The obvious solution, then, is to move the existing data down to the beginning of the buffer |
| 96 | //! when there is no more room at the end so that more reads/writes into the buffer can be issued. |
| 97 | //! This works, and may suit some use-cases where the amount of data left is small and thus copying |
| 98 | //! it would be inexpensive, but it is non-optimal. However, this option is provided |
| 99 | //! as the `.make_room()` methods, and is utilized by [`policy::MinBuffered`](policy::MinBuffered) |
| 100 | //! and [`policy::FlushExact`](policy::FlushExact). |
| 101 | //! |
| 102 | //! ### Ringbuffers / `slice-deque` Feature |
| 103 | //! Instead of moving data, however, it is also possible to use virtual-memory tricks to |
| 104 | //! allocate a ringbuffer that loops around on itself in memory and thus is always contiguous, |
| 105 | //! as described in [the Wikipedia article on Ringbuffers][ringbuf-wikipedia]. |
| 106 | //! |
| 107 | //! This is the exact trick used by [the `slice-deque` crate](https://crates.io/crates/slice-deque), |
| 108 | //! which is now provided as an optional feature `slice-deque` exposed via the |
| 109 | //! `new_ringbuf()` and `with_capacity_ringbuf()` constructors added to the buffered types here. |
| 110 | //! When a buffered type is constructed using one of these functions, `.make_room()` is turned into |
| 111 | //! a no-op as consuming bytes from the head of the buffer simultaneously makes room at the tail. |
| 112 | //! However, this has some caveats: |
| 113 | //! |
| 114 | //! * It is only available on target platforms with virtual memory support, namely fully fledged |
| 115 | //! OSes such as Windows and Unix-derivative platforms like Linux, OS X, BSD variants, etc. |
| 116 | //! |
| 117 | //! * The default capacity varies based on platform, and custom capacities are rounded up to a |
| 118 | //! multiple of their minimum size, typically the page size of the platform. |
| 119 | //! Windows' minimum size is comparably quite large (**64 KiB**) due to some legacy reasons, |
| 120 | //! so this may be less optimal than the default capacity for a normal buffer (8 KiB) for some |
| 121 | //! use-cases. |
| 122 | //! |
| 123 | //! * Due to the nature of the virtual-memory trick, the virtual address space the buffer |
| 124 | //! allocates will be double its capacity. This means that your program will *appear* to use more |
| 125 | //! memory than it would if it was using a normal buffer of the same capacity. The physical memory |
| 126 | //! usage will be the same in both cases, but if address space is at a premium in your application |
| 127 | //! (32-bit targets) then this may be a concern. |
| 128 | //! |
| 129 | //! [ringbuf-wikipedia]: https://en.wikipedia.org/wiki/Circular_buffer#Optimization |
| 130 | #![warn(missing_docs)] |
| 131 | |
| 132 | // std::io's tests require exact allocation which slice_deque cannot provide |
| 133 | mod bufferrust-analyzer cargo buffer-redux 1.1.0 buffer/; |
| 134 | #[cfg(all(test, feature = "slice-deque"))] |
| 135 | mod ringbuf_tests; |
| 136 | #[cfg(test)] |
| 137 | mod std_testsrust-analyzer cargo buffer-redux 1.1.0 std_tests/; |
| 138 | |
| 139 | pub mod policyrust-analyzer cargo buffer-redux 1.1.0 policy/; |
| 140 | |
| 141 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::anyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core any/::Anyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core any/Any#; |
| 142 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/::RefCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/RefCell#; |
| 143 | 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/::preluderust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/prelude/::*; |
| 144 | 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/::SeekFromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#; |
| 145 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::ManuallyDroprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/ManuallyDrop#; |
| 146 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#; |
| 147 | use 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/, errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/, fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/, iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/, ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/}; |
| 148 | |
| 149 | use selfrust-analyzer cargo buffer-redux 1.1.0 crate/::policyrust-analyzer cargo buffer-redux 1.1.0 policy/::{FlushOnNewlinerust-analyzer cargo buffer-redux 1.1.0 policy/FlushOnNewline#, ReaderPolicyrust-analyzer cargo buffer-redux 1.1.0 policy/ReaderPolicy#, StdPolicyrust-analyzer cargo buffer-redux 1.1.0 policy/StdPolicy#, WriterPolicyrust-analyzer cargo buffer-redux 1.1.0 policy/WriterPolicy#}; |
| 150 | use craterust-analyzer cargo buffer-redux 1.1.0 crate/::bufferrust-analyzer cargo buffer-redux 1.1.0 buffer/::BufImplrust-analyzer cargo buffer-redux 1.1.0 buffer/BufImpl#; |
| 151 | |
| 152 | const DEFAULT_BUF_SIZErust-analyzer cargo buffer-redux 1.1.0 DEFAULT_BUF_SIZE.: usize = 8 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().1024; |
| 153 | |
| 154 | /// A drop-in replacement for `std::io::BufReader` with more functionality. |
| 155 | /// |
| 156 | /// Original method names/signatures and implemented traits are left untouched, |
| 157 | /// making replacement as simple as swapping the import of the type. |
| 158 | /// |
| 159 | /// By default this type implements the behavior of its `std` counterpart: it only reads into |
| 160 | /// the buffer when it is empty. |
| 161 | /// |
| 162 | /// To change this type's behavior, change the policy with [`.set_policy()`] using a type |
| 163 | /// from the [`policy` module] or your own implementation of [`ReaderPolicy`]. |
| 164 | /// |
| 165 | /// Policies that perform alternating reads and consumes without completely emptying the buffer |
| 166 | /// may benefit from using a ringbuffer via the [`new_ringbuf()`] and [`with_capacity_ringbuf()`] |
| 167 | /// constructors. Ringbuffers are only available on supported platforms with the |
| 168 | /// `slice-deque` feature and have some other caveats; see [the crate root docs][ringbufs-root] |
| 169 | /// for more details. |
| 170 | /// |
| 171 | /// [`.set_policy()`]: BufReader::set_policy |
| 172 | /// [`policy` module]: policy |
| 173 | /// [`ReaderPolicy`]: policy::ReaderPolicy |
| 174 | /// [`new_ringbuf()`]: BufReader::new_ringbuf |
| 175 | /// [`with_capacity_ringbuf()`]: BufReader::with_capacity_ringbuf |
| 176 | /// [ringbufs-root]: index.html#ringbuffers--slice-deque-feature |
| 177 | pub struct BufReaderrust-analyzer cargo buffer-redux 1.1.0 BufReader#<R, P = StdPolicyrust-analyzer cargo buffer-redux 1.1.0 policy/StdPolicy#> { |
| 178 | // First field for null pointer optimization. |
| 179 | bufrust-analyzer cargo buffer-redux 1.1.0 BufReader#buf.: Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer#, |
| 180 | innerrust-analyzer cargo buffer-redux 1.1.0 BufReader#inner.: R, |
| 181 | policyrust-analyzer cargo buffer-redux 1.1.0 BufReader#policy.: P, |
| 182 | } |
| 183 | |
| 184 | impl<R> BufReaderrust-analyzer cargo buffer-redux 1.1.0 BufReader#<R, StdPolicyrust-analyzer cargo buffer-redux 1.1.0 policy/StdPolicy#> { |
| 185 | /// Create a new `BufReader` wrapping `inner`, utilizing a buffer of |
| 186 | /// default capacity and the default [`ReaderPolicy`](policy::ReaderPolicy). |
| 187 | pub fn newrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R>`]new().(inner: R) -> Selfrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R>`] { |
| 188 | Selfrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R>`]::with_capacityrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R>`]with_capacity().(DEFAULT_BUF_SIZErust-analyzer cargo buffer-redux 1.1.0 DEFAULT_BUF_SIZE., inner) |
| 189 | } |
| 190 | |
| 191 | /// Create a new `BufReader` wrapping `inner`, utilizing a buffer with a capacity |
| 192 | /// of *at least* `cap` bytes and the default [`ReaderPolicy`](policy::ReaderPolicy). |
| 193 | /// |
| 194 | /// The actual capacity of the buffer may vary based on implementation details of the global |
| 195 | /// allocator. |
| 196 | pub fn with_capacityrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R>`]with_capacity().(cap: usize, inner: R) -> Selfrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R>`] { |
| 197 | Selfrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R>`]::with_bufferrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R>`]with_buffer().(Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer#::with_capacityrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]with_capacity().(cap), inner) |
| 198 | } |
| 199 | |
| 200 | /// Create a new `BufReader` wrapping `inner`, utilizing a ringbuffer with the default capacity |
| 201 | /// and `ReaderPolicy`. |
| 202 | /// |
| 203 | /// A ringbuffer never has to move data to make room; consuming bytes from the head |
| 204 | /// simultaneously makes room at the tail. This is useful in conjunction with a policy like |
| 205 | /// [`MinBuffered`](policy::MinBuffered) to ensure there is always room to read more data |
| 206 | /// if necessary, without expensive copying operations. |
| 207 | /// |
| 208 | /// Only available on platforms with virtual memory support and with the `slice-deque` feature |
| 209 | /// enabled. The default capacity will differ between Windows and Unix-derivative targets. |
| 210 | /// See [`Buffer::new_ringbuf()`](struct.Buffer.html#method.new_ringbuf) |
| 211 | /// or [the crate root docs](index.html#ringbuffers--slice-deque-feature) for more info. |
| 212 | #[cfg(feature = "slice-deque")] |
| 213 | pub fn new_ringbuf(inner: R) -> Selfrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R>`] { |
| 214 | Selfrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R>`]::with_capacity_ringbuf(DEFAULT_BUF_SIZErust-analyzer cargo buffer-redux 1.1.0 DEFAULT_BUF_SIZE., inner) |
| 215 | } |
| 216 | |
| 217 | /// Create a new `BufReader` wrapping `inner`, utilizing a ringbuffer with *at least* the given |
| 218 | /// capacity and the default `ReaderPolicy`. |
| 219 | /// |
| 220 | /// A ringbuffer never has to move data to make room; consuming bytes from the head |
| 221 | /// simultaneously makes room at the tail. This is useful in conjunction with a policy like |
| 222 | /// [`MinBuffered`](policy::MinBuffered) to ensure there is always room to read more data |
| 223 | /// if necessary, without expensive copying operations. |
| 224 | /// |
| 225 | /// Only available on platforms with virtual memory support and with the `slice-deque` feature |
| 226 | /// enabled. The capacity will be rounded up to the minimum size for the target platform. |
| 227 | /// See [`Buffer::with_capacity_ringbuf()`](struct.Buffer.html#method.with_capacity_ringbuf) |
| 228 | /// or [the crate root docs](index.html#ringbuffers--slice-deque-feature) for more info. |
| 229 | #[cfg(feature = "slice-deque")] |
| 230 | pub fn with_capacity_ringbuf(cap: usize, inner: R) -> Selfrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R>`] { |
| 231 | Selfrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R>`]::with_buffer(Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer#::with_capacity_ringbuf(cap), inner) |
| 232 | } |
| 233 | |
| 234 | /// Wrap `inner` with an existing `Buffer` instance and the default `ReaderPolicy`. |
| 235 | /// |
| 236 | /// ### Note |
| 237 | /// Does **not** clear the buffer first! If there is data already in the buffer |
| 238 | /// then it will be returned in `read()` and `fill_buf()` ahead of any data from `inner`. |
| 239 | pub fn with_bufferrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R>`]with_buffer().(buf: Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer#, inner: R) -> Selfrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R>`] { |
| 240 | BufReaderrust-analyzer cargo buffer-redux 1.1.0 BufReader# { |
| 241 | bufrust-analyzer cargo buffer-redux 1.1.0 BufReader#buf., |
| 242 | innerrust-analyzer cargo buffer-redux 1.1.0 BufReader#inner., |
| 243 | policyrust-analyzer cargo buffer-redux 1.1.0 BufReader#policy.: StdPolicyrust-analyzer cargo buffer-redux 1.1.0 policy/StdPolicy#, |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | impl<R, P> BufReaderrust-analyzer cargo buffer-redux 1.1.0 BufReader#<R, P> { |
| 249 | /// Apply a new `ReaderPolicy` to this `BufReader`, returning the transformed type. |
| 250 | pub fn set_policyrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`]set_policy().<P_: ReaderPolicyrust-analyzer cargo buffer-redux 1.1.0 policy/ReaderPolicy#>(self, policy: P_) -> BufReaderrust-analyzer cargo buffer-redux 1.1.0 BufReader#<R, P_> { |
| 251 | BufReaderrust-analyzer cargo buffer-redux 1.1.0 BufReader# { |
| 252 | innerrust-analyzer cargo buffer-redux 1.1.0 BufReader#inner.: self.innerrust-analyzer cargo buffer-redux 1.1.0 BufReader#inner., |
| 253 | bufrust-analyzer cargo buffer-redux 1.1.0 BufReader#buf.: self.bufrust-analyzer cargo buffer-redux 1.1.0 BufReader#buf., |
| 254 | policyrust-analyzer cargo buffer-redux 1.1.0 BufReader#policy., |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | /// Mutate the current [`ReaderPolicy`](policy::ReaderPolicy) in-place. |
| 259 | /// |
| 260 | /// If you want to change the type, use `.set_policy()`. |
| 261 | pub fn policy_mutrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`]policy_mut().(&mut self) -> &mut P { |
| 262 | &mut self.policyrust-analyzer cargo buffer-redux 1.1.0 BufReader#policy. |
| 263 | } |
| 264 | |
| 265 | /// Inspect the current `ReaderPolicy`. |
| 266 | pub fn policyrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`]policy().(&self) -> &P { |
| 267 | &self.policyrust-analyzer cargo buffer-redux 1.1.0 BufReader#policy. |
| 268 | } |
| 269 | |
| 270 | /// Move data to the start of the buffer, making room at the end for more |
| 271 | /// reading. |
| 272 | /// |
| 273 | /// This is a no-op with the `*_ringbuf()` constructors (requires `slice-deque` feature). |
| 274 | pub fn make_roomrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`]make_room().(&mut self) { |
| 275 | self.bufrust-analyzer cargo buffer-redux 1.1.0 BufReader#buf..make_roomrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]make_room().(); |
| 276 | } |
| 277 | |
| 278 | /// Ensure room in the buffer for *at least* `additional` bytes. May not be |
| 279 | /// quite exact due to implementation details of the buffer's allocator. |
| 280 | pub fn reserverust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`]reserve().(&mut self, additional: usize) { |
| 281 | self.bufrust-analyzer cargo buffer-redux 1.1.0 BufReader#buf..reserverust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]reserve().(additional); |
| 282 | } |
| 283 | |
| 284 | // RFC: pub fn shrink(&mut self, new_len: usize) ? |
| 285 | |
| 286 | /// Get the section of the buffer containing valid data; may be empty. |
| 287 | /// |
| 288 | /// Call `.consume()` to remove bytes from the beginning of this section. |
| 289 | #[inline] |
| 290 | pub fn bufferrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`]buffer().(&self) -> &[u8] { |
| 291 | self.bufrust-analyzer cargo buffer-redux 1.1.0 BufReader#buf..bufrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]buf().() |
| 292 | } |
| 293 | |
| 294 | /// Get the current number of bytes available in the buffer. |
| 295 | pub fn buf_lenrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`]buf_len().(&self) -> usize { |
| 296 | self.bufrust-analyzer cargo buffer-redux 1.1.0 BufReader#buf..lenrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]len().() |
| 297 | } |
| 298 | |
| 299 | /// Get the total buffer capacity. |
| 300 | pub fn capacityrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`]capacity().(&self) -> usize { |
| 301 | self.bufrust-analyzer cargo buffer-redux 1.1.0 BufReader#buf..capacityrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]capacity().() |
| 302 | } |
| 303 | |
| 304 | /// Get an immutable reference to the underlying reader. |
| 305 | pub fn get_refrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`]get_ref().(&self) -> &R { |
| 306 | &self.innerrust-analyzer cargo buffer-redux 1.1.0 BufReader#inner. |
| 307 | } |
| 308 | |
| 309 | /// Get a mutable reference to the underlying reader. |
| 310 | /// |
| 311 | /// ## Note |
| 312 | /// Reading directly from the underlying reader is not recommended, as some |
| 313 | /// data has likely already been moved into the buffer. |
| 314 | pub fn get_mutrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`]get_mut().(&mut self) -> &mut R { |
| 315 | &mut self.innerrust-analyzer cargo buffer-redux 1.1.0 BufReader#inner. |
| 316 | } |
| 317 | |
| 318 | /// Consume `self` and return the inner reader only. |
| 319 | pub fn into_innerrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`]into_inner().(self) -> R { |
| 320 | self.innerrust-analyzer cargo buffer-redux 1.1.0 BufReader#inner. |
| 321 | } |
| 322 | |
| 323 | /// Consume `self` and return both the underlying reader and the buffer. |
| 324 | /// |
| 325 | /// See also: `BufReader::unbuffer()` |
| 326 | pub fn into_inner_with_bufferrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`]into_inner_with_buffer().(self) -> (R, Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer#) { |
| 327 | (self.innerrust-analyzer cargo buffer-redux 1.1.0 BufReader#inner., self.bufrust-analyzer cargo buffer-redux 1.1.0 BufReader#buf.) |
| 328 | } |
| 329 | |
| 330 | /// Consume `self` and return an adapter which implements `Read` and will |
| 331 | /// empty the buffer before reading directly from the underlying reader. |
| 332 | pub fn unbufferrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`]unbuffer().(self) -> Unbufferrust-analyzer cargo buffer-redux 1.1.0 Unbuffer#<R> { |
| 333 | Unbufferrust-analyzer cargo buffer-redux 1.1.0 Unbuffer# { |
| 334 | innerrust-analyzer cargo buffer-redux 1.1.0 Unbuffer#inner.: self.innerrust-analyzer cargo buffer-redux 1.1.0 BufReader#inner., |
| 335 | bufrust-analyzer cargo buffer-redux 1.1.0 Unbuffer#buf.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(self.bufrust-analyzer cargo buffer-redux 1.1.0 BufReader#buf.), |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | impl<R, P: ReaderPolicyrust-analyzer cargo buffer-redux 1.1.0 policy/ReaderPolicy#> BufReaderrust-analyzer cargo buffer-redux 1.1.0 BufReader#<R, P> { |
| 341 | #[inline] |
| 342 | fn should_readrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`]should_read().(&mut self) -> bool { |
| 343 | self.policyrust-analyzer cargo buffer-redux 1.1.0 BufReader#policy..before_readrust-analyzer cargo buffer-redux 1.1.0 policy/ReaderPolicy#before_read().(&mut self.bufrust-analyzer cargo buffer-redux 1.1.0 BufReader#buf.).0rust-analyzer cargo buffer-redux 1.1.0 policy/DoRead#0. |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | impl<R: Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#, P> BufReaderrust-analyzer cargo buffer-redux 1.1.0 BufReader#<R, P> { |
| 348 | /// Unconditionally perform a read into the buffer. |
| 349 | /// |
| 350 | /// Does not invoke `ReaderPolicy` methods. |
| 351 | /// |
| 352 | /// If the read was successful, returns the number of bytes read. |
| 353 | pub fn read_into_bufrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`]read_into_buf().(&mut self) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<usize> { |
| 354 | self.bufrust-analyzer cargo buffer-redux 1.1.0 BufReader#buf..read_fromrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]read_from().(&mut self.innerrust-analyzer cargo buffer-redux 1.1.0 BufReader#inner.) |
| 355 | } |
| 356 | |
| 357 | /// Box the inner reader without losing data. |
| 358 | pub fn boxedrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`]boxed().<'a>(self) -> BufReaderrust-analyzer cargo buffer-redux 1.1.0 BufReader#<Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<dyn Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read# + 'a>, P> |
| 359 | where |
| 360 | R: 'a, |
| 361 | { |
| 362 | let inner: Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<dyn Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read# + 'a> = Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(self.innerrust-analyzer cargo buffer-redux 1.1.0 BufReader#inner.); |
| 363 | |
| 364 | BufReaderrust-analyzer cargo buffer-redux 1.1.0 BufReader# { |
| 365 | innerrust-analyzer cargo buffer-redux 1.1.0 BufReader#inner., |
| 366 | bufrust-analyzer cargo buffer-redux 1.1.0 BufReader#buf.: self.bufrust-analyzer cargo buffer-redux 1.1.0 BufReader#buf., |
| 367 | policyrust-analyzer cargo buffer-redux 1.1.0 BufReader#policy.: self.policyrust-analyzer cargo buffer-redux 1.1.0 BufReader#policy., |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | impl<R: Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#, P: ReaderPolicyrust-analyzer cargo buffer-redux 1.1.0 policy/ReaderPolicy#> Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read# for BufReaderrust-analyzer cargo buffer-redux 1.1.0 BufReader#<R, P> { |
| 373 | fn readrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`][Read]read().(&mut self, out: &mut [u8]) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<usize> { |
| 374 | // If we don't have any buffered data and we're doing a read matching |
| 375 | // or exceeding the internal buffer's capacity, bypass the buffer. |
| 376 | if self.bufrust-analyzer cargo buffer-redux 1.1.0 BufReader#buf..is_emptyrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]is_empty().() && out.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]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.bufrust-analyzer cargo buffer-redux 1.1.0 BufReader#buf..capacityrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]capacity().() { |
| 377 | return self.innerrust-analyzer cargo buffer-redux 1.1.0 BufReader#inner..readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read().(out); |
| 378 | } |
| 379 | |
| 380 | let nread = self.fill_bufrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`][BufRead]fill_buf().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/impls/impl#[`&[u8]`][Read]read().(out)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 381 | self.consumerust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`][BufRead]consume().(nread); |
| 382 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(nread) |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | impl<R: Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#, P: ReaderPolicyrust-analyzer cargo buffer-redux 1.1.0 policy/ReaderPolicy#> BufReadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/BufRead# for BufReaderrust-analyzer cargo buffer-redux 1.1.0 BufReader#<R, P> { |
| 387 | fn fill_bufrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`][BufRead]fill_buf().(&mut self) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<&[u8]> { |
| 388 | // If we've reached the end of our internal buffer then we need to fetch |
| 389 | // some more data from the underlying reader. |
| 390 | // This execution order is important; the policy may want to resize the buffer or move data |
| 391 | // before reading into it. |
| 392 | while self.should_readrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`]should_read().() && self.bufrust-analyzer cargo buffer-redux 1.1.0 BufReader#buf..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>`]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().0 { |
| 393 | if self.read_into_bufrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`]read_into_buf().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().0 { |
| 394 | break; |
| 395 | }; |
| 396 | } |
| 397 | |
| 398 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(self.bufferrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`]buffer().()) |
| 399 | } |
| 400 | |
| 401 | fn consumerust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`][BufRead]consume().(&mut self, mut amt: usize) { |
| 402 | amt = cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/min().(amt, self.buf_lenrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`]buf_len().()); |
| 403 | self.bufrust-analyzer cargo buffer-redux 1.1.0 BufReader#buf..consumerust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]consume().(amt); |
| 404 | self.policyrust-analyzer cargo buffer-redux 1.1.0 BufReader#policy..after_consumerust-analyzer cargo buffer-redux 1.1.0 policy/ReaderPolicy#after_consume().(&mut self.bufrust-analyzer cargo buffer-redux 1.1.0 BufReader#buf., amt); |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | impl<R: fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug#, P: fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug#> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# for BufReaderrust-analyzer cargo buffer-redux 1.1.0 BufReader#<R, P> { |
| 409 | fn fmtrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`][Debug]fmt().(&self, fmt: &mut fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 410 | fmt.debug_structrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]debug_struct().("buffer_redux::BufReader") |
| 411 | .fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]field().("reader", &self.innerrust-analyzer cargo buffer-redux 1.1.0 BufReader#inner.) |
| 412 | .fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]field().("buf_len", &self.buf_lenrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`]buf_len().()) |
| 413 | .fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]field().("capacity", &self.capacityrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`]capacity().()) |
| 414 | .fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]field().("policy", &self.policyrust-analyzer cargo buffer-redux 1.1.0 BufReader#policy.) |
| 415 | .finishrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]finish().() |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | impl<R: Seekrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Seek#, P: ReaderPolicyrust-analyzer cargo buffer-redux 1.1.0 policy/ReaderPolicy#> Seekrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Seek# for BufReaderrust-analyzer cargo buffer-redux 1.1.0 BufReader#<R, P> { |
| 420 | /// Seek to an ofPet, in bytes, in the underlying reader. |
| 421 | /// |
| 422 | /// The position used for seeking with `SeekFrom::Current(_)` is the |
| 423 | /// position the underlying reader would be at if the `BufReader` had no |
| 424 | /// internal buffer. |
| 425 | /// |
| 426 | /// Seeking always discards the internal buffer, even if the seek position |
| 427 | /// would otherwise fall within it. This guarantees that calling |
| 428 | /// `.unwrap()` immediately after a seek yields the underlying reader at |
| 429 | /// the same position. |
| 430 | /// |
| 431 | /// See `std::io::Seek` for more details. |
| 432 | /// |
| 433 | /// Note: In the edge case where you're seeking with `SeekFrom::Current(n)` |
| 434 | /// where `n` minus the internal buffer length underflows an `i64`, two |
| 435 | /// seeks will be performed instead of one. If the second seek returns |
| 436 | /// `Err`, the underlying reader will be left at the same position it would |
| 437 | /// have if you seeked to `SeekFrom::Current(0)`. |
| 438 | fn seekrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`][Seek]seek().(&mut self, pos: SeekFromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<u64> { |
| 439 | let result: u64; |
| 440 | if let SeekFromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#::Currentrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#Current#(n) = pos { |
| 441 | let remainder = self.buf_lenrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufReader<R, P>`]buf_len().() as i64; |
| 442 | // it should be safe to assume that remainder fits within an i64 as the alternative |
| 443 | // means we managed to allocate 8 ebibytes and that's absurd. |
| 444 | // But it's not out of the realm of possibility for some weird underlying reader to |
| 445 | // support seeking by i64::min_value() so we need to handle underflow when subtracting |
| 446 | // remainder. |
| 447 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(offset) = n.checked_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i64]checked_sub().(remainder) { |
| 448 | result = self.innerrust-analyzer cargo buffer-redux 1.1.0 BufReader#inner..seekrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Seek#seek().(SeekFromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#::Currentrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#Current#(offset))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 449 | } else { |
| 450 | // seek backwards by our remainder, and then by the offset |
| 451 | self.innerrust-analyzer cargo buffer-redux 1.1.0 BufReader#inner..seekrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Seek#seek().(SeekFromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#::Currentrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#Current#(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i64][Neg]neg().remainder))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 452 | self.bufrust-analyzer cargo buffer-redux 1.1.0 BufReader#buf..clearrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]clear().(); // empty the buffer |
| 453 | result = self.innerrust-analyzer cargo buffer-redux 1.1.0 BufReader#inner..seekrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Seek#seek().(SeekFromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#::Currentrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#Current#(n))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 454 | } |
| 455 | } else { |
| 456 | // Seeking with Start/End doesn't care about our buffer length. |
| 457 | result = self.innerrust-analyzer cargo buffer-redux 1.1.0 BufReader#inner..seekrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Seek#seek().(pos)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 458 | } |
| 459 | self.bufrust-analyzer cargo buffer-redux 1.1.0 BufReader#buf..clearrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]clear().(); |
| 460 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(result) |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | /// A drop-in replacement for `std::io::BufWriter` with more functionality. |
| 465 | /// |
| 466 | /// Original method names/signatures and implemented traits are left untouched, |
| 467 | /// making replacement as simple as swapping the import of the type. |
| 468 | /// |
| 469 | /// By default this type implements the behavior of its `std` counterpart: it only flushes |
| 470 | /// the buffer if an incoming write is larger than the remaining space. |
| 471 | /// |
| 472 | /// To change this type's behavior, change the policy with [`.set_policy()`] using a type |
| 473 | /// from the [`policy` module] or your own implentation of [`WriterPolicy`]. |
| 474 | /// |
| 475 | /// Policies that perform alternating writes and flushes without completely emptying the buffer |
| 476 | /// may benefit from using a ringbuffer via the [`new_ringbuf()`] and [`with_capacity_ringbuf()`] |
| 477 | /// constructors. Ringbuffers are only available on supported platforms with the |
| 478 | /// `slice-deque` feature and have some caveats; see [the docs at the crate root][ringbufs-root] |
| 479 | /// for more details. |
| 480 | /// |
| 481 | /// [`.set_policy()`]: BufWriter::set_policy |
| 482 | /// [`policy` module]: policy |
| 483 | /// [`WriterPolicy`]: policy::WriterPolicy |
| 484 | /// [`new_ringbuf()`]: BufWriter::new_ringbuf |
| 485 | /// [`with_capacity_ringbuf()`]: BufWriter::with_capacity_ringbuf |
| 486 | /// [ringbufs-root]: index.html#ringbuffers--slice-deque-feature |
| 487 | pub struct BufWriterrust-analyzer cargo buffer-redux 1.1.0 BufWriter#<W: Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#, P = StdPolicyrust-analyzer cargo buffer-redux 1.1.0 policy/StdPolicy#> { |
| 488 | bufrust-analyzer cargo buffer-redux 1.1.0 BufWriter#buf.: Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer#, |
| 489 | innerrust-analyzer cargo buffer-redux 1.1.0 BufWriter#inner.: W, |
| 490 | policyrust-analyzer cargo buffer-redux 1.1.0 BufWriter#policy.: P, |
| 491 | panickedrust-analyzer cargo buffer-redux 1.1.0 BufWriter#panicked.: bool, |
| 492 | } |
| 493 | |
| 494 | impl<W: Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#> BufWriterrust-analyzer cargo buffer-redux 1.1.0 BufWriter#<W> { |
| 495 | /// Create a new `BufWriter` wrapping `inner` with the default buffer capacity and |
| 496 | /// [`WriterPolicy`](policy::WriterPolicy). |
| 497 | pub fn newrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W>`]new().(inner: W) -> Selfrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W>`] { |
| 498 | Selfrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W>`]::with_bufferrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W>`]with_buffer().(Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer#::newrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]new().(), inner) |
| 499 | } |
| 500 | |
| 501 | /// Create a new `BufWriter` wrapping `inner`, utilizing a buffer with a capacity |
| 502 | /// of *at least* `cap` bytes and the default [`WriterPolicy`](policy::WriterPolicy). |
| 503 | /// |
| 504 | /// The actual capacity of the buffer may vary based on implementation details of the global |
| 505 | /// allocator. |
| 506 | pub fn with_capacityrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W>`]with_capacity().(cap: usize, inner: W) -> Selfrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W>`] { |
| 507 | Selfrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W>`]::with_bufferrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W>`]with_buffer().(Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer#::with_capacityrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]with_capacity().(cap), inner) |
| 508 | } |
| 509 | |
| 510 | /// Create a new `BufWriter` wrapping `inner`, utilizing a ringbuffer with the default |
| 511 | /// capacity and [`WriterPolicy`](policy::WriterPolicy). |
| 512 | /// |
| 513 | /// A ringbuffer never has to move data to make room; consuming bytes from the head |
| 514 | /// simultaneously makes room at the tail. This is useful in conjunction with a policy like |
| 515 | /// [`FlushExact`](policy::FlushExact) to ensure there is always room to write more data if |
| 516 | /// necessary, without expensive copying operations. |
| 517 | /// |
| 518 | /// Only available on platforms with virtual memory support and with the `slice-deque` feature |
| 519 | /// enabled. The default capacity will differ between Windows and Unix-derivative targets. |
| 520 | /// See [`Buffer::new_ringbuf()`](Buffer::new_ringbuf) |
| 521 | /// or [the crate root docs](index.html#ringbuffers--slice-deque-feature) for more info. |
| 522 | #[cfg(feature = "slice-deque")] |
| 523 | pub fn new_ringbuf(inner: W) -> Selfrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W>`] { |
| 524 | Selfrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W>`]::with_buffer(Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer#::new_ringbuf(), inner) |
| 525 | } |
| 526 | |
| 527 | /// Create a new `BufWriter` wrapping `inner`, utilizing a ringbuffer with *at least* `cap` |
| 528 | /// capacity and the default [`WriterPolicy`](policy::WriterPolicy). |
| 529 | /// |
| 530 | /// A ringbuffer never has to move data to make room; consuming bytes from the head |
| 531 | /// simultaneously makes room at the tail. This is useful in conjunction with a policy like |
| 532 | /// [`FlushExact`](policy::FlushExact) to ensure there is always room to write more data if |
| 533 | /// necessary, without expensive copying operations. |
| 534 | /// |
| 535 | /// Only available on platforms with virtual memory support and with the `slice-deque` feature |
| 536 | /// enabled. The capacity will be rounded up to the minimum size for the target platform. |
| 537 | /// See [`Buffer::with_capacity_ringbuf()`](Buffer::with_capacity_ringbuf) |
| 538 | /// or [the crate root docs](index.html#ringbuffers--slice-deque-feature) for more info. |
| 539 | #[cfg(feature = "slice-deque")] |
| 540 | pub fn with_capacity_ringbuf(cap: usize, inner: W) -> Selfrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W>`] { |
| 541 | Selfrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W>`]::with_buffer(Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer#::with_capacity_ringbuf(cap), inner) |
| 542 | } |
| 543 | |
| 544 | /// Create a new `BufWriter` wrapping `inner`, utilizing the existing [`Buffer`](Buffer) |
| 545 | /// instance and the default [`WriterPolicy`](policy::WriterPolicy). |
| 546 | /// |
| 547 | /// ### Note |
| 548 | /// Does **not** clear the buffer first! If there is data already in the buffer |
| 549 | /// it will be written out on the next flush! |
| 550 | pub fn with_bufferrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W>`]with_buffer().(buf: Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer#, inner: W) -> BufWriterrust-analyzer cargo buffer-redux 1.1.0 BufWriter#<W> { |
| 551 | BufWriterrust-analyzer cargo buffer-redux 1.1.0 BufWriter# { |
| 552 | bufrust-analyzer cargo buffer-redux 1.1.0 BufWriter#buf., |
| 553 | innerrust-analyzer cargo buffer-redux 1.1.0 BufWriter#inner., |
| 554 | policyrust-analyzer cargo buffer-redux 1.1.0 BufWriter#policy.: StdPolicyrust-analyzer cargo buffer-redux 1.1.0 policy/StdPolicy#, |
| 555 | panickedrust-analyzer cargo buffer-redux 1.1.0 BufWriter#panicked.: false, |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | impl<W: Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#, P> BufWriterrust-analyzer cargo buffer-redux 1.1.0 BufWriter#<W, P> { |
| 561 | /// Set a new [`WriterPolicy`](policy::WriterPolicy), returning the transformed type. |
| 562 | pub fn set_policyrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]set_policy().<P_: WriterPolicyrust-analyzer cargo buffer-redux 1.1.0 policy/WriterPolicy#>(self, policy: P_) -> BufWriterrust-analyzer cargo buffer-redux 1.1.0 BufWriter#<W, P_> { |
| 563 | let panicked = self.panickedrust-analyzer cargo buffer-redux 1.1.0 BufWriter#panicked.; |
| 564 | let (inner, buf) = self.into_inner_rust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]into_inner_().(); |
| 565 | |
| 566 | BufWriterrust-analyzer cargo buffer-redux 1.1.0 BufWriter# { |
| 567 | innerrust-analyzer cargo buffer-redux 1.1.0 BufWriter#inner., |
| 568 | bufrust-analyzer cargo buffer-redux 1.1.0 BufWriter#buf., |
| 569 | policyrust-analyzer cargo buffer-redux 1.1.0 BufWriter#policy., |
| 570 | panickedrust-analyzer cargo buffer-redux 1.1.0 BufWriter#panicked., |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | /// Mutate the current [`WriterPolicy`](policy::WriterPolicy). |
| 575 | pub fn policy_mutrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]policy_mut().(&mut self) -> &mut P { |
| 576 | &mut self.policyrust-analyzer cargo buffer-redux 1.1.0 BufWriter#policy. |
| 577 | } |
| 578 | |
| 579 | /// Inspect the current `WriterPolicy`. |
| 580 | pub fn policyrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]policy().(&self) -> &P { |
| 581 | &self.policyrust-analyzer cargo buffer-redux 1.1.0 BufWriter#policy. |
| 582 | } |
| 583 | |
| 584 | /// Get a reference to the inner writer. |
| 585 | pub fn get_refrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]get_ref().(&self) -> &W { |
| 586 | &self.innerrust-analyzer cargo buffer-redux 1.1.0 BufWriter#inner. |
| 587 | } |
| 588 | |
| 589 | /// Get a mutable reference to the inner writer. |
| 590 | /// |
| 591 | /// ### Note |
| 592 | /// If the buffer has not been flushed, writing directly to the inner type will cause |
| 593 | /// data inconsistency. |
| 594 | pub fn get_mutrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]get_mut().(&mut self) -> &mut W { |
| 595 | &mut self.innerrust-analyzer cargo buffer-redux 1.1.0 BufWriter#inner. |
| 596 | } |
| 597 | |
| 598 | /// Get the capacty of the inner buffer. |
| 599 | pub fn capacityrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]capacity().(&self) -> usize { |
| 600 | self.bufrust-analyzer cargo buffer-redux 1.1.0 BufWriter#buf..capacityrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]capacity().() |
| 601 | } |
| 602 | |
| 603 | /// Get the number of bytes currently in the buffer. |
| 604 | pub fn buf_lenrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]buf_len().(&self) -> usize { |
| 605 | self.bufrust-analyzer cargo buffer-redux 1.1.0 BufWriter#buf..lenrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]len().() |
| 606 | } |
| 607 | |
| 608 | /// Reserve space in the buffer for at least `additional` bytes. May not be |
| 609 | /// quite exact due to implementation details of the buffer's allocator. |
| 610 | pub fn reserverust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]reserve().(&mut self, additional: usize) { |
| 611 | self.bufrust-analyzer cargo buffer-redux 1.1.0 BufWriter#buf..reserverust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]reserve().(additional); |
| 612 | } |
| 613 | |
| 614 | /// Move data to the start of the buffer, making room at the end for more |
| 615 | /// writing. |
| 616 | /// |
| 617 | /// This is a no-op with the `*_ringbuf()` constructors (requires `slice-deque` feature). |
| 618 | pub fn make_roomrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]make_room().(&mut self) { |
| 619 | self.bufrust-analyzer cargo buffer-redux 1.1.0 BufWriter#buf..make_roomrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]make_room().(); |
| 620 | } |
| 621 | |
| 622 | /// Consume `self` and return both the underlying writer and the buffer |
| 623 | pub fn into_inner_with_bufferrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]into_inner_with_buffer().(self) -> (W, Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer#) { |
| 624 | self.into_inner_rust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]into_inner_().() |
| 625 | } |
| 626 | |
| 627 | // copy the fields out and forget `self` to avoid dropping twice |
| 628 | fn into_inner_rust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]into_inner_().(self) -> (W, Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer#) { |
| 629 | let s = ManuallyDroprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/ManuallyDrop#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/impl#[`ManuallyDrop<T>`]new().(self); |
| 630 | unsafe { |
| 631 | // safe because we immediately forget `self` |
| 632 | let inner = ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::readrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/read().(&s.innerrust-analyzer cargo buffer-redux 1.1.0 BufWriter#inner.); |
| 633 | let buf = ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::readrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/read().(&s.bufrust-analyzer cargo buffer-redux 1.1.0 BufWriter#buf.); |
| 634 | (inner, buf) |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | fn flush_bufrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]flush_buf().(&mut self, amt: usize) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()> { |
| 639 | if amt rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().0 || amt 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().self.bufrust-analyzer cargo buffer-redux 1.1.0 BufWriter#buf..lenrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]len().() { |
| 640 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()); |
| 641 | } |
| 642 | |
| 643 | self.panickedrust-analyzer cargo buffer-redux 1.1.0 BufWriter#panicked. = true; |
| 644 | let ret = self.bufrust-analyzer cargo buffer-redux 1.1.0 BufWriter#buf..write_maxrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]write_max().(amt, &mut self.innerrust-analyzer cargo buffer-redux 1.1.0 BufWriter#inner.); |
| 645 | self.panickedrust-analyzer cargo buffer-redux 1.1.0 BufWriter#panicked. = false; |
| 646 | ret |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | impl<W: Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#, P: WriterPolicyrust-analyzer cargo buffer-redux 1.1.0 policy/WriterPolicy#> BufWriterrust-analyzer cargo buffer-redux 1.1.0 BufWriter#<W, P> { |
| 651 | /// Flush the buffer and unwrap, returning the inner writer on success, |
| 652 | /// or a type wrapping `self` plus the error otherwise. |
| 653 | pub fn into_innerrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]into_inner().(mut self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<W, IntoInnerErrorrust-analyzer cargo buffer-redux 1.1.0 IntoInnerError#<Selfrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]>> { |
| 654 | match self.flushrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`][Write]flush().() { |
| 655 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e) => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(IntoInnerErrorrust-analyzer cargo buffer-redux 1.1.0 IntoInnerError#(self, e)), |
| 656 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(self.into_inner_rust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]into_inner_().().0), |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | /// Flush the buffer and unwrap, returning the inner writer and |
| 661 | /// any error encountered during flushing. |
| 662 | pub fn into_inner_with_errrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]into_inner_with_err().(mut self) -> (W, Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#>) { |
| 663 | let err = self.flushrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`][Write]flush().().errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]err().(); |
| 664 | (self.into_inner_rust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]into_inner_().().0, err) |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | impl<W: Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#, P: WriterPolicyrust-analyzer cargo buffer-redux 1.1.0 policy/WriterPolicy#> Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write# for BufWriterrust-analyzer cargo buffer-redux 1.1.0 BufWriter#<W, P> { |
| 669 | fn writerust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`][Write]write().(&mut self, buf: &[u8]) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<usize> { |
| 670 | let flush_amt = self.policyrust-analyzer cargo buffer-redux 1.1.0 BufWriter#policy..before_writerust-analyzer cargo buffer-redux 1.1.0 policy/WriterPolicy#before_write().(&mut self.bufrust-analyzer cargo buffer-redux 1.1.0 BufWriter#buf., buf.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()).0rust-analyzer cargo buffer-redux 1.1.0 policy/FlushAmt#0.; |
| 671 | self.flush_bufrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]flush_buf().(flush_amt)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 672 | |
| 673 | let written = if self.bufrust-analyzer cargo buffer-redux 1.1.0 BufWriter#buf..is_emptyrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]is_empty().() && buf.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]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.bufrust-analyzer cargo buffer-redux 1.1.0 BufWriter#buf..capacityrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]capacity().() { |
| 674 | self.panickedrust-analyzer cargo buffer-redux 1.1.0 BufWriter#panicked. = true; |
| 675 | let result = self.innerrust-analyzer cargo buffer-redux 1.1.0 BufWriter#inner..writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write().(buf); |
| 676 | self.panickedrust-analyzer cargo buffer-redux 1.1.0 BufWriter#panicked. = false; |
| 677 | result?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). |
| 678 | } else { |
| 679 | self.bufrust-analyzer cargo buffer-redux 1.1.0 BufWriter#buf..copy_from_slicerust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]copy_from_slice().(buf) |
| 680 | }; |
| 681 | |
| 682 | let flush_amt = self.policyrust-analyzer cargo buffer-redux 1.1.0 BufWriter#policy..after_writerust-analyzer cargo buffer-redux 1.1.0 policy/WriterPolicy#after_write().(&self.bufrust-analyzer cargo buffer-redux 1.1.0 BufWriter#buf.).0rust-analyzer cargo buffer-redux 1.1.0 policy/FlushAmt#0.; |
| 683 | |
| 684 | let _ = self.flush_bufrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]flush_buf().(flush_amt); |
| 685 | |
| 686 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(written) |
| 687 | } |
| 688 | |
| 689 | fn flushrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`][Write]flush().(&mut self) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()> { |
| 690 | let flush_amt = self.bufrust-analyzer cargo buffer-redux 1.1.0 BufWriter#buf..lenrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]len().(); |
| 691 | self.flush_bufrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]flush_buf().(flush_amt)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 692 | self.innerrust-analyzer cargo buffer-redux 1.1.0 BufWriter#inner..flushrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#flush().() |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | impl<W: Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write# + Seekrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Seek#, P: WriterPolicyrust-analyzer cargo buffer-redux 1.1.0 policy/WriterPolicy#> Seekrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Seek# for BufWriterrust-analyzer cargo buffer-redux 1.1.0 BufWriter#<W, P> { |
| 697 | /// Seek to the ofPet, in bytes, in the underlying writer. |
| 698 | /// |
| 699 | /// Seeking always writes out the internal buffer before seeking. |
| 700 | fn seekrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`][Seek]seek().(&mut self, pos: SeekFromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<u64> { |
| 701 | self.flushrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`][Write]flush().().and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]and_then().(|_| self.get_mutrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]get_mut().().seekrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Seek#seek().(pos)) |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | impl<W: Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write# + fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug#, P: fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug#> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# for BufWriterrust-analyzer cargo buffer-redux 1.1.0 BufWriter#<W, P> { |
| 706 | fn fmtrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`][Debug]fmt().(&self, f: &mut fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 707 | f.debug_structrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]debug_struct().("buffer_redux::BufWriter") |
| 708 | .fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]field().("writer", &self.innerrust-analyzer cargo buffer-redux 1.1.0 BufWriter#inner.) |
| 709 | .fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]field().("capacity", &self.capacityrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]capacity().()) |
| 710 | .fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]field().("policy", &self.policyrust-analyzer cargo buffer-redux 1.1.0 BufWriter#policy.) |
| 711 | .finishrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]finish().() |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | /// Attempt to flush the buffer to the underlying writer. |
| 716 | /// |
| 717 | /// If an error occurs, the thread-local handler is invoked, if one was previously |
| 718 | /// set by [`set_drop_err_handler`](set_drop_err_handler) for this thread. |
| 719 | impl<W: Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#, P> Droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/drop/Drop# for BufWriterrust-analyzer cargo buffer-redux 1.1.0 BufWriter#<W, P> { |
| 720 | fn droprust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`][Drop]drop().(&mut self) { |
| 721 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().self.panickedrust-analyzer cargo buffer-redux 1.1.0 BufWriter#panicked. { |
| 722 | // instead of ignoring a failed flush, call the handler |
| 723 | let buf_len = self.bufrust-analyzer cargo buffer-redux 1.1.0 BufWriter#buf..lenrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]len().(); |
| 724 | if let Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) = self.flush_bufrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]flush_buf().(buf_len) { |
| 725 | DROP_ERR_HANDLERrust-analyzer cargo buffer-redux 1.1.0 DROP_ERR_HANDLER..withrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/local/impl#[`LocalKey<T>`]with().(|deh| (*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`Ref<'_, T>`][Deref]deref().deh.borrowrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]borrow().())(&mut self.innerrust-analyzer cargo buffer-redux 1.1.0 BufWriter#inner., &mut self.bufrust-analyzer cargo buffer-redux 1.1.0 BufWriter#buf., err)); |
| 726 | } |
| 727 | } |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | /// A drop-in replacement for `std::io::LineWriter` with more functionality. |
| 732 | /// |
| 733 | /// This is, in fact, only a thin wrapper around |
| 734 | /// [`BufWriter`](BufWriter)`<W, `[`policy::FlushOnNewline`](policy::FlushOnNewline)`>`, which |
| 735 | /// demonstrates the power of custom [`WriterPolicy`](policy::WriterPolicy) implementations. |
| 736 | pub struct LineWriterrust-analyzer cargo buffer-redux 1.1.0 LineWriter#<W: Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#>(BufWriterrust-analyzer cargo buffer-redux 1.1.0 BufWriter#<W, FlushOnNewlinerust-analyzer cargo buffer-redux 1.1.0 policy/FlushOnNewline#>); |
| 737 | |
| 738 | impl<W: Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#> LineWriterrust-analyzer cargo buffer-redux 1.1.0 LineWriter#<W> { |
| 739 | /// Wrap `inner` with the default buffer capacity. |
| 740 | pub fn newrust-analyzer cargo buffer-redux 1.1.0 impl#[`LineWriter<W>`]new().(inner: W) -> Selfrust-analyzer cargo buffer-redux 1.1.0 impl#[`LineWriter<W>`] { |
| 741 | Selfrust-analyzer cargo buffer-redux 1.1.0 impl#[`LineWriter<W>`]::with_bufferrust-analyzer cargo buffer-redux 1.1.0 impl#[`LineWriter<W>`]with_buffer().(Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer#::newrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]new().(), inner) |
| 742 | } |
| 743 | |
| 744 | /// Wrap `inner` with the given buffer capacity. |
| 745 | pub fn with_capacityrust-analyzer cargo buffer-redux 1.1.0 impl#[`LineWriter<W>`]with_capacity().(cap: usize, inner: W) -> Selfrust-analyzer cargo buffer-redux 1.1.0 impl#[`LineWriter<W>`] { |
| 746 | Selfrust-analyzer cargo buffer-redux 1.1.0 impl#[`LineWriter<W>`]::with_bufferrust-analyzer cargo buffer-redux 1.1.0 impl#[`LineWriter<W>`]with_buffer().(Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer#::with_capacityrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]with_capacity().(cap), inner) |
| 747 | } |
| 748 | |
| 749 | /// Wrap `inner` with the default buffer capacity using a ringbuffer. |
| 750 | #[cfg(feature = "slice-deque")] |
| 751 | pub fn new_ringbuf(inner: W) -> Selfrust-analyzer cargo buffer-redux 1.1.0 impl#[`LineWriter<W>`] { |
| 752 | Selfrust-analyzer cargo buffer-redux 1.1.0 impl#[`LineWriter<W>`]::with_buffer(Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer#::new_ringbuf(), inner) |
| 753 | } |
| 754 | |
| 755 | /// Wrap `inner` with the given buffer capacity using a ringbuffer. |
| 756 | #[cfg(feature = "slice-deque")] |
| 757 | pub fn with_capacity_ringbuf(cap: usize, inner: W) -> Selfrust-analyzer cargo buffer-redux 1.1.0 impl#[`LineWriter<W>`] { |
| 758 | Selfrust-analyzer cargo buffer-redux 1.1.0 impl#[`LineWriter<W>`]::with_buffer(Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer#::with_capacity_ringbuf(cap), inner) |
| 759 | } |
| 760 | |
| 761 | /// Wrap `inner` with an existing `Buffer` instance. |
| 762 | /// |
| 763 | /// ### Note |
| 764 | /// Does **not** clear the buffer first! If there is data already in the buffer |
| 765 | /// it will be written out on the next flush! |
| 766 | pub fn with_bufferrust-analyzer cargo buffer-redux 1.1.0 impl#[`LineWriter<W>`]with_buffer().(buf: Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer#, inner: W) -> LineWriterrust-analyzer cargo buffer-redux 1.1.0 LineWriter#<W> { |
| 767 | LineWriterrust-analyzer cargo buffer-redux 1.1.0 LineWriter#(BufWriterrust-analyzer cargo buffer-redux 1.1.0 BufWriter#::with_bufferrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W>`]with_buffer().(buf, inner).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#)) |
| 768 | } |
| 769 | |
| 770 | /// Get a reference to the inner writer. |
| 771 | pub fn get_refrust-analyzer cargo buffer-redux 1.1.0 impl#[`LineWriter<W>`]get_ref().(&self) -> &W { |
| 772 | self.0rust-analyzer cargo buffer-redux 1.1.0 LineWriter#0..get_refrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]get_ref().() |
| 773 | } |
| 774 | |
| 775 | /// Get a mutable reference to the inner writer. |
| 776 | /// |
| 777 | /// ### Note |
| 778 | /// If the buffer has not been flushed, writing directly to the inner type will cause |
| 779 | /// data inconsistency. |
| 780 | pub fn get_mutrust-analyzer cargo buffer-redux 1.1.0 impl#[`LineWriter<W>`]get_mut().(&mut self) -> &mut W { |
| 781 | self.0rust-analyzer cargo buffer-redux 1.1.0 LineWriter#0..get_mutrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]get_mut().() |
| 782 | } |
| 783 | |
| 784 | /// Get the capacity of the inner buffer. |
| 785 | pub fn capacityrust-analyzer cargo buffer-redux 1.1.0 impl#[`LineWriter<W>`]capacity().(&self) -> usize { |
| 786 | self.0rust-analyzer cargo buffer-redux 1.1.0 LineWriter#0..capacityrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]capacity().() |
| 787 | } |
| 788 | |
| 789 | /// Get the number of bytes currently in the buffer. |
| 790 | pub fn buf_lenrust-analyzer cargo buffer-redux 1.1.0 impl#[`LineWriter<W>`]buf_len().(&self) -> usize { |
| 791 | self.0rust-analyzer cargo buffer-redux 1.1.0 LineWriter#0..buf_lenrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]buf_len().() |
| 792 | } |
| 793 | |
| 794 | /// Ensure enough space in the buffer for *at least* `additional` bytes. May not be |
| 795 | /// quite exact due to implementation details of the buffer's allocator. |
| 796 | pub fn reserverust-analyzer cargo buffer-redux 1.1.0 impl#[`LineWriter<W>`]reserve().(&mut self, additional: usize) { |
| 797 | self.0rust-analyzer cargo buffer-redux 1.1.0 LineWriter#0..reserverust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]reserve().(additional); |
| 798 | } |
| 799 | |
| 800 | /// Flush the buffer and unwrap, returning the inner writer on success, |
| 801 | /// or a type wrapping `self` plus the error otherwise. |
| 802 | pub fn into_innerrust-analyzer cargo buffer-redux 1.1.0 impl#[`LineWriter<W>`]into_inner().(self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<W, IntoInnerErrorrust-analyzer cargo buffer-redux 1.1.0 IntoInnerError#<Selfrust-analyzer cargo buffer-redux 1.1.0 impl#[`LineWriter<W>`]>> { |
| 803 | self.0rust-analyzer cargo buffer-redux 1.1.0 LineWriter#0. |
| 804 | .into_innerrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]into_inner().() |
| 805 | .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|IntoInnerErrorrust-analyzer cargo buffer-redux 1.1.0 IntoInnerError#(inner, e)| IntoInnerErrorrust-analyzer cargo buffer-redux 1.1.0 IntoInnerError#(LineWriterrust-analyzer cargo buffer-redux 1.1.0 LineWriter#(inner), e)) |
| 806 | } |
| 807 | |
| 808 | /// Flush the buffer and unwrap, returning the inner writer and |
| 809 | /// any error encountered during flushing. |
| 810 | pub fn into_inner_with_errrust-analyzer cargo buffer-redux 1.1.0 impl#[`LineWriter<W>`]into_inner_with_err().(self) -> (W, Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#>) { |
| 811 | self.0rust-analyzer cargo buffer-redux 1.1.0 LineWriter#0..into_inner_with_errrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]into_inner_with_err().() |
| 812 | } |
| 813 | |
| 814 | /// Consume `self` and return both the underlying writer and the buffer. |
| 815 | pub fn into_inner_with_bufrust-analyzer cargo buffer-redux 1.1.0 impl#[`LineWriter<W>`]into_inner_with_buf().(self) -> (W, Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer#) { |
| 816 | self.0rust-analyzer cargo buffer-redux 1.1.0 LineWriter#0..into_inner_with_bufferrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`]into_inner_with_buffer().() |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | impl<W: Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#> Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write# for LineWriterrust-analyzer cargo buffer-redux 1.1.0 LineWriter#<W> { |
| 821 | fn writerust-analyzer cargo buffer-redux 1.1.0 impl#[`LineWriter<W>`][Write]write().(&mut self, buf: &[u8]) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<usize> { |
| 822 | self.0rust-analyzer cargo buffer-redux 1.1.0 LineWriter#0..writerust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`][Write]write().(buf) |
| 823 | } |
| 824 | |
| 825 | fn flushrust-analyzer cargo buffer-redux 1.1.0 impl#[`LineWriter<W>`][Write]flush().(&mut self) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()> { |
| 826 | self.0rust-analyzer cargo buffer-redux 1.1.0 LineWriter#0..flushrust-analyzer cargo buffer-redux 1.1.0 impl#[`BufWriter<W, P>`][Write]flush().() |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | impl<W: Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write# + fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug#> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# for LineWriterrust-analyzer cargo buffer-redux 1.1.0 LineWriter#<W> { |
| 831 | fn fmtrust-analyzer cargo buffer-redux 1.1.0 impl#[`LineWriter<W>`][Debug]fmt().(&self, f: &mut fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 832 | f.debug_structrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]debug_struct().("buffer_redux::LineWriter") |
| 833 | .fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]field().("writer", self.get_refrust-analyzer cargo buffer-redux 1.1.0 impl#[`LineWriter<W>`]get_ref().()) |
| 834 | .fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]field().("capacity", &self.capacityrust-analyzer cargo buffer-redux 1.1.0 impl#[`LineWriter<W>`]capacity().()) |
| 835 | .finishrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]finish().() |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | /// The error type for `BufWriter::into_inner()`, |
| 840 | /// contains the `BufWriter` as well as the error that occurred. |
| 841 | #[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:)] |
| 842 | pub struct IntoInnerErrorrust-analyzer cargo buffer-redux 1.1.0 IntoInnerError#<W>(pub W, pub iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#); |
| 843 | |
| 844 | impl<W> IntoInnerErrorrust-analyzer cargo buffer-redux 1.1.0 IntoInnerError#<W> { |
| 845 | /// Get the error |
| 846 | pub fn errorrust-analyzer cargo buffer-redux 1.1.0 impl#[`IntoInnerError<W>`]error().(&self) -> &iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error# { |
| 847 | &self.1rust-analyzer cargo buffer-redux 1.1.0 IntoInnerError#1. |
| 848 | } |
| 849 | |
| 850 | /// Take the writer. |
| 851 | pub fn into_innerrust-analyzer cargo buffer-redux 1.1.0 impl#[`IntoInnerError<W>`]into_inner().(self) -> W { |
| 852 | self.0rust-analyzer cargo buffer-redux 1.1.0 IntoInnerError#0. |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | impl<W> Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<IntoInnerErrorrust-analyzer cargo buffer-redux 1.1.0 IntoInnerError#<W>> for iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error# { |
| 857 | fn fromrust-analyzer cargo buffer-redux 1.1.0 impl#[`io::Error`][`From<IntoInnerError<W>>`]from().(val: IntoInnerErrorrust-analyzer cargo buffer-redux 1.1.0 IntoInnerError#<W>) -> Selfrust-analyzer cargo buffer-redux 1.1.0 impl#[`io::Error`][`From<IntoInnerError<W>>`] { |
| 858 | val.1rust-analyzer cargo buffer-redux 1.1.0 IntoInnerError#1. |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | impl<W: Anyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core any/Any# + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug#> errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# for IntoInnerErrorrust-analyzer cargo buffer-redux 1.1.0 IntoInnerError#<W> { |
| 863 | fn causerust-analyzer cargo buffer-redux 1.1.0 impl#[`IntoInnerError<W>`][Error]cause().(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&dyn errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error#> { |
| 864 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&self.1rust-analyzer cargo buffer-redux 1.1.0 IntoInnerError#1.) |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | impl<W> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for IntoInnerErrorrust-analyzer cargo buffer-redux 1.1.0 IntoInnerError#<W> { |
| 869 | fn fmtrust-analyzer cargo buffer-redux 1.1.0 impl#[`IntoInnerError<W>`][Display]fmt().(&self, f: &mut fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 870 | self.errorrust-analyzer cargo buffer-redux 1.1.0 impl#[`IntoInnerError<W>`]error().().fmtrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error][Display]fmt().(f) |
| 871 | } |
| 872 | } |
| 873 | |
| 874 | /// A deque-like datastructure for managing bytes. |
| 875 | /// |
| 876 | /// Supports interacting via I/O traits like `Read` and `Write`, and direct access. |
| 877 | pub struct Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer# { |
| 878 | bufrust-analyzer cargo buffer-redux 1.1.0 Buffer#buf.: BufImplrust-analyzer cargo buffer-redux 1.1.0 buffer/BufImpl#, |
| 879 | zeroedrust-analyzer cargo buffer-redux 1.1.0 Buffer#zeroed.: usize, |
| 880 | } |
| 881 | |
| 882 | impl Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer# { |
| 883 | fn defaultrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer][Default]default().() -> Selfrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer][Default] { |
| 884 | Selfrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer][Default]::with_capacityrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]with_capacity().(DEFAULT_BUF_SIZErust-analyzer cargo buffer-redux 1.1.0 DEFAULT_BUF_SIZE.) |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | impl Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer# { |
| 889 | /// Create a new buffer with a default capacity. |
| 890 | pub fn newrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]new().() -> Selfrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer] { |
| 891 | Selfrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]::defaultrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer][Default]default().() |
| 892 | } |
| 893 | |
| 894 | /// Create a new buffer with *at least* the given capacity. |
| 895 | /// |
| 896 | /// If the global allocator returns extra capacity, `Buffer` will use all of it. |
| 897 | pub fn with_capacityrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]with_capacity().(cap: usize) -> Selfrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer] { |
| 898 | Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer# { |
| 899 | bufrust-analyzer cargo buffer-redux 1.1.0 Buffer#buf.: BufImplrust-analyzer cargo buffer-redux 1.1.0 buffer/BufImpl#::with_capacityrust-analyzer cargo buffer-redux 1.1.0 buffer/impl#[BufImpl]with_capacity().(cap), |
| 900 | zeroedrust-analyzer cargo buffer-redux 1.1.0 Buffer#zeroed.: 0, |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | /// Allocate a buffer with a default capacity that never needs to move data to make room |
| 905 | /// (consuming from the head simultaneously makes more room at the tail). |
| 906 | /// |
| 907 | /// The default capacity varies based on the target platform: |
| 908 | /// |
| 909 | /// * Unix-derivative platforms; Linux, OS X, BSDs, etc: **8KiB** (the default buffer size for |
| 910 | /// `std::io` buffered types) |
| 911 | /// * Windows: **64KiB** because of legacy reasons, of course (see below) |
| 912 | /// |
| 913 | /// Only available on platforms with virtual memory support and with the `slice-deque` feature |
| 914 | /// enabled. The current platforms that are supported/tested are listed |
| 915 | /// [in the README for the `slice-deque` crate][slice-deque]. |
| 916 | /// |
| 917 | /// [slice-deque]: https://github.com/gnzlbg/slice_deque#platform-support |
| 918 | #[cfg(feature = "slice-deque")] |
| 919 | pub fn new_ringbuf() -> Selfrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer] { |
| 920 | Selfrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]::with_capacity_ringbuf(DEFAULT_BUF_SIZErust-analyzer cargo buffer-redux 1.1.0 DEFAULT_BUF_SIZE.) |
| 921 | } |
| 922 | |
| 923 | /// Allocate a buffer with *at least* the given capacity that never needs to move data to |
| 924 | /// make room (consuming from the head simultaneously makes more room at the tail). |
| 925 | /// |
| 926 | /// The capacity will be rounded up to the minimum size for the current target: |
| 927 | /// |
| 928 | /// * Unix-derivative platforms; Linux, OS X, BSDs, etc: the next multiple of the page size |
| 929 | /// (typically 4KiB but can vary based on system configuration) |
| 930 | /// * Windows: the next muliple of **64KiB**; see [this Microsoft dev blog post][Win-why-64k] |
| 931 | /// for why it's 64KiB and not the page size (TL;DR: Alpha AXP needs it and it's applied on |
| 932 | /// all targets for consistency/portability) |
| 933 | /// |
| 934 | /// [Win-why-64k]: https://blogs.msdn.microsoft.com/oldnewthing/20031008-00/?p=42223 |
| 935 | /// |
| 936 | /// Only available on platforms with virtual memory support and with the `slice-deque` feature |
| 937 | /// enabled. The current platforms that are supported/tested are listed |
| 938 | /// [in the README for the `slice-deque` crate][slice-deque]. |
| 939 | /// |
| 940 | /// [slice-deque]: https://github.com/gnzlbg/slice_deque#platform-support |
| 941 | #[cfg(feature = "slice-deque")] |
| 942 | pub fn with_capacity_ringbuf(cap: usize) -> Selfrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer] { |
| 943 | Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer# { |
| 944 | buf: BufImplrust-analyzer cargo buffer-redux 1.1.0 buffer/BufImpl#::with_capacity_ringbuf(cap), |
| 945 | zeroed: 0, |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | /// Return `true` if this is a ringbuffer. |
| 950 | pub fn is_ringbufrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]is_ringbuf().(&self) -> bool { |
| 951 | self.bufrust-analyzer cargo buffer-redux 1.1.0 Buffer#buf..is_ringbufrust-analyzer cargo buffer-redux 1.1.0 buffer/impl#[BufImpl]is_ringbuf().() |
| 952 | } |
| 953 | |
| 954 | /// Return the number of bytes currently in this buffer. |
| 955 | /// |
| 956 | /// Equivalent to `self.buf().len()`. |
| 957 | pub fn lenrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]len().(&self) -> usize { |
| 958 | self.bufrust-analyzer cargo buffer-redux 1.1.0 Buffer#buf..lenrust-analyzer cargo buffer-redux 1.1.0 buffer/impl#[BufImpl]len().() |
| 959 | } |
| 960 | |
| 961 | /// Return the number of bytes that can be read into this buffer before it needs |
| 962 | /// to grow or the data in the buffer needs to be moved. |
| 963 | /// |
| 964 | /// This may not constitute all free space in the buffer if bytes have been consumed |
| 965 | /// from the head. Use `free_space()` to determine the total free space in the buffer. |
| 966 | pub fn usable_spacerust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]usable_space().(&self) -> usize { |
| 967 | self.bufrust-analyzer cargo buffer-redux 1.1.0 Buffer#buf..usable_spacerust-analyzer cargo buffer-redux 1.1.0 buffer/impl#[BufImpl]usable_space().() |
| 968 | } |
| 969 | |
| 970 | /// Returns the total amount of free space in the buffer, including bytes |
| 971 | /// already consumed from the head. |
| 972 | /// |
| 973 | /// This will be greater than or equal to `usable_space()`. On supported platforms |
| 974 | /// with the `slice-deque` feature enabled, it should be equal. |
| 975 | pub fn free_spacerust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]free_space().(&self) -> usize { |
| 976 | self.capacityrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]capacity().() 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().self.lenrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]len().() |
| 977 | } |
| 978 | |
| 979 | /// Return the total capacity of this buffer. |
| 980 | pub fn capacityrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]capacity().(&self) -> usize { |
| 981 | self.bufrust-analyzer cargo buffer-redux 1.1.0 Buffer#buf..capacityrust-analyzer cargo buffer-redux 1.1.0 buffer/impl#[BufImpl]capacity().() |
| 982 | } |
| 983 | |
| 984 | /// Returns `true` if there are no bytes in the buffer, false otherwise. |
| 985 | pub fn is_emptyrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]is_empty().(&self) -> bool { |
| 986 | self.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][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().0 |
| 987 | } |
| 988 | |
| 989 | /// Move bytes down in the buffer to maximize usable space. |
| 990 | /// |
| 991 | /// This is a no-op on supported platforms with the `slice-deque` feature enabled. |
| 992 | pub fn make_roomrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]make_room().(&mut self) { |
| 993 | self.bufrust-analyzer cargo buffer-redux 1.1.0 Buffer#buf..make_roomrust-analyzer cargo buffer-redux 1.1.0 buffer/impl#[BufImpl]make_room().(); |
| 994 | } |
| 995 | |
| 996 | /// Ensure space for at least `additional` more bytes in the buffer. |
| 997 | /// |
| 998 | /// This is a no-op if `usable_space() >= additional`. Note that this will reallocate |
| 999 | /// even if there is enough free space at the head of the buffer for `additional` bytes, |
| 1000 | /// because that free space is not at the tail where it can be read into. |
| 1001 | /// If you prefer copying data down in the buffer before attempting to reallocate you may wish |
| 1002 | /// to call `.make_room()` first. |
| 1003 | /// |
| 1004 | /// ### Panics |
| 1005 | /// If `self.capacity() + additional` overflows. |
| 1006 | pub fn reserverust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]reserve().(&mut self, additional: usize) { |
| 1007 | // Returns `true` if we reallocated out-of-place and thus need to re-zero. |
| 1008 | if self.bufrust-analyzer cargo buffer-redux 1.1.0 Buffer#buf..reserverust-analyzer cargo buffer-redux 1.1.0 buffer/impl#[BufImpl]reserve().(additional) { |
| 1009 | self.zeroedrust-analyzer cargo buffer-redux 1.1.0 Buffer#zeroed. = 0; |
| 1010 | } |
| 1011 | } |
| 1012 | |
| 1013 | /// Get an immutable slice of the available bytes in this buffer. |
| 1014 | /// |
| 1015 | /// Call `.consume()` to remove bytes from the beginning of this slice. |
| 1016 | #[inline] |
| 1017 | pub fn bufrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]buf().(&self) -> &[u8] { |
| 1018 | self.bufrust-analyzer cargo buffer-redux 1.1.0 Buffer#buf..bufrust-analyzer cargo buffer-redux 1.1.0 buffer/impl#[BufImpl]buf().() |
| 1019 | } |
| 1020 | |
| 1021 | /// Get a mutable slice representing the available bytes in this buffer. |
| 1022 | /// |
| 1023 | /// Call `.consume()` to remove bytes from the beginning of this slice. |
| 1024 | pub fn buf_mutrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]buf_mut().(&mut self) -> &mut [u8] { |
| 1025 | self.bufrust-analyzer cargo buffer-redux 1.1.0 Buffer#buf..buf_mutrust-analyzer cargo buffer-redux 1.1.0 buffer/impl#[BufImpl]buf_mut().() |
| 1026 | } |
| 1027 | |
| 1028 | /// Read from `rdr`, returning the number of bytes read or any errors. |
| 1029 | /// |
| 1030 | /// If there is no more room at the head of the buffer, this will return `Ok(0)`. |
| 1031 | /// |
| 1032 | /// ### Panics |
| 1033 | /// If the returned count from `rdr.read()` overflows the tail cursor of this buffer. |
| 1034 | pub fn read_fromrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]read_from().<R: Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read# + ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#>(&mut self, rdr: &mut R) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<usize> { |
| 1035 | if self.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][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().0 { |
| 1036 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(0); |
| 1037 | } |
| 1038 | |
| 1039 | let cap = self.capacityrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]capacity().(); |
| 1040 | if self.zeroedrust-analyzer cargo buffer-redux 1.1.0 Buffer#zeroed. 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().cap { |
| 1041 | unsafe { |
| 1042 | let buf = self.bufrust-analyzer cargo buffer-redux 1.1.0 Buffer#buf..write_bufrust-analyzer cargo buffer-redux 1.1.0 buffer/impl#[BufImpl]write_buf().(); |
| 1043 | // TODO: use MaybeUninit::fill once stabilized |
| 1044 | for el in buf { |
| 1045 | el.writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/impl#[`MaybeUninit<T>`]write().(0); |
| 1046 | } |
| 1047 | } |
| 1048 | |
| 1049 | self.zeroedrust-analyzer cargo buffer-redux 1.1.0 Buffer#zeroed. = cap; |
| 1050 | } |
| 1051 | |
| 1052 | let read = { |
| 1053 | let buf = unsafe { self.bufrust-analyzer cargo buffer-redux 1.1.0 Buffer#buf..write_bufrust-analyzer cargo buffer-redux 1.1.0 buffer/impl#[BufImpl]write_buf().() }; |
| 1054 | // SAFETY: everything upto `cap` was zeroed above |
| 1055 | // TODO: use MaybeUninit::slice_assume_init_mut once stabilized |
| 1056 | let buf = unsafe { &mut *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#deref().(buf as *mut [MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#<u8>] as *mut [u8]) }; |
| 1057 | // TODO: use BorrowedCursor once stabilized |
| 1058 | rdr.readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read().(buf)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). |
| 1059 | }; |
| 1060 | |
| 1061 | unsafe { |
| 1062 | self.bufrust-analyzer cargo buffer-redux 1.1.0 Buffer#buf..bytes_writtenrust-analyzer cargo buffer-redux 1.1.0 buffer/impl#[BufImpl]bytes_written().(read); |
| 1063 | } |
| 1064 | |
| 1065 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(read) |
| 1066 | } |
| 1067 | |
| 1068 | /// Copy from `src` to the tail of this buffer. Returns the number of bytes copied. |
| 1069 | /// |
| 1070 | /// This will **not** grow the buffer if `src` is larger than `self.usable_space()`; instead, |
| 1071 | /// it will fill the usable space and return the number of bytes copied. If there is no usable |
| 1072 | /// space, this returns 0. |
| 1073 | pub fn copy_from_slicerust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]copy_from_slice().(&mut self, src: &[u8]) -> usize { |
| 1074 | let len = unsafe { |
| 1075 | let buf = self.bufrust-analyzer cargo buffer-redux 1.1.0 Buffer#buf..write_bufrust-analyzer cargo buffer-redux 1.1.0 buffer/impl#[BufImpl]write_buf().(); |
| 1076 | let len = cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/min().(buf.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(), src.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()); |
| 1077 | |
| 1078 | // TODO: use MaybeUninit::copy_from_slice once stabilized |
| 1079 | |
| 1080 | // SAFETY: &[T] and &[MaybeUninit<T>] have the same layout |
| 1081 | let uninit_src: &[MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#<u8>] = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::transmuterust-analyzer cargo core https://github.com/rust-lang/rust/library/core intrinsics/transmute().(src); |
| 1082 | buf[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#len]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut()..copy_from_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]copy_from_slice().(&uninit_src[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#len]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().); |
| 1083 | |
| 1084 | len |
| 1085 | }; |
| 1086 | |
| 1087 | unsafe { |
| 1088 | self.bufrust-analyzer cargo buffer-redux 1.1.0 Buffer#buf..bytes_writtenrust-analyzer cargo buffer-redux 1.1.0 buffer/impl#[BufImpl]bytes_written().(len); |
| 1089 | } |
| 1090 | |
| 1091 | len |
| 1092 | } |
| 1093 | |
| 1094 | /// Write bytes from this buffer to `wrt`. Returns the number of bytes written or any errors. |
| 1095 | /// |
| 1096 | /// If the buffer is empty, returns `Ok(0)`. |
| 1097 | /// |
| 1098 | /// ### Panics |
| 1099 | /// If the count returned by `wrt.write()` would cause the head cursor to overflow or pass |
| 1100 | /// the tail cursor if added to it. |
| 1101 | pub fn write_torust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]write_to().<W: Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write# + ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#>(&mut self, wrt: &mut W) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<usize> { |
| 1102 | if self.is_emptyrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]is_empty().() { |
| 1103 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(0); |
| 1104 | } |
| 1105 | |
| 1106 | let written = wrt.writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write().(self.bufrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]buf().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 1107 | self.consumerust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]consume().(written); |
| 1108 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(written) |
| 1109 | } |
| 1110 | |
| 1111 | /// Write, at most, the given number of bytes from this buffer to `wrt`, continuing |
| 1112 | /// to write and ignoring interrupts until the number is reached or the buffer is empty. |
| 1113 | /// |
| 1114 | /// ### Panics |
| 1115 | /// If the count returned by `wrt.write()` would cause the head cursor to overflow or pass |
| 1116 | /// the tail cursor if added to it. |
| 1117 | pub fn write_maxrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]write_max().<W: Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write# + ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#>(&mut self, mut max: usize, wrt: &mut W) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()> { |
| 1118 | while !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().self.is_emptyrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]is_empty().() && max 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().0 { |
| 1119 | let len = cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/min().(self.lenrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]len().(), max); |
| 1120 | let n = match wrt.writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write().(&self.bufrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]buf().()[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#len]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().) { |
| 1121 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(0) => { |
| 1122 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().( |
| 1123 | iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::WriteZerorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#WriteZero#, |
| 1124 | "Buffer::write_all() got zero-sized write", |
| 1125 | )) |
| 1126 | } |
| 1127 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(n) => n, |
| 1128 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ref e) if e.kindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]kind().() rust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[ErrorKind][`PartialEq<Self>`]eq().==rust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[ErrorKind][`PartialEq<Self>`]eq(). rust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[ErrorKind][`PartialEq<Self>`]eq().iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::Interruptedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#Interrupted# => continue, |
| 1129 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e) => return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e), |
| 1130 | }; |
| 1131 | |
| 1132 | self.consumerust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]consume().(n); |
| 1133 | max = max.saturating_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_sub().(n); |
| 1134 | } |
| 1135 | |
| 1136 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 1137 | } |
| 1138 | |
| 1139 | /// Write all bytes in this buffer to `wrt`, ignoring interrupts. Continues writing until |
| 1140 | /// the buffer is empty or an error is returned. |
| 1141 | /// |
| 1142 | /// ### Panics |
| 1143 | /// If `self.write_to(wrt)` panics. |
| 1144 | pub fn write_allrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]write_all().<W: Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write# + ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#>(&mut self, wrt: &mut W) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()> { |
| 1145 | while !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().self.is_emptyrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]is_empty().() { |
| 1146 | match self.write_torust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]write_to().(wrt) { |
| 1147 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(0) => { |
| 1148 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().( |
| 1149 | iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::WriteZerorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#WriteZero#, |
| 1150 | "Buffer::write_all() got zero-sized write", |
| 1151 | )) |
| 1152 | } |
| 1153 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(_) => (), |
| 1154 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ref e) if e.kindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]kind().() rust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[ErrorKind][`PartialEq<Self>`]eq().==rust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[ErrorKind][`PartialEq<Self>`]eq(). rust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[ErrorKind][`PartialEq<Self>`]eq().iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::Interruptedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#Interrupted# => (), |
| 1155 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e) => return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e), |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 1160 | } |
| 1161 | |
| 1162 | /// Copy bytes to `out` from this buffer, returning the number of bytes written. |
| 1163 | pub fn copy_to_slicerust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]copy_to_slice().(&mut self, out: &mut [u8]) -> usize { |
| 1164 | let len = { |
| 1165 | let buf = self.bufrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]buf().(); |
| 1166 | |
| 1167 | let len = cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/min().(buf.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(), out.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()); |
| 1168 | out[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#len]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut()..copy_from_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]copy_from_slice().(&buf[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#len]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().); |
| 1169 | len |
| 1170 | }; |
| 1171 | |
| 1172 | self.consumerust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]consume().(len); |
| 1173 | |
| 1174 | len |
| 1175 | } |
| 1176 | |
| 1177 | /// Push `bytes` to the end of the buffer, growing it if necessary. |
| 1178 | /// |
| 1179 | /// If you prefer moving bytes down in the buffer to reallocating, you may wish to call |
| 1180 | /// `.make_room()` first. |
| 1181 | pub fn push_bytesrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]push_bytes().(&mut self, bytes: &[u8]) { |
| 1182 | let s_len = bytes.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(); |
| 1183 | |
| 1184 | if self.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().s_len { |
| 1185 | self.reserverust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]reserve().(s_len 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); |
| 1186 | } |
| 1187 | |
| 1188 | unsafe { |
| 1189 | // TODO: use MaybeUninit::copy_from_slice once stabilized |
| 1190 | |
| 1191 | // SAFETY: &[T] and &[MaybeUninit<T>] have the same layout |
| 1192 | let uninit_src: &[MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#<u8>] = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::transmuterust-analyzer cargo core https://github.com/rust-lang/rust/library/core intrinsics/transmute().(bytes); |
| 1193 | let buf = self.bufrust-analyzer cargo buffer-redux 1.1.0 Buffer#buf..write_bufrust-analyzer cargo buffer-redux 1.1.0 buffer/impl#[BufImpl]write_buf().(); |
| 1194 | buf[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#s_len]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut()..copy_from_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]copy_from_slice().(uninit_src); |
| 1195 | |
| 1196 | self.bufrust-analyzer cargo buffer-redux 1.1.0 Buffer#buf..bytes_writtenrust-analyzer cargo buffer-redux 1.1.0 buffer/impl#[BufImpl]bytes_written().(s_len); |
| 1197 | } |
| 1198 | } |
| 1199 | |
| 1200 | /// Consume `amt` bytes from the head of this buffer. |
| 1201 | pub fn consumerust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]consume().(&mut self, amt: usize) { |
| 1202 | self.bufrust-analyzer cargo buffer-redux 1.1.0 Buffer#buf..consumerust-analyzer cargo buffer-redux 1.1.0 buffer/impl#[BufImpl]consume().(amt); |
| 1203 | } |
| 1204 | |
| 1205 | /// Empty this buffer by consuming all bytes. |
| 1206 | pub fn clearrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]clear().(&mut self) { |
| 1207 | let buf_len = self.lenrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]len().(); |
| 1208 | self.consumerust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]consume().(buf_len); |
| 1209 | } |
| 1210 | } |
| 1211 | |
| 1212 | impl fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# for Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer# { |
| 1213 | fn fmtrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer][Debug]fmt().(&self, f: &mut fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 1214 | f.debug_structrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]debug_struct().("buffer_redux::Buffer") |
| 1215 | .fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]field().("capacity", &self.capacityrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]capacity().()) |
| 1216 | .fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]field().("len", &self.lenrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]len().()) |
| 1217 | .finishrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]finish().() |
| 1218 | } |
| 1219 | } |
| 1220 | |
| 1221 | /// A `Read` adapter for a consumed `BufReader` which will empty bytes from the buffer before |
| 1222 | /// reading from `R` directly. Frees the buffer when it has been emptied. |
| 1223 | pub struct Unbufferrust-analyzer cargo buffer-redux 1.1.0 Unbuffer#<R> { |
| 1224 | innerrust-analyzer cargo buffer-redux 1.1.0 Unbuffer#inner.: R, |
| 1225 | bufrust-analyzer cargo buffer-redux 1.1.0 Unbuffer#buf.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer#>, |
| 1226 | } |
| 1227 | |
| 1228 | impl<R> Unbufferrust-analyzer cargo buffer-redux 1.1.0 Unbuffer#<R> { |
| 1229 | /// Returns `true` if the buffer still has some bytes left, `false` otherwise. |
| 1230 | pub fn is_buf_emptyrust-analyzer cargo buffer-redux 1.1.0 impl#[`Unbuffer<R>`]is_buf_empty().(&self) -> bool { |
| 1231 | self.bufrust-analyzer cargo buffer-redux 1.1.0 Unbuffer#buf..is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().() |
| 1232 | } |
| 1233 | |
| 1234 | /// Returns the number of bytes remaining in the buffer. |
| 1235 | pub fn buf_lenrust-analyzer cargo buffer-redux 1.1.0 impl#[`Unbuffer<R>`]buf_len().(&self) -> usize { |
| 1236 | self.bufrust-analyzer cargo buffer-redux 1.1.0 Unbuffer#buf..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_ref().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer#::lenrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]len().).unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(0) |
| 1237 | } |
| 1238 | |
| 1239 | /// Get a slice over the available bytes in the buffer. |
| 1240 | #[inline] |
| 1241 | pub fn bufrust-analyzer cargo buffer-redux 1.1.0 impl#[`Unbuffer<R>`]buf().(&self) -> &[u8] { |
| 1242 | self.bufrust-analyzer cargo buffer-redux 1.1.0 Unbuffer#buf..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_ref().().map_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map_or().(&[], Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer#::bufrust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]buf().) |
| 1243 | } |
| 1244 | |
| 1245 | /// Return the underlying reader, releasing the buffer. |
| 1246 | pub fn into_innerrust-analyzer cargo buffer-redux 1.1.0 impl#[`Unbuffer<R>`]into_inner().(self) -> R { |
| 1247 | self.innerrust-analyzer cargo buffer-redux 1.1.0 Unbuffer#inner. |
| 1248 | } |
| 1249 | } |
| 1250 | |
| 1251 | impl<R: Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#> Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read# for Unbufferrust-analyzer cargo buffer-redux 1.1.0 Unbuffer#<R> { |
| 1252 | fn readrust-analyzer cargo buffer-redux 1.1.0 impl#[`Unbuffer<R>`][Read]read().(&mut self, out: &mut [u8]) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<usize> { |
| 1253 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(ref mut buf) = self.bufrust-analyzer cargo buffer-redux 1.1.0 Unbuffer#buf..as_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_mut().() { |
| 1254 | let read = buf.copy_to_slicerust-analyzer cargo buffer-redux 1.1.0 impl#[Buffer]copy_to_slice().(out); |
| 1255 | |
| 1256 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().out.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]is_empty().() && read rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().0 { |
| 1257 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(read); |
| 1258 | } |
| 1259 | } |
| 1260 | |
| 1261 | self.bufrust-analyzer cargo buffer-redux 1.1.0 Unbuffer#buf. = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 1262 | |
| 1263 | self.innerrust-analyzer cargo buffer-redux 1.1.0 Unbuffer#inner..readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read().(out) |
| 1264 | } |
| 1265 | } |
| 1266 | |
| 1267 | impl<R: fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug#> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# for Unbufferrust-analyzer cargo buffer-redux 1.1.0 Unbuffer#<R> { |
| 1268 | fn fmtrust-analyzer cargo buffer-redux 1.1.0 impl#[`Unbuffer<R>`][Debug]fmt().(&self, fmt: &mut fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 1269 | fmt.debug_structrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]debug_struct().("buffer_redux::Unbuffer") |
| 1270 | .fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]field().("reader", &self.innerrust-analyzer cargo buffer-redux 1.1.0 Unbuffer#inner.) |
| 1271 | .fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]field().("buffer", &self.bufrust-analyzer cargo buffer-redux 1.1.0 Unbuffer#buf.) |
| 1272 | .finishrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]finish().() |
| 1273 | } |
| 1274 | } |
| 1275 | |
| 1276 | /// Copy data between a `BufRead` and a `Write` without an intermediate buffer. |
| 1277 | /// |
| 1278 | /// Retries on interrupts. Returns the total bytes copied or the first error; |
| 1279 | /// even if an error is returned some bytes may still have been copied. |
| 1280 | pub fn copy_bufrust-analyzer cargo buffer-redux 1.1.0 copy_buf().<B: BufReadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/BufRead#, W: Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#>(b: &mut B, w: &mut W) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<u64> { |
| 1281 | let mut total_copied = 0; |
| 1282 | |
| 1283 | loop { |
| 1284 | let copied = match b.fill_bufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/BufRead#fill_buf().().and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]and_then().(|buf| w.writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write().(buf)) { |
| 1285 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ref e) if e.kindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]kind().() rust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[ErrorKind][`PartialEq<Self>`]eq().==rust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[ErrorKind][`PartialEq<Self>`]eq(). rust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[ErrorKind][`PartialEq<Self>`]eq().iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::Interruptedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#Interrupted# => continue, |
| 1286 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e) => return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e), |
| 1287 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(buf) => buf, |
| 1288 | }; |
| 1289 | |
| 1290 | if copied rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().0 { |
| 1291 | break; |
| 1292 | } |
| 1293 | |
| 1294 | b.consumerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/BufRead#consume().(copied); |
| 1295 | |
| 1296 | total_copied rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign().copied as u64; |
| 1297 | } |
| 1298 | |
| 1299 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(total_copied) |
| 1300 | } |
| 1301 | |
| 1302 | type DropHandlerrust-analyzer cargo buffer-redux 1.1.0 DropHandler# = Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<dyn Fnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/Fn#(&mut dyn Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#, &mut Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer#, iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#)>; |
| 1303 | thread_localrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/local/thread_local!!( |
| 1304 | static DROP_ERR_HANDLERrust-analyzer cargo buffer-redux 1.1.0 DROP_ERR_HANDLER.: RefCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/RefCell#<DropHandlerrust-analyzer cargo buffer-redux 1.1.0 DropHandler#> |
| 1305 | = RefCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/RefCell#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]new().(Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(|_, _, _| ())) |
| 1306 | ); |
| 1307 | |
| 1308 | /// Set a thread-local handler for errors thrown in `BufWriter`'s `Drop` impl. |
| 1309 | /// |
| 1310 | /// The `Write` impl, buffer (at the time of the erroring write) and IO error are provided. |
| 1311 | /// |
| 1312 | /// Replaces the previous handler. By default this is a no-op. |
| 1313 | /// |
| 1314 | /// ### Panics |
| 1315 | /// If called from within a handler previously provided to this function. |
| 1316 | pub fn set_drop_err_handlerrust-analyzer cargo buffer-redux 1.1.0 set_drop_err_handler().<F>(handler: F) |
| 1317 | where |
| 1318 | F: 'static + Fnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/Fn#(&mut dyn Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#, &mut Bufferrust-analyzer cargo buffer-redux 1.1.0 Buffer#, iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#), |
| 1319 | { |
| 1320 | DROP_ERR_HANDLERrust-analyzer cargo buffer-redux 1.1.0 DROP_ERR_HANDLER..withrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/local/impl#[`LocalKey<T>`]with().(|deh| *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefMut<'_, T>`][DerefMut]deref_mut().deh.borrow_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]borrow_mut().() = Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(handler)) |
| 1321 | } |