Skip to content

Commit

Permalink
feature: add read_size async fn to get the size
Browse files Browse the repository at this point in the history
  • Loading branch information
rklaehn committed Feb 8, 2023
1 parent 2329a8f commit 009f74e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,15 @@ mod tokio_io {
}

impl<T: AsyncRead + Unpin> SliceDecoderInner<T> {

async fn read_size(&mut self) -> io::Result<u64> {
if let NextRead::Header = self.shared.state.read_next() {
// call poll_input until we have a chunk
std::future::poll_fn(|cx| self.poll_input(cx)).await?;
}
Ok(self.shared.state.parser.content_len().unwrap())
}

fn poll_input(&mut self, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
// If we haven't done the initial seek yet, do the full seek loop
// first. Note that this will never leave any buffered output. The only
Expand Down Expand Up @@ -819,6 +828,14 @@ mod tokio_io {
Self(SliceDecoderState::Reading(Box::new(state)))
}

pub async fn read_size(&mut self) -> io::Result<u64> {
match &mut self.0 {
SliceDecoderState::Reading(state) => state.read_size().await,
SliceDecoderState::Output(state) => state.read_size().await,
SliceDecoderState::Taken => unreachable!(),
}
}

pub fn into_inner(self) -> T {
match self.0 {
SliceDecoderState::Reading(state) => state.shared.input,
Expand Down
4 changes: 4 additions & 0 deletions src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,10 @@ impl ParseState {
self.content_position
}

pub fn content_len(&self) -> Option<u64> {
self.content_len
}

fn at_root(&self) -> bool {
self.content_position < CHUNK_SIZE as u64 && self.stack_depth == 1
}
Expand Down

0 comments on commit 009f74e

Please sign in to comment.