@@ -8,10 +8,10 @@ use super::objstr;
8
8
use super :: objtype;
9
9
use num_bigint:: { BigInt , ToBigInt } ;
10
10
use num_traits:: { Pow , Signed , ToPrimitive , Zero } ;
11
+ use std:: hash:: { Hash , Hasher } ;
11
12
12
13
// This proxy allows for easy switching between types.
13
- // TODO: maybe this is a good idea:
14
- // type IntType = BigInt;
14
+ type IntType = BigInt ;
15
15
16
16
fn int_repr ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
17
17
arg_check ! ( vm, args, required = [ ( int, Some ( vm. ctx. int_type( ) ) ) ] ) ;
@@ -47,7 +47,7 @@ pub fn to_int(
47
47
vm : & mut VirtualMachine ,
48
48
obj : & PyObjectRef ,
49
49
base : u32 ,
50
- ) -> Result < BigInt , PyObjectRef > {
50
+ ) -> Result < IntType , PyObjectRef > {
51
51
let val = if objtype:: isinstance ( obj, & vm. ctx . int_type ( ) ) {
52
52
get_value ( obj)
53
53
} else if objtype:: isinstance ( obj, & vm. ctx . float_type ( ) ) {
@@ -75,7 +75,7 @@ pub fn to_int(
75
75
}
76
76
77
77
// Retrieve inner int value:
78
- pub fn get_value ( obj : & PyObjectRef ) -> BigInt {
78
+ pub fn get_value ( obj : & PyObjectRef ) -> IntType {
79
79
if let PyObjectKind :: Integer { value } = & obj. borrow ( ) . kind {
80
80
value. clone ( )
81
81
} else {
@@ -171,9 +171,11 @@ fn int_ge(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
171
171
172
172
fn int_hash ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
173
173
arg_check ! ( vm, args, required = [ ( zelf, Some ( vm. ctx. int_type( ) ) ) ] ) ;
174
-
175
- // TODO: how to hash int?
176
- Ok ( zelf. clone ( ) )
174
+ let value = BigInt :: from_pyobj ( zelf) ;
175
+ let mut hasher = std:: collections:: hash_map:: DefaultHasher :: new ( ) ;
176
+ value. hash ( & mut hasher) ;
177
+ let hash = hasher. finish ( ) ;
178
+ Ok ( vm. ctx . new_int ( hash. to_bigint ( ) . unwrap ( ) ) )
177
179
}
178
180
179
181
fn int_abs ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
0 commit comments