solana_sdk/
program_utils.rsuse crate::instruction::InstructionError;
pub fn limited_deserialize<T>(instruction_data: &[u8]) -> Result<T, InstructionError>
where
T: serde::de::DeserializeOwned,
{
solana_program::program_utils::limited_deserialize(
instruction_data,
crate::packet::PACKET_DATA_SIZE as u64,
)
}
#[cfg(test)]
pub mod tests {
use super::*;
#[test]
fn test_limited_deserialize() {
#[derive(Deserialize, Serialize)]
enum Foo {
Bar(Vec<u8>),
}
let item = Foo::Bar([1; crate::packet::PACKET_DATA_SIZE - 12].to_vec()); let serialized = bincode::serialize(&item).unwrap();
assert!(limited_deserialize::<Foo>(&serialized).is_ok());
let item = Foo::Bar([1; crate::packet::PACKET_DATA_SIZE - 11].to_vec()); let serialized = bincode::serialize(&item).unwrap();
assert!(limited_deserialize::<Foo>(&serialized).is_err());
}
}