@@ -12,8 +12,9 @@ extern mod std;
12
12
13
13
use std:: from_str:: FromStr ;
14
14
use std:: libc:: { c_char, c_double, c_int, c_long, c_ulong, c_void, size_t} ;
15
- use std:: num:: { IntConvertible , One , Zero } ;
15
+ use std:: num:: { One , Zero } ;
16
16
use std:: unstable:: intrinsics:: uninit;
17
+ use std:: mem:: size_of;
17
18
use std:: { cmp, int, str, to_str, uint, vec} ;
18
19
19
20
struct mpz_struct {
@@ -419,20 +420,34 @@ impl Neg<Mpz> for Mpz {
419
420
}
420
421
}
421
422
422
- impl IntConvertible for Mpz {
423
- fn to_int (&self) -> int {
423
+ impl ToPrimitive for Mpz {
424
+ fn to_i64 (&self) -> Option<i64> {
424
425
fail!(~" not implemented")
425
426
}
427
+ fn to_u64(&self) -> Option<u64> {
428
+ fail!(~" not implemented")
429
+ }
430
+ }
431
+
432
+ impl FromPrimitive for Mpz {
426
433
#[fixed_stack_segment]
427
- fn from_int(other: int) -> Mpz {
434
+ fn from_u64(other: u64) -> Option<Mpz> {
435
+ unsafe {
436
+ let mut res = Mpz::new();
437
+ __gmpz_import(&mut res.mpz, 1, 1, size_of::<u64>() as size_t, 0, 0,
438
+ std::util::id::<*u64>(&other) as *c_void);
439
+ Some(res)
440
+ }
441
+ }
442
+ fn from_i64(other: i64) -> Option<Mpz> {
428
443
unsafe {
429
444
let mut res = Mpz::new();
430
- __gmpz_import(&mut res.mpz, 1, 1, int::bytes as size_t, 0, 0,
431
- std::util::id::<*int >(&other.abs()) as *c_void);
445
+ __gmpz_import(&mut res.mpz, 1, 1, size_of::<i64>() as size_t, 0, 0,
446
+ std::util::id::<*i64 >(&other.abs()) as *c_void);
432
447
if other.is_negative() {
433
448
__gmpz_neg(&mut res.mpz, &res.mpz)
434
449
}
435
- res
450
+ Some( res)
436
451
}
437
452
}
438
453
}
@@ -794,14 +809,25 @@ impl Neg<Mpq> for Mpq {
794
809
}
795
810
}
796
811
797
- impl IntConvertible for Mpq {
798
- fn to_int(&self) -> int {
812
+ impl ToPrimitive for Mpq {
813
+ fn to_i64(&self) -> Option<i64> {
814
+ fail!(~" not implemented")
815
+ }
816
+ fn to_u64(&self) -> Option<u64> {
799
817
fail!(~" not implemented")
800
818
}
801
- fn from_int(other: int) -> Mpq {
819
+ }
820
+
821
+ impl FromPrimitive for Mpq {
822
+ fn from_i64(other: i64) -> Option<Mpq> {
823
+ let mut res = Mpq::new();
824
+ res.set_z(&FromPrimitive::from_i64(other).unwrap());
825
+ Some(res)
826
+ }
827
+ fn from_u64(other: u64) -> Option<Mpq> {
802
828
let mut res = Mpq::new();
803
- res.set_z(&IntConvertible::from_int (other));
804
- res
829
+ res.set_z(&FromPrimitive::from_u64 (other).unwrap( ));
830
+ Some( res)
805
831
}
806
832
}
807
833
0 commit comments