revm_bytecode/eof/
decode_helpers.rs

1use super::EofDecodeError;
2
3/// Consumes a single byte from the input slice and returns a tuple containing the remaining input slice
4/// and the consumed byte as a u8.
5///
6/// Returns `EofDecodeError::MissingInput` if the input slice is empty.
7#[inline]
8pub(crate) fn consume_u8(input: &[u8]) -> Result<(&[u8], u8), EofDecodeError> {
9    if input.is_empty() {
10        return Err(EofDecodeError::MissingInput);
11    }
12    Ok((&input[1..], input[0]))
13}
14
15/// Consumes a u16 from the input.
16///
17/// Returns `EofDecodeError::MissingInput` if the input slice is less than 2 bytes.
18#[inline]
19pub(crate) fn consume_u16(input: &[u8]) -> Result<(&[u8], u16), EofDecodeError> {
20    if input.len() < 2 {
21        return Err(EofDecodeError::MissingInput);
22    }
23    let (int_bytes, rest) = input.split_at(2);
24    Ok((rest, u16::from_be_bytes([int_bytes[0], int_bytes[1]])))
25}