devol_accounts_kit/instructions_data/
dvl_deserializable_instruction.rs

1use crate::dvl_error::DvlError;
2use crate::errors::ContractError;
3
4pub trait DvlDeserializableInstruction<'a> {
5    fn expected_size() -> usize;
6    fn expected_version() -> u8;
7    #[inline(always)]
8    fn check_version(vec: &'a [u8]) -> Result<(), DvlError> {
9        // Check the second byte for the version number.
10        // The first byte usually contains the command identifier,
11        // and the second byte consistently holds the version number of the instruction format.
12        if vec[1] != Self::expected_version() {
13            return Err(DvlError::new(ContractError::InstructionDataVersion));
14        }
15        Ok(())
16    }
17
18    fn from_vec_le(vec: &'a [u8]) -> Result<&'a Self, DvlError> where Self: Sized {
19        if vec.len() != Self::expected_size() {
20            return Err(DvlError::new(ContractError::InstructionDataLength));
21        }
22        Self::check_version(vec)?;
23        Ok(unsafe { &*(vec.as_ptr() as *const Self) })
24    }
25}