Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Range variant with built-in composite definitions #130

Merged
merged 2 commits into from
Sep 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 16 additions & 3 deletions src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ use crate::{
TypeDefArray,
TypeDefCompact,
TypeDefPrimitive,
TypeDefRange,
TypeDefSequence,
TypeDefTuple,
TypeInfo,
Expand Down Expand Up @@ -359,7 +358,14 @@ where
{
type Identity = Self;
fn type_info() -> Type {
TypeDefRange::new::<Idx>(false).into()
Type::builder()
.path(Path::prelude("Range"))
.type_params(type_params![Idx])
.composite(
Fields::named()
.field(|f| f.name("start").ty::<Idx>().type_name("Idx"))
.field(|f| f.name("end").ty::<Idx>().type_name("Idx")),
)
}
}

Expand All @@ -369,7 +375,14 @@ where
{
type Identity = Self;
fn type_info() -> Type {
TypeDefRange::new::<Idx>(true).into()
Type::builder()
.path(Path::prelude("RangeInclusive"))
.type_params(type_params![Idx])
.composite(
Fields::named()
.field(|f| f.name("start").ty::<Idx>().type_name("Idx"))
.field(|f| f.name("end").ty::<Idx>().type_name("Idx")),
)
}
}

Expand Down
62 changes: 0 additions & 62 deletions src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// limitations under the License.

use crate::prelude::{
fmt,
vec,
vec::Vec,
};
Expand Down Expand Up @@ -115,7 +114,6 @@ impl_from_type_def_for_type!(
TypeDefArray,
TypeDefSequence,
TypeDefTuple,
TypeDefRange,
TypeDefCompact,
TypeDefBitSequence,
);
Expand Down Expand Up @@ -271,9 +269,6 @@ pub enum TypeDef<T: Form = MetaForm> {
/// A type representing a sequence of bits.
#[codec(index = 7)]
BitSequence(TypeDefBitSequence<T>),
/// A Range type.
#[codec(index = 8)]
Range(TypeDefRange<T>),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we should make TypeDef be #[non_exhaustive]?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure because all TypeDef variants need to be handled usually, a wildcard might not make sense.

}

impl IntoPortable for TypeDef {
Expand All @@ -289,7 +284,6 @@ impl IntoPortable for TypeDef {
TypeDef::Primitive(primitive) => primitive.into(),
TypeDef::Compact(compact) => compact.into_portable(registry).into(),
TypeDef::BitSequence(bitseq) => bitseq.into_portable(registry).into(),
TypeDef::Range(range) => range.into_portable(registry).into(),
}
}
}
Expand Down Expand Up @@ -454,62 +448,6 @@ where
}
}

/// Type describing a [`Range`].
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "serde",
serde(bound(
serialize = "T::Type: Serialize, T::String: Serialize",
deserialize = "T::Type: DeserializeOwned, T::String: DeserializeOwned",
))
)]
#[cfg_attr(any(feature = "std", feature = "decode"), derive(scale::Decode))]
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Encode, Debug)]
pub struct TypeDefRange<T: Form = MetaForm> {
index_type: T::Type,
inclusive: bool,
}

impl TypeDefRange {
/// Creates a new [`TypeDefRange`] for the supplied index type. Use the `inclusive` parameter to
/// distinguish between [`core::ops::Range`] and [`core::ops::RangeInclusive`] range types.
pub fn new<Idx>(inclusive: bool) -> Self
where
Idx: PartialOrd + fmt::Debug + TypeInfo + 'static,
{
Self {
index_type: MetaType::new::<Idx>(),
inclusive,
}
}
}

impl<T> TypeDefRange<T>
where
T: Form,
{
/// Returns the type of the index of the range.
pub fn index_type(&self) -> &T::Type {
&self.index_type
}

/// Returns true if the range is inclusive as with [`core::ops::RangeInclusive`].
pub fn inclusive(&self) -> bool {
self.inclusive
}
}

impl IntoPortable for TypeDefRange {
type Output = TypeDefRange<PortableForm>;

fn into_portable(self, registry: &mut Registry) -> Self::Output {
TypeDefRange {
index_type: registry.register_type(&self.index_type),
inclusive: self.inclusive,
}
}
}

/// A type to refer to a sequence of elements of the same type.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(any(feature = "std", feature = "decode"), derive(scale::Decode))]
Expand Down
26 changes: 24 additions & 2 deletions test_suite/tests/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,18 @@ fn test_ranges() {
{
"id": 1,
"type": {
"def": { "range": { "index_type": 2, "inclusive": false} },
"path": ["Range"],
"params": [
{ "name": "Idx", "type": 2 }
],
"def": {
"composite": {
"fields": [
{ "name": "start", "type": 2, "typeName": "Idx" },
{ "name": "end", "type": 2, "typeName": "Idx" },
],
},
}
}
},
{
Expand All @@ -406,7 +417,18 @@ fn test_ranges() {
{
"id": 3,
"type": {
"def": { "range": { "index_type": 4, "inclusive": true} },
"path": ["RangeInclusive"],
"params": [
{ "name": "Idx", "type": 4 }
],
"def": {
"composite": {
"fields": [
{ "name": "start", "type": 4, "typeName": "Idx" },
{ "name": "end", "type": 4, "typeName": "Idx" },
],
},
}
}
},
{
Expand Down