lighthouse/eth2/utils/ssz/src/lib.rs

61 lines
1.5 KiB
Rust
Raw Normal View History

2019-05-13 04:13:15 +00:00
//! Provides encoding (serialization) and decoding (deserialization) in the SimpleSerialize (SSZ)
//! format designed for use in Ethereum 2.0.
//!
//! Conforms to
//! [v0.6.1](https://github.com/ethereum/eth2.0-specs/blob/v0.6.1/specs/simple-serialize.md) of the
//! Ethereum 2.0 specification.
//!
//! ## Example
//!
//! ```rust
//! use ssz_derive::{Encode, Decode};
//! use ssz::{Decodable, Encodable};
//!
//! #[derive(PartialEq, Debug, Encode, Decode)]
//! struct Foo {
//! a: u64,
//! b: Vec<u16>,
//! }
//!
//! fn main() {
//! let foo = Foo {
//! a: 42,
//! b: vec![1, 3, 3, 7]
//! };
//!
//! let ssz_bytes: Vec<u8> = foo.as_ssz_bytes();
//!
//! let decoded_foo = Foo::from_ssz_bytes(&ssz_bytes).unwrap();
//!
//! assert_eq!(foo, decoded_foo);
//! }
//!
//! ```
//!
//! See `examples/` for manual implementations of the `Encodable` and `Decodable` traits.
mod decode;
mod encode;
2019-05-13 02:33:59 +00:00
mod macros;
pub use decode::{
2019-05-13 04:13:15 +00:00
impls::decode_list_of_variable_length_items, Decodable, DecodeError, SszDecoder,
SszDecoderBuilder,
};
2019-05-05 23:26:58 +00:00
pub use encode::{Encodable, SszEncoder};
2019-05-13 04:13:15 +00:00
/// The number of bytes used to represent an offset.
pub const BYTES_PER_LENGTH_OFFSET: usize = 4;
2019-05-13 04:13:15 +00:00
/// The maximum value that can be represented using `BYTES_PER_LENGTH_OFFSET`.
pub const MAX_LENGTH_VALUE: usize = (1 << (BYTES_PER_LENGTH_OFFSET * 8)) - 1;
/// Convenience function to SSZ encode an object supporting ssz::Encode.
2019-05-05 23:26:58 +00:00
///
/// Equivalent to `val.as_ssz_bytes()`.
pub fn ssz_encode<T>(val: &T) -> Vec<u8>
where
T: Encodable,
{
2019-05-05 23:26:58 +00:00
val.as_ssz_bytes()
}