Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rename Form type param from F to T to avoid same name as scale Decode…
… derive
  • Loading branch information
ascjones committed Aug 7, 2020
commit 7cb5d0ba92c3bc55dde4a03bd9abed90d1817bb0
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,28 @@ pub trait TypeInfo {
Types implementing this trait build up and return a `Type` struct:

```rust
pub struct Type<F: Form = MetaForm> {
pub struct Type<T: Form = MetaForm> {
/// The unique path to the type. Can be empty for built-in types
path: Path<F>,
path: Path<T>,
/// The generic type parameters of the type in use. Empty for non generic types
type_params: Vec<F::TypeId>,
type_params: Vec<T::TypeId>,
/// The actual type definition
type_def: TypeDef<F>,
type_def: TypeDef<T>,
}
```
Types are defined as one of the following variants:
```rust
pub enum TypeDef<F: Form = MetaForm> {
pub enum TypeDef<T: Form = MetaForm> {
/// A composite type (e.g. a struct or a tuple)
Composite(TypeDefComposite<F>),
Composite(TypeDefComposite<T>),
/// A variant type (e.g. an enum)
Variant(TypeDefVariant<F>),
Variant(TypeDefVariant<T>),
/// A sequence type with runtime known length.
Sequence(TypeDefSequence<F>),
Sequence(TypeDefSequence<T>),
/// An array type with compile-time known length.
Array(TypeDefArray<F>),
Array(TypeDefArray<T>),
/// A tuple type.
Tuple(TypeDefTuple<F>),
Tuple(TypeDefTuple<T>),
/// A Rust primitive type.
Primitive(TypeDefPrimitive),
}
Expand Down
5 changes: 3 additions & 2 deletions src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use crate::{
meta_type::MetaType,
Type,
};
use scale::{Encode, Decode};
use serde::{Deserialize, Serialize};

/// Compacts the implementor using a registry.
Expand Down Expand Up @@ -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::<u32>() + mem::size_of::<Type<CompactForm>>() * self.types.len()
}
Expand Down Expand Up @@ -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<Type<OwnedForm>>,
}
Expand Down
6 changes: 3 additions & 3 deletions src/ty/composite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F: Form = MetaForm> {
pub struct TypeDefComposite<T: Form = MetaForm> {
#[serde(skip_serializing_if = "Vec::is_empty")]
fields: Vec<Field<F>>,
fields: Vec<Field<T>>,
}

impl IntoCompact for TypeDefComposite {
Expand Down
13 changes: 7 additions & 6 deletions src/ty/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F: Form = MetaForm> {
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Serialize, Deserialize, Encode, Decode)]
#[serde(bound = "T::TypeId: Serialize")]
pub struct Field<T: Form = MetaForm> {
/// The name of the field. None for unnamed fields.
#[serde(skip_serializing_if = "Option::is_none")]
name: Option<F::String>,
name: Option<T::String>,
/// The type of the field.
#[serde(rename = "type")]
ty: F::TypeId,
ty: T::TypeId,
}

impl IntoCompact for Field {
Expand Down
42 changes: 21 additions & 21 deletions src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F: Form = MetaForm> {
#[serde(bound = "T::TypeId: Serialize")]
pub struct Type<T: Form = MetaForm> {
/// The unique path to the type. Can be empty for built-in types
#[serde(skip_serializing_if = "Path::is_empty")]
path: Path<F>,
path: Path<T>,
/// 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<F::TypeId>,
type_params: Vec<T::TypeId>,
/// The actual type definition
#[serde(rename = "def")]
type_def: TypeDef<F>,
type_def: TypeDef<T>,
}

impl IntoCompact for Type {
Expand Down Expand Up @@ -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<F: Form = MetaForm> {
pub enum TypeDef<T: Form = MetaForm> {
/// A composite type (e.g. a struct or a tuple)
Composite(TypeDefComposite<F>),
Composite(TypeDefComposite<T>),
/// A variant type (e.g. an enum)
Variant(TypeDefVariant<F>),
Variant(TypeDefVariant<T>),
/// A sequence type with runtime known length.
Sequence(TypeDefSequence<F>),
Sequence(TypeDefSequence<T>),
/// An array type with compile-time known length.
Array(TypeDefArray<F>),
Array(TypeDefArray<T>),
/// A tuple type.
Tuple(TypeDefTuple<F>),
Tuple(TypeDefTuple<T>),
/// A Rust primitive type.
Primitive(TypeDefPrimitive),
}
Expand Down Expand Up @@ -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<F: Form = MetaForm> {
#[serde(bound = "T::TypeId: Serialize")]
pub struct TypeDefArray<T: Form = MetaForm> {
/// 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 {
Expand All @@ -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<F: Form = MetaForm> {
pub struct TypeDefTuple<T: Form = MetaForm> {
/// The types of the tuple fields.
fields: Vec<F::TypeId>,
fields: Vec<T::TypeId>,
}

impl IntoCompact for TypeDefTuple {
Expand Down Expand Up @@ -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<F: Form = MetaForm> {
#[serde(bound = "T::TypeId: Serialize")]
pub struct TypeDefSequence<T: Form = MetaForm> {
/// The element type of the sequence type.
#[serde(rename = "type")]
type_param: F::TypeId,
type_param: T::TypeId,
}

impl IntoCompact for TypeDefSequence {
Expand Down
21 changes: 11 additions & 10 deletions src/ty/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -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<F: Form = MetaForm> {
pub struct Path<T: Form = MetaForm> {
/// The segments of the namespace.
segments: Vec<F::String>,
segments: Vec<T::String>,
}

impl<F> Default for Path<F>
impl<T> Default for Path<T>
where
F: Form,
T: Form,
{
fn default() -> Self {
Path { segments: Vec::new() }
Expand Down Expand Up @@ -96,22 +97,22 @@ impl Path {
}
}

impl<F> Path<F>
impl<T> Path<T>
where
F: Form
T: 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<F::String> {
pub fn ident(&self) -> Option<T::String> {
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(&[])
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/ty/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F: Form = MetaForm> {
pub struct TypeDefVariant<T: Form = MetaForm> {
#[serde(skip_serializing_if = "Vec::is_empty")]
variants: Vec<Variant<F>>,
variants: Vec<Variant<T>>,
}

impl IntoCompact for TypeDefVariant {
Expand Down Expand Up @@ -106,13 +106,13 @@ impl TypeDefVariant {
/// }
/// ```
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Serialize, scale::Decode)]
#[serde(bound = "F::TypeId: Serialize")]
pub struct Variant<F: Form = MetaForm> {
#[serde(bound = "T::TypeId: Serialize")]
pub struct Variant<T: Form = MetaForm> {
/// 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<Field<F>>,
fields: Vec<Field<T>>,
/// The discriminant of the variant.
///
/// # Note
Expand Down