Repository: tokio-rs/tokio
File: tokio-util/src/codec/length_delimited.rs (Rust)

Code before:

    fn get_num_skip(&self) -> usize {
        self.num_skip
            .unwrap_or(self.length_field_offset + self.length_field_len)
    }

    fn adjust_max_frame_len(&mut self) {
        // This function is basically `std::u64::saturating_add_signed`. Since it
        // requires MSRV 1.66, its implementation is copied here.
        //
        // TODO: use the method from std when MSRV becomes >= 1.66
        fn saturating_add_signed(num: u64, rhs: i64) -> u64 {
            let (res, overflow) = num.overflowing_add(rhs as u64);
            if overflow == (rhs < 0) {
                res
            } else if overflow {
                u64::MAX
            } else {
                0
            }
        }

        // Calculate the maximum number that can be represented using `length_field_len` bytes.
        let max_number = 2u64.saturating_pow((8 * self.length_field_len - 1) as u32);
        // In order to prevent an overflow, we do the last part manually.
        let max_number = max_number + (max_number - 1);

        let max_allowed_len = saturating_add_signed(max_number, self.length_adjustment as i64);

        if self.max_frame_len as u64 > max_allowed_len {
            self.max_frame_len = usize::try_from(max_allowed_len).unwrap_or(usize::MAX);
        }
    }
}

impl Default for Builder {
    fn default() -> Self {
        Self::new()
    }
}

// ===== impl LengthDelimitedCodecError =====

impl fmt::Debug for LengthDelimitedCodecError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("LengthDelimitedCodecError").finish()
    }
}

impl fmt::Display for LengthDelimitedCodecError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

Review comment (performance):
I would expect that `saturating_pow` is rather expensive compared to a shift.
```suggestion
        let max_number = match 1.checked_shl(8 * self.length_field_len) {
            Some(shl) => shl - 1,
            None => u64::MAX,
        };
```

Code after:

    fn get_num_skip(&self) -> usize {
        self.num_skip
            .unwrap_or(self.length_field_offset + self.length_field_len)
    }

    fn adjust_max_frame_len(&mut self) {
        // This function is basically `std::u64::saturating_add_signed`. Since it
        // requires MSRV 1.66, its implementation is copied here.
        //
        // TODO: use the method from std when MSRV becomes >= 1.66
        fn saturating_add_signed(num: u64, rhs: i64) -> u64 {
            let (res, overflow) = num.overflowing_add(rhs as u64);
            if overflow == (rhs < 0) {
                res
            } else if overflow {
                u64::MAX
            } else {
                0
            }
        }

        // Calculate the maximum number that can be represented using `length_field_len` bytes.
        let max_number = match 1u64.checked_shl((8 * self.length_field_len) as u32) {
            Some(shl) => shl - 1,
            None => u64::MAX,
        };

        let max_allowed_len = saturating_add_signed(max_number, self.length_adjustment as i64);

        if self.max_frame_len as u64 > max_allowed_len {
            self.max_frame_len = usize::try_from(max_allowed_len).unwrap_or(usize::MAX);
        }
    }
}

impl Default for Builder {
    fn default() -> Self {
        Self::new()
    }
}

// ===== impl LengthDelimitedCodecError =====

impl fmt::Debug for LengthDelimitedCodecError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("LengthDelimitedCodecError").finish()
    }
}

impl fmt::Display for LengthDelimitedCodecError {