Skip to content

Commit

Permalink
Add get_ref, get_mut and into_components
Browse files Browse the repository at this point in the history
  • Loading branch information
al8n committed Jan 29, 2024
1 parent 7109c68 commit 6be8b3e
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 8 deletions.
12 changes: 9 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# UNRELEASED
# CHANGELOG

# 0.1.2 (January 6th, 2022)
## UNRELEASED

FEATURES
## RELEASED

### 0.2.0 (Jan 30th, 2024)

- Add `get_ref`, `get_mut` and `into_components` APIs

### 0.1.0 (2024)

- `AsyncPeekable` and `Peekable`
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[package]
name = "peekable"
version = "0.1.0"
version = "0.2.0"
edition = "2021"
repository = "https://github.com/al8n/peekable"
homepage = "https://github.com/al8n/peekable"
documentation = "https://docs.rs/peekable"
description = "Peakable reader and async reader"
license = "MIT/Apache-2.0"
license = "MIT or Apache-2.0"
rust-version = "1.56"
keywords = ["peek", "io", "async", "tokio", "futures"]
categories = ["asynchronous", "network-programming", "development-tools"]
Expand All @@ -19,7 +19,6 @@ tokio = ["dep:tokio", "pin-project-lite", "bytes"]
[dependencies]
smallvec = { version = "1", optional = true }

# futures-io = { version = "0.3", optional = true }
futures-util = { version = "=0.3.29", optional = true, features = ["io"] }
pin-project-lite = { version = "0.2", optional = true }

Expand Down
22 changes: 22 additions & 0 deletions src/future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,28 @@ impl<R> AsyncPeekable<R> {
buffer: Buffer::with_capacity(capacity),
}
}

/// 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.**
#[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.**
#[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.
#[inline]
pub fn into_components(self) -> (Buffer, R) {
(self.buffer, self.reader)
}
}

impl<R: AsyncRead + Unpin> AsyncPeekable<R> {
Expand Down
28 changes: 26 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ use std::{
mem,
};

#[doc(hidden)]
#[cfg(feature = "smallvec")]
type Buffer = smallvec::SmallVec<[u8; 64]>;
pub type Buffer = smallvec::SmallVec<[u8; 64]>;

#[doc(hidden)]
#[cfg(not(feature = "smallvec"))]
type Buffer = Vec<u8>;
pub type Buffer = Vec<u8>;

/// Extracts the successful type of a `Poll<T>`.
///
Expand Down Expand Up @@ -124,6 +126,28 @@ impl<R> Peekable<R> {
buffer: Buffer::with_capacity(capacity),
}
}

/// 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.**
#[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.**
#[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.
#[inline]
pub fn into_components(self) -> (Buffer, R) {
(self.buffer, self.reader)
}
}

impl<R: Read> Peekable<R> {
Expand Down
22 changes: 22 additions & 0 deletions src/tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,28 @@ impl<R> AsyncPeekable<R> {
buffer: Buffer::with_capacity(capacity),
}
}

/// 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.**
#[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.**
#[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.
#[inline]
pub fn into_components(self) -> (Buffer, R) {
(self.buffer, self.reader)
}
}

/// An extension trait which adds peek related utility methods to [`AsyncRead`] types
Expand Down

0 comments on commit 6be8b3e

Please sign in to comment.