pub(crate) fn read(
        &mut self,
        max_length: usize,
        ordered: bool,
    ) -> Result<Option<Chunk>, AssembleError> {
        if self.is_stopped() {
            return Err(AssembleError::UnknownStream);
        } else if ordered && !self.state.is_ordered() {
            return Err(AssembleError::IllegalOrderedRead);
        } else if !ordered && self.state.is_ordered() {
            // Enter unordered mode
            let mut recvd = RangeSet::new();
            recvd.insert(0..self.bytes_read);
            for chunk in &self.data {
                recvd.insert(chunk.offset..chunk.offset + chunk.bytes.len() as u64);
            }
            self.state = State::Unordered { recvd };
        }

        loop {
            let mut chunk = match self.data.peek_mut() {
                Some(chunk) => chunk,
                None => return Ok(None),
            };

            if ordered {
                if chunk.offset > self.bytes_read {
                    // Next chunk is after current read index
                    return Ok(None);
                } else if (chunk.offset + chunk.bytes.len() as u64) <= self.bytes_read {
                    // Next chunk is useless as the read index is beyond its end
                    PeekMut::pop(chunk);
                    self.defragmented = self.defragmented.saturating_sub(1);
                    continue;
                }

                // Determine `start` and `len` of the slice of useful data in chunk
                let start = (self.bytes_read - chunk.offset) as usize;
                if start > 0 {
                    chunk.bytes.advance(start);
                    chunk.offset += start as u64;
                }
            }

            return Ok(Some(if max_length < chunk.bytes.len() {
                self.bytes_read += max_length as u64;
                let offset = chunk.offset;
                chunk.offset += max_length as u64;
                Chunk::new(offset, chunk.bytes.split_to(max_length))
            } else {
                self.bytes_read += chunk.bytes.len() as u64;
                self.defragmented = self.defragmented.saturating_sub(1);
                let chunk = PeekMut::pop(chunk);
                Chunk::new(chunk.offset, chunk.bytes)
            }));
        }
    }
// Get the the next ordered chunk
Get the the next ordered chunk
Get the the next ordered chunk
pub(crate) fn read(
        &mut self,
        max_length: usize,
        ordered: bool,
    ) -> Result<Option<Chunk>, AssembleError> {
        if self.is_stopped() {
            return Err(AssembleError::UnknownStream);
        } else if ordered && !self.state.is_ordered() {
            return Err(AssembleError::IllegalOrderedRead);
        } else if !ordered && self.state.is_ordered() {
            // Enter unordered mode
            let mut recvd = RangeSet::new();
            recvd.insert(0..self.bytes_read);
            for chunk in &self.data {
                recvd.insert(chunk.offset..chunk.offset + chunk.bytes.len() as u64);
            }
            self.state = State::Unordered { recvd };
        }
        loop {
            let mut chunk = match self.data.peek_mut() {
                Some(chunk) => chunk,
                None => return Ok(None),
            };
            if ordered {
                if chunk.offset > self.bytes_read {
                    // Next chunk is after current read index
                    return Ok(None);
                } else if (chunk.offset + chunk.bytes.len() as u64) <= self.bytes_read {
                    // Next chunk is useless as the read index is beyond its end
                    PeekMut::pop(chunk);
                    self.defragmented = self.defragmented.saturating_sub(1);
                    continue;
                }
                // Determine `start` and `len` of the slice of useful data in chunk
                let start = (self.bytes_read - chunk.offset) as usize;
                if start > 0 {
                    chunk.bytes.advance(start);
                    chunk.offset += start as u64;
                }
            }
            return Ok(Some(if max_length < chunk.bytes.len() {
                self.bytes_read += max_length as u64;
                let offset = chunk.offset;
                chunk.offset += max_length as u64;
                Chunk::new(offset, chunk.bytes.split_to(max_length))
            } else {
                self.bytes_read += chunk.bytes.len() as u64;
                self.defragmented = self.defragmented.saturating_sub(1);
                let chunk = PeekMut::pop(chunk);
                Chunk::new(chunk.offset, chunk.bytes)
            }));
        }
    }
pub ( crate ) fn read ( & mut self , max_length : usize , ordered : bool , ) -> Result < Option < Chunk > , AssembleError > { if self . is_stopped ( ) { return Err ( AssembleError :: UnknownStream ) ; } else if ordered && ! self . state . is_ordered ( ) { return Err ( AssembleError :: IllegalOrderedRead ) ; } else if ! ordered && self . state . is_ordered ( ) { // Enter unordered mode let mut recvd = RangeSet :: new ( ) ; recvd . insert ( 0 .. self . bytes_read ) ; for chunk in & self . data { recvd . insert ( chunk . offset .. chunk . offset + chunk . bytes . len ( ) as u64 ) ; } self . state = State :: Unordered { recvd } ; } loop { let mut chunk = match self . data . peek_mut ( ) { Some ( chunk ) => chunk , None => return Ok ( None ) , } ; if ordered { if chunk . offset > self . bytes_read { // Next chunk is after current read index return Ok ( None ) ; } else if ( chunk . offset + chunk . bytes . len ( ) as u64 ) <= self . bytes_read { // Next chunk is useless as the read index is beyond its end PeekMut :: pop ( chunk ) ; self . defragmented = self . defragmented . saturating_sub ( 1 ) ; continue ; } // Determine `start` and `len` of the slice of useful data in chunk let start = ( self . bytes_read - chunk . offset ) as usize ; if start > 0 { chunk . bytes . advance ( start ) ; chunk . offset += start as u64 ; } } return Ok ( Some ( if max_length < chunk . bytes . len ( ) { self . bytes_read += max_length as u64 ; let offset = chunk . offset ; chunk . offset += max_length as u64 ; Chunk :: new ( offset , chunk . bytes . split_to ( max_length ) ) } else { self . bytes_read += chunk . bytes . len ( ) as u64 ; self . defragmented = self . defragmented . saturating_sub ( 1 ) ; let chunk = PeekMut :: pop ( chunk ) ; Chunk :: new ( chunk . offset , chunk . bytes ) } ) ) ; } }
Get the the next ordered chunk
Get the the next ordered chunk
&mut self
max_length
usize
ordered
bool