Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 40 additions & 49 deletions src/mpz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -845,63 +845,54 @@ impl<'a> From<&'a [u8]> for Mpz {
}
}

impl From<u64> for Mpz {
fn from(other: u64) -> Mpz {
unsafe {
let mut res = Mpz::new();
__gmpz_import(&mut res.mpz, 1, -1, size_of::<u64>() as size_t, 0, 0,
&other as *const u64 as *const c_void);
res
}
}
}

impl From<u32> for Mpz {
fn from(other: u32) -> Mpz {
unsafe {
let mut res = Mpz::new();
__gmpz_import(&mut res.mpz, 1, -1, size_of::<u32>() as size_t, 0, 0,
&other as *const u32 as *const c_void);
res
}
}
}

impl From<i64> for Mpz {
fn from(other: i64) -> Mpz {
unsafe {
let mut res = Mpz::new();

if other.is_negative() {
__gmpz_import(&mut res.mpz, 1, -1, size_of::<i64>() as size_t, 0, 0,
&(other ^ -1i64) as *const i64 as *const c_void);
__gmpz_com(&mut res.mpz, &res.mpz);
} else {
__gmpz_import(&mut res.mpz, 1, -1, size_of::<i64>() as size_t, 0, 0,
&other as *const i64 as *const c_void);
macro_rules! impl_from_uint {
($typ: ident) => {
impl From<$typ> for Mpz {
fn from(other: $typ) -> Mpz {
let mut res = Mpz::new();
unsafe {
__gmpz_import(&mut res.mpz, 1, -1, size_of::<$typ>() as size_t, 0, 0,
&other as *const $typ as *const c_void);
}
res
}
res
}
}
}

impl From<i32> for Mpz {
fn from(other: i32) -> Mpz {
unsafe {
let mut res = Mpz::new();

if other.is_negative() {
__gmpz_import(&mut res.mpz, 1, -1, size_of::<i32>() as size_t, 0, 0,
&(other ^ -1i32) as *const i32 as *const c_void);
__gmpz_com(&mut res.mpz, &res.mpz);
} else {
__gmpz_import(&mut res.mpz, 1, -1, size_of::<i32>() as size_t, 0, 0,
&other as *const i32 as *const c_void);
impl_from_uint!(u128);
impl_from_uint!(u64);
impl_from_uint!(u32);
impl_from_uint!(u16);
impl_from_uint!(u8);
impl_from_uint!(usize);

macro_rules! impl_from_iint {
($typ: ident) => {
impl From<$typ> for Mpz {
fn from(other: $typ) -> Mpz {
let mut res = Mpz::new();
unsafe {
if other.is_negative() {
__gmpz_import(&mut res.mpz, 1, -1, size_of::<$typ>() as size_t, 0, 0,
&(other ^ (-1 as $typ)) as *const $typ as *const c_void);
__gmpz_com(&mut res.mpz, &res.mpz);
} else {
__gmpz_import(&mut res.mpz, 1, -1, size_of::<$typ>() as size_t, 0, 0,
&other as *const $typ as *const c_void);
}
}
res
}
res
}

}
}
impl_from_iint!(i128);
impl_from_iint!(i64);
impl_from_iint!(i32);
impl_from_iint!(i16);
impl_from_iint!(i8);
impl_from_iint!(isize);

impl_oper!(BitAnd, bitand, BitAndAssign, bitand_assign, __gmpz_and);
impl_oper!(BitOr, bitor, BitOrAssign, bitor_assign, __gmpz_ior);
Expand Down
32 changes: 29 additions & 3 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,35 @@ mod mpz {

#[test]
fn test_from_int() {
let x: Mpz = From::<i64>::from(150);
assert!(x.to_string() == "150".to_string());
assert!(x == FromStr::from_str("150").unwrap());
fn tst<T>(x: T, expected: &str)
where
Mpz: From<T>,
{
let x: Mpz = Mpz::from(x);
assert_eq!(x.to_string(), expected);
assert_eq!(x, FromStr::from_str(expected).unwrap());
}

tst(150_u8, "150");
tst(150_u16, "150");
tst(150_u32, "150");
tst(150_u64, "150");
tst(150_u128, "150");
tst(150_usize, "150");

tst(75_i8, "75");
tst(150_i16, "150");
tst(150_i32, "150");
tst(150_i64, "150");
tst(150_i128, "150");
tst(150_isize, "150");

tst(-75_i8, "-75");
tst(-150_i16, "-150");
tst(-150_i32, "-150");
tst(-150_i64, "-150");
tst(-150_i128, "-150");
tst(-150_isize, "-150");
}

#[test]
Expand Down