Skip to content

Commit

Permalink
fix compact encoding on structs (#32)
Browse files Browse the repository at this point in the history
* broken commit

* use simple approach

* make is_compact pub(crate)

* strange test error still there

* fix decode_item but i guess the byte number is wrong somehow

* add tests

* add tuple conversion compact support

* use macro and support compact single entry tuples
  • Loading branch information
tadeohepperle committed Aug 2, 2023
1 parent f63456f commit b38d80c
Show file tree
Hide file tree
Showing 12 changed files with 266 additions and 408 deletions.
2 changes: 1 addition & 1 deletion scale-decode-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ fn generate_struct_impl(
-> Result<Self, #path_to_scale_decode::Error>
{
let path = #path_to_scale_decode::EMPTY_SCALE_INFO_PATH;
let mut composite = #path_to_scale_decode::visitor::types::Composite::new(input, path, fields, types);
let mut composite = #path_to_scale_decode::visitor::types::Composite::new(input, path, fields, types, false);
use #path_to_scale_decode::{ Visitor, IntoVisitor };
let val = <#path_to_type #ty_generics>::into_visitor().visit_composite(&mut composite, #path_to_scale_decode::visitor::TypeId(0));

Expand Down
42 changes: 1 addition & 41 deletions scale-decode/examples/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use codec::Encode;
use scale_decode::visitor::{
self,
types::{Array, BitSequence, Compact, Composite, Sequence, Str, Tuple, Variant},
types::{Array, BitSequence, Composite, Sequence, Str, Tuple, Variant},
TypeId,
};

Expand All @@ -37,11 +37,6 @@ enum Value {
I64(i64),
I128(i128),
I256([u8; 32]),
CompactU8(u8),
CompactU16(u16),
CompactU32(u32),
CompactU64(u64),
CompactU128(u128),
Sequence(Vec<Value>),
Composite(Vec<(String, Value)>),
Tuple(Vec<Value>),
Expand Down Expand Up @@ -156,41 +151,6 @@ impl visitor::Visitor for ValueVisitor {
) -> Result<Self::Value<'_, 'info>, Self::Error> {
Ok(Value::I256(*value))
}
fn visit_compact_u8<'scale, 'info>(
self,
value: Compact<u8>,
_type_id: TypeId,
) -> Result<Self::Value<'scale, 'info>, Self::Error> {
Ok(Value::CompactU8(value.value()))
}
fn visit_compact_u16<'scale, 'info>(
self,
value: Compact<u16>,
_type_id: TypeId,
) -> Result<Self::Value<'scale, 'info>, Self::Error> {
Ok(Value::CompactU16(value.value()))
}
fn visit_compact_u32<'scale, 'info>(
self,
value: Compact<u32>,
_type_id: TypeId,
) -> Result<Self::Value<'scale, 'info>, Self::Error> {
Ok(Value::CompactU32(value.value()))
}
fn visit_compact_u64<'scale, 'info>(
self,
value: Compact<u64>,
_type_id: TypeId,
) -> Result<Self::Value<'scale, 'info>, Self::Error> {
Ok(Value::CompactU64(value.value()))
}
fn visit_compact_u128<'scale, 'info>(
self,
value: Compact<u128>,
_type_id: TypeId,
) -> Result<Self::Value<'scale, 'info>, Self::Error> {
Ok(Value::CompactU128(value.value()))
}
fn visit_sequence<'scale, 'info>(
self,
value: &mut Sequence<'scale, 'info>,
Expand Down
2 changes: 1 addition & 1 deletion scale-decode/src/impls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ macro_rules! impl_decode_tuple {
where $( $t: IntoVisitor, Error: From<<$t::Visitor as Visitor>::Error>, )*
{
fn decode_as_fields<'info>(input: &mut &[u8], fields: &mut dyn FieldIter<'info>, types: &'info scale_info::PortableRegistry) -> Result<Self, Error> {
let mut composite = crate::visitor::types::Composite::new(input, crate::EMPTY_SCALE_INFO_PATH, fields, types);
let mut composite = crate::visitor::types::Composite::new(input, crate::EMPTY_SCALE_INFO_PATH, fields, types, false);
let val = <($($t,)*)>::into_visitor().visit_composite(&mut composite, crate::visitor::TypeId(0));

// Skip over bytes that we decoded:
Expand Down
26 changes: 24 additions & 2 deletions scale-decode/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,21 @@ pub trait DecodeAsType: Sized {
input: &mut &[u8],
type_id: u32,
types: &PortableRegistry,
) -> Result<Self, Error> {
Self::decode_as_type_maybe_compact(input, type_id, types, false)
}

/// Given some input bytes, a `type_id`, and type registry, attempt to decode said bytes into
/// `Self`. Implementations should modify the `&mut` reference to the bytes such that any bytes
/// not used in the course of decoding are still pointed to after decoding is complete.
///
/// If is_compact=true, it is assumed the value is compact encoded (only works for some types).
#[doc(hidden)]
fn decode_as_type_maybe_compact(
input: &mut &[u8],
type_id: u32,
types: &PortableRegistry,
is_compact: bool,
) -> Result<Self, Error>;
}

Expand All @@ -182,12 +197,19 @@ where
T: IntoVisitor,
Error: From<<T::Visitor as Visitor>::Error>,
{
fn decode_as_type(
fn decode_as_type_maybe_compact(
input: &mut &[u8],
type_id: u32,
types: &scale_info::PortableRegistry,
is_compact: bool,
) -> Result<Self, Error> {
let res = visitor::decode_with_visitor(input, type_id, types, T::into_visitor())?;
let res = visitor::decode_with_visitor_maybe_compact(
input,
type_id,
types,
T::into_visitor(),
is_compact,
)?;
Ok(res)
}
}
Expand Down
Loading

0 comments on commit b38d80c

Please sign in to comment.