Skip to content

Commit

Permalink
removed TypeInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-marinica committed Oct 23, 2022
1 parent 9e37d97 commit 0e30827
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 131 deletions.
10 changes: 2 additions & 8 deletions elrond-codec/src/impl_for_types/impl_bool.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use crate::{
dep_encode_num_mimic, DecodeError, DecodeErrorHandler, EncodeErrorHandler, NestedDecode,
NestedDecodeInput, NestedEncode, NestedEncodeOutput, TopDecode, TopDecodeInput, TopEncode,
TopEncodeOutput, TypeInfo,
TopEncodeOutput,
};

impl TopEncode for bool {
const TYPE_INFO: TypeInfo = TypeInfo::Bool;

#[inline]
fn top_encode_or_handle_err<O, H>(&self, output: O, _h: H) -> Result<(), H::HandledErr>
where
Expand All @@ -23,8 +21,6 @@ impl TopEncode for bool {
}

impl TopDecode for bool {
const TYPE_INFO: TypeInfo = TypeInfo::Bool;

fn top_decode_or_handle_err<I, H>(input: I, h: H) -> Result<Self, H::HandledErr>
where
I: TopDecodeInput,
Expand All @@ -38,11 +34,9 @@ impl TopDecode for bool {
}
}

dep_encode_num_mimic! {bool, u8, TypeInfo::Bool}
dep_encode_num_mimic! {bool, u8}

impl NestedDecode for bool {
const TYPE_INFO: TypeInfo = TypeInfo::Bool;

fn dep_decode_or_handle_err<I, H>(input: &mut I, h: H) -> Result<Self, H::HandledErr>
where
I: NestedDecodeInput,
Expand Down
52 changes: 24 additions & 28 deletions elrond-codec/src/impl_for_types/impl_num_signed.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::{
dep_encode_num_mimic, num_conv::universal_decode_number, DecodeError, DecodeErrorHandler,
EncodeErrorHandler, NestedDecode, NestedDecodeInput, NestedEncode, NestedEncodeOutput,
TopDecode, TopDecodeInput, TopEncode, TopEncodeOutput, TypeInfo,
TopDecode, TopDecodeInput, TopEncode, TopEncodeOutput,
};

macro_rules! top_encode_num_signed {
($num_type:ty, $size_in_bits:expr, $type_info:expr) => {
($num_type:ty, $size_in_bits:expr) => {
impl TopEncode for $num_type {
#[inline]
fn top_encode_or_handle_err<O, H>(&self, output: O, _h: H) -> Result<(), H::HandledErr>
Expand All @@ -20,23 +20,21 @@ macro_rules! top_encode_num_signed {
};
}

top_encode_num_signed! {i64, 64, TypeInfo::I64}
top_encode_num_signed! {i32, 32, TypeInfo::I32}
top_encode_num_signed! {isize, 32, TypeInfo::ISIZE}
top_encode_num_signed! {i16, 16, TypeInfo::I16}
top_encode_num_signed! {i8, 8, TypeInfo::I8}
top_encode_num_signed! {i64, 64}
top_encode_num_signed! {i32, 32}
top_encode_num_signed! {isize, 32}
top_encode_num_signed! {i16, 16}
top_encode_num_signed! {i8, 8}

dep_encode_num_mimic! {i64, u64, TypeInfo::I64}
dep_encode_num_mimic! {i32, u32, TypeInfo::I32}
dep_encode_num_mimic! {isize, u32, TypeInfo::ISIZE}
dep_encode_num_mimic! {i16, u16, TypeInfo::I16}
dep_encode_num_mimic! {i8, u8, TypeInfo::I8}
dep_encode_num_mimic! {i64, u64}
dep_encode_num_mimic! {i32, u32}
dep_encode_num_mimic! {isize, u32}
dep_encode_num_mimic! {i16, u16}
dep_encode_num_mimic! {i8, u8}

macro_rules! dep_decode_num_signed {
($ty:ty, $num_bytes:expr, $type_info:expr) => {
($ty:ty, $num_bytes:expr) => {
impl NestedDecode for $ty {
const TYPE_INFO: TypeInfo = $type_info;

fn dep_decode_or_handle_err<I, H>(input: &mut I, h: H) -> Result<Self, H::HandledErr>
where
I: NestedDecodeInput,
Expand All @@ -51,17 +49,15 @@ macro_rules! dep_decode_num_signed {
};
}

dep_decode_num_signed!(i8, 1, TypeInfo::I8);
dep_decode_num_signed!(i16, 2, TypeInfo::I16);
dep_decode_num_signed!(i32, 4, TypeInfo::I32);
dep_decode_num_signed!(isize, 4, TypeInfo::ISIZE);
dep_decode_num_signed!(i64, 8, TypeInfo::I64);
dep_decode_num_signed!(i8, 1);
dep_decode_num_signed!(i16, 2);
dep_decode_num_signed!(i32, 4);
dep_decode_num_signed!(isize, 4);
dep_decode_num_signed!(i64, 8);

macro_rules! top_decode_num_signed {
($ty:ty, $bounds_ty:ty, $type_info:expr) => {
($ty:ty, $bounds_ty:ty) => {
impl TopDecode for $ty {
const TYPE_INFO: TypeInfo = $type_info;

fn top_decode_or_handle_err<I, H>(input: I, h: H) -> Result<Self, H::HandledErr>
where
I: TopDecodeInput,
Expand All @@ -80,11 +76,11 @@ macro_rules! top_decode_num_signed {
};
}

top_decode_num_signed!(i8, i8, TypeInfo::I8);
top_decode_num_signed!(i16, i16, TypeInfo::I16);
top_decode_num_signed!(i32, i32, TypeInfo::I32);
top_decode_num_signed!(isize, i32, TypeInfo::ISIZE); // even if isize can be 64 bits on some platforms, we always deserialize as max 32 bits
top_decode_num_signed!(i64, i64, TypeInfo::I64);
top_decode_num_signed!(i8, i8);
top_decode_num_signed!(i16, i16);
top_decode_num_signed!(i32, i32);
top_decode_num_signed!(isize, i32); // even if isize can be 64 bits on some platforms, we always deserialize as max 32 bits
top_decode_num_signed!(i64, i64);

#[cfg(test)]
pub mod tests {
Expand Down
56 changes: 23 additions & 33 deletions elrond-codec/src/impl_for_types/impl_num_unsigned.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use crate::{
dep_encode_num_mimic, num_conv::universal_decode_number, DecodeError, DecodeErrorHandler,
EncodeErrorHandler, NestedDecode, NestedDecodeInput, NestedEncode, NestedEncodeOutput,
TopDecode, TopDecodeInput, TopEncode, TopEncodeOutput, TypeInfo,
TopDecode, TopDecodeInput, TopEncode, TopEncodeOutput,
};

// No reversing needed for u8, because it is a single byte.
impl NestedEncode for u8 {
const TYPE_INFO: TypeInfo = TypeInfo::U8;

#[inline]
fn dep_encode_or_handle_err<O, H>(&self, dest: &mut O, _h: H) -> Result<(), H::HandledErr>
where
Expand All @@ -27,14 +25,12 @@ impl NestedEncode for u8 {
}
}

dep_encode_num_mimic! {usize, u32, TypeInfo::USIZE}
dep_encode_num_mimic! {usize, u32}

// The main unsigned types need to be reversed before serializing.
macro_rules! dep_encode_num_unsigned {
($num_type:ty, $size_in_bits:expr, $type_info:expr) => {
($num_type:ty, $size_in_bits:expr) => {
impl NestedEncode for $num_type {
const TYPE_INFO: TypeInfo = $type_info;

#[inline]
fn dep_encode_or_handle_err<O, H>(
&self,
Expand All @@ -52,12 +48,12 @@ macro_rules! dep_encode_num_unsigned {
};
}

dep_encode_num_unsigned! {u64, 64, TypeInfo::U64}
dep_encode_num_unsigned! {u32, 32, TypeInfo::U32}
dep_encode_num_unsigned! {u16, 16, TypeInfo::U16}
dep_encode_num_unsigned! {u64, 64}
dep_encode_num_unsigned! {u32, 32}
dep_encode_num_unsigned! {u16, 16}

macro_rules! top_encode_num_unsigned {
($num_type:ty, $size_in_bits:expr, $type_info:expr) => {
($num_type:ty, $size_in_bits:expr) => {
impl TopEncode for $num_type {
#[inline]
fn top_encode_or_handle_err<O, H>(&self, output: O, _h: H) -> Result<(), H::HandledErr>
Expand All @@ -72,15 +68,13 @@ macro_rules! top_encode_num_unsigned {
};
}

top_encode_num_unsigned! {u64, 64, TypeInfo::U64}
top_encode_num_unsigned! {u32, 32, TypeInfo::U32}
top_encode_num_unsigned! {usize, 32, TypeInfo::USIZE}
top_encode_num_unsigned! {u16, 16, TypeInfo::U16}
top_encode_num_unsigned! {u8, 8, TypeInfo::U8}
top_encode_num_unsigned! {u64, 64}
top_encode_num_unsigned! {u32, 32}
top_encode_num_unsigned! {usize, 32}
top_encode_num_unsigned! {u16, 16}
top_encode_num_unsigned! {u8, 8}

impl NestedDecode for u8 {
const TYPE_INFO: TypeInfo = TypeInfo::U8;

fn dep_decode_or_handle_err<I, H>(input: &mut I, h: H) -> Result<Self, H::HandledErr>
where
I: NestedDecodeInput,
Expand All @@ -99,10 +93,8 @@ impl NestedDecode for u8 {
}

macro_rules! dep_decode_num_unsigned {
($ty:ty, $num_bytes:expr, $type_info:expr) => {
($ty:ty, $num_bytes:expr) => {
impl NestedDecode for $ty {
const TYPE_INFO: TypeInfo = $type_info;

fn dep_decode_or_handle_err<I, H>(input: &mut I, h: H) -> Result<Self, H::HandledErr>
where
I: NestedDecodeInput,
Expand All @@ -117,16 +109,14 @@ macro_rules! dep_decode_num_unsigned {
};
}

dep_decode_num_unsigned!(u16, 2, TypeInfo::U16);
dep_decode_num_unsigned!(u32, 4, TypeInfo::U32);
dep_decode_num_unsigned!(usize, 4, TypeInfo::USIZE);
dep_decode_num_unsigned!(u64, 8, TypeInfo::U64);
dep_decode_num_unsigned!(u16, 2);
dep_decode_num_unsigned!(u32, 4);
dep_decode_num_unsigned!(usize, 4);
dep_decode_num_unsigned!(u64, 8);

macro_rules! top_decode_num_unsigned {
($ty:ty, $bounds_ty:ty, $type_info:expr) => {
($ty:ty, $bounds_ty:ty) => {
impl TopDecode for $ty {
const TYPE_INFO: TypeInfo = $type_info;

fn top_decode_or_handle_err<I, H>(input: I, h: H) -> Result<Self, H::HandledErr>
where
I: TopDecodeInput,
Expand All @@ -144,11 +134,11 @@ macro_rules! top_decode_num_unsigned {
};
}

top_decode_num_unsigned!(u8, u8, TypeInfo::U8);
top_decode_num_unsigned!(u16, u16, TypeInfo::U16);
top_decode_num_unsigned!(u32, u32, TypeInfo::U32);
top_decode_num_unsigned!(usize, u32, TypeInfo::USIZE); // even if usize can be 64 bits on some platforms, we always deserialize as max 32 bits
top_decode_num_unsigned!(u64, u64, TypeInfo::U64);
top_decode_num_unsigned!(u8, u8);
top_decode_num_unsigned!(u16, u16);
top_decode_num_unsigned!(u32, u32);
top_decode_num_unsigned!(usize, u32); // even if usize can be 64 bits on some platforms, we always deserialize as max 32 bits
top_decode_num_unsigned!(u64, u64);

#[cfg(test)]
pub mod tests {
Expand Down
6 changes: 1 addition & 5 deletions elrond-codec/src/impl_for_types/impl_unit.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use crate::{
DecodeErrorHandler, EncodeErrorHandler, NestedDecode, NestedDecodeInput, NestedEncode,
NestedEncodeOutput, TypeInfo,
NestedEncodeOutput,
};

impl NestedEncode for () {
const TYPE_INFO: TypeInfo = TypeInfo::Unit;

#[inline]
fn dep_encode_or_handle_err<O, H>(&self, _: &mut O, _h: H) -> Result<(), H::HandledErr>
where
Expand All @@ -17,8 +15,6 @@ impl NestedEncode for () {
}

impl NestedDecode for () {
const TYPE_INFO: TypeInfo = TypeInfo::Unit;

fn dep_decode_or_handle_err<I, H>(_input: &mut I, _h: H) -> Result<Self, H::HandledErr>
where
I: NestedDecodeInput,
Expand Down
4 changes: 1 addition & 3 deletions elrond-codec/src/impl_for_types/local_macro.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// Derive the implementation of the other types by casting.
#[macro_export]
macro_rules! dep_encode_num_mimic {
($num_type:ty, $mimic_type:ident, $type_info:expr) => {
($num_type:ty, $mimic_type:ident) => {
impl NestedEncode for $num_type {
const TYPE_INFO: TypeInfo = $type_info;

#[inline]
fn dep_encode_or_handle_err<O, H>(
&self,
Expand Down
23 changes: 0 additions & 23 deletions elrond-codec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,3 @@ pub use multi::*;
pub use single::*;

pub use transmute::{boxed_slice_into_vec, vec_into_boxed_slice};

/// !INTERNAL USE ONLY!
///
/// This enum provides type information to optimize encoding/decoding by doing fake specialization.
#[doc(hidden)]
#[allow(clippy::upper_case_acronyms)]
#[derive(PartialEq, Eq)]
pub enum TypeInfo {
/// Default value of [`NestedEncode::TYPE_INFO`] to not require implementors to set this value in the trait.
Unknown,
U8,
I8,
U16,
I16,
U32,
I32,
USIZE,
ISIZE,
U64,
I64,
Bool,
Unit,
}
9 changes: 1 addition & 8 deletions elrond-codec/src/single/nested_de.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
// use core::ops::Try;

use crate::{
codec_err::DecodeError, DecodeErrorHandler, DefaultErrorHandler, NestedDecodeInput, TypeInfo,
};
use crate::{codec_err::DecodeError, DecodeErrorHandler, DefaultErrorHandler, NestedDecodeInput};

/// Trait that allows zero-copy read of value-references from slices in LE format.
pub trait NestedDecode: Sized {
// !INTERNAL USE ONLY!
// This const helps elrond-wasm to optimize the encoding/decoding by doing fake specialization.
#[doc(hidden)]
const TYPE_INFO: TypeInfo = TypeInfo::Unknown;

/// Attempt to deserialise the value from input,
/// using the format of an object nested inside another structure.
/// In case of success returns the deserialized value and the number of bytes consumed during the operation.
Expand Down
9 changes: 1 addition & 8 deletions elrond-codec/src/single/nested_en.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
use crate::{
codec_err::EncodeError, DefaultErrorHandler, EncodeErrorHandler, NestedEncodeOutput, TypeInfo,
};
use crate::{codec_err::EncodeError, DefaultErrorHandler, EncodeErrorHandler, NestedEncodeOutput};
use alloc::vec::Vec;

/// Trait that allows zero-copy write of value-references to slices in LE format.
///
/// Implementations should override `using_top_encoded` for value types and `dep_encode` and `size_hint` for allocating types.
/// Wrapper types should override all methods.
pub trait NestedEncode: Sized {
// !INTERNAL USE ONLY!
// This const helps SCALE to optimize the encoding/decoding by doing fake specialization.
#[doc(hidden)]
const TYPE_INFO: TypeInfo = TypeInfo::Unknown;

/// NestedEncode to output, using the format of an object nested inside another structure.
/// Does not provide compact version.
fn dep_encode<O: NestedEncodeOutput>(&self, dest: &mut O) -> Result<(), EncodeError> {
Expand Down
5 changes: 1 addition & 4 deletions elrond-codec/src/single/top_de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use alloc::boxed::Box;

use crate::{
codec_err::DecodeError, DecodeErrorHandler, DefaultErrorHandler, NestedDecode,
NestedDecodeInput, TopDecodeInput, TypeInfo,
NestedDecodeInput, TopDecodeInput,
};

/// Trait that allows zero-copy read of values from an underlying API in big endian format.
Expand All @@ -19,9 +19,6 @@ use crate::{
/// and would overly complicate the trait.
///
pub trait TopDecode: Sized {
#[doc(hidden)]
const TYPE_INFO: TypeInfo = TypeInfo::Unknown;

/// Attempt to deserialize the value from input.
fn top_decode<I>(input: I) -> Result<Self, DecodeError>
where
Expand Down
6 changes: 1 addition & 5 deletions elrond-codec/src/single/top_en.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
use crate::{
codec_err::EncodeError, DefaultErrorHandler, EncodeErrorHandler, NestedEncode,
PanicErrorHandler, TopEncodeOutput, TypeInfo,
PanicErrorHandler, TopEncodeOutput,
};
use alloc::vec::Vec;

pub trait TopEncode: Sized {
// !INTERNAL USE ONLY!
#[doc(hidden)]
const TYPE_INFO: TypeInfo = TypeInfo::Unknown;

/// Attempt to serialize the value to ouput.
fn top_encode<O>(&self, output: O) -> Result<(), EncodeError>
where
Expand Down
Loading

0 comments on commit 0e30827

Please sign in to comment.