fn shift_buffer_left()

in src/connection.rs [508:539]


    fn shift_buffer_left(
        &mut self,
        line_start_index: usize,
        end_cursor: usize,
    ) -> Result<(), RequestError> {
        if end_cursor > self.buffer.len() {
            return Err(RequestError::Overflow);
        }
        // We don't want to shift something that is already at the beginning.
        let delta_bytes = end_cursor
            .checked_sub(line_start_index)
            .ok_or(RequestError::Underflow)?;
        if line_start_index != 0 {
            // Move the bytes from `line_start_index` to the beginning of the buffer.
            for cursor in 0..delta_bytes {
                // The unchecked addition is safe, guaranteed by the result of the substraction
                // above.
                // The slice access is safe, as `line_start_index + cursor` is <= `end_cursor`,
                // checked at the start of the function.
                self.buffer[cursor] = self.buffer[line_start_index + cursor];
            }

            // Clear the rest of the buffer.
            for cursor in delta_bytes..end_cursor {
                self.buffer[cursor] = 0;
            }
        }

        // Update `read_cursor`.
        self.read_cursor = delta_bytes;
        Ok(())
    }