diff --git a/packages/storage-plus/src/de.rs b/packages/storage-plus/src/de.rs index ee6a4ffb6..d65382b86 100644 --- a/packages/storage-plus/src/de.rs +++ b/packages/storage-plus/src/de.rs @@ -121,18 +121,22 @@ impl KeyDeserialize for TimestampKey { } } +fn parse_length(value: &[u8]) -> StdResult { + Ok(u16::from_be_bytes( + value + .try_into() + .map_err(|_| StdError::generic_err("Could not read 2 byte length"))?, + ) + .into()) +} + impl KeyDeserialize for (T, U) { type Output = (T::Output, U::Output); #[inline(always)] fn from_vec(mut value: Vec) -> StdResult { let mut tu = value.split_off(2); - let t_len = u16::from_be_bytes( - value - .as_slice() - .try_into() - .map_err(|err: TryFromSliceError| StdError::generic_err(err.to_string()))?, - ) as usize; + let t_len = parse_length(&value)?; let u = tu.split_off(t_len); Ok((T::from_vec(tu)?, U::from_vec(u)?)) @@ -145,21 +149,11 @@ impl KeyDeserialize for #[inline(always)] fn from_vec(mut value: Vec) -> StdResult { let mut tuv = value.split_off(2); - let t_len = u16::from_be_bytes( - value - .as_slice() - .try_into() - .map_err(|err: TryFromSliceError| StdError::generic_err(err.to_string()))?, - ) as usize; + let t_len = parse_length(&value)?; let mut len_uv = tuv.split_off(t_len); let mut uv = len_uv.split_off(2); - let u_len = u16::from_be_bytes( - len_uv - .as_slice() - .try_into() - .map_err(|err: TryFromSliceError| StdError::generic_err(err.to_string()))?, - ) as usize; + let u_len = parse_length(&len_uv)?; let v = uv.split_off(u_len); Ok((T::from_vec(tuv)?, U::from_vec(uv)?, V::from_vec(v)?))