Add comments to ssz decode

This commit is contained in:
Paul Hauner 2018-09-11 13:32:23 +02:00
parent c205208b63
commit dfae0acd06

View File

@ -13,7 +13,10 @@ pub trait Decodable: Sized {
fn ssz_decode(bytes: &[u8]) -> Result<Self, DecodeError>; fn ssz_decode(bytes: &[u8]) -> Result<Self, DecodeError>;
} }
/// Decode the nth element of some ssz list.
///
/// A single ssz encoded value can be considered a list of
/// one element, so this function will work on it too.
pub fn decode_ssz_list_element<T>(ssz_bytes: &[u8], n: usize) pub fn decode_ssz_list_element<T>(ssz_bytes: &[u8], n: usize)
-> Result<T, DecodeError> -> Result<T, DecodeError>
where T: Decodable where T: Decodable
@ -21,6 +24,12 @@ pub fn decode_ssz_list_element<T>(ssz_bytes: &[u8], n: usize)
T::ssz_decode(nth_value(ssz_bytes, n)?) T::ssz_decode(nth_value(ssz_bytes, n)?)
} }
/// Return the nth value in some ssz encoded list.
///
/// The four-byte length prefix is not included in the return.
///
/// A single ssz encoded value can be considered a list of
/// one element, so this function will work on it too.
fn nth_value(ssz_bytes: &[u8], n: usize) fn nth_value(ssz_bytes: &[u8], n: usize)
-> Result<&[u8], DecodeError> -> Result<&[u8], DecodeError>
{ {
@ -42,6 +51,9 @@ fn nth_value(ssz_bytes: &[u8], n: usize)
Err(DecodeError::OutOfBounds) Err(DecodeError::OutOfBounds)
} }
/// Given some number of bytes, interpret the first four
/// bytes as a 32-bit big-endian integer and return the
/// result.
fn decode_length(bytes: &[u8], length_bytes: usize) fn decode_length(bytes: &[u8], length_bytes: usize)
-> Result<usize, DecodeError> -> Result<usize, DecodeError>
{ {