Skip to content

Commit

Permalink
add scale_info feauture (#175)
Browse files Browse the repository at this point in the history
add optional scale_info feature

* add scale_info for
- UInt
- B0
- B1
- UTerm

* scale_info derive for
- Greater
- Less
- Equal
- PInt
- NInt
- ATerm
- TArr

Co-authored-by: Maks Nabokov <[email protected]>
  • Loading branch information
mn13 and Maks Nabokov authored Dec 26, 2021
1 parent 273bc6b commit 1ff35d4
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ against this Rust version.
### Unreleased
- [fixed] Cross-compilation issue due to doing math in build script.

### 1.15.0 (2021-12-06)
- [added] New feauture `scale_info` for using inside [Substrate](https://github.com/paritytech/substrate.git)-based runtimes
(PR #175)

### 1.14.0 (2021-09-01)
- [changed] Sealed all marker traits. Documentation already stated that these
should not be implemented outside the crate, so this is not considered a
Expand Down
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "typenum"
build = "build/main.rs"
version = "1.14.0" # remember to update html_root_url
version = "1.15.0" # remember to update html_root_url
authors = [
"Paho Lurie-Gregg <[email protected]>",
"Andre Bogus <[email protected]>"
Expand All @@ -17,6 +17,9 @@
categories = ["no-std"]
edition = "2018"

[dependencies]
scale-info = { version = "1.0", default-features = false, optional=true }

[lib]
name = "typenum"

Expand All @@ -25,3 +28,4 @@
i128 = []
strict = []
force_unix_path_separator = []
scale_info = ["scale-info/derive"]
2 changes: 2 additions & 0 deletions src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use super::*;

/// The terminating type for type arrays.
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Hash, Debug)]
#[cfg_attr(feature = "scale_info", derive(scale_info::TypeInfo))]
pub struct ATerm;

impl TypeArray for ATerm {}
Expand All @@ -19,6 +20,7 @@ impl TypeArray for ATerm {}
/// This array is only really designed to contain `Integer` types. If you use it with others, you
/// may find it lacking functionality.
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Hash, Debug)]
#[cfg_attr(feature = "scale_info", derive(scale_info::TypeInfo))]
pub struct TArr<V, A> {
first: V,
rest: A,
Expand Down
2 changes: 2 additions & 0 deletions src/bit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub use crate::marker_traits::Bit;

/// The type-level bit 0.
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Hash, Debug, Default)]
#[cfg_attr(feature = "scale_info", derive(scale_info::TypeInfo))]
pub struct B0;

impl B0 {
Expand All @@ -28,6 +29,7 @@ impl B0 {

/// The type-level bit 1.
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Hash, Debug, Default)]
#[cfg_attr(feature = "scale_info", derive(scale_info::TypeInfo))]
pub struct B1;

impl B1 {
Expand Down
3 changes: 3 additions & 0 deletions src/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ use core::ops::{Add, Div, Mul, Neg, Rem, Sub};

/// Type-level signed integers with positive sign.
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Hash, Debug, Default)]
#[cfg_attr(feature = "scale_info", derive(scale_info::TypeInfo))]
pub struct PInt<U: Unsigned + NonZero> {
pub(crate) n: U,
}

/// Type-level signed integers with negative sign.
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Hash, Debug, Default)]
#[cfg_attr(feature = "scale_info", derive(scale_info::TypeInfo))]
pub struct NInt<U: Unsigned + NonZero> {
pub(crate) n: U,
}
Expand All @@ -66,6 +68,7 @@ impl<U: Unsigned + NonZero> NInt<U> {

/// The type-level signed integer 0.
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Hash, Debug, Default)]
#[cfg_attr(feature = "scale_info", derive(scale_info::TypeInfo))]
pub struct Z0;

impl Z0 {
Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
)
)]
#![cfg_attr(feature = "cargo-clippy", deny(clippy::missing_inline_in_public_items))]
#![doc(html_root_url = "https://docs.rs/typenum/1.14.0")]
#![doc(html_root_url = "https://docs.rs/typenum/1.15.0")]

// For debugging macros:
// #![feature(trace_macros)]
Expand Down Expand Up @@ -103,16 +103,19 @@ pub use crate::{
/// A potential output from `Cmp`, this is the type equivalent to the enum variant
/// `core::cmp::Ordering::Greater`.
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Hash, Debug, Default)]
#[cfg_attr(feature = "scale_info", derive(scale_info::TypeInfo))]
pub struct Greater;

/// A potential output from `Cmp`, this is the type equivalent to the enum variant
/// `core::cmp::Ordering::Less`.
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Hash, Debug, Default)]
#[cfg_attr(feature = "scale_info", derive(scale_info::TypeInfo))]
pub struct Less;

/// A potential output from `Cmp`, this is the type equivalent to the enum variant
/// `core::cmp::Ordering::Equal`.
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Hash, Debug, Default)]
#[cfg_attr(feature = "scale_info", derive(scale_info::TypeInfo))]
pub struct Equal;

/// Returns `core::cmp::Ordering::Greater`
Expand Down
2 changes: 2 additions & 0 deletions src/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub use crate::marker_traits::{PowerOfTwo, Unsigned};
/// The terminating type for `UInt`; it always comes after the most significant
/// bit. `UTerm` by itself represents zero, which is aliased to `U0`.
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Hash, Debug, Default)]
#[cfg_attr(feature = "scale_info", derive(scale_info::TypeInfo))]
pub struct UTerm;

impl UTerm {
Expand Down Expand Up @@ -143,6 +144,7 @@ impl Unsigned for UTerm {
/// type U6 = UInt<UInt<UInt<UTerm, B1>, B1>, B0>;
/// ```
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Hash, Debug, Default)]
#[cfg_attr(feature = "scale_info", derive(scale_info::TypeInfo))]
pub struct UInt<U, B> {
/// The more significant bits of `Self`.
pub(crate) msb: U,
Expand Down

0 comments on commit 1ff35d4

Please sign in to comment.