1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
//! Peakable peeker and async peeker
//!
//!
#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(docsrs, allow(unused_attributes))]
#![deny(missing_docs)]
use std::{
cmp,
io::{IoSliceMut, Read, Result},
mem,
};
#[doc(hidden)]
#[cfg(feature = "smallvec")]
pub type Buffer = smallvec::SmallVec<[u8; 64]>;
#[doc(hidden)]
#[cfg(not(feature = "smallvec"))]
pub type Buffer = Vec<u8>;
/// Extracts the successful type of a `Poll<T>`.
///
/// This macro bakes in propagation of `Pending` signals by returning early.
#[cfg(any(feature = "tokio", feature = "future"))]
#[macro_export]
macro_rules! ready {
($e:expr $(,)?) => {
match $e {
::std::task::Poll::Ready(t) => t,
::std::task::Poll::Pending => return ::std::task::Poll::Pending,
}
};
}
/// Asynchronous peek I/O
///
/// This crate contains the `AsyncPeek` and `AsyncPeekExt`
/// traits, the asynchronous analogs to
/// `peekable::{Peek, Peekable}`. The primary difference is
/// that these traits integrate with the asynchronous task system.
///
/// All items of this library are only available when the `future` feature of this
/// library is activated, and it is not activated by default.
#[cfg(feature = "future")]
#[cfg_attr(docsrs, doc(cfg(feature = "future")))]
pub mod future;
/// Traits, helpers, and type definitions for asynchronous peekable I/O functionality.
///
/// This module is the asynchronous version of `peekable::{Peek, Peekable}`. Primarily, it
/// defines one trait, [`AsyncPeek`], which is asynchronous
/// version of the [`Peek`] trait.
#[cfg(feature = "tokio")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
pub mod tokio;
/// A wrapper around an [`Read`] types to make them support peek related methods.
pub struct Peekable<R> {
/// The inner reader.
reader: R,
/// The buffer used to store peeked bytes.
buffer: Buffer,
}
impl<R: Read> Read for Peekable<R> {
fn read(&mut self, mut buf: &mut [u8]) -> Result<usize> {
let this = self;
let want_peek = buf.len();
// check if the peek buffer has data
let buffer_len = this.buffer.len();
if buffer_len > 0 {
return match want_peek.cmp(&buffer_len) {
cmp::Ordering::Less => {
buf.copy_from_slice(&this.buffer[..want_peek]);
this.buffer.drain(..want_peek);
return Ok(want_peek);
}
cmp::Ordering::Equal => {
buf.copy_from_slice(&this.buffer);
this.buffer.clear();
return Ok(want_peek);
}
cmp::Ordering::Greater => {
buf[..buffer_len].copy_from_slice(&this.buffer);
buf = &mut buf[buffer_len..];
match this.reader.read(buf) {
Ok(bytes) => {
this.buffer.clear();
Ok(bytes + buffer_len)
}
Err(e) => Err(e),
}
}
};
}
this.reader.read(buf)
}
}
impl<R> From<R> for Peekable<R> {
fn from(reader: R) -> Self {
Peekable {
reader,
buffer: Buffer::new(),
}
}
}
impl<R> Peekable<R> {
/// Creates a new peekable wrapper around the given reader.
///
/// # Examples
///
/// ```rust
/// use std::io::Cursor;
///
/// let peekable = peekable::Peekable::new(Cursor::new([1, 2, 3, 4]));
/// ```
pub fn new(reader: R) -> Self {
Self {
reader,
buffer: Buffer::new(),
}
}
/// Creates a new peekable wrapper around the given reader with the specified
/// capacity for the peek buffer.
///
/// # Examples
///
/// ```rust
/// use std::io::Cursor;
///
/// let peekable = peekable::Peekable::with_capacity(Cursor::new([0; 1024]), 1024);
/// ```
pub fn with_capacity(reader: R, capacity: usize) -> Self {
Self {
reader,
buffer: Buffer::with_capacity(capacity),
}
}
/// Clears the peek buffer.
///
/// # Examples
///
/// ```rust
/// use std::io::Cursor;
///
/// let mut peekable = peekable::Peekable::from(Cursor::new([1, 2, 3, 4]));
///
/// let mut output = [0u8; 2];
/// let bytes = peekable.peek(&mut output).unwrap();
/// assert_eq!(bytes, 2);
/// assert_eq!(output, [1, 2]);
///
/// let consumed = peekable.consume();
/// assert_eq!(consumed.as_slice(), [1, 2].as_slice());
///
/// let mut output = [0u8; 2];
/// let bytes = peekable.peek(&mut output).unwrap();
/// assert_eq!(bytes, 2);
/// assert_eq!(output, [3, 4]);
/// ```
#[inline]
pub fn consume(&mut self) -> Buffer {
mem::take(&mut self.buffer)
}
/// Consumes the peek buffer in place so that the peek buffer can be reused and avoid allocating.
///
/// # Examples
///
/// ```rust
/// use std::io::Cursor;
///
/// let mut peekable = peekable::Peekable::from(Cursor::new([1, 2, 3, 4]));
///
/// let mut output = [0u8; 2];
/// let bytes = peekable.peek(&mut output).unwrap();
/// assert_eq!(bytes, 2);
/// assert_eq!(output, [1, 2]);
///
/// peekable.consume_in_place();
///
/// let mut output = [0u8; 2];
/// let bytes = peekable.peek(&mut output).unwrap();
/// assert_eq!(bytes, 2);
/// assert_eq!(output, [3, 4]);
/// ```
#[inline]
pub fn consume_in_place(&mut self) {
self.buffer.clear();
}
/// Returns the bytes already be peeked into memory and a mutable reference to the underlying reader.
///
/// **WARNING: If you invoke `AsyncRead` or `AsyncReadExt` methods on the underlying reader, may lead to unexpected read behaviors.**
///
/// # Examples
///
/// ```rust
/// use std::io::Cursor;
///
/// let mut peekable = peekable::Peekable::new(Cursor::new([1, 2, 3, 4]));
///
/// let mut output = [0u8; 2];
/// let bytes = peekable.peek(&mut output).unwrap();
/// assert_eq!(bytes, 2);
/// assert_eq!(output, [1, 2]);
///
/// let (peeked, reader) = peekable.get_mut();
/// assert_eq!(peeked, [1, 2]);
/// ```
#[inline]
pub fn get_mut(&mut self) -> (&[u8], &mut R) {
(&self.buffer, &mut self.reader)
}
/// Returns the bytes already be peeked into memory and a reference to the underlying reader.
///
/// **WARNING: If you invoke `AsyncRead` or `AsyncReadExt` methods on the underlying reader, may lead to unexpected read behaviors.**
///
/// # Examples
///
/// ```rust
/// use std::io::Cursor;
///
/// let mut peekable = peekable::Peekable::new(Cursor::new([1, 2, 3, 4]));
///
/// let mut output = [0u8; 2];
/// let bytes = peekable.peek(&mut output).unwrap();
/// assert_eq!(bytes, 2);
/// assert_eq!(output, [1, 2]);
///
/// let (peeked, reader) = peekable.get_ref();
/// assert_eq!(peeked, [1, 2]);
/// ```
#[inline]
pub fn get_ref(&self) -> (&[u8], &R) {
(&self.buffer, &self.reader)
}
/// Consumes the `AsyncPeekable`, returning the a vec may contain the bytes already be peeked into memory and the wrapped reader.
///
///
/// # Examples
///
/// ```rust
/// use std::io::Cursor;
///
/// let mut peekable = peekable::Peekable::new(Cursor::new([1, 2, 3, 4]));
///
/// let mut output = [0u8; 2];
///
/// let bytes = peekable.peek(&mut output).unwrap();
/// assert_eq!(bytes, 2);
/// assert_eq!(output, [1, 2]);
///
/// let (peeked, reader) = peekable.into_components();
///
/// assert_eq!(peeked.as_slice(), [1, 2].as_slice());
/// ```
#[inline]
pub fn into_components(self) -> (Buffer, R) {
(self.buffer, self.reader)
}
}
impl<R: Read> Peekable<R> {
/// Pull some bytes from this source into the specified buffer, returning
/// how many bytes were peeked.
///
/// This function does not provide any guarantees about whether it blocks
/// waiting for data, but if an object needs to block for a peek and cannot,
/// it will typically signal this via an [`Err`] return value.
///
/// If the return value of this method is [`Ok(n)`], then implementations must
/// guarantee that `0 <= n <= buf.len()`. A nonzero `n` value indicates
/// that the buffer `buf` has been filled in with `n` bytes of data from this
/// source. If `n` is `0`, then it can indicate one of two scenarios:
///
/// 1. This peeker has reached its "end of file" and will likely no longer
/// be able to produce bytes. Note that this does not mean that the
/// peeker will *always* no longer be able to produce bytes. As an example,
/// on Linux, this method will call the `recv` syscall for a [`TcpStream`],
/// where returning zero indicates the connection was shut down correctly. While
/// for [`File`], it is possible to reach the end of file and get zero as result,
/// but if more data is appended to the file, future calls to `peek` will return
/// more data.
/// 2. The buffer specified was 0 bytes in length.
///
/// It is not an error if the returned value `n` is smaller than the buffer size,
/// even when the peeker is not at the end of the stream yet.
/// This may happen for example because fewer bytes are actually available right now
/// (e. g. being close to end-of-file) or because peek() was interrupted by a signal.
///
/// As this trait is safe to implement, callers in unsafe code cannot rely on
/// `n <= buf.len()` for safety.
/// Extra care needs to be taken when `unsafe` functions are used to access the peek bytes.
/// Callers have to ensure that no unchecked out-of-bounds accesses are possible even if
/// `n > buf.len()`.
///
/// No guarantees are provided about the contents of `buf` when this
/// function is called, so implementations cannot rely on any property of the
/// contents of `buf` being true. It is recommended that *implementations*
/// only write data to `buf` instead of peeking its contents.
///
/// Correspondingly, however, *callers* of this method in unsafe code must not assume
/// any guarantees about how the implementation uses `buf`. The trait is safe to implement,
/// so it is possible that the code that's supposed to write to the buffer might also peek
/// from it. It is your responsibility to make sure that `buf` is initialized
/// before calling `peek`. Calling `peek` with an uninitialized `buf` (of the kind one
/// obtains via [`MaybeUninit<T>`]) is not safe, and can lead to undefined behavior.
///
/// [`MaybeUninit<T>`]: crate::mem::MaybeUninit
///
/// # Errors
///
/// If this function encounters any form of I/O or other error, an error
/// variant will be returned. If an error is returned then it must be
/// guaranteed that no bytes were peek.
///
/// An error of the [`ErrorKind::Interrupted`] kind is non-fatal and the peek
/// operation should be retried if there is nothing else to do.
///
/// # Examples
///
///
/// [`Ok(n)`]: Ok
/// [`File`]: std::fs::File
/// [`TcpStream`]: std::net::TcpStream
///
/// ```rust
/// use std::io;
/// use std::io::{Cursor, Read};
/// use peekable::PeekExt;
///
/// # fn main() -> io::Result<()> {
///
/// let mut peekable = Cursor::new([1, 2, 3, 4]).peekable();
/// let mut output = [0u8; 5];
///
/// let bytes = peekable.peek(&mut output[..3])?;
///
/// // This is only guaranteed to be 4 because `&[u8]` is a synchronous
/// // reader. In a real system you could get anywhere from 1 to
/// // `output.len()` bytes in a single read.
/// assert_eq!(bytes, 3);
/// assert_eq!(output, [1, 2, 3, 0, 0]);
///
/// // you can peek mutiple times
///
/// let bytes = peekable.peek(&mut output[..])?;
/// assert_eq!(bytes, 4);
/// assert_eq!(output, [1, 2, 3, 4, 0]);
///
/// // you can read after peek
/// let mut output = [0u8; 5];
/// let bytes = peekable.read(&mut output[..2])?;
/// assert_eq!(bytes, 2);
/// assert_eq!(output, [1, 2, 0, 0, 0]);
///
/// // peek after read
/// let mut output = [0u8; 5];
/// let bytes = peekable.peek(&mut output[..])?;
/// assert_eq!(bytes, 2);
/// assert_eq!(output, [3, 4, 0, 0, 0]);
/// # Ok(())
/// # }
/// ```
pub fn peek(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
let want_peek = buf.len();
// check if the peek buffer has data
let buffer_len = self.buffer.len();
if buffer_len > 0 {
return match want_peek.cmp(&buffer_len) {
cmp::Ordering::Less => {
buf.copy_from_slice(&self.buffer[..want_peek]);
Ok(want_peek)
}
cmp::Ordering::Equal => {
buf.copy_from_slice(&self.buffer);
Ok(want_peek)
}
cmp::Ordering::Greater => {
let this = self;
this.buffer.resize(want_peek, 0);
match this.reader.read(&mut this.buffer[buffer_len..]) {
Ok(n) => {
this.buffer.truncate(n + buffer_len);
buf[..buffer_len + n].copy_from_slice(&this.buffer);
Ok(buffer_len + n)
}
Err(e) => Err(e),
}
}
};
}
let this = self;
match this.reader.read(buf) {
Ok(bytes) => {
this.buffer.extend_from_slice(&buf[..bytes]);
Ok(bytes)
}
Err(e) => Err(e),
}
}
/// Like `peek`, except that it peeks into a slice of buffers.
///
/// Data is copied to fill each buffer in order, with the final buffer
/// written to possibly being only partially filled. This method must
/// behave equivalently to a single call to `peek` with concatenated
/// buffers.
///
/// The default implementation calls `peek` with either the first nonempty
/// buffer provided, or an empty one if none exists.
pub fn peek_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize> {
for b in bufs {
if !b.is_empty() {
return self.peek(b);
}
}
self.peek(&mut [])
}
/// Peek all bytes until EOF in this source, placing them into `buf`.
///
/// All bytes peek from this source will be appended to the specified buffer
/// `buf`. This function will continuously call [`peek()`] to append more data to
/// `buf` until [`peek()`] returns either [`Ok(0)`] or an error of
/// non-[`ErrorKind::Interrupted`] kind.
///
/// If successful, this function will return the total number of bytes peek.
///
/// # Errors
///
/// If this function encounters an error of the kind
/// [`ErrorKind::Interrupted`] then the error is ignored and the operation
/// will continue.
///
/// If any other peek error is encountered then this function immediately
/// returns. Any bytes which have alpeeky been peek will be appended to
/// `buf`.
///
/// # Examples
///
/// [`peek()`]: Peekable::peek
/// [`Ok(0)`]: Ok
///
/// ```rust
/// use std::io;
/// use std::io::{Cursor, Read};
/// use peekable::PeekExt;
///
/// # fn main() -> io::Result<()> {
/// let mut peekable = Cursor::new([1, 2, 3, 4]).peekable();
/// let mut output = Vec::with_capacity(4);
///
/// let bytes = peekable.peek_to_end(&mut output)?;
///
/// assert_eq!(bytes, 4);
/// assert_eq!(output, vec![1, 2, 3, 4]);
///
/// // read after peek
/// let mut output = Vec::with_capacity(4);
///
/// let bytes = peekable.read_to_end(&mut output)?;
///
/// assert_eq!(bytes, 4);
/// assert_eq!(output, vec![1, 2, 3, 4]);
/// # Ok(())
/// # }
/// ```
pub fn peek_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize> {
let this = &mut *self;
let inbuf = this.buffer.len();
let original_buf = buf.len();
buf.extend_from_slice(&this.buffer);
let fut = this.reader.read_to_end(buf);
match fut {
Ok(read) => {
this.buffer.extend_from_slice(&buf[original_buf + inbuf..]);
Ok(read + inbuf)
}
Err(e) => Err(e),
}
}
/// Peek all bytes until EOF in this source, appending them to `buf`.
///
/// If successful, this function returns the number of bytes which were peek
/// and appended to `buf`.
///
/// # Errors
///
/// If the data in this stream is *not* valid UTF-8 then an error is
/// returned and `buf` is unchanged.
///
/// See [`peek_to_end`] for other error semantics.
///
/// [`peek_to_end`]: Peek::peek_to_end
///
/// # Examples
///
///
/// ```rust
/// use std::io;
/// use std::io::{Cursor, Read};
/// use peekable::PeekExt;
///
/// # fn main() -> io::Result<()> {
///
/// let mut peekable = Cursor::new(&b"1234"[..]).peekable();
/// let mut buffer = String::with_capacity(4);
///
/// let bytes = peekable.peek_to_string(&mut buffer)?;
///
/// assert_eq!(bytes, 4);
/// assert_eq!(buffer, String::from("1234"));
///
/// // read after peek
/// let mut buffer = String::with_capacity(4);
/// let bytes = peekable.peek_to_string(&mut buffer)?;
///
/// assert_eq!(bytes, 4);
/// assert_eq!(buffer, String::from("1234"));
///
/// // peek invalid utf-8
/// let mut peekable = Cursor::new([255; 4]).peekable();
/// let mut buffer = String::with_capacity(4);
/// assert!(peekable.peek_to_string(&mut buffer).is_err());
/// # Ok(())
/// # }
/// ```
pub fn peek_to_string(&mut self, buf: &mut String) -> Result<usize> {
let s = match core::str::from_utf8(&self.buffer) {
Ok(s) => s,
Err(_) => {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
"stream did not contain valid UTF-8",
))
}
};
buf.push_str(s);
let inbuf = self.buffer.len();
let fut = self.reader.read_to_string(buf);
match fut {
Ok(read) => {
self.buffer.extend_from_slice(&buf.as_bytes()[inbuf..]);
Ok(read + inbuf)
}
Err(e) => Err(e),
}
}
/// Peek the exact number of bytes required to fill `buf`.
///
/// This function peeks as many bytes as necessary to completely fill the
/// specified buffer `buf`.
///
/// No guarantees are provided about the contents of `buf` when this
/// function is called, so implementations cannot rely on any property of the
/// contents of `buf` being true. It is recommended that implementations
/// only write data to `buf` instead of peeking its contents. The
/// documentation on [`peek`] has a more detailed explanation on this
/// subject.
///
/// # Errors
///
/// If this function encounters an error of the kind
/// [`ErrorKind::Interrupted`](std::io::ErrorKind::Interrupted) then the error is ignored and the operation
/// will continue.
///
/// If this function encounters an "end of file" before completely filling
/// the buffer, it returns an error of the kind [`ErrorKind::UnexpectedEof`](std::io::ErrorKind::Interrupted).
/// The contents of `buf` are unspecified in this case.
///
/// If any other peek error is encountered then this function immediately
/// returns. The contents of `buf` are unspecified in this case.
///
/// If this function returns an error, it is unspecified how many bytes it
/// has peek, but it will never peek more than would be necessary to
/// completely fill the buffer.
///
/// # Examples
///
/// ```rust
/// use std::io;
/// use std::io::{Cursor, Read};
/// use peekable::PeekExt;
///
/// # fn main() -> io::Result<()> {
///
/// let mut peekable = Cursor::new([1, 2, 3, 4]).peekable();
/// let mut output = [0u8; 4];
///
/// peekable.peek_exact(&mut output)?;
///
/// assert_eq!(output, [1, 2, 3, 4]);
///
/// // read after peek
/// let mut output = [0u8; 2];
///
/// peekable.read_exact(&mut output[..])?;
///
/// assert_eq!(output, [1, 2]);
///
/// // peek after read
/// let mut output = [0u8; 2];
/// peekable.peek_exact(&mut output)?;
///
/// assert_eq!(output, [3, 4]);
/// # Ok(())
/// # }
/// ```
///
/// ## EOF is hit before `buf` is filled
///
/// ```
/// use std::io;
/// use std::io::{Cursor, Read};
/// use peekable::PeekExt;
///
/// # fn main() -> io::Result<()> {
///
/// let mut peekable = Cursor::new([1, 2, 3, 4]).peekable();
/// let mut output = [0u8; 5];
///
/// let result = peekable.peek_exact(&mut output);
/// assert_eq!(
/// result.unwrap_err().kind(),
/// std::io::ErrorKind::UnexpectedEof
/// );
///
/// let result = peekable.peek_exact(&mut output[..4]);
/// assert!(result.is_ok());
/// assert_eq!(output, [1, 2, 3, 4, 0]);
///
/// # Ok(())
/// # }
/// ```
pub fn peek_exact(&mut self, mut buf: &mut [u8]) -> Result<()> {
let this = self;
let buf_len = buf.len();
let peek_buf_len = this.buffer.len();
if buf_len <= peek_buf_len {
buf.copy_from_slice(&this.buffer[..buf_len]);
return Ok(());
}
buf[..peek_buf_len].copy_from_slice(&this.buffer);
{
let (_read, rest) = mem::take(&mut buf).split_at_mut(peek_buf_len);
buf = rest;
}
let mut readed = peek_buf_len;
while !buf.is_empty() {
let n = this.reader.read(buf)?;
{
let (read, rest) = mem::take(&mut buf).split_at_mut(n);
this.buffer.extend_from_slice(read);
readed += n;
buf = rest;
}
if n == 0 && readed != buf_len {
return Err(std::io::ErrorKind::UnexpectedEof.into());
}
}
Ok(())
}
/// Try to fill the peek buffer with more data. Returns the number of bytes peeked.
///
/// # Examples
///
/// ```rust
/// use std::io;
/// use std::io::{Cursor, Read};
/// use peekable::PeekExt;
///
/// let mut peekable = Cursor::new([1, 2, 3, 4]).peekable_with_capacity(5);
/// let mut output = [0u8; 4];
///
/// peekable.peek_exact(&mut output[..1]).unwrap();
/// assert_eq!(output, [1, 0, 0, 0]);
///
/// let bytes = peekable.fill_peek_buf().unwrap();
/// assert_eq!(bytes, 3);
///
/// let bytes = peekable.peek(&mut output).unwrap();
/// assert_eq!(output, [1, 2, 3, 4].as_slice());
/// ````
pub fn fill_peek_buf(&mut self) -> Result<usize> {
let cap = self.buffer.capacity();
let cur = self.buffer.len();
self.buffer.resize(cap, 0);
let peeked = self.reader.read(&mut self.buffer[cur..])?;
self.buffer.truncate(cur + peeked);
Ok(peeked)
}
}
/// An extension trait which adds utility methods to [`Read`] types.
pub trait PeekExt: Read {
/// Wraps a [`Read`] type in a `Peekable` which provides a `peek` related methods.
fn peekable(self) -> Peekable<Self>
where
Self: Sized,
{
Peekable {
reader: self,
buffer: Buffer::new(),
}
}
/// Wraps a [`Read`] type in a `Peekable` which provides a `peek` related methods with a specified capacity.
fn peekable_with_capacity(self, capacity: usize) -> Peekable<Self>
where
Self: Sized,
{
Peekable {
reader: self,
buffer: Buffer::with_capacity(capacity),
}
}
}
impl<R: Read + ?Sized> PeekExt for R {}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Cursor;
#[test]
fn test_peek_exact_peek_exact_read_exact() {
let mut peekable = Cursor::new([1, 2, 3, 4, 5, 6, 7, 8, 9]).peekable();
let mut buf1 = [0; 2];
peekable.peek_exact(&mut buf1).unwrap();
assert_eq!(buf1, [1, 2]);
let mut buf2 = [0; 4];
peekable.peek_exact(&mut buf2).unwrap();
assert_eq!(buf2, [1, 2, 3, 4]);
let mut buf3 = [0; 4];
peekable.read_exact(&mut buf3).unwrap();
assert_eq!(buf3, [1, 2, 3, 4]);
}
}