Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
paholg authored Dec 5, 2022
2 parents 2ad6e88 + d3b30c8 commit cf9f811
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ against this Rust version.

### Unreleased

### 1.15.0 (2021-12-25)
- [fixed] Cross-compilation issue due to doing math in build script. (PR #177)
- [added] New feature `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
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -26,3 +29,4 @@
strict = []
force_unix_path_separator = []
const-generics = []
scale_info = ["scale-info/derive"]
8 changes: 6 additions & 2 deletions build/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@ pub fn no_std() {}

const HIGHEST: u64 = 1024;
fn uints() -> impl Iterator<Item = u64> {
let first2: u32 = (HIGHEST as f64).log(2.0).round() as u32 + 1;
let first10: u32 = (HIGHEST as f64).log(10.0) as u32 + 1;
// Use hardcoded values to avoid issues with cross-compilation.
// See https://github.com/paholg/typenum/issues/162
let first2: u32 = 11; // (highest as f64).log(2.0).round() as u32 + 1;
let first10: u32 = 4; // (highest as f64).log(10.0) as u32 + 1;
(0..(HIGHEST + 1))
.chain((first2..64).map(|i| 2u64.pow(i)))
.chain((first10..20).map(|i| 10u64.pow(i)))
Expand All @@ -90,8 +92,10 @@ fn uints() -> impl Iterator<Item = u64> {
#[allow(dead_code)]
fn main() {
println!("cargo:rerun-if-changed=build/main.rs"); // Allow caching the generation if `src/*` files change.

let out_dir = env::var("OUT_DIR").unwrap();
let dest = Path::new(&out_dir).join("consts.rs");
#[cfg(not(feature = "force_unix_path_separator"))]
println!("cargo:rustc-env=TYPENUM_BUILD_CONSTS={}", dest.display());

let mut f = File::create(&dest).unwrap();
Expand Down
1 change: 1 addition & 0 deletions build/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ struct Op {
pub fn write_op_macro() -> ::std::io::Result<()> {
let out_dir = ::std::env::var("OUT_DIR").unwrap();
let dest = ::std::path::Path::new(&out_dir).join("op.rs");
#[cfg(not(feature = "force_unix_path_separator"))]
println!("cargo:rustc-env=TYPENUM_BUILD_OP={}", dest.display());
let mut f = ::std::fs::File::create(&dest).unwrap();

Expand Down
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
51 changes: 51 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 Expand Up @@ -1184,27 +1187,31 @@ impl ToInt<i8> for Z0 {
fn to_int() -> i8 {
Self::I8
}
const INT: i8 = Self::I8;
}

impl ToInt<i16> for Z0 {
#[inline]
fn to_int() -> i16 {
Self::I16
}
const INT: i16 = Self::I16;
}

impl ToInt<i32> for Z0 {
#[inline]
fn to_int() -> i32 {
Self::I32
}
const INT: i32 = Self::I32;
}

impl ToInt<i64> for Z0 {
#[inline]
fn to_int() -> i64 {
Self::I64
}
const INT: i64 = Self::I64;
}

// negative numbers
Expand All @@ -1217,6 +1224,7 @@ where
fn to_int() -> i8 {
Self::I8
}
const INT: i8 = Self::I8;
}

impl<U> ToInt<i16> for NInt<U>
Expand All @@ -1227,6 +1235,7 @@ where
fn to_int() -> i16 {
Self::I16
}
const INT: i16 = Self::I16;
}

impl<U> ToInt<i32> for NInt<U>
Expand All @@ -1237,6 +1246,7 @@ where
fn to_int() -> i32 {
Self::I32
}
const INT: i32 = Self::I32;
}

impl<U> ToInt<i64> for NInt<U>
Expand All @@ -1247,6 +1257,7 @@ where
fn to_int() -> i64 {
Self::I64
}
const INT: i64 = Self::I64;
}

// positive numbers
Expand All @@ -1259,6 +1270,7 @@ where
fn to_int() -> i8 {
Self::I8
}
const INT: i8 = Self::I8;
}

impl<U> ToInt<i16> for PInt<U>
Expand All @@ -1269,6 +1281,7 @@ where
fn to_int() -> i16 {
Self::I16
}
const INT: i16 = Self::I16;
}

impl<U> ToInt<i32> for PInt<U>
Expand All @@ -1279,6 +1292,7 @@ where
fn to_int() -> i32 {
Self::I32
}
const INT: i32 = Self::I32;
}

impl<U> ToInt<i64> for PInt<U>
Expand All @@ -1289,6 +1303,7 @@ where
fn to_int() -> i64 {
Self::I64
}
const INT: i64 = Self::I64;
}

#[cfg(test)]
Expand All @@ -1313,6 +1328,15 @@ mod tests {
assert_eq!(-2_i8, N2::to_int());
assert_eq!(-3_i8, N3::to_int());
assert_eq!(-4_i8, N4::to_int());
assert_eq!(0_i8, Z0::INT);
assert_eq!(1_i8, P1::INT);
assert_eq!(2_i8, P2::INT);
assert_eq!(3_i8, P3::INT);
assert_eq!(4_i8, P4::INT);
assert_eq!(-1_i8, N1::INT);
assert_eq!(-2_i8, N2::INT);
assert_eq!(-3_i8, N3::INT);
assert_eq!(-4_i8, N4::INT);

// i16
assert_eq!(0_i16, Z0::to_int());
Expand All @@ -1324,6 +1348,15 @@ mod tests {
assert_eq!(-2_i16, N2::to_int());
assert_eq!(-3_i16, N3::to_int());
assert_eq!(-4_i16, N4::to_int());
assert_eq!(0_i16, Z0::INT);
assert_eq!(1_i16, P1::INT);
assert_eq!(2_i16, P2::INT);
assert_eq!(3_i16, P3::INT);
assert_eq!(4_i16, P4::INT);
assert_eq!(-1_i16, N1::INT);
assert_eq!(-2_i16, N2::INT);
assert_eq!(-3_i16, N3::INT);
assert_eq!(-4_i16, N4::INT);

// i32
assert_eq!(0_i32, Z0::to_int());
Expand All @@ -1335,6 +1368,15 @@ mod tests {
assert_eq!(-2_i32, N2::to_int());
assert_eq!(-3_i32, N3::to_int());
assert_eq!(-4_i32, N4::to_int());
assert_eq!(0_i32, Z0::INT);
assert_eq!(1_i32, P1::INT);
assert_eq!(2_i32, P2::INT);
assert_eq!(3_i32, P3::INT);
assert_eq!(4_i32, P4::INT);
assert_eq!(-1_i32, N1::INT);
assert_eq!(-2_i32, N2::INT);
assert_eq!(-3_i32, N3::INT);
assert_eq!(-4_i32, N4::INT);

// i64
assert_eq!(0_i64, Z0::to_int());
Expand All @@ -1346,5 +1388,14 @@ mod tests {
assert_eq!(-2_i64, N2::to_int());
assert_eq!(-3_i64, N3::to_int());
assert_eq!(-4_i64, N4::to_int());
assert_eq!(0_i64, Z0::INT);
assert_eq!(1_i64, P1::INT);
assert_eq!(2_i64, P2::INT);
assert_eq!(3_i64, P3::INT);
assert_eq!(4_i64, P4::INT);
assert_eq!(-1_i64, N1::INT);
assert_eq!(-2_i64, N2::INT);
assert_eq!(-3_i64, N3::INT);
assert_eq!(-4_i64, N4::INT);
}
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,19 @@ pub use generic_const_mappings::{Const, ToUInt, U};
/// 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/type_operators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,4 +587,6 @@ pub trait Gcd<Rhs> {
pub trait ToInt<T> {
/// Method returning the concrete value for the type.
fn to_int() -> T;
/// The concrete value for the type. Can be used in `const` contexts.
const INT: T;
}
Loading

0 comments on commit cf9f811

Please sign in to comment.