mirror of
https://github.com/sigp/lighthouse.git
synced 2026-04-18 05:18:30 +00:00
Add decode_ssz function for general deserialization
This commit is contained in:
@@ -10,7 +10,21 @@ pub enum DecodeError {
|
||||
}
|
||||
|
||||
pub trait Decodable: Sized {
|
||||
fn ssz_decode(bytes: &[u8]) -> Result<Self, DecodeError>;
|
||||
fn ssz_decode(bytes: &[u8], index: usize) -> Result<Self, DecodeError>;
|
||||
}
|
||||
|
||||
/// Decode the given bytes for the given type
|
||||
///
|
||||
/// The single ssz encoded value will be decoded as the given type at the
|
||||
/// given index.
|
||||
pub fn decode_ssz<T>(ssz_bytes: &[u8], index: usize)
|
||||
-> Result<T, DecodeError>
|
||||
where T: Decodable
|
||||
{
|
||||
if index >= ssz_bytes.len() {
|
||||
return Err(DecodeError::OutOfBounds)
|
||||
}
|
||||
T::ssz_decode(ssz_bytes, index)
|
||||
}
|
||||
|
||||
/// Decode the nth element of some ssz list.
|
||||
|
||||
Reference in New Issue
Block a user