Skip to content

Commit

Permalink
Add a const field to the ToInt trait to allow its use in const
Browse files Browse the repository at this point in the history
…contexts (#179)
  • Loading branch information
winestone committed Dec 5, 2022
1 parent 8de7047 commit cb3d2d5
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/int.rs
Expand Up @@ -1187,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 @@ -1220,6 +1224,7 @@ where
fn to_int() -> i8 {
Self::I8
}
const INT: i8 = Self::I8;
}

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

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

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

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

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

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

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

#[cfg(test)]
Expand All @@ -1316,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 @@ -1327,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 @@ -1338,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 @@ -1349,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);
}
}
2 changes: 2 additions & 0 deletions src/type_operators.rs
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;
}

0 comments on commit cb3d2d5

Please sign in to comment.