Skip to content

Instantly share code, notes, and snippets.

@zackw
Created May 24, 2017 13:34
Show Gist options
  • Save zackw/7bb9f12c5509850ccae35a75a2fac791 to your computer and use it in GitHub Desktop.
Save zackw/7bb9f12c5509850ccae35a75a2fac791 to your computer and use it in GitHub Desktop.
I think this works
pub fn deserialize(bytes: &[u8]) -> Result<Self, AlchemistError> {
let key_length = bytestou32(&bytes[0..4]) as usize;
let value_length = bytestou32(&bytes[4..8]) as usize;
let maybe_key = String::from_utf8(bytes[8 .. 8 + key_length].to_vec());
let maybe_value = String::from_utf8(bytes[8 + key_length .. 8 + key_length + value_length].to_vec())
match (maybe_key, maybe_value) {
(Ok(ref key), Ok(ref value)) => Ok(Self::new(key, value)),
_ => Err(AlchemistError::DeserializationFailed)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment