fn defragment(&mut self) {
        let buffered = self.data.iter().map(|c| c.bytes.len()).sum::<usize>();
        let mut buffer = BytesMut::with_capacity(buffered);
        let mut offset = self
            .data
            .peek()
            .as_ref()
            .expect("defragment is only called when data is buffered")
            .offset;

        let new = BinaryHeap::with_capacity(self.data.len());
        let old = mem::replace(&mut self.data, new);
        for chunk in old.into_sorted_vec().into_iter().rev() {
            let end = offset + (buffer.len() as u64);
            if let Some(overlap) = end.checked_sub(chunk.offset) {
                if let Some(bytes) = chunk.bytes.get(overlap as usize..) {
                    buffer.extend_from_slice(bytes);
                }
            } else {
                let bytes = buffer.split().freeze();
                self.data.push(Chunk { offset, bytes });
                offset = chunk.offset;
                buffer.extend_from_slice(&chunk.bytes);
            }
        }

        let bytes = buffer.split().freeze();
        self.data.push(Chunk { offset, bytes });
        self.defragmented = self.data.len();
    }
// Copy the buffered chunk data to new chunks backed by a single buffer to
// make sure we're not unnecessarily holding on to many larger allocations.
// Merge contiguous chunks in the process of doing so. Reset the `defragmented`
// counter to the new number of chunks left in the heap so that we can decide
// when to defragment the queue again if necessary.
Copy the buffered chunk data to new chunks backed by a single buffer to
make sure we're not unnecessarily holding on to many larger allocations.
Merge contiguous chunks in the process of doing so. Reset the `defragmented`
counter to the new number of chunks left in the heap so that we can decide
when to defragment the queue again if necessary.
Copy the buffered chunk data to new chunks backed by a single buffer to make sure we ' re not unnecessarily holding on to many larger allocations . Merge contiguous chunks in the process of doing so . Reset the ` defragmented ` counter to the new number of chunks left in the heap so that we can decide when to defragment the queue again if necessary .
fn defragment(&mut self) {
        let buffered = self.data.iter().map(|c| c.bytes.len()).sum::<usize>();
        let mut buffer = BytesMut::with_capacity(buffered);
        let mut offset = self
            .data
            .peek()
            .as_ref()
            .expect("defragment is only called when data is buffered")
            .offset;
        let new = BinaryHeap::with_capacity(self.data.len());
        let old = mem::replace(&mut self.data, new);
        for chunk in old.into_sorted_vec().into_iter().rev() {
            let end = offset + (buffer.len() as u64);
            if let Some(overlap) = end.checked_sub(chunk.offset) {
                if let Some(bytes) = chunk.bytes.get(overlap as usize..) {
                    buffer.extend_from_slice(bytes);
                }
            } else {
                let bytes = buffer.split().freeze();
                self.data.push(Chunk { offset, bytes });
                offset = chunk.offset;
                buffer.extend_from_slice(&chunk.bytes);
            }
        }
        let bytes = buffer.split().freeze();
        self.data.push(Chunk { offset, bytes });
        self.defragmented = self.data.len();
    }
fn defragment ( & mut self ) { let buffered = self . data . iter ( ) . map ( | c | c . bytes . len ( ) ) . sum :: < usize > ( ) ; let mut buffer = BytesMut :: with_capacity ( buffered ) ; let mut offset = self . data . peek ( ) . as_ref ( ) . expect ( " " ) . offset ; let new = BinaryHeap :: with_capacity ( self . data . len ( ) ) ; let old = mem :: replace ( & mut self . data , new ) ; for chunk in old . into_sorted_vec ( ) . into_iter ( ) . rev ( ) { let end = offset + ( buffer . len ( ) as u64 ) ; if let Some ( overlap ) = end . checked_sub ( chunk . offset ) { if let Some ( bytes ) = chunk . bytes . get ( overlap as usize .. ) { buffer . extend_from_slice ( bytes ) ; } } else { let bytes = buffer . split ( ) . freeze ( ) ; self . data . push ( Chunk { offset , bytes } ) ; offset = chunk . offset ; buffer . extend_from_slice ( & chunk . bytes ) ; } } let bytes = buffer . split ( ) . freeze ( ) ; self . data . push ( Chunk { offset , bytes } ) ; self . defragmented = self . data . len ( ) ; }
Copy the buffered chunk data to new chunks backed by a single buffer to
make sure we're not unnecessarily holding on to many larger allocations.
Copy the buffered chunk data to new chunks backed by a single buffer to make sure we ' re not unnecessarily holding on to many larger allocations .
&mut self