diff --git a/token/program-2022/src/instruction.rs b/token/program-2022/src/instruction.rs index 116b1609..45c68a9d 100644 --- a/token/program-2022/src/instruction.rs +++ b/token/program-2022/src/instruction.rs @@ -1759,11 +1759,21 @@ pub fn decode_instruction_type>(input: &[u8]) -> Result(input: &[u8]) -> Result<&T, ProgramError> { - if input.len() != pod_get_packed_len::().saturating_add(1) { +/// +/// Note: This function expects the entire instruction input, including the +/// instruction type as the first byte. This makes the code concise and safe +/// at the expense of clarity, allowing flows such as: +/// +/// match decode_instruction_type(input)? { +/// InstructionType::First => { +/// let FirstData { ... } = decode_instruction_data(input)?; +/// } +/// } +pub fn decode_instruction_data(input_with_type: &[u8]) -> Result<&T, ProgramError> { + if input_with_type.len() != pod_get_packed_len::().saturating_add(1) { Err(ProgramError::InvalidInstructionData) } else { - pod_from_bytes(&input[1..]) + pod_from_bytes(&input_with_type[1..]) } }