Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow usage without Seek: RevReader approach #36

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/reading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,9 +528,6 @@ impl BasePacketReader {
}
}

// TODO replace 'a with GAT as soon as they are stabilized
// (https://github.com/rust-lang/rust/issues/44265)

/// A trait for [Read] implementations that allow reversible reads to a buffer.
/// After such a read, the reader's position can be reverted as with [Seek],
/// however only as long as it is still within the bounds of the previous
Expand All @@ -540,9 +537,9 @@ impl BasePacketReader {
/// lifetime.
///
/// Usually, you will not need to implement this yourself. Instead, you can
/// rely either on the blanket implementation provided for all type that
/// implement [Read] and [Seek] or use the [RevReader] to wrap a type that only
/// implements [Read].
/// rely either on the blanket implementation provided for all types that
/// implement [Read] and [Seek] or use the [RevReader] struct to wrap a type
/// that only implements [Read].
pub trait RevRead<'a>: Read {

/// The type of buffer returned by this reversible reader.
Expand Down Expand Up @@ -575,6 +572,8 @@ pub trait RevRead<'a>: Read {
/// # Arguments
///
/// * `amount`: The number of bytes by which to revert the previous read.
est31 marked this conversation as resolved.
Show resolved Hide resolved
/// This may be at most the number of bytes returned by the previous call
est31 marked this conversation as resolved.
Show resolved Hide resolved
/// to [RevRead::rev_read].
///
/// # Errors
///
Expand Down Expand Up @@ -632,6 +631,11 @@ impl<R> RevReader<R> {
buf_idx: 0
}
}
est31 marked this conversation as resolved.
Show resolved Hide resolved

/// Returns the underlying reader wrapped in this instance.
pub fn into_inner(self) -> R {
self.read
}
}

impl<R: Read> Read for RevReader<R> {
Expand All @@ -644,8 +648,7 @@ impl<R: Read> Read for RevReader<R> {
(&mut buf[..read_len]).copy_from_slice(read_data);
self.buf_idx += read_len;
Ok(read_len)
}
else {
} else {
// Invalidate the buffer
self.buf_len = 0;
self.read.read(buf)
Expand Down