From 10094987ceb6bc384794c179869880f20fdeb8a1 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 6 Aug 2020 16:28:45 +0100 Subject: [PATCH 01/18] Add owned form for decoding --- src/form.rs | 15 +++++++++++++++ src/ty/mod.rs | 4 ++-- src/ty/path.rs | 34 ++++++++++++++++++++++++---------- 3 files changed, 41 insertions(+), 12 deletions(-) diff --git a/src/form.rs b/src/form.rs index beae8933..16af1003 100644 --- a/src/form.rs +++ b/src/form.rs @@ -42,6 +42,8 @@ use serde::Serialize; pub trait Form { /// The type identifier type. type TypeId: PartialEq + Eq + PartialOrd + Ord + Clone + core::fmt::Debug; + /// The string type. + type String: Serialize + PartialEq + Eq + PartialOrd + Ord + Clone + core::fmt::Debug; } /// A meta meta-type. @@ -53,6 +55,7 @@ pub enum MetaForm {} impl Form for MetaForm { type TypeId = MetaType; + type String = &'static str; } /// Compact form that has its lifetime untracked in association to its interner. @@ -62,9 +65,21 @@ impl Form for MetaForm { /// This resolves some lifetime issues with self-referential structs (such as /// the registry itself) but can no longer be used to resolve to the original /// underlying data. +/// +/// `type String` is owned in order to enable decoding #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Serialize, Debug)] pub enum CompactForm {} impl Form for CompactForm { type TypeId = UntrackedSymbol; + type String = &'static str; +} + +/// Owned form for decoding +#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Serialize, Debug)] +pub enum OwnedForm {} + +impl Form for OwnedForm { + type TypeId = UntrackedSymbol; + type String = String; } diff --git a/src/ty/mod.rs b/src/ty/mod.rs index 84ee120c..f603e3c9 100644 --- a/src/ty/mod.rs +++ b/src/ty/mod.rs @@ -35,7 +35,7 @@ pub use self::{composite::*, fields::*, path::*, variant::*}; pub struct Type { /// The unique path to the type. Can be empty for built-in types #[serde(skip_serializing_if = "Path::is_empty")] - path: Path, + path: Path, /// The generic type parameters of the type in use. Empty for non generic types #[serde(rename = "params", skip_serializing_if = "Vec::is_empty")] type_params: Vec, @@ -49,7 +49,7 @@ impl IntoCompact for Type { fn into_compact(self, registry: &mut Registry) -> Self::Output { Type { - path: self.path, + path: self.path.into_compact(registry), type_params: registry.register_types(self.type_params), type_def: self.type_def.into_compact(registry), } diff --git a/src/ty/path.rs b/src/ty/path.rs index c22050c7..0799a89f 100644 --- a/src/ty/path.rs +++ b/src/ty/path.rs @@ -12,9 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use crate::tm_std::*; - -use crate::utils::is_rust_identifier; +use crate::{IntoCompact, form::{CompactForm, Form, MetaForm}, tm_std::*, utils::is_rust_identifier, Registry}; use serde::Serialize; /// Represents the path of a type definition. @@ -26,17 +24,30 @@ use serde::Serialize; /// Rust prelude type may have an empty namespace definition. #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Debug)] #[serde(transparent)] -pub struct Path { +pub struct Path { /// The segments of the namespace. - segments: Vec<&'static str>, + segments: Vec, } -impl Default for Path { +impl Default for Path +where + F: Form, +{ fn default() -> Self { Path { segments: Vec::new() } } } +impl IntoCompact for Path { + type Output = Path; + + fn into_compact(self, _registry: &mut Registry) -> Self::Output { + Path { + segments: self.segments, + } + } +} + impl Path { /// Create a new Path /// @@ -85,19 +96,22 @@ impl Path { } } -impl Path { +impl Path +where + F: Form +{ /// Returns `true` if the path is empty pub fn is_empty(&self) -> bool { self.segments.is_empty() } /// Get the ident segment of the Path - pub fn ident(&self) -> Option<&str> { - self.segments.iter().last().copied() + pub fn ident(&self) -> Option { + self.segments.iter().last().cloned() } /// Get the namespace segments of the Path - pub fn namespace(&self) -> &[&'static str] { + pub fn namespace(&self) -> &[F::String] { self.segments.split_last().map(|(_, ns)| ns).unwrap_or(&[]) } } From c96769c79dc6864715ad6220313dace1c8baf9e4 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 7 Aug 2020 11:56:41 +0100 Subject: [PATCH 02/18] Derive scale::Decode --- Cargo.toml | 2 ++ src/form.rs | 4 +++- src/registry.rs | 17 ++++++++++++++++- src/ty/composite.rs | 2 +- src/ty/fields.rs | 4 ++-- src/ty/mod.rs | 14 ++++++++------ src/ty/path.rs | 2 +- src/ty/variant.rs | 6 +++--- 8 files changed, 36 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d0fcd63d..57f8084a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,11 +17,13 @@ include = ["Cargo.toml", "src/**/*.rs", "README.md", "LICENSE"] scale-info-derive = { version = "0.2.0", path = "derive", default-features = false, optional = true } serde = { version = "1", default-features = false, features = ["derive", "alloc"] } derive_more = { version = "0.99.1", default-features = false, features = ["from"] } +scale = { package = "parity-scale-codec", version = "1.3.4", default-features = false, features = ["derive"] } [features] default = ["std"] std = [ "serde/std", + "scale/std" ] derive = [ "scale-info-derive" diff --git a/src/form.rs b/src/form.rs index 16af1003..2509e914 100644 --- a/src/form.rs +++ b/src/form.rs @@ -80,6 +80,8 @@ impl Form for CompactForm { pub enum OwnedForm {} impl Form for OwnedForm { - type TypeId = UntrackedSymbol; + /// For correctness should be [`NonZeroU32`](`core::num::NonZeroU32`), but the current + /// (`1.3.x`) release of `parity-scale-codec` does not include the `NoneZero*` Decode impls. + type TypeId = u32; type String = String; } diff --git a/src/registry.rs b/src/registry.rs index e096aef0..0b0a9359 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -26,7 +26,7 @@ use crate::tm_std::*; use crate::{ - form::CompactForm, + form::{CompactForm, OwnedForm}, interner::{Interner, UntrackedSymbol}, meta_type::MetaType, Type, @@ -148,3 +148,18 @@ impl Registry { iter.into_iter().map(|i| i.into_compact(self)).collect::>() } } + +/// A read-only registry, to be used for decoding/deserializing +#[derive(Debug, PartialEq, Eq, scale::Decode)] +struct RegistryReadOnly { + ty: Type, + types: Vec>, +} + +impl RegistryReadOnly { + // Returns the type definition for the given identifier. + pub fn resolve(&self, id: NonZeroU32) -> Option<&Type> { + self.types.get((id.get() - 1) as usize); + None + } +} diff --git a/src/ty/composite.rs b/src/ty/composite.rs index 4c03f922..82f80d8d 100644 --- a/src/ty/composite.rs +++ b/src/ty/composite.rs @@ -47,7 +47,7 @@ use serde::Serialize; /// ``` /// struct JustAMarker; /// ``` -#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Serialize, From)] +#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Serialize, From, scale::Decode)] #[serde(bound = "F::TypeId: Serialize")] #[serde(rename_all = "lowercase")] pub struct TypeDefComposite { diff --git a/src/ty/fields.rs b/src/ty/fields.rs index 8d1fe411..1e4734c5 100644 --- a/src/ty/fields.rs +++ b/src/ty/fields.rs @@ -25,12 +25,12 @@ use serde::Serialize; /// Name is optional so it can represent both named and unnamed fields. /// /// This can be a named field of a struct type or a struct variant. -#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Serialize)] +#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Serialize, scale::Decode)] #[serde(bound = "F::TypeId: Serialize")] pub struct Field { /// The name of the field. None for unnamed fields. #[serde(skip_serializing_if = "Option::is_none")] - name: Option<&'static str>, + name: Option, /// The type of the field. #[serde(rename = "type")] ty: F::TypeId, diff --git a/src/ty/mod.rs b/src/ty/mod.rs index f603e3c9..ce96cf32 100644 --- a/src/ty/mod.rs +++ b/src/ty/mod.rs @@ -20,6 +20,7 @@ use crate::{ IntoCompact, MetaType, Registry, TypeInfo, }; use derive_more::From; +use scale::{Decode}; use serde::Serialize; mod composite; @@ -30,7 +31,7 @@ mod variant; pub use self::{composite::*, fields::*, path::*, variant::*}; /// A [`Type`] definition with optional metadata. -#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, From, Debug, Serialize)] +#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, From, Debug, Serialize, Decode)] #[serde(bound = "F::TypeId: Serialize")] pub struct Type { /// The unique path to the type. Can be empty for built-in types @@ -95,12 +96,13 @@ impl Type { path, type_params: type_params.into_iter().collect(), type_def: type_def.into(), + // marker: MetaForm, } } } /// The possible types a SCALE encodable Rust value could have. -#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, From, Debug, Serialize)] +#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, From, Debug, Serialize, Decode)] #[serde(bound = "F::TypeId: Serialize")] #[serde(rename_all = "camelCase")] pub enum TypeDef { @@ -134,7 +136,7 @@ impl IntoCompact for TypeDef { } /// A primitive Rust type. -#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Debug)] +#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Decode, Debug)] #[serde(rename_all = "lowercase")] pub enum TypeDefPrimitive { /// `bool` type @@ -166,7 +168,7 @@ pub enum TypeDefPrimitive { } /// An array type. -#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Debug)] +#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Decode, Debug)] #[serde(bound = "F::TypeId: Serialize")] pub struct TypeDefArray { /// The length of the array type. @@ -195,7 +197,7 @@ impl TypeDefArray { } /// A type to refer to tuple types. -#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Debug)] +#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Decode, Debug)] #[serde(bound = "F::TypeId: Serialize")] #[serde(transparent)] pub struct TypeDefTuple { @@ -231,7 +233,7 @@ impl TypeDefTuple { } /// A type to refer to a sequence of elements of the same type. -#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Debug)] +#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Decode, Debug)] #[serde(bound = "F::TypeId: Serialize")] pub struct TypeDefSequence { /// The element type of the sequence type. diff --git a/src/ty/path.rs b/src/ty/path.rs index 0799a89f..71d7c0ca 100644 --- a/src/ty/path.rs +++ b/src/ty/path.rs @@ -22,7 +22,7 @@ use serde::Serialize; /// has been defined. The last /// /// Rust prelude type may have an empty namespace definition. -#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Debug)] +#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Debug, scale::Decode)] #[serde(transparent)] pub struct Path { /// The segments of the namespace. diff --git a/src/ty/variant.rs b/src/ty/variant.rs index 04f93aba..7e7e84e6 100644 --- a/src/ty/variant.rs +++ b/src/ty/variant.rs @@ -60,7 +60,7 @@ use serde::Serialize; /// ``` /// enum JustAMarker {} /// ``` -#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Serialize, From)] +#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Serialize, From, scale::Decode)] #[serde(bound = "F::TypeId: Serialize")] #[serde(rename_all = "lowercase")] pub struct TypeDefVariant { @@ -105,11 +105,11 @@ impl TypeDefVariant { /// // ^^^^^^^^^^^^^^^^^^^^^ this is a struct enum variant /// } /// ``` -#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Serialize)] +#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Serialize, scale::Decode)] #[serde(bound = "F::TypeId: Serialize")] pub struct Variant { /// The name of the struct variant. - name: &'static str, + name: F::String, /// The fields of the struct variant. #[serde(skip_serializing_if = "Vec::is_empty")] fields: Vec>, From 26089cb0459c3669f2ab6cb8136d6ff923cab987 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 7 Aug 2020 16:47:01 +0100 Subject: [PATCH 03/18] Implement scale encoding --- src/lib.rs | 2 +- src/registry.rs | 26 ++++++++++++++---- src/tm_std.rs | 1 + src/ty/mod.rs | 18 ++++++------- test_suite/Cargo.toml | 1 + test_suite/tests/codec.rs | 57 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 90 insertions(+), 15 deletions(-) create mode 100644 test_suite/tests/codec.rs diff --git a/src/lib.rs b/src/lib.rs index bea069c5..75c808ff 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -125,7 +125,7 @@ mod tests; pub use self::{ meta_type::MetaType, - registry::{IntoCompact, Registry}, + registry::{IntoCompact, Registry, RegistryReadOnly}, ty::*, }; diff --git a/src/registry.rs b/src/registry.rs index 0b0a9359..4b27cfeb 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -31,7 +31,7 @@ use crate::{ meta_type::MetaType, Type, }; -use serde::Serialize; +use serde::{Deserialize, Serialize}; /// Compacts the implementor using a registry. pub trait IntoCompact { @@ -88,6 +88,23 @@ impl Default for Registry { } } +impl scale::Encode for Registry { + fn size_hint(&self) -> usize { + mem::size_of::() + mem::size_of::>() * self.types.len() + } + + fn encode_to(&self, dest: &mut W) { + if self.types.len() > u32::max_value() as usize { + panic!("Attempted to encode too many elements."); + } + scale::Compact(self.types.len() as u32).encode_to(dest); + + for ty in self.types.values() { + ty.encode_to(dest); + } + } +} + impl Registry { /// Creates a new empty registry. pub fn new() -> Self { @@ -150,14 +167,13 @@ impl Registry { } /// A read-only registry, to be used for decoding/deserializing -#[derive(Debug, PartialEq, Eq, scale::Decode)] -struct RegistryReadOnly { - ty: Type, +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, scale::Decode)] +pub struct RegistryReadOnly { types: Vec>, } impl RegistryReadOnly { - // Returns the type definition for the given identifier. + /// Returns the type definition for the given identifier, `None` if no type found for that ID. pub fn resolve(&self, id: NonZeroU32) -> Option<&Type> { self.types.get((id.get() - 1) as usize); None diff --git a/src/tm_std.rs b/src/tm_std.rs index 8916d07f..ec0cccec 100644 --- a/src/tm_std.rs +++ b/src/tm_std.rs @@ -40,6 +40,7 @@ pub use self::core::{ fmt::{Debug, Error as FmtError, Formatter}, hash::{Hash, Hasher}, iter, + mem, }; mod alloc { diff --git a/src/ty/mod.rs b/src/ty/mod.rs index ce96cf32..ce428c08 100644 --- a/src/ty/mod.rs +++ b/src/ty/mod.rs @@ -20,8 +20,8 @@ use crate::{ IntoCompact, MetaType, Registry, TypeInfo, }; use derive_more::From; -use scale::{Decode}; -use serde::Serialize; +use scale::{Decode, Encode}; +use serde::{Deserialize, Serialize}; mod composite; mod fields; @@ -31,7 +31,7 @@ mod variant; pub use self::{composite::*, fields::*, path::*, variant::*}; /// A [`Type`] definition with optional metadata. -#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, From, Debug, Serialize, Decode)] +#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, From, Debug, Serialize, Deserialize, Encode, Decode)] #[serde(bound = "F::TypeId: Serialize")] pub struct Type { /// The unique path to the type. Can be empty for built-in types @@ -102,7 +102,7 @@ impl Type { } /// The possible types a SCALE encodable Rust value could have. -#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, From, Debug, Serialize, Decode)] +#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, From, Debug, Serialize, Deserialize, Encode, Decode)] #[serde(bound = "F::TypeId: Serialize")] #[serde(rename_all = "camelCase")] pub enum TypeDef { @@ -136,7 +136,7 @@ impl IntoCompact for TypeDef { } /// A primitive Rust type. -#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Decode, Debug)] +#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Deserialize, Encode, Decode, Debug)] #[serde(rename_all = "lowercase")] pub enum TypeDefPrimitive { /// `bool` type @@ -168,7 +168,7 @@ pub enum TypeDefPrimitive { } /// An array type. -#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Decode, Debug)] +#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Deserialize, Encode, Decode, Debug)] #[serde(bound = "F::TypeId: Serialize")] pub struct TypeDefArray { /// The length of the array type. @@ -197,8 +197,8 @@ impl TypeDefArray { } /// A type to refer to tuple types. -#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Decode, Debug)] -#[serde(bound = "F::TypeId: Serialize")] +#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Deserialize, Encode, Decode, Debug)] +#[serde(bound = "F::TypeId: Serialize + Deserialize")] #[serde(transparent)] pub struct TypeDefTuple { /// The types of the tuple fields. @@ -233,7 +233,7 @@ impl TypeDefTuple { } /// A type to refer to a sequence of elements of the same type. -#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Decode, Debug)] +#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Deserialize, Encode, Decode, Debug)] #[serde(bound = "F::TypeId: Serialize")] pub struct TypeDefSequence { /// The element type of the sequence type. diff --git a/test_suite/Cargo.toml b/test_suite/Cargo.toml index d9c9034d..830b5e15 100644 --- a/test_suite/Cargo.toml +++ b/test_suite/Cargo.toml @@ -12,6 +12,7 @@ license = "Apache-2.0" [dependencies] scale-info = { path = "..", features = ["derive"] } +scale = { package = "parity-scale-codec", version = "1.3.4", default-features = false, features = ["derive"] } serde = "1.0" serde_json = "1.0" pretty_assertions = "0.6.1" diff --git a/test_suite/tests/codec.rs b/test_suite/tests/codec.rs new file mode 100644 index 00000000..ebc770f6 --- /dev/null +++ b/test_suite/tests/codec.rs @@ -0,0 +1,57 @@ +// Copyright 2019-2020 Parity Technologies (UK) Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#![allow(unused)] +#![cfg_attr(not(feature = "std"), no_std)] +#![allow(dead_code)] + +#[cfg(not(feature = "std"))] +extern crate alloc; + +#[cfg(not(feature = "std"))] +use alloc::{vec, vec::Vec}; + +use pretty_assertions::{assert_eq, assert_ne}; +use scale::{Encode, Decode}; +use scale_info::{form::CompactForm, MetaType, IntoCompact as _, Registry, RegistryReadOnly, TypeInfo}; + +#[derive(TypeInfo)] +struct A { + a: bool, + b: Result, + c: T +} + +#[derive(TypeInfo)] +enum B { + A, + B(A), + C { + d: [u8; 32] + } +} + +#[test] +fn encode_decode_to_readonly() { + let mut registry = Registry::new(); + registry.register_type(&MetaType::new::>()); + + let mut encoded = registry.encode(); + let original_serialized = serde_json::to_value(registry).unwrap(); + + let readonly_decoded = RegistryReadOnly::decode(&mut encoded[..]).unwrap(); + let decoded_serialized = serde_json::to_value(readonly_decoded).unwrap(); + + assert_eq!(decoded_serialized, original_serialized); +} From 7cb5d0ba92c3bc55dde4a03bd9abed90d1817bb0 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 7 Aug 2020 17:03:41 +0100 Subject: [PATCH 04/18] Rename Form type param from F to T to avoid same name as scale Decode derive --- README.md | 20 ++++++++++---------- src/registry.rs | 5 +++-- src/ty/composite.rs | 6 +++--- src/ty/fields.rs | 13 +++++++------ src/ty/mod.rs | 42 +++++++++++++++++++++--------------------- src/ty/path.rs | 21 +++++++++++---------- src/ty/variant.rs | 14 +++++++------- 7 files changed, 62 insertions(+), 59 deletions(-) diff --git a/README.md b/README.md index 32fcb44c..fa4e6e5b 100644 --- a/README.md +++ b/README.md @@ -22,28 +22,28 @@ pub trait TypeInfo { Types implementing this trait build up and return a `Type` struct: ```rust -pub struct Type { +pub struct Type { /// The unique path to the type. Can be empty for built-in types - path: Path, + path: Path, /// The generic type parameters of the type in use. Empty for non generic types - type_params: Vec, + type_params: Vec, /// The actual type definition - type_def: TypeDef, + type_def: TypeDef, } ``` Types are defined as one of the following variants: ```rust -pub enum TypeDef { +pub enum TypeDef { /// A composite type (e.g. a struct or a tuple) - Composite(TypeDefComposite), + Composite(TypeDefComposite), /// A variant type (e.g. an enum) - Variant(TypeDefVariant), + Variant(TypeDefVariant), /// A sequence type with runtime known length. - Sequence(TypeDefSequence), + Sequence(TypeDefSequence), /// An array type with compile-time known length. - Array(TypeDefArray), + Array(TypeDefArray), /// A tuple type. - Tuple(TypeDefTuple), + Tuple(TypeDefTuple), /// A Rust primitive type. Primitive(TypeDefPrimitive), } diff --git a/src/registry.rs b/src/registry.rs index 4b27cfeb..74f1e298 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -31,6 +31,7 @@ use crate::{ meta_type::MetaType, Type, }; +use scale::{Encode, Decode}; use serde::{Deserialize, Serialize}; /// Compacts the implementor using a registry. @@ -88,7 +89,7 @@ impl Default for Registry { } } -impl scale::Encode for Registry { +impl Encode for Registry { fn size_hint(&self) -> usize { mem::size_of::() + mem::size_of::>() * self.types.len() } @@ -167,7 +168,7 @@ impl Registry { } /// A read-only registry, to be used for decoding/deserializing -#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, scale::Decode)] +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Decode)] pub struct RegistryReadOnly { types: Vec>, } diff --git a/src/ty/composite.rs b/src/ty/composite.rs index 82f80d8d..2f5a642a 100644 --- a/src/ty/composite.rs +++ b/src/ty/composite.rs @@ -48,11 +48,11 @@ use serde::Serialize; /// struct JustAMarker; /// ``` #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Serialize, From, scale::Decode)] -#[serde(bound = "F::TypeId: Serialize")] +#[serde(bound = "T::TypeId: Serialize")] #[serde(rename_all = "lowercase")] -pub struct TypeDefComposite { +pub struct TypeDefComposite { #[serde(skip_serializing_if = "Vec::is_empty")] - fields: Vec>, + fields: Vec>, } impl IntoCompact for TypeDefComposite { diff --git a/src/ty/fields.rs b/src/ty/fields.rs index 1e4734c5..7003e8ea 100644 --- a/src/ty/fields.rs +++ b/src/ty/fields.rs @@ -18,22 +18,23 @@ use crate::{ form::{CompactForm, Form, MetaForm}, IntoCompact, MetaType, Registry, TypeInfo, }; -use serde::Serialize; +use scale::{Decode, Encode}; +use serde::{Serialize, Deserialize}; /// A field of a struct like data type. /// /// Name is optional so it can represent both named and unnamed fields. /// /// This can be a named field of a struct type or a struct variant. -#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Serialize, scale::Decode)] -#[serde(bound = "F::TypeId: Serialize")] -pub struct Field { +#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Serialize, Deserialize, Encode, Decode)] +#[serde(bound = "T::TypeId: Serialize")] +pub struct Field { /// The name of the field. None for unnamed fields. #[serde(skip_serializing_if = "Option::is_none")] - name: Option, + name: Option, /// The type of the field. #[serde(rename = "type")] - ty: F::TypeId, + ty: T::TypeId, } impl IntoCompact for Field { diff --git a/src/ty/mod.rs b/src/ty/mod.rs index ce428c08..ee23eb5d 100644 --- a/src/ty/mod.rs +++ b/src/ty/mod.rs @@ -32,17 +32,17 @@ pub use self::{composite::*, fields::*, path::*, variant::*}; /// A [`Type`] definition with optional metadata. #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, From, Debug, Serialize, Deserialize, Encode, Decode)] -#[serde(bound = "F::TypeId: Serialize")] -pub struct Type { +#[serde(bound = "T::TypeId: Serialize")] +pub struct Type { /// The unique path to the type. Can be empty for built-in types #[serde(skip_serializing_if = "Path::is_empty")] - path: Path, + path: Path, /// The generic type parameters of the type in use. Empty for non generic types #[serde(rename = "params", skip_serializing_if = "Vec::is_empty")] - type_params: Vec, + type_params: Vec, /// The actual type definition #[serde(rename = "def")] - type_def: TypeDef, + type_def: TypeDef, } impl IntoCompact for Type { @@ -103,19 +103,19 @@ impl Type { /// The possible types a SCALE encodable Rust value could have. #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, From, Debug, Serialize, Deserialize, Encode, Decode)] -#[serde(bound = "F::TypeId: Serialize")] +#[serde(bound = "T::TypeId: Serialize")] #[serde(rename_all = "camelCase")] -pub enum TypeDef { +pub enum TypeDef { /// A composite type (e.g. a struct or a tuple) - Composite(TypeDefComposite), + Composite(TypeDefComposite), /// A variant type (e.g. an enum) - Variant(TypeDefVariant), + Variant(TypeDefVariant), /// A sequence type with runtime known length. - Sequence(TypeDefSequence), + Sequence(TypeDefSequence), /// An array type with compile-time known length. - Array(TypeDefArray), + Array(TypeDefArray), /// A tuple type. - Tuple(TypeDefTuple), + Tuple(TypeDefTuple), /// A Rust primitive type. Primitive(TypeDefPrimitive), } @@ -169,13 +169,13 @@ pub enum TypeDefPrimitive { /// An array type. #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Deserialize, Encode, Decode, Debug)] -#[serde(bound = "F::TypeId: Serialize")] -pub struct TypeDefArray { +#[serde(bound = "T::TypeId: Serialize")] +pub struct TypeDefArray { /// The length of the array type. pub len: u32, /// The element type of the array type. #[serde(rename = "type")] - pub type_param: F::TypeId, + pub type_param: T::TypeId, } impl IntoCompact for TypeDefArray { @@ -198,11 +198,11 @@ impl TypeDefArray { /// A type to refer to tuple types. #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Deserialize, Encode, Decode, Debug)] -#[serde(bound = "F::TypeId: Serialize + Deserialize")] +#[serde(bound = "T::TypeId: Serialize")] #[serde(transparent)] -pub struct TypeDefTuple { +pub struct TypeDefTuple { /// The types of the tuple fields. - fields: Vec, + fields: Vec, } impl IntoCompact for TypeDefTuple { @@ -234,11 +234,11 @@ impl TypeDefTuple { /// A type to refer to a sequence of elements of the same type. #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Deserialize, Encode, Decode, Debug)] -#[serde(bound = "F::TypeId: Serialize")] -pub struct TypeDefSequence { +#[serde(bound = "T::TypeId: Serialize")] +pub struct TypeDefSequence { /// The element type of the sequence type. #[serde(rename = "type")] - type_param: F::TypeId, + type_param: T::TypeId, } impl IntoCompact for TypeDefSequence { diff --git a/src/ty/path.rs b/src/ty/path.rs index 71d7c0ca..16d9ee22 100644 --- a/src/ty/path.rs +++ b/src/ty/path.rs @@ -13,7 +13,8 @@ // limitations under the License. use crate::{IntoCompact, form::{CompactForm, Form, MetaForm}, tm_std::*, utils::is_rust_identifier, Registry}; -use serde::Serialize; +use scale::{Decode, Encode}; +use serde::{Serialize, Deserialize}; /// Represents the path of a type definition. /// @@ -22,16 +23,16 @@ use serde::Serialize; /// has been defined. The last /// /// Rust prelude type may have an empty namespace definition. -#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Debug, scale::Decode)] +#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Deserialize, Encode, Decode)] #[serde(transparent)] -pub struct Path { +pub struct Path { /// The segments of the namespace. - segments: Vec, + segments: Vec, } -impl Default for Path +impl Default for Path where - F: Form, + T: Form, { fn default() -> Self { Path { segments: Vec::new() } @@ -96,9 +97,9 @@ impl Path { } } -impl Path +impl Path where - F: Form + T: Form { /// Returns `true` if the path is empty pub fn is_empty(&self) -> bool { @@ -106,12 +107,12 @@ where } /// Get the ident segment of the Path - pub fn ident(&self) -> Option { + pub fn ident(&self) -> Option { self.segments.iter().last().cloned() } /// Get the namespace segments of the Path - pub fn namespace(&self) -> &[F::String] { + pub fn namespace(&self) -> &[T::String] { self.segments.split_last().map(|(_, ns)| ns).unwrap_or(&[]) } } diff --git a/src/ty/variant.rs b/src/ty/variant.rs index 7e7e84e6..ed9acdfd 100644 --- a/src/ty/variant.rs +++ b/src/ty/variant.rs @@ -61,11 +61,11 @@ use serde::Serialize; /// enum JustAMarker {} /// ``` #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Serialize, From, scale::Decode)] -#[serde(bound = "F::TypeId: Serialize")] +#[serde(bound = "T::TypeId: Serialize")] #[serde(rename_all = "lowercase")] -pub struct TypeDefVariant { +pub struct TypeDefVariant { #[serde(skip_serializing_if = "Vec::is_empty")] - variants: Vec>, + variants: Vec>, } impl IntoCompact for TypeDefVariant { @@ -106,13 +106,13 @@ impl TypeDefVariant { /// } /// ``` #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Serialize, scale::Decode)] -#[serde(bound = "F::TypeId: Serialize")] -pub struct Variant { +#[serde(bound = "T::TypeId: Serialize")] +pub struct Variant { /// The name of the struct variant. - name: F::String, + name: T::String, /// The fields of the struct variant. #[serde(skip_serializing_if = "Vec::is_empty")] - fields: Vec>, + fields: Vec>, /// The discriminant of the variant. /// /// # Note From 4802d45ece32e6642df60beafc1d79738ed5b54e Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 7 Aug 2020 17:30:42 +0100 Subject: [PATCH 05/18] Derive serde::Deserialize --- src/interner.rs | 9 +++++++-- src/ty/composite.rs | 7 ++++--- src/ty/fields.rs | 4 ++-- src/ty/mod.rs | 12 ++++++------ src/ty/path.rs | 2 +- src/ty/variant.rs | 9 +++++---- 6 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/interner.rs b/src/interner.rs index 2bedfe19..b797d155 100644 --- a/src/interner.rs +++ b/src/interner.rs @@ -22,13 +22,14 @@ //! elements and is later used for compact serialization within the registry. use crate::tm_std::*; -use serde::Serialize; +use scale::Encode; +use serde::{Serialize, Deserialize, de::DeserializeOwned}; /// A symbol that is not lifetime tracked. /// /// This can be used by self-referential types but /// can no longer be used to resolve instances. -#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize)] +#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] #[serde(transparent)] pub struct UntrackedSymbol { id: NonZeroU32, @@ -36,6 +37,10 @@ pub struct UntrackedSymbol { marker: PhantomData T>, } +impl Encode for UntrackedSymbol { + +} + /// A symbol from an interner. /// /// Can be used to resolve to the associated instance. diff --git a/src/ty/composite.rs b/src/ty/composite.rs index 2f5a642a..88fcbc67 100644 --- a/src/ty/composite.rs +++ b/src/ty/composite.rs @@ -19,7 +19,8 @@ use crate::{ Field, IntoCompact, Registry, }; use derive_more::From; -use serde::Serialize; +use scale::{Encode, Decode}; +use serde::{Serialize, Deserialize, de::DeserializeOwned}; /// A composite type, consisting of either named (struct) or unnamed (tuple /// struct) fields @@ -47,8 +48,8 @@ use serde::Serialize; /// ``` /// struct JustAMarker; /// ``` -#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Serialize, From, scale::Decode)] -#[serde(bound = "T::TypeId: Serialize")] +#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, From, Serialize, Deserialize, Encode, Decode)] +#[serde(bound(serialize = "T::TypeId: Serialize", deserialize = "T::TypeId: DeserializeOwned"))] #[serde(rename_all = "lowercase")] pub struct TypeDefComposite { #[serde(skip_serializing_if = "Vec::is_empty")] diff --git a/src/ty/fields.rs b/src/ty/fields.rs index 7003e8ea..9ff17fc6 100644 --- a/src/ty/fields.rs +++ b/src/ty/fields.rs @@ -19,7 +19,7 @@ use crate::{ IntoCompact, MetaType, Registry, TypeInfo, }; use scale::{Decode, Encode}; -use serde::{Serialize, Deserialize}; +use serde::{Serialize, Deserialize, de::DeserializeOwned}; /// A field of a struct like data type. /// @@ -27,7 +27,7 @@ use serde::{Serialize, Deserialize}; /// /// This can be a named field of a struct type or a struct variant. #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Serialize, Deserialize, Encode, Decode)] -#[serde(bound = "T::TypeId: Serialize")] +#[serde(bound(serialize = "T::TypeId: Serialize", deserialize = "T::TypeId: DeserializeOwned"))] pub struct Field { /// The name of the field. None for unnamed fields. #[serde(skip_serializing_if = "Option::is_none")] diff --git a/src/ty/mod.rs b/src/ty/mod.rs index ee23eb5d..1bcd373e 100644 --- a/src/ty/mod.rs +++ b/src/ty/mod.rs @@ -21,7 +21,7 @@ use crate::{ }; use derive_more::From; use scale::{Decode, Encode}; -use serde::{Deserialize, Serialize}; +use serde::{Serialize, Deserialize, de::DeserializeOwned}; mod composite; mod fields; @@ -32,7 +32,7 @@ pub use self::{composite::*, fields::*, path::*, variant::*}; /// A [`Type`] definition with optional metadata. #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, From, Debug, Serialize, Deserialize, Encode, Decode)] -#[serde(bound = "T::TypeId: Serialize")] +#[serde(bound(serialize = "T::TypeId: Serialize", deserialize = "T::TypeId: DeserializeOwned"))] pub struct Type { /// The unique path to the type. Can be empty for built-in types #[serde(skip_serializing_if = "Path::is_empty")] @@ -103,7 +103,7 @@ impl Type { /// The possible types a SCALE encodable Rust value could have. #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, From, Debug, Serialize, Deserialize, Encode, Decode)] -#[serde(bound = "T::TypeId: Serialize")] +#[serde(bound(serialize = "T::TypeId: Serialize", deserialize = "T::TypeId: DeserializeOwned"))] #[serde(rename_all = "camelCase")] pub enum TypeDef { /// A composite type (e.g. a struct or a tuple) @@ -169,7 +169,7 @@ pub enum TypeDefPrimitive { /// An array type. #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Deserialize, Encode, Decode, Debug)] -#[serde(bound = "T::TypeId: Serialize")] +#[serde(bound(serialize = "T::TypeId: Serialize", deserialize = "T::TypeId: DeserializeOwned"))] pub struct TypeDefArray { /// The length of the array type. pub len: u32, @@ -198,7 +198,7 @@ impl TypeDefArray { /// A type to refer to tuple types. #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Deserialize, Encode, Decode, Debug)] -#[serde(bound = "T::TypeId: Serialize")] +#[serde(bound(serialize = "T::TypeId: Serialize", deserialize = "T::TypeId: DeserializeOwned"))] #[serde(transparent)] pub struct TypeDefTuple { /// The types of the tuple fields. @@ -234,7 +234,7 @@ impl TypeDefTuple { /// A type to refer to a sequence of elements of the same type. #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Deserialize, Encode, Decode, Debug)] -#[serde(bound = "T::TypeId: Serialize")] +#[serde(bound(serialize = "T::TypeId: Serialize", deserialize = "T::TypeId: DeserializeOwned"))] pub struct TypeDefSequence { /// The element type of the sequence type. #[serde(rename = "type")] diff --git a/src/ty/path.rs b/src/ty/path.rs index 16d9ee22..4ecd8698 100644 --- a/src/ty/path.rs +++ b/src/ty/path.rs @@ -14,7 +14,7 @@ use crate::{IntoCompact, form::{CompactForm, Form, MetaForm}, tm_std::*, utils::is_rust_identifier, Registry}; use scale::{Decode, Encode}; -use serde::{Serialize, Deserialize}; +use serde::{Serialize, Deserialize, de::DeserializeOwned}; /// Represents the path of a type definition. /// diff --git a/src/ty/variant.rs b/src/ty/variant.rs index ed9acdfd..f048f099 100644 --- a/src/ty/variant.rs +++ b/src/ty/variant.rs @@ -20,7 +20,8 @@ use crate::{ Field, IntoCompact, Registry, }; use derive_more::From; -use serde::Serialize; +use scale::{Encode, Decode}; +use serde::{Serialize, Deserialize, de::DeserializeOwned}; /// A Enum type (consisting of variants). /// @@ -60,8 +61,8 @@ use serde::Serialize; /// ``` /// enum JustAMarker {} /// ``` -#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Serialize, From, scale::Decode)] -#[serde(bound = "T::TypeId: Serialize")] +#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, From, Serialize, Deserialize, Encode, Decode)] +#[serde(bound(serialize = "T::TypeId: Serialize", deserialize = "T::TypeId: DeserializeOwned"))] #[serde(rename_all = "lowercase")] pub struct TypeDefVariant { #[serde(skip_serializing_if = "Vec::is_empty")] @@ -106,7 +107,7 @@ impl TypeDefVariant { /// } /// ``` #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Serialize, scale::Decode)] -#[serde(bound = "T::TypeId: Serialize")] +#[serde(bound(serialize = "T::TypeId: Serialize", deserialize = "T::TypeId: DeserializeOwned"))] pub struct Variant { /// The name of the struct variant. name: T::String, From bd5c3e7bbe1c9082d932e906ac3a80af3d122cbd Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 10 Aug 2020 08:53:35 +0100 Subject: [PATCH 06/18] Add missing derives --- src/interner.rs | 8 ++------ src/ty/composite.rs | 5 ++++- src/ty/fields.rs | 5 ++++- src/ty/mod.rs | 10 ++++++++-- src/ty/path.rs | 4 ++++ src/ty/variant.rs | 12 +++++++++--- test_suite/tests/codec.rs | 12 ++++++------ 7 files changed, 37 insertions(+), 19 deletions(-) diff --git a/src/interner.rs b/src/interner.rs index b797d155..7b3f9cc7 100644 --- a/src/interner.rs +++ b/src/interner.rs @@ -23,13 +23,13 @@ use crate::tm_std::*; use scale::Encode; -use serde::{Serialize, Deserialize, de::DeserializeOwned}; +use serde::Serialize; /// A symbol that is not lifetime tracked. /// /// This can be used by self-referential types but /// can no longer be used to resolve instances. -#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] +#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize)] #[serde(transparent)] pub struct UntrackedSymbol { id: NonZeroU32, @@ -37,10 +37,6 @@ pub struct UntrackedSymbol { marker: PhantomData T>, } -impl Encode for UntrackedSymbol { - -} - /// A symbol from an interner. /// /// Can be used to resolve to the associated instance. diff --git a/src/ty/composite.rs b/src/ty/composite.rs index 88fcbc67..2adfb645 100644 --- a/src/ty/composite.rs +++ b/src/ty/composite.rs @@ -49,7 +49,10 @@ use serde::{Serialize, Deserialize, de::DeserializeOwned}; /// struct JustAMarker; /// ``` #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, From, Serialize, Deserialize, Encode, Decode)] -#[serde(bound(serialize = "T::TypeId: Serialize", deserialize = "T::TypeId: DeserializeOwned"))] +#[serde(bound( + serialize = "T::TypeId: Serialize, T::String: Serialize", + deserialize = "T::TypeId: DeserializeOwned, T::String: DeserializeOwned" +))] #[serde(rename_all = "lowercase")] pub struct TypeDefComposite { #[serde(skip_serializing_if = "Vec::is_empty")] diff --git a/src/ty/fields.rs b/src/ty/fields.rs index 9ff17fc6..a73fd106 100644 --- a/src/ty/fields.rs +++ b/src/ty/fields.rs @@ -27,7 +27,10 @@ use serde::{Serialize, Deserialize, de::DeserializeOwned}; /// /// This can be a named field of a struct type or a struct variant. #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Serialize, Deserialize, Encode, Decode)] -#[serde(bound(serialize = "T::TypeId: Serialize", deserialize = "T::TypeId: DeserializeOwned"))] +#[serde(bound( + serialize = "T::TypeId: Serialize, T::String: Serialize", + deserialize = "T::TypeId: DeserializeOwned, T::String: DeserializeOwned" +))] pub struct Field { /// The name of the field. None for unnamed fields. #[serde(skip_serializing_if = "Option::is_none")] diff --git a/src/ty/mod.rs b/src/ty/mod.rs index 1bcd373e..2341f624 100644 --- a/src/ty/mod.rs +++ b/src/ty/mod.rs @@ -32,7 +32,10 @@ pub use self::{composite::*, fields::*, path::*, variant::*}; /// A [`Type`] definition with optional metadata. #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, From, Debug, Serialize, Deserialize, Encode, Decode)] -#[serde(bound(serialize = "T::TypeId: Serialize", deserialize = "T::TypeId: DeserializeOwned"))] +#[serde(bound( + serialize = "T::TypeId: Serialize, T::String: Serialize", + deserialize = "T::TypeId: DeserializeOwned, T::String: DeserializeOwned" +))] pub struct Type { /// The unique path to the type. Can be empty for built-in types #[serde(skip_serializing_if = "Path::is_empty")] @@ -103,7 +106,10 @@ impl Type { /// The possible types a SCALE encodable Rust value could have. #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, From, Debug, Serialize, Deserialize, Encode, Decode)] -#[serde(bound(serialize = "T::TypeId: Serialize", deserialize = "T::TypeId: DeserializeOwned"))] +#[serde(bound( + serialize = "T::TypeId: Serialize, T::String: Serialize", + deserialize = "T::TypeId: DeserializeOwned, T::String: DeserializeOwned" +))] #[serde(rename_all = "camelCase")] pub enum TypeDef { /// A composite type (e.g. a struct or a tuple) diff --git a/src/ty/path.rs b/src/ty/path.rs index 4ecd8698..ff8bda8e 100644 --- a/src/ty/path.rs +++ b/src/ty/path.rs @@ -25,6 +25,10 @@ use serde::{Serialize, Deserialize, de::DeserializeOwned}; /// Rust prelude type may have an empty namespace definition. #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Deserialize, Encode, Decode)] #[serde(transparent)] +#[serde(bound( + serialize = "T::TypeId: Serialize, T::String: Serialize", + deserialize = "T::TypeId: DeserializeOwned, T::String: DeserializeOwned" +))] pub struct Path { /// The segments of the namespace. segments: Vec, diff --git a/src/ty/variant.rs b/src/ty/variant.rs index f048f099..e2d9aa01 100644 --- a/src/ty/variant.rs +++ b/src/ty/variant.rs @@ -62,7 +62,10 @@ use serde::{Serialize, Deserialize, de::DeserializeOwned}; /// enum JustAMarker {} /// ``` #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, From, Serialize, Deserialize, Encode, Decode)] -#[serde(bound(serialize = "T::TypeId: Serialize", deserialize = "T::TypeId: DeserializeOwned"))] +#[serde(bound( + serialize = "T::TypeId: Serialize, T::String: Serialize", + deserialize = "T::TypeId: DeserializeOwned, T::String: DeserializeOwned" +))] #[serde(rename_all = "lowercase")] pub struct TypeDefVariant { #[serde(skip_serializing_if = "Vec::is_empty")] @@ -106,8 +109,11 @@ impl TypeDefVariant { /// // ^^^^^^^^^^^^^^^^^^^^^ this is a struct enum variant /// } /// ``` -#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Serialize, scale::Decode)] -#[serde(bound(serialize = "T::TypeId: Serialize", deserialize = "T::TypeId: DeserializeOwned"))] +#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Serialize, Deserialize, Encode, Decode)] +#[serde(bound( + serialize = "T::TypeId: Serialize, T::String: Serialize", + deserialize = "T::TypeId: DeserializeOwned, T::String: DeserializeOwned" +))] pub struct Variant { /// The name of the struct variant. name: T::String, diff --git a/test_suite/tests/codec.rs b/test_suite/tests/codec.rs index ebc770f6..2ba651ac 100644 --- a/test_suite/tests/codec.rs +++ b/test_suite/tests/codec.rs @@ -48,10 +48,10 @@ fn encode_decode_to_readonly() { registry.register_type(&MetaType::new::>()); let mut encoded = registry.encode(); - let original_serialized = serde_json::to_value(registry).unwrap(); - - let readonly_decoded = RegistryReadOnly::decode(&mut encoded[..]).unwrap(); - let decoded_serialized = serde_json::to_value(readonly_decoded).unwrap(); - - assert_eq!(decoded_serialized, original_serialized); + // let original_serialized = serde_json::to_value(registry).unwrap(); + // + // let readonly_decoded = RegistryReadOnly::decode(&mut &encoded[..]).unwrap(); + // let decoded_serialized = serde_json::to_value(readonly_decoded).unwrap(); + // + // assert_eq!(decoded_serialized, original_serialized); } From d725cc321312ab363d0e0c01febb9860530970a4 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 10 Aug 2020 10:21:43 +0100 Subject: [PATCH 07/18] Fix up test --- src/interner.rs | 6 +++++- test_suite/tests/codec.rs | 14 +++++++------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/interner.rs b/src/interner.rs index 7b3f9cc7..a212adfe 100644 --- a/src/interner.rs +++ b/src/interner.rs @@ -22,7 +22,6 @@ //! elements and is later used for compact serialization within the registry. use crate::tm_std::*; -use scale::Encode; use serde::Serialize; /// A symbol that is not lifetime tracked. @@ -36,6 +35,11 @@ pub struct UntrackedSymbol { #[serde(skip)] marker: PhantomData T>, } +impl scale::Encode for UntrackedSymbol { + fn encode_to(&self, dest: &mut W) { + self.id.get().encode_to(dest) + } +} /// A symbol from an interner. /// diff --git a/test_suite/tests/codec.rs b/test_suite/tests/codec.rs index 2ba651ac..29b47081 100644 --- a/test_suite/tests/codec.rs +++ b/test_suite/tests/codec.rs @@ -43,15 +43,15 @@ enum B { } #[test] -fn encode_decode_to_readonly() { +fn scale_encode_then_decode_to_readonly() { let mut registry = Registry::new(); registry.register_type(&MetaType::new::>()); let mut encoded = registry.encode(); - // let original_serialized = serde_json::to_value(registry).unwrap(); - // - // let readonly_decoded = RegistryReadOnly::decode(&mut &encoded[..]).unwrap(); - // let decoded_serialized = serde_json::to_value(readonly_decoded).unwrap(); - // - // assert_eq!(decoded_serialized, original_serialized); + let original_serialized = serde_json::to_value(registry).unwrap(); + + let readonly_decoded = RegistryReadOnly::decode(&mut &encoded[..]).unwrap(); + let decoded_serialized = serde_json::to_value(readonly_decoded).unwrap(); + + assert_eq!(decoded_serialized, original_serialized); } From d4276ecef6234d57920e6da7c93df0d9779bfdee Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 10 Aug 2020 10:53:18 +0100 Subject: [PATCH 08/18] Fix up deserializing --- src/ty/composite.rs | 2 +- src/ty/fields.rs | 2 +- src/ty/mod.rs | 4 ++-- src/ty/variant.rs | 6 +++--- test_suite/tests/codec.rs | 13 +++++++++++++ 5 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/ty/composite.rs b/src/ty/composite.rs index 2adfb645..0bb8fdd3 100644 --- a/src/ty/composite.rs +++ b/src/ty/composite.rs @@ -55,7 +55,7 @@ use serde::{Serialize, Deserialize, de::DeserializeOwned}; ))] #[serde(rename_all = "lowercase")] pub struct TypeDefComposite { - #[serde(skip_serializing_if = "Vec::is_empty")] + #[serde(skip_serializing_if = "Vec::is_empty", default)] fields: Vec>, } diff --git a/src/ty/fields.rs b/src/ty/fields.rs index a73fd106..d4249b7a 100644 --- a/src/ty/fields.rs +++ b/src/ty/fields.rs @@ -33,7 +33,7 @@ use serde::{Serialize, Deserialize, de::DeserializeOwned}; ))] pub struct Field { /// The name of the field. None for unnamed fields. - #[serde(skip_serializing_if = "Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none", default)] name: Option, /// The type of the field. #[serde(rename = "type")] diff --git a/src/ty/mod.rs b/src/ty/mod.rs index 2341f624..3188205e 100644 --- a/src/ty/mod.rs +++ b/src/ty/mod.rs @@ -38,10 +38,10 @@ pub use self::{composite::*, fields::*, path::*, variant::*}; ))] pub struct Type { /// The unique path to the type. Can be empty for built-in types - #[serde(skip_serializing_if = "Path::is_empty")] + #[serde(skip_serializing_if = "Path::is_empty", default)] path: Path, /// The generic type parameters of the type in use. Empty for non generic types - #[serde(rename = "params", skip_serializing_if = "Vec::is_empty")] + #[serde(rename = "params", skip_serializing_if = "Vec::is_empty", default)] type_params: Vec, /// The actual type definition #[serde(rename = "def")] diff --git a/src/ty/variant.rs b/src/ty/variant.rs index e2d9aa01..0b3da065 100644 --- a/src/ty/variant.rs +++ b/src/ty/variant.rs @@ -68,7 +68,7 @@ use serde::{Serialize, Deserialize, de::DeserializeOwned}; ))] #[serde(rename_all = "lowercase")] pub struct TypeDefVariant { - #[serde(skip_serializing_if = "Vec::is_empty")] + #[serde(skip_serializing_if = "Vec::is_empty", default)] variants: Vec>, } @@ -118,7 +118,7 @@ pub struct Variant { /// The name of the struct variant. name: T::String, /// The fields of the struct variant. - #[serde(skip_serializing_if = "Vec::is_empty")] + #[serde(skip_serializing_if = "Vec::is_empty", default)] fields: Vec>, /// The discriminant of the variant. /// @@ -127,7 +127,7 @@ pub struct Variant { /// Even though setting the discriminant is optional /// every C-like enum variant has a discriminant specified /// upon compile-time. - #[serde(skip_serializing_if = "Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none", default)] discriminant: Option, } diff --git a/test_suite/tests/codec.rs b/test_suite/tests/codec.rs index 29b47081..b769b3ca 100644 --- a/test_suite/tests/codec.rs +++ b/test_suite/tests/codec.rs @@ -55,3 +55,16 @@ fn scale_encode_then_decode_to_readonly() { assert_eq!(decoded_serialized, original_serialized); } + +#[test] +fn json_serialize_then_deserialize_to_readonly() { + let mut registry = Registry::new(); + registry.register_type(&MetaType::new::>()); + + let original_serialized = serde_json::to_value(registry).unwrap(); + // assert_eq!(original_serialized, serde_json::Value::Null); + let readonly_deserialized: RegistryReadOnly = serde_json::from_value(original_serialized.clone()).unwrap(); + let readonly_serialized = serde_json::to_value(readonly_deserialized).unwrap(); + + assert_eq!(readonly_serialized, original_serialized); +} From 51494ad72bee38ebb22e4b54285e239fdd1be8f8 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 10 Aug 2020 11:13:04 +0100 Subject: [PATCH 09/18] Fmt --- src/registry.rs | 2 +- src/ty/composite.rs | 4 ++-- src/ty/fields.rs | 2 +- src/ty/mod.rs | 2 +- src/ty/path.rs | 11 ++++++++--- src/ty/variant.rs | 4 ++-- test_suite/tests/codec.rs | 10 ++++------ 7 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/registry.rs b/src/registry.rs index 74f1e298..783ffbef 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -31,7 +31,7 @@ use crate::{ meta_type::MetaType, Type, }; -use scale::{Encode, Decode}; +use scale::{Decode, Encode}; use serde::{Deserialize, Serialize}; /// Compacts the implementor using a registry. diff --git a/src/ty/composite.rs b/src/ty/composite.rs index 0bb8fdd3..0de269e8 100644 --- a/src/ty/composite.rs +++ b/src/ty/composite.rs @@ -19,8 +19,8 @@ use crate::{ Field, IntoCompact, Registry, }; use derive_more::From; -use scale::{Encode, Decode}; -use serde::{Serialize, Deserialize, de::DeserializeOwned}; +use scale::{Decode, Encode}; +use serde::{de::DeserializeOwned, Deserialize, Serialize}; /// A composite type, consisting of either named (struct) or unnamed (tuple /// struct) fields diff --git a/src/ty/fields.rs b/src/ty/fields.rs index d4249b7a..e03be299 100644 --- a/src/ty/fields.rs +++ b/src/ty/fields.rs @@ -19,7 +19,7 @@ use crate::{ IntoCompact, MetaType, Registry, TypeInfo, }; use scale::{Decode, Encode}; -use serde::{Serialize, Deserialize, de::DeserializeOwned}; +use serde::{de::DeserializeOwned, Deserialize, Serialize}; /// A field of a struct like data type. /// diff --git a/src/ty/mod.rs b/src/ty/mod.rs index 3188205e..4cfe04e6 100644 --- a/src/ty/mod.rs +++ b/src/ty/mod.rs @@ -21,7 +21,7 @@ use crate::{ }; use derive_more::From; use scale::{Decode, Encode}; -use serde::{Serialize, Deserialize, de::DeserializeOwned}; +use serde::{de::DeserializeOwned, Deserialize, Serialize}; mod composite; mod fields; diff --git a/src/ty/path.rs b/src/ty/path.rs index ff8bda8e..dc0cba8e 100644 --- a/src/ty/path.rs +++ b/src/ty/path.rs @@ -12,9 +12,14 @@ // See the License for the specific language governing permissions and // limitations under the License. -use crate::{IntoCompact, form::{CompactForm, Form, MetaForm}, tm_std::*, utils::is_rust_identifier, Registry}; +use crate::{ + form::{CompactForm, Form, MetaForm}, + tm_std::*, + utils::is_rust_identifier, + IntoCompact, Registry, +}; use scale::{Decode, Encode}; -use serde::{Serialize, Deserialize, de::DeserializeOwned}; +use serde::{de::DeserializeOwned, Deserialize, Serialize}; /// Represents the path of a type definition. /// @@ -103,7 +108,7 @@ impl Path { impl Path where - T: Form + T: Form, { /// Returns `true` if the path is empty pub fn is_empty(&self) -> bool { diff --git a/src/ty/variant.rs b/src/ty/variant.rs index 0b3da065..20080026 100644 --- a/src/ty/variant.rs +++ b/src/ty/variant.rs @@ -20,8 +20,8 @@ use crate::{ Field, IntoCompact, Registry, }; use derive_more::From; -use scale::{Encode, Decode}; -use serde::{Serialize, Deserialize, de::DeserializeOwned}; +use scale::{Decode, Encode}; +use serde::{de::DeserializeOwned, Deserialize, Serialize}; /// A Enum type (consisting of variants). /// diff --git a/test_suite/tests/codec.rs b/test_suite/tests/codec.rs index b769b3ca..5fd96a6b 100644 --- a/test_suite/tests/codec.rs +++ b/test_suite/tests/codec.rs @@ -23,23 +23,21 @@ extern crate alloc; use alloc::{vec, vec::Vec}; use pretty_assertions::{assert_eq, assert_ne}; -use scale::{Encode, Decode}; -use scale_info::{form::CompactForm, MetaType, IntoCompact as _, Registry, RegistryReadOnly, TypeInfo}; +use scale::{Decode, Encode}; +use scale_info::{form::CompactForm, IntoCompact as _, MetaType, Registry, RegistryReadOnly, TypeInfo}; #[derive(TypeInfo)] struct A { a: bool, b: Result, - c: T + c: T, } #[derive(TypeInfo)] enum B { A, B(A), - C { - d: [u8; 32] - } + C { d: [u8; 32] }, } #[test] From b89a13bc8e4464798518480e2b52eb5e022c7ba1 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 10 Aug 2020 11:58:11 +0100 Subject: [PATCH 10/18] Add full parity-scale-codec dependency for no_std, remove cruft --- test_suite/derive_tests_no_std/Cargo.toml | 1 + test_suite/derive_tests_no_std/src/main.rs | 40 ---------------------- 2 files changed, 1 insertion(+), 40 deletions(-) diff --git a/test_suite/derive_tests_no_std/Cargo.toml b/test_suite/derive_tests_no_std/Cargo.toml index ad1f4f30..de28ea8e 100644 --- a/test_suite/derive_tests_no_std/Cargo.toml +++ b/test_suite/derive_tests_no_std/Cargo.toml @@ -11,6 +11,7 @@ license = "Apache-2.0" [dependencies] scale-info = { path = "../..", default-features = false, features = ["derive"] } +scale = { package = "parity-scale-codec", version = "1.3.4", features = ["full"] } libc = { version = "0.2", default-features = false } diff --git a/test_suite/derive_tests_no_std/src/main.rs b/test_suite/derive_tests_no_std/src/main.rs index b94f026c..cc3e992e 100644 --- a/test_suite/derive_tests_no_std/src/main.rs +++ b/test_suite/derive_tests_no_std/src/main.rs @@ -14,48 +14,8 @@ // limitations under the License. #![no_std] -#![feature(alloc_error_handler, lang_items, start)] - -#[start] -fn start(_argc: isize, _argv: *const *const u8) -> isize { - 0 -} - -#[lang = "eh_personality"] -pub extern "C" fn rust_eh_personality() {} -#[panic_handler] -fn panic(_: &core::panic::PanicInfo) -> ! { - unsafe { - libc::abort(); - } -} -#[alloc_error_handler] -fn error_handler(_: core::alloc::Layout) -> ! { - unsafe { - libc::abort(); - } -} extern crate alloc; -use alloc::alloc::{GlobalAlloc, Layout}; - -pub struct Allocator; - -#[global_allocator] -static ALLOCATOR: Allocator = Allocator; - -extern "C" { - fn ext_malloc(size: usize) -> *mut u8; - fn ext_free(ptr: *mut u8); -} -unsafe impl GlobalAlloc for Allocator { - unsafe fn alloc(&self, layout: Layout) -> *mut u8 { - ext_malloc(layout.size()) as *mut u8 - } - unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) { - ext_free(ptr as *mut u8) - } -} use scale_info::TypeInfo; From c9b2d78415c4cf77fd71ac19c026c6eb2e105c52 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 10 Aug 2020 14:26:14 +0100 Subject: [PATCH 11/18] Add main fn --- test_suite/derive_tests_no_std/src/main.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test_suite/derive_tests_no_std/src/main.rs b/test_suite/derive_tests_no_std/src/main.rs index cc3e992e..f15311af 100644 --- a/test_suite/derive_tests_no_std/src/main.rs +++ b/test_suite/derive_tests_no_std/src/main.rs @@ -48,3 +48,6 @@ enum E { B { b: T }, C, } + +fn main() { +} From b217127267701ff5c3f1d6bfeb55b92f47295bdc Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 18 Aug 2020 16:10:01 +0100 Subject: [PATCH 12/18] Remove OwnedForm, use a String for CompactForm --- src/form.rs | 11 ----------- src/interner.rs | 16 ++++++++++++++-- src/registry.rs | 14 +++++++++++--- src/ty/fields.rs | 2 +- src/ty/path.rs | 4 ++-- src/ty/variant.rs | 2 +- 6 files changed, 29 insertions(+), 20 deletions(-) diff --git a/src/form.rs b/src/form.rs index 2509e914..10902f3e 100644 --- a/src/form.rs +++ b/src/form.rs @@ -72,16 +72,5 @@ pub enum CompactForm {} impl Form for CompactForm { type TypeId = UntrackedSymbol; - type String = &'static str; -} - -/// Owned form for decoding -#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Serialize, Debug)] -pub enum OwnedForm {} - -impl Form for OwnedForm { - /// For correctness should be [`NonZeroU32`](`core::num::NonZeroU32`), but the current - /// (`1.3.x`) release of `parity-scale-codec` does not include the `NoneZero*` Decode impls. - type TypeId = u32; type String = String; } diff --git a/src/interner.rs b/src/interner.rs index a212adfe..a114a633 100644 --- a/src/interner.rs +++ b/src/interner.rs @@ -22,25 +22,37 @@ //! elements and is later used for compact serialization within the registry. use crate::tm_std::*; -use serde::Serialize; +use serde::{Serialize, Deserialize}; /// A symbol that is not lifetime tracked. /// /// This can be used by self-referential types but /// can no longer be used to resolve instances. -#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize)] +#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] #[serde(transparent)] pub struct UntrackedSymbol { id: NonZeroU32, #[serde(skip)] marker: PhantomData T>, } + impl scale::Encode for UntrackedSymbol { fn encode_to(&self, dest: &mut W) { self.id.get().encode_to(dest) } } +impl scale::Decode for UntrackedSymbol { + fn decode(value: &mut I) -> Result { + let id = ::decode(value)?; + if id < 1 { + return Err("UntrackedSymbol::id should be a non-zero unsigned integer".into()); + } + let id = NonZeroU32::new(id).expect("ID is non zero"); + Ok(UntrackedSymbol { id, marker: Default::default() }) + } +} + /// A symbol from an interner. /// /// Can be used to resolve to the associated instance. diff --git a/src/registry.rs b/src/registry.rs index 783ffbef..ab6dd07f 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -26,7 +26,7 @@ use crate::tm_std::*; use crate::{ - form::{CompactForm, OwnedForm}, + form::{CompactForm, Form}, interner::{Interner, UntrackedSymbol}, meta_type::MetaType, Type, @@ -43,6 +43,14 @@ pub trait IntoCompact { fn into_compact(self, registry: &mut Registry) -> Self::Output; } +impl IntoCompact for &'static str { + type Output = ::String; + + fn into_compact(self, _registry: &mut Registry) -> Self::Output { + self.to_owned() + } +} + /// The registry for compaction of type identifiers and definitions. /// /// The registry consists of a cache for already compactified type identifiers and definitions. @@ -170,12 +178,12 @@ impl Registry { /// A read-only registry, to be used for decoding/deserializing #[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Decode)] pub struct RegistryReadOnly { - types: Vec>, + types: Vec>, } impl RegistryReadOnly { /// Returns the type definition for the given identifier, `None` if no type found for that ID. - pub fn resolve(&self, id: NonZeroU32) -> Option<&Type> { + pub fn resolve(&self, id: NonZeroU32) -> Option<&Type> { self.types.get((id.get() - 1) as usize); None } diff --git a/src/ty/fields.rs b/src/ty/fields.rs index e03be299..ea9d1d7f 100644 --- a/src/ty/fields.rs +++ b/src/ty/fields.rs @@ -45,7 +45,7 @@ impl IntoCompact for Field { fn into_compact(self, registry: &mut Registry) -> Self::Output { Field { - name: self.name, + name: self.name.map(|name| name.into_compact(registry)), ty: registry.register_type(&self.ty), } } diff --git a/src/ty/path.rs b/src/ty/path.rs index dc0cba8e..f934de84 100644 --- a/src/ty/path.rs +++ b/src/ty/path.rs @@ -51,9 +51,9 @@ where impl IntoCompact for Path { type Output = Path; - fn into_compact(self, _registry: &mut Registry) -> Self::Output { + fn into_compact(self, registry: &mut Registry) -> Self::Output { Path { - segments: self.segments, + segments: registry.map_into_compact(self.segments), } } } diff --git a/src/ty/variant.rs b/src/ty/variant.rs index 20080026..eca6fe34 100644 --- a/src/ty/variant.rs +++ b/src/ty/variant.rs @@ -136,7 +136,7 @@ impl IntoCompact for Variant { fn into_compact(self, registry: &mut Registry) -> Self::Output { Variant { - name: self.name, + name: self.name.into_compact(registry), fields: registry.map_into_compact(self.fields), discriminant: self.discriminant, } From 2899991c12fe08de18a442234d850466f7e32336 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 18 Aug 2020 16:29:26 +0100 Subject: [PATCH 13/18] Fix ReadOnly resolve --- src/registry.rs | 3 +-- test_suite/tests/codec.rs | 4 +++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/registry.rs b/src/registry.rs index ab6dd07f..e53b13a7 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -184,7 +184,6 @@ pub struct RegistryReadOnly { impl RegistryReadOnly { /// Returns the type definition for the given identifier, `None` if no type found for that ID. pub fn resolve(&self, id: NonZeroU32) -> Option<&Type> { - self.types.get((id.get() - 1) as usize); - None + self.types.get((id.get() - 1) as usize) } } diff --git a/test_suite/tests/codec.rs b/test_suite/tests/codec.rs index 5fd96a6b..7a77f978 100644 --- a/test_suite/tests/codec.rs +++ b/test_suite/tests/codec.rs @@ -21,7 +21,7 @@ extern crate alloc; #[cfg(not(feature = "std"))] use alloc::{vec, vec::Vec}; - +use core::num::NonZeroU32; use pretty_assertions::{assert_eq, assert_ne}; use scale::{Decode, Encode}; use scale_info::{form::CompactForm, IntoCompact as _, MetaType, Registry, RegistryReadOnly, TypeInfo}; @@ -49,6 +49,7 @@ fn scale_encode_then_decode_to_readonly() { let original_serialized = serde_json::to_value(registry).unwrap(); let readonly_decoded = RegistryReadOnly::decode(&mut &encoded[..]).unwrap(); + assert!(readonly_decoded.resolve(NonZeroU32::new(1).unwrap()).is_some()); let decoded_serialized = serde_json::to_value(readonly_decoded).unwrap(); assert_eq!(decoded_serialized, original_serialized); @@ -62,6 +63,7 @@ fn json_serialize_then_deserialize_to_readonly() { let original_serialized = serde_json::to_value(registry).unwrap(); // assert_eq!(original_serialized, serde_json::Value::Null); let readonly_deserialized: RegistryReadOnly = serde_json::from_value(original_serialized.clone()).unwrap(); + assert!(readonly_deserialized.resolve(NonZeroU32::new(1).unwrap()).is_some()); let readonly_serialized = serde_json::to_value(readonly_deserialized).unwrap(); assert_eq!(readonly_serialized, original_serialized); From 9b642d900ddf2ae2093882709dee8a3c78f24738 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 18 Aug 2020 16:35:18 +0100 Subject: [PATCH 14/18] Fmt --- src/interner.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/interner.rs b/src/interner.rs index a114a633..a3b811cc 100644 --- a/src/interner.rs +++ b/src/interner.rs @@ -22,7 +22,7 @@ //! elements and is later used for compact serialization within the registry. use crate::tm_std::*; -use serde::{Serialize, Deserialize}; +use serde::{Deserialize, Serialize}; /// A symbol that is not lifetime tracked. /// @@ -48,8 +48,11 @@ impl scale::Decode for UntrackedSymbol { if id < 1 { return Err("UntrackedSymbol::id should be a non-zero unsigned integer".into()); } - let id = NonZeroU32::new(id).expect("ID is non zero"); - Ok(UntrackedSymbol { id, marker: Default::default() }) + let id = NonZeroU32::new(id).expect("ID is non zero"); + Ok(UntrackedSymbol { + id, + marker: Default::default(), + }) } } From 0757f682128520f90bfab126d476acd69d5c47f8 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 18 Aug 2020 16:38:10 +0100 Subject: [PATCH 15/18] Remove commented out line --- src/ty/mod.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ty/mod.rs b/src/ty/mod.rs index 4cfe04e6..48fdb13f 100644 --- a/src/ty/mod.rs +++ b/src/ty/mod.rs @@ -99,7 +99,6 @@ impl Type { path, type_params: type_params.into_iter().collect(), type_def: type_def.into(), - // marker: MetaForm, } } } From 789495b06440ef7d9f6f126875b16f20a8a3595f Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 18 Aug 2020 16:39:17 +0100 Subject: [PATCH 16/18] Indentation --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 57f8084a..91c1943c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,7 +22,7 @@ scale = { package = "parity-scale-codec", version = "1.3.4", default-features = [features] default = ["std"] std = [ - "serde/std", + "serde/std", "scale/std" ] derive = [ From a44570e89ca33ab7a3bdc58fcc019e33f5d98d3d Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 18 Aug 2020 16:41:17 +0100 Subject: [PATCH 17/18] Tabs --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 91c1943c..37a028e3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,7 @@ std = [ "scale/std" ] derive = [ - "scale-info-derive" + "scale-info-derive" ] [workspace] From cc82920b316b1c7cd908788332d1805667a00479 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 18 Aug 2020 16:50:43 +0100 Subject: [PATCH 18/18] to_string --- src/registry.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/registry.rs b/src/registry.rs index e53b13a7..391edbce 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -47,7 +47,7 @@ impl IntoCompact for &'static str { type Output = ::String; fn into_compact(self, _registry: &mut Registry) -> Self::Output { - self.to_owned() + self.to_string() } }