Update SSZ

- Implement generic list decoding>
- Expose `encode` mod.
- Add convenience encoding function.
This commit is contained in:
Paul Hauner 2018-10-16 13:44:26 +11:00
parent e91317ca27
commit 1621901f0d
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C
2 changed files with 22 additions and 1 deletions

View File

@ -1,4 +1,5 @@
use super::ethereum_types::H256;
use super::decode::decode_ssz_list;
use super::{
DecodeError,
Decodable,
@ -65,6 +66,16 @@ impl Decodable for H256 {
}
}
impl<T> Decodable for Vec<T>
where T: Decodable
{
fn ssz_decode(bytes: &[u8], index: usize)
-> Result<(Self, usize), DecodeError>
{
decode_ssz_list(bytes, index)
}
}
#[cfg(test)]
mod tests {
use super::*;

View File

@ -11,8 +11,8 @@ extern crate bytes;
extern crate ethereum_types;
pub mod decode;
pub mod encode;
mod encode;
mod impl_encode;
mod impl_decode;
@ -29,3 +29,13 @@ pub use encode::{
pub const LENGTH_BYTES: usize = 4;
pub const MAX_LIST_SIZE : usize = 1 << (4 * 8);
/// Convenience function to SSZ encode an object supporting ssz::Encode.
pub fn ssz_encode<T>(val: &T) -> Vec<u8>
where T: Encodable
{
let mut ssz_stream = SszStream::new();
ssz_stream.append(val);
ssz_stream.drain()
}