@@ -32,7 +32,7 @@ use crate::{
3232 consts:: { N1 , P1 , U0 , U1 } ,
3333 private:: { Internal , InternalMarker , PrivateDivInt , PrivateIntegerAdd , PrivateRem } ,
3434 uint:: { UInt , Unsigned } ,
35- Cmp , Equal , Greater , Less , NonZero , Pow , PowerOfTwo , Zero ,
35+ Cmp , Equal , Greater , Less , NonZero , Pow , PowerOfTwo , ToInt , Zero ,
3636} ;
3737use core:: ops:: { Add , Div , Mul , Neg , Rem , Sub } ;
3838
@@ -1176,13 +1176,175 @@ where
11761176 }
11771177}
11781178
1179+ // -----------------------------------------
1180+ // ToInt
1181+
1182+ impl ToInt < i8 > for Z0 {
1183+ #[ inline]
1184+ fn to_int ( ) -> i8 {
1185+ Self :: I8
1186+ }
1187+ }
1188+
1189+ impl ToInt < i16 > for Z0 {
1190+ #[ inline]
1191+ fn to_int ( ) -> i16 {
1192+ Self :: I16
1193+ }
1194+ }
1195+
1196+ impl ToInt < i32 > for Z0 {
1197+ #[ inline]
1198+ fn to_int ( ) -> i32 {
1199+ Self :: I32
1200+ }
1201+ }
1202+
1203+ impl ToInt < i64 > for Z0 {
1204+ #[ inline]
1205+ fn to_int ( ) -> i64 {
1206+ Self :: I64
1207+ }
1208+ }
1209+
1210+ // negative numbers
1211+
1212+ impl < U > ToInt < i8 > for NInt < U >
1213+ where
1214+ U : Unsigned + NonZero ,
1215+ {
1216+ #[ inline]
1217+ fn to_int ( ) -> i8 {
1218+ Self :: I8
1219+ }
1220+ }
1221+
1222+ impl < U > ToInt < i16 > for NInt < U >
1223+ where
1224+ U : Unsigned + NonZero ,
1225+ {
1226+ #[ inline]
1227+ fn to_int ( ) -> i16 {
1228+ Self :: I16
1229+ }
1230+ }
1231+
1232+ impl < U > ToInt < i32 > for NInt < U >
1233+ where
1234+ U : Unsigned + NonZero ,
1235+ {
1236+ #[ inline]
1237+ fn to_int ( ) -> i32 {
1238+ Self :: I32
1239+ }
1240+ }
1241+
1242+ impl < U > ToInt < i64 > for NInt < U >
1243+ where
1244+ U : Unsigned + NonZero ,
1245+ {
1246+ #[ inline]
1247+ fn to_int ( ) -> i64 {
1248+ Self :: I64
1249+ }
1250+ }
1251+
1252+ // positive numbers
1253+
1254+ impl < U > ToInt < i8 > for PInt < U >
1255+ where
1256+ U : Unsigned + NonZero ,
1257+ {
1258+ #[ inline]
1259+ fn to_int ( ) -> i8 {
1260+ Self :: I8
1261+ }
1262+ }
1263+
1264+ impl < U > ToInt < i16 > for PInt < U >
1265+ where
1266+ U : Unsigned + NonZero ,
1267+ {
1268+ #[ inline]
1269+ fn to_int ( ) -> i16 {
1270+ Self :: I16
1271+ }
1272+ }
1273+
1274+ impl < U > ToInt < i32 > for PInt < U >
1275+ where
1276+ U : Unsigned + NonZero ,
1277+ {
1278+ #[ inline]
1279+ fn to_int ( ) -> i32 {
1280+ Self :: I32
1281+ }
1282+ }
1283+
1284+ impl < U > ToInt < i64 > for PInt < U >
1285+ where
1286+ U : Unsigned + NonZero ,
1287+ {
1288+ #[ inline]
1289+ fn to_int ( ) -> i64 {
1290+ Self :: I64
1291+ }
1292+ }
1293+
11791294#[ cfg( test) ]
11801295mod tests {
1181- use crate :: { consts:: * , Integer } ;
1296+ use crate :: { consts:: * , Integer , ToInt } ;
11821297
11831298 #[ test]
11841299 fn to_ix_min ( ) {
11851300 assert_eq ! ( N128 :: to_i8( ) , :: core:: i8 :: MIN ) ;
11861301 assert_eq ! ( N32768 :: to_i16( ) , :: core:: i16 :: MIN ) ;
11871302 }
1303+
1304+ #[ test]
1305+ fn int_toint_test ( ) {
1306+ // i8
1307+ assert_eq ! ( 0_i8 , Z0 :: to_int( ) ) ;
1308+ assert_eq ! ( 1_i8 , P1 :: to_int( ) ) ;
1309+ assert_eq ! ( 2_i8 , P2 :: to_int( ) ) ;
1310+ assert_eq ! ( 3_i8 , P3 :: to_int( ) ) ;
1311+ assert_eq ! ( 4_i8 , P4 :: to_int( ) ) ;
1312+ assert_eq ! ( -1_i8 , N1 :: to_int( ) ) ;
1313+ assert_eq ! ( -2_i8 , N2 :: to_int( ) ) ;
1314+ assert_eq ! ( -3_i8 , N3 :: to_int( ) ) ;
1315+ assert_eq ! ( -4_i8 , N4 :: to_int( ) ) ;
1316+
1317+ // i16
1318+ assert_eq ! ( 0_i16 , Z0 :: to_int( ) ) ;
1319+ assert_eq ! ( 1_i16 , P1 :: to_int( ) ) ;
1320+ assert_eq ! ( 2_i16 , P2 :: to_int( ) ) ;
1321+ assert_eq ! ( 3_i16 , P3 :: to_int( ) ) ;
1322+ assert_eq ! ( 4_i16 , P4 :: to_int( ) ) ;
1323+ assert_eq ! ( -1_i16 , N1 :: to_int( ) ) ;
1324+ assert_eq ! ( -2_i16 , N2 :: to_int( ) ) ;
1325+ assert_eq ! ( -3_i16 , N3 :: to_int( ) ) ;
1326+ assert_eq ! ( -4_i16 , N4 :: to_int( ) ) ;
1327+
1328+ // i32
1329+ assert_eq ! ( 0_i32 , Z0 :: to_int( ) ) ;
1330+ assert_eq ! ( 1_i32 , P1 :: to_int( ) ) ;
1331+ assert_eq ! ( 2_i32 , P2 :: to_int( ) ) ;
1332+ assert_eq ! ( 3_i32 , P3 :: to_int( ) ) ;
1333+ assert_eq ! ( 4_i32 , P4 :: to_int( ) ) ;
1334+ assert_eq ! ( -1_i32 , N1 :: to_int( ) ) ;
1335+ assert_eq ! ( -2_i32 , N2 :: to_int( ) ) ;
1336+ assert_eq ! ( -3_i32 , N3 :: to_int( ) ) ;
1337+ assert_eq ! ( -4_i32 , N4 :: to_int( ) ) ;
1338+
1339+ // i64
1340+ assert_eq ! ( 0_i64 , Z0 :: to_int( ) ) ;
1341+ assert_eq ! ( 1_i64 , P1 :: to_int( ) ) ;
1342+ assert_eq ! ( 2_i64 , P2 :: to_int( ) ) ;
1343+ assert_eq ! ( 3_i64 , P3 :: to_int( ) ) ;
1344+ assert_eq ! ( 4_i64 , P4 :: to_int( ) ) ;
1345+ assert_eq ! ( -1_i64 , N1 :: to_int( ) ) ;
1346+ assert_eq ! ( -2_i64 , N2 :: to_int( ) ) ;
1347+ assert_eq ! ( -3_i64 , N3 :: to_int( ) ) ;
1348+ assert_eq ! ( -4_i64 , N4 :: to_int( ) ) ;
1349+ }
11881350}
0 commit comments