mirror of
https://github.com/sigp/lighthouse.git
synced 2026-04-18 05:18:30 +00:00
Rename SSZ traits (Encodable -> Encode)
This commit is contained in:
@@ -38,8 +38,8 @@ spec is decided.*\
|
||||
+ [bytes v0.4.9](#bytes-v049)
|
||||
+ [ethereum-types](#ethereum-types)
|
||||
* [Interface](#interface)
|
||||
+ [Encodable](#encodable)
|
||||
+ [Decodable](#decodable)
|
||||
+ [Encode](#encodable)
|
||||
+ [Decode](#decodable)
|
||||
+ [SszStream](#sszstream)
|
||||
- [new()](#new)
|
||||
- [append(&mut self, value: &E) -> &mut Self](#appendmut-self-value-e---mut-self)
|
||||
@@ -299,24 +299,24 @@ Github: [ https://github.com/paritytech/primitives ](https://github.com/parityte
|
||||
|
||||
## Interface
|
||||
|
||||
### Encodable
|
||||
### Encode
|
||||
|
||||
A type is **Encodable** if it has a valid ``ssz_append`` function. This is
|
||||
A type is **Encode** if it has a valid ``ssz_append`` function. This is
|
||||
used to ensure that the object/type can be serialized.
|
||||
|
||||
```rust
|
||||
pub trait Encodable {
|
||||
pub trait Encode {
|
||||
fn ssz_append(&self, s: &mut SszStream);
|
||||
}
|
||||
```
|
||||
|
||||
### Decodable
|
||||
### Decode
|
||||
|
||||
A type is **Decodable** if it has a valid ``ssz_decode`` function. This is
|
||||
A type is **Decode** if it has a valid ``ssz_decode`` function. This is
|
||||
used to ensure the object is deserializable.
|
||||
|
||||
```rust
|
||||
pub trait Decodable: Sized {
|
||||
pub trait Decode: Sized {
|
||||
fn ssz_decode(bytes: &[u8], index: usize) -> Result<(Self, usize), DecodeError>;
|
||||
}
|
||||
```
|
||||
@@ -342,7 +342,7 @@ Appends a value that can be encoded into the stream.
|
||||
|
||||
| Parameter | Description |
|
||||
|:---------:|:-----------------------------------------|
|
||||
| ``value`` | Encodable value to append to the stream. |
|
||||
| ``value`` | Encode value to append to the stream. |
|
||||
|
||||
**Example**
|
||||
|
||||
@@ -371,7 +371,7 @@ Appends some vector (list) of encodable values to the stream.
|
||||
|
||||
| Parameter | Description |
|
||||
|:---------:|:----------------------------------------------|
|
||||
| ``vec`` | Vector of Encodable objects to be serialized. |
|
||||
| ``vec`` | Vector of Encode objects to be serialized. |
|
||||
|
||||
**Example**
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ extern crate criterion;
|
||||
|
||||
use criterion::black_box;
|
||||
use criterion::{Benchmark, Criterion};
|
||||
use ssz::{Decodable, Encodable};
|
||||
use ssz::{Decode, Encode};
|
||||
use ssz_derive::{Decode, Encode};
|
||||
|
||||
#[derive(Clone, Copy, Encode, Decode)]
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
//!
|
||||
//! Useful for `cargo flamegraph`.
|
||||
|
||||
use ssz::{Decodable, Encodable};
|
||||
use ssz::{Decode, Encode};
|
||||
|
||||
fn main() {
|
||||
let vec: Vec<u64> = vec![4242; 8196];
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
//!
|
||||
//! Useful for `cargo flamegraph`.
|
||||
|
||||
use ssz::{Decodable, Encodable};
|
||||
use ssz::{Decode, Encode};
|
||||
use ssz_derive::{Decode, Encode};
|
||||
|
||||
#[derive(Clone, Copy, Encode, Decode)]
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use ssz::{Decodable, DecodeError, Encodable, SszDecoderBuilder, SszEncoder};
|
||||
use ssz::{Decode, DecodeError, Encode, SszDecoderBuilder, SszEncoder};
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct Foo {
|
||||
@@ -7,15 +7,15 @@ pub struct Foo {
|
||||
c: u16,
|
||||
}
|
||||
|
||||
impl Encodable for Foo {
|
||||
impl Encode for Foo {
|
||||
fn is_ssz_fixed_len() -> bool {
|
||||
<u16 as Encodable>::is_ssz_fixed_len() && <Vec<u16> as Encodable>::is_ssz_fixed_len()
|
||||
<u16 as Encode>::is_ssz_fixed_len() && <Vec<u16> as Encode>::is_ssz_fixed_len()
|
||||
}
|
||||
|
||||
fn ssz_append(&self, buf: &mut Vec<u8>) {
|
||||
let offset = <u16 as Encodable>::ssz_fixed_len()
|
||||
+ <Vec<u16> as Encodable>::ssz_fixed_len()
|
||||
+ <u16 as Encodable>::ssz_fixed_len();
|
||||
let offset = <u16 as Encode>::ssz_fixed_len()
|
||||
+ <Vec<u16> as Encode>::ssz_fixed_len()
|
||||
+ <u16 as Encode>::ssz_fixed_len();
|
||||
|
||||
let mut encoder = SszEncoder::container(buf, offset);
|
||||
|
||||
@@ -27,9 +27,9 @@ impl Encodable for Foo {
|
||||
}
|
||||
}
|
||||
|
||||
impl Decodable for Foo {
|
||||
impl Decode for Foo {
|
||||
fn is_ssz_fixed_len() -> bool {
|
||||
<u16 as Decodable>::is_ssz_fixed_len() && <Vec<u16> as Decodable>::is_ssz_fixed_len()
|
||||
<u16 as Decode>::is_ssz_fixed_len() && <Vec<u16> as Decode>::is_ssz_fixed_len()
|
||||
}
|
||||
|
||||
fn from_ssz_bytes(bytes: &[u8]) -> Result<Self, DecodeError> {
|
||||
|
||||
@@ -26,7 +26,7 @@ pub enum DecodeError {
|
||||
///
|
||||
/// See `examples/` for manual implementations or the crate root for implementations using
|
||||
/// `#[derive(Decode)]`.
|
||||
pub trait Decodable: Sized {
|
||||
pub trait Decode: Sized {
|
||||
/// Returns `true` if this object has a fixed-length.
|
||||
///
|
||||
/// I.e., there are no variable length items in this object or any of it's contained objects.
|
||||
@@ -80,7 +80,7 @@ impl<'a> SszDecoderBuilder<'a> {
|
||||
}
|
||||
|
||||
/// Declares that some type `T` is the next item in `bytes`.
|
||||
pub fn register_type<T: Decodable>(&mut self) -> Result<(), DecodeError> {
|
||||
pub fn register_type<T: Decode>(&mut self) -> Result<(), DecodeError> {
|
||||
if T::is_ssz_fixed_len() {
|
||||
let start = self.items_index;
|
||||
self.items_index += T::ssz_fixed_len();
|
||||
@@ -173,7 +173,7 @@ impl<'a> SszDecoderBuilder<'a> {
|
||||
///
|
||||
/// ```rust
|
||||
/// use ssz_derive::{Encode, Decode};
|
||||
/// use ssz::{Decodable, Encodable, SszDecoder, SszDecoderBuilder};
|
||||
/// use ssz::{Decode, Encode, SszDecoder, SszDecoderBuilder};
|
||||
///
|
||||
/// #[derive(PartialEq, Debug, Encode, Decode)]
|
||||
/// struct Foo {
|
||||
@@ -215,7 +215,7 @@ impl<'a> SszDecoder<'a> {
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics when attempting to decode more items than actually exist.
|
||||
pub fn decode_next<T: Decodable>(&mut self) -> Result<T, DecodeError> {
|
||||
pub fn decode_next<T: Decode>(&mut self) -> Result<T, DecodeError> {
|
||||
T::from_ssz_bytes(self.items.remove(0))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ use ethereum_types::H256;
|
||||
|
||||
macro_rules! impl_decodable_for_uint {
|
||||
($type: ident, $bit_size: expr) => {
|
||||
impl Decodable for $type {
|
||||
impl Decode for $type {
|
||||
fn is_ssz_fixed_len() -> bool {
|
||||
true
|
||||
}
|
||||
@@ -14,7 +14,7 @@ macro_rules! impl_decodable_for_uint {
|
||||
|
||||
fn from_ssz_bytes(bytes: &[u8]) -> Result<Self, DecodeError> {
|
||||
let len = bytes.len();
|
||||
let expected = <Self as Decodable>::ssz_fixed_len();
|
||||
let expected = <Self as Decode>::ssz_fixed_len();
|
||||
|
||||
if len != expected {
|
||||
Err(DecodeError::InvalidByteLength { len, expected })
|
||||
@@ -35,7 +35,7 @@ impl_decodable_for_uint!(u32, 32);
|
||||
impl_decodable_for_uint!(u64, 64);
|
||||
impl_decodable_for_uint!(usize, 64);
|
||||
|
||||
impl Decodable for bool {
|
||||
impl Decode for bool {
|
||||
fn is_ssz_fixed_len() -> bool {
|
||||
true
|
||||
}
|
||||
@@ -46,7 +46,7 @@ impl Decodable for bool {
|
||||
|
||||
fn from_ssz_bytes(bytes: &[u8]) -> Result<Self, DecodeError> {
|
||||
let len = bytes.len();
|
||||
let expected = <Self as Decodable>::ssz_fixed_len();
|
||||
let expected = <Self as Decode>::ssz_fixed_len();
|
||||
|
||||
if len != expected {
|
||||
Err(DecodeError::InvalidByteLength { len, expected })
|
||||
@@ -64,7 +64,7 @@ impl Decodable for bool {
|
||||
}
|
||||
}
|
||||
|
||||
impl Decodable for H256 {
|
||||
impl Decode for H256 {
|
||||
fn is_ssz_fixed_len() -> bool {
|
||||
true
|
||||
}
|
||||
@@ -75,7 +75,7 @@ impl Decodable for H256 {
|
||||
|
||||
fn from_ssz_bytes(bytes: &[u8]) -> Result<Self, DecodeError> {
|
||||
let len = bytes.len();
|
||||
let expected = <Self as Decodable>::ssz_fixed_len();
|
||||
let expected = <Self as Decode>::ssz_fixed_len();
|
||||
|
||||
if len != expected {
|
||||
Err(DecodeError::InvalidByteLength { len, expected })
|
||||
@@ -87,7 +87,7 @@ impl Decodable for H256 {
|
||||
|
||||
macro_rules! impl_decodable_for_u8_array {
|
||||
($len: expr) => {
|
||||
impl Decodable for [u8; $len] {
|
||||
impl Decode for [u8; $len] {
|
||||
fn is_ssz_fixed_len() -> bool {
|
||||
true
|
||||
}
|
||||
@@ -98,7 +98,7 @@ macro_rules! impl_decodable_for_u8_array {
|
||||
|
||||
fn from_ssz_bytes(bytes: &[u8]) -> Result<Self, DecodeError> {
|
||||
let len = bytes.len();
|
||||
let expected = <Self as Decodable>::ssz_fixed_len();
|
||||
let expected = <Self as Decode>::ssz_fixed_len();
|
||||
|
||||
if len != expected {
|
||||
Err(DecodeError::InvalidByteLength { len, expected })
|
||||
@@ -115,7 +115,7 @@ macro_rules! impl_decodable_for_u8_array {
|
||||
|
||||
impl_decodable_for_u8_array!(4);
|
||||
|
||||
impl<T: Decodable> Decodable for Vec<T> {
|
||||
impl<T: Decode> Decode for Vec<T> {
|
||||
fn is_ssz_fixed_len() -> bool {
|
||||
false
|
||||
}
|
||||
@@ -139,7 +139,7 @@ impl<T: Decodable> Decodable for Vec<T> {
|
||||
/// The `ssz::SszDecoder` can also perform this functionality, however it it significantly faster
|
||||
/// as it is optimized to read same-typed items whilst `ssz::SszDecoder` supports reading items of
|
||||
/// differing types.
|
||||
pub fn decode_list_of_variable_length_items<T: Decodable>(
|
||||
pub fn decode_list_of_variable_length_items<T: Decode>(
|
||||
bytes: &[u8],
|
||||
) -> Result<Vec<T>, DecodeError> {
|
||||
let mut next_variable_byte = read_offset(bytes)?;
|
||||
|
||||
@@ -6,7 +6,7 @@ mod impls;
|
||||
///
|
||||
/// See `examples/` for manual implementations or the crate root for implementations using
|
||||
/// `#[derive(Encode)]`.
|
||||
pub trait Encodable {
|
||||
pub trait Encode {
|
||||
/// Returns `true` if this object has a fixed-length.
|
||||
///
|
||||
/// I.e., there are no variable length items in this object or any of it's contained objects.
|
||||
@@ -50,7 +50,7 @@ pub trait Encodable {
|
||||
///
|
||||
/// ```rust
|
||||
/// use ssz_derive::{Encode, Decode};
|
||||
/// use ssz::{Decodable, Encodable, SszEncoder};
|
||||
/// use ssz::{Decode, Encode, SszEncoder};
|
||||
///
|
||||
/// #[derive(PartialEq, Debug, Encode, Decode)]
|
||||
/// struct Foo {
|
||||
@@ -65,7 +65,7 @@ pub trait Encodable {
|
||||
/// };
|
||||
///
|
||||
/// let mut buf: Vec<u8> = vec![];
|
||||
/// let offset = <u64 as Encodable>::ssz_fixed_len() + <Vec<u16> as Encodable>::ssz_fixed_len();
|
||||
/// let offset = <u64 as Encode>::ssz_fixed_len() + <Vec<u16> as Encode>::ssz_fixed_len();
|
||||
///
|
||||
/// let mut encoder = SszEncoder::container(&mut buf, offset);
|
||||
///
|
||||
@@ -104,7 +104,7 @@ impl<'a> SszEncoder<'a> {
|
||||
}
|
||||
|
||||
/// Append some `item` to the SSZ bytes.
|
||||
pub fn append<T: Encodable>(&mut self, item: &T) {
|
||||
pub fn append<T: Encode>(&mut self, item: &T) {
|
||||
if T::is_ssz_fixed_len() {
|
||||
item.ssz_append(&mut self.buf);
|
||||
} else {
|
||||
|
||||
@@ -3,7 +3,7 @@ use ethereum_types::H256;
|
||||
|
||||
macro_rules! impl_encodable_for_uint {
|
||||
($type: ident, $bit_size: expr) => {
|
||||
impl Encodable for $type {
|
||||
impl Encode for $type {
|
||||
fn is_ssz_fixed_len() -> bool {
|
||||
true
|
||||
}
|
||||
@@ -25,7 +25,7 @@ impl_encodable_for_uint!(u32, 32);
|
||||
impl_encodable_for_uint!(u64, 64);
|
||||
impl_encodable_for_uint!(usize, 64);
|
||||
|
||||
impl<T: Encodable> Encodable for Vec<T> {
|
||||
impl<T: Encode> Encode for Vec<T> {
|
||||
fn is_ssz_fixed_len() -> bool {
|
||||
false
|
||||
}
|
||||
@@ -49,7 +49,7 @@ impl<T: Encodable> Encodable for Vec<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodable for bool {
|
||||
impl Encode for bool {
|
||||
fn is_ssz_fixed_len() -> bool {
|
||||
true
|
||||
}
|
||||
@@ -63,7 +63,7 @@ impl Encodable for bool {
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodable for H256 {
|
||||
impl Encode for H256 {
|
||||
fn is_ssz_fixed_len() -> bool {
|
||||
true
|
||||
}
|
||||
@@ -79,7 +79,7 @@ impl Encodable for H256 {
|
||||
|
||||
macro_rules! impl_encodable_for_u8_array {
|
||||
($len: expr) => {
|
||||
impl Encodable for [u8; $len] {
|
||||
impl Encode for [u8; $len] {
|
||||
fn is_ssz_fixed_len() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
//!
|
||||
//! ```rust
|
||||
//! use ssz_derive::{Encode, Decode};
|
||||
//! use ssz::{Decodable, Encodable};
|
||||
//! use ssz::{Decode, Encode};
|
||||
//!
|
||||
//! #[derive(PartialEq, Debug, Encode, Decode)]
|
||||
//! struct Foo {
|
||||
@@ -32,17 +32,16 @@
|
||||
//!
|
||||
//! ```
|
||||
//!
|
||||
//! See `examples/` for manual implementations of the `Encodable` and `Decodable` traits.
|
||||
//! See `examples/` for manual implementations of the `Encode` and `Decode` traits.
|
||||
|
||||
mod decode;
|
||||
mod encode;
|
||||
mod macros;
|
||||
|
||||
pub use decode::{
|
||||
impls::decode_list_of_variable_length_items, Decodable, DecodeError, SszDecoder,
|
||||
SszDecoderBuilder,
|
||||
impls::decode_list_of_variable_length_items, Decode, DecodeError, SszDecoder, SszDecoderBuilder,
|
||||
};
|
||||
pub use encode::{Encodable, SszEncoder};
|
||||
pub use encode::{Encode, SszEncoder};
|
||||
|
||||
/// The number of bytes used to represent an offset.
|
||||
pub const BYTES_PER_LENGTH_OFFSET: usize = 4;
|
||||
@@ -54,7 +53,7 @@ pub const MAX_LENGTH_VALUE: usize = (1 << (BYTES_PER_LENGTH_OFFSET * 8)) - 1;
|
||||
/// Equivalent to `val.as_ssz_bytes()`.
|
||||
pub fn ssz_encode<T>(val: &T) -> Vec<u8>
|
||||
where
|
||||
T: Encodable,
|
||||
T: Encode,
|
||||
{
|
||||
val.as_ssz_bytes()
|
||||
}
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
/// Implements `Encodable` for `$impl_type` using an implementation of `From<$impl_type> for
|
||||
/// Implements `Encode` for `$impl_type` using an implementation of `From<$impl_type> for
|
||||
/// $from_type`.
|
||||
///
|
||||
/// In effect, this allows for easy implementation of `Encodable` for some type that implements a
|
||||
/// `From` conversion into another type that already has `Encodable` implemented.
|
||||
/// In effect, this allows for easy implementation of `Encode` for some type that implements a
|
||||
/// `From` conversion into another type that already has `Encode` implemented.
|
||||
#[macro_export]
|
||||
macro_rules! impl_encode_via_from {
|
||||
($impl_type: ty, $from_type: ty) => {
|
||||
impl ssz::Encodable for $impl_type {
|
||||
impl ssz::Encode for $impl_type {
|
||||
fn is_ssz_fixed_len() -> bool {
|
||||
<$from_type as ssz::Encodable>::is_ssz_fixed_len()
|
||||
<$from_type as ssz::Encode>::is_ssz_fixed_len()
|
||||
}
|
||||
|
||||
fn ssz_fixed_len() -> usize {
|
||||
<$from_type as ssz::Encodable>::ssz_fixed_len()
|
||||
<$from_type as ssz::Encode>::ssz_fixed_len()
|
||||
}
|
||||
|
||||
fn ssz_append(&self, buf: &mut Vec<u8>) {
|
||||
@@ -24,21 +24,21 @@ macro_rules! impl_encode_via_from {
|
||||
};
|
||||
}
|
||||
|
||||
/// Implements `Decodable` for `$impl_type` using an implementation of `From<$impl_type> for
|
||||
/// Implements `Decode` for `$impl_type` using an implementation of `From<$impl_type> for
|
||||
/// $from_type`.
|
||||
///
|
||||
/// In effect, this allows for easy implementation of `Decodable` for some type that implements a
|
||||
/// `From` conversion into another type that already has `Decodable` implemented.
|
||||
/// In effect, this allows for easy implementation of `Decode` for some type that implements a
|
||||
/// `From` conversion into another type that already has `Decode` implemented.
|
||||
#[macro_export]
|
||||
macro_rules! impl_decode_via_from {
|
||||
($impl_type: ty, $from_type: tt) => {
|
||||
impl ssz::Decodable for $impl_type {
|
||||
impl ssz::Decode for $impl_type {
|
||||
fn is_ssz_fixed_len() -> bool {
|
||||
<$from_type as ssz::Decodable>::is_ssz_fixed_len()
|
||||
<$from_type as ssz::Decode>::is_ssz_fixed_len()
|
||||
}
|
||||
|
||||
fn ssz_fixed_len() -> usize {
|
||||
<$from_type as ssz::Decodable>::ssz_fixed_len()
|
||||
<$from_type as ssz::Decode>::ssz_fixed_len()
|
||||
}
|
||||
|
||||
fn from_ssz_bytes(bytes: &[u8]) -> Result<Self, ssz::DecodeError> {
|
||||
@@ -51,7 +51,7 @@ macro_rules! impl_decode_via_from {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate as ssz;
|
||||
use ssz::{Decodable, Encodable};
|
||||
use ssz::{Decode, Encode};
|
||||
|
||||
#[derive(PartialEq, Debug, Clone, Copy)]
|
||||
struct Wrapper(u64);
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
use ethereum_types::H256;
|
||||
use ssz::{Decodable, DecodeError, Encodable};
|
||||
use ssz::{Decode, DecodeError, Encode};
|
||||
use ssz_derive::{Decode, Encode};
|
||||
|
||||
mod round_trip {
|
||||
use super::*;
|
||||
|
||||
fn round_trip<T: Encodable + Decodable + std::fmt::Debug + PartialEq>(items: Vec<T>) {
|
||||
fn round_trip<T: Encode + Decode + std::fmt::Debug + PartialEq>(items: Vec<T>) {
|
||||
for item in items {
|
||||
let encoded = &item.as_ssz_bytes();
|
||||
assert_eq!(T::from_ssz_bytes(&encoded), Ok(item));
|
||||
|
||||
Reference in New Issue
Block a user