Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit b45641f

Browse files
authored
token-2022: Clarify decode_instruction_data (#3760)
1 parent 8d79d85 commit b45641f

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

token/program-2022/src/instruction.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1759,11 +1759,21 @@ pub fn decode_instruction_type<T: TryFrom<u8>>(input: &[u8]) -> Result<T, Progra
17591759
}
17601760

17611761
/// Utility function for decoding instruction data
1762-
pub fn decode_instruction_data<T: Pod>(input: &[u8]) -> Result<&T, ProgramError> {
1763-
if input.len() != pod_get_packed_len::<T>().saturating_add(1) {
1762+
///
1763+
/// Note: This function expects the entire instruction input, including the
1764+
/// instruction type as the first byte. This makes the code concise and safe
1765+
/// at the expense of clarity, allowing flows such as:
1766+
///
1767+
/// match decode_instruction_type(input)? {
1768+
/// InstructionType::First => {
1769+
/// let FirstData { ... } = decode_instruction_data(input)?;
1770+
/// }
1771+
/// }
1772+
pub fn decode_instruction_data<T: Pod>(input_with_type: &[u8]) -> Result<&T, ProgramError> {
1773+
if input_with_type.len() != pod_get_packed_len::<T>().saturating_add(1) {
17641774
Err(ProgramError::InvalidInstructionData)
17651775
} else {
1766-
pod_from_bytes(&input[1..])
1776+
pod_from_bytes(&input_with_type[1..])
17671777
}
17681778
}
17691779

0 commit comments

Comments
 (0)