-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update code to use newer rust versions and eliminate use of obsolete methods #33
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,20 @@ | ||
[package] | ||
name = "rust-gmp" | ||
version = "0.5.0" | ||
authors = [ "thestinger <[email protected]>", "Bartłomiej Kamiński <[email protected]>" ] | ||
description = "Rust bindings for GMP" | ||
repository = "https://github.com/fizyk20/rust-gmp" | ||
documentation = "https://docs.rs/rust-gmp" | ||
license = "MIT" | ||
edition = "2018" | ||
keywords = [ "gmp", "multi", "precision", "arithmetic", "bignum" ] | ||
license = "MIT" | ||
name = "rust-gmp" | ||
repository = "https://github.com/fizyk20/rust-gmp" | ||
version = "0.5.1" | ||
|
||
[badges] | ||
maintenance = { status = "passively-maintained" } | ||
|
||
[lib] | ||
name = "gmp" | ||
|
||
[dependencies] | ||
libc = "~0.2" | ||
num-traits = "0.1" | ||
num-traits = "0.2" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
use mpz::*; | ||
use crate::mpz::*; | ||
|
||
#[link(name = "gmp")] | ||
extern "C" { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
use super::mpz::{mpz_struct, Mpz, mpz_ptr, mpz_srcptr}; | ||
use super::mpf::{Mpf, mpf_srcptr}; | ||
use super::sign::Sign; | ||
use ffi::*; | ||
use libc::{c_char, c_double, c_int, c_ulong}; | ||
use crate::mpz::{mpz_struct, Mpz, mpz_ptr, mpz_srcptr}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like all these changes :) |
||
use crate::mpf::{Mpf, mpf_srcptr}; | ||
use crate::sign::Sign; | ||
use crate::ffi::*; | ||
use std::os::raw::{c_char, c_double, c_int, c_ulong}; | ||
use std::ffi::CString; | ||
use std::str::FromStr; | ||
use std::error::Error; | ||
use std::convert::From; | ||
use std::mem::uninitialized; | ||
use std::mem::MaybeUninit; | ||
use std::fmt; | ||
use std::cmp::Ordering::{self, Greater, Less, Equal}; | ||
use std::ops::{Div, DivAssign, Mul, MulAssign, Add, AddAssign, Sub, SubAssign, Neg}; | ||
|
@@ -72,7 +72,7 @@ impl Mpq { | |
|
||
pub fn new() -> Mpq { | ||
unsafe { | ||
let mut mpq = uninitialized(); | ||
let mut mpq = MaybeUninit::uninit().assume_init(); | ||
__gmpq_init(&mut mpq); | ||
Mpq { mpq: mpq } | ||
} | ||
|
@@ -197,7 +197,7 @@ pub struct ParseMpqError { | |
|
||
impl fmt::Display for ParseMpqError { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
self.description().fmt(f) | ||
write!(f, "{:?}", self) | ||
} | ||
} | ||
|
||
|
@@ -206,7 +206,7 @@ impl Error for ParseMpqError { | |
"invalid rational number" | ||
} | ||
|
||
fn cause(&self) -> Option<&'static Error> { | ||
fn cause(&self) -> Option<&'static dyn Error> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep, seems you were using Clippy to steer this too :) |
||
None | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
use libc::{c_char, c_int, c_long, c_ulong, c_void, c_double, size_t}; | ||
use super::rand::gmp_randstate_t; | ||
use super::sign::Sign; | ||
use libc::size_t; | ||
use std::os::raw::{c_char, c_int, c_long, c_ulong, c_void, c_double}; | ||
use crate::rand::gmp_randstate_t; | ||
use crate::sign::Sign; | ||
use std::convert::From; | ||
use std::mem::{uninitialized,size_of}; | ||
use std::mem::{MaybeUninit,size_of}; | ||
use std::{fmt, hash}; | ||
use std::cmp::Ordering::{self, Greater, Less, Equal}; | ||
use std::str::FromStr; | ||
|
@@ -12,7 +13,7 @@ use std::ffi::CString; | |
use std::{u32, i32}; | ||
use num_traits::{Zero, One}; | ||
|
||
use ffi::*; | ||
use crate::ffi::*; | ||
|
||
#[repr(C)] | ||
pub struct mpz_struct { | ||
|
@@ -60,8 +61,10 @@ extern "C" { | |
fn __gmpz_abs(rop: mpz_ptr, op: mpz_srcptr); | ||
fn __gmpz_tdiv_q(q: mpz_ptr, n: mpz_srcptr, d: mpz_srcptr); | ||
fn __gmpz_tdiv_r(r: mpz_ptr, n: mpz_srcptr, d: mpz_srcptr); | ||
fn __gmpz_tdiv_qr(q: mpz_ptr, r: mpz_ptr, n: mpz_srcptr, d: mpz_srcptr); | ||
fn __gmpz_tdiv_q_ui(q: mpz_ptr, n: mpz_srcptr, d: c_ulong); | ||
fn __gmpz_tdiv_r_ui(r: mpz_ptr, n: mpz_srcptr, d: c_ulong); | ||
fn __gmpz_tdiv_qr_ui(q: mpz_ptr, r: mpz_ptr, n: mpz_srcptr, d: c_ulong); | ||
fn __gmpz_fdiv_r(r: mpz_ptr, n: mpz_srcptr, d: mpz_srcptr); | ||
fn __gmpz_fdiv_q_2exp(q: mpz_ptr, n: mpz_srcptr, b: mp_bitcnt_t); | ||
fn __gmpz_mod(r: mpz_ptr, n: mpz_srcptr, d: mpz_srcptr); | ||
|
@@ -127,15 +130,15 @@ impl Mpz { | |
|
||
pub fn new() -> Mpz { | ||
unsafe { | ||
let mut mpz = uninitialized(); | ||
let mut mpz = MaybeUninit::uninit().assume_init(); | ||
__gmpz_init(&mut mpz); | ||
Mpz { mpz: mpz } | ||
} | ||
} | ||
|
||
pub fn new_reserve(n: usize) -> Mpz { | ||
unsafe { | ||
let mut mpz = uninitialized(); | ||
let mut mpz = MaybeUninit::uninit().assume_init(); | ||
__gmpz_init2(&mut mpz, n as c_ulong); | ||
Mpz { mpz: mpz } | ||
} | ||
|
@@ -190,7 +193,7 @@ impl Mpz { | |
let s = CString::new(s.to_string()).map_err(|_| ParseMpzError { _priv: () })?; | ||
unsafe { | ||
assert!(base == 0 || (base >= 2 && base <= 62)); | ||
let mut mpz = uninitialized(); | ||
let mut mpz = MaybeUninit::uninit().assume_init(); | ||
let r = __gmpz_init_set_str(&mut mpz, s.as_ptr(), base as c_int); | ||
if r == 0 { | ||
Ok(Mpz { mpz: mpz }) | ||
|
@@ -256,6 +259,19 @@ impl Mpz { | |
} | ||
} | ||
|
||
pub fn div_rem(&self, other: &Mpz) -> (Mpz, Mpz) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't work out why you add this? Was there a clippy warning after changing to 2018 that I didn't see? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I use it for certain arithmetic operations where I get both results with one call |
||
unsafe { | ||
if other.is_zero() { | ||
panic!("divde by zero"); | ||
} | ||
|
||
let mut q_res = Mpz::new(); | ||
let mut r_res = Mpz::new(); | ||
__gmpz_tdiv_qr(&mut q_res.mpz, &mut r_res.mpz, &self.mpz, &other.mpz); | ||
(q_res, r_res) | ||
} | ||
} | ||
|
||
/// Determine whether n is prime. | ||
/// | ||
/// This function performs some trial divisions, then reps Miller-Rabin probabilistic primality tests. A higher reps value will reduce the chances of a non-prime being identified as “probably prime”. A composite number will be identified as a prime with a probability of less than 4^(-reps). Reasonable values of reps are between 15 and 50. | ||
|
@@ -439,7 +455,7 @@ impl Mpz { | |
|
||
pub fn one() -> Mpz { | ||
unsafe { | ||
let mut mpz = uninitialized(); | ||
let mut mpz = MaybeUninit::uninit().assume_init(); | ||
__gmpz_init_set_ui(&mut mpz, 1); | ||
Mpz { mpz: mpz } | ||
} | ||
|
@@ -459,7 +475,7 @@ pub struct ParseMpzError { | |
|
||
impl fmt::Display for ParseMpzError { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
self.description().fmt(f) | ||
write!(f, "{:?}", self) | ||
} | ||
} | ||
|
||
|
@@ -468,15 +484,15 @@ impl Error for ParseMpzError { | |
"invalid integer" | ||
} | ||
|
||
fn cause(&self) -> Option<&'static Error> { | ||
fn cause(&self) -> Option<&'static dyn Error> { | ||
None | ||
} | ||
} | ||
|
||
impl Clone for Mpz { | ||
fn clone(&self) -> Mpz { | ||
unsafe { | ||
let mut mpz = uninitialized(); | ||
let mut mpz = MaybeUninit::uninit().assume_init(); | ||
__gmpz_init_set(&mut mpz, &self.mpz); | ||
Mpz { mpz: mpz } | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,9 +15,9 @@ fn test_limb_size() { | |
} | ||
|
||
mod mpz { | ||
use super::super::mpz::Mpz; | ||
use super::super::mpz::ProbabPrimeResult; | ||
use super::super::sign::Sign; | ||
use crate::mpz::Mpz; | ||
use crate::mpz::ProbabPrimeResult; | ||
use crate::sign::Sign; | ||
use std::str::FromStr; | ||
use std::convert::{From, Into}; | ||
use std::{i64, u64}; | ||
|
@@ -98,15 +98,15 @@ mod mpz { | |
fn test_div_zero() { | ||
let x: Mpz = From::<i64>::from(1); | ||
let y = Mpz::new(); | ||
x / y; | ||
let _ = x / y; | ||
} | ||
|
||
#[test] | ||
#[should_panic] | ||
fn test_rem_zero() { | ||
let x: Mpz = From::<i64>::from(1); | ||
let y = Mpz::new(); | ||
x % y; | ||
let _ = x % y; | ||
} | ||
|
||
#[test] | ||
|
@@ -313,7 +313,7 @@ mod mpz { | |
|
||
#[test] | ||
fn test_popcount() { | ||
Mpz::from_str_radix("1010010011", 2).unwrap().popcount() == 5; | ||
assert_eq!(Mpz::from_str_radix("1010010011", 2).unwrap().popcount(), 5); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh nice, I did this one wrong. I fixed my change in line with this :) |
||
} | ||
|
||
#[test] | ||
|
@@ -570,6 +570,17 @@ mod mpz { | |
assert_eq!(five.sign(), Sign::Positive); | ||
assert_eq!(minus_five.sign(), Sign::Negative); | ||
} | ||
|
||
#[test] | ||
fn test_div_rem() { | ||
let seven: Mpz = From::<i64>::from(7); | ||
let three: Mpz = From::<i64>::from(3); | ||
let two: Mpz = From::<i64>::from(2); | ||
let one = Mpz::one(); | ||
let (q, r) = seven.div_rem(&three); | ||
assert_eq!(q, two); | ||
assert_eq!(r, one); | ||
} | ||
} | ||
|
||
mod rand { | ||
|
@@ -609,7 +620,7 @@ mod mpq { | |
fn test_div_zero() { | ||
let x: Mpq = From::<i64>::from(1); | ||
let y = Mpq::new(); | ||
x / y; | ||
let _ = x / y; | ||
} | ||
|
||
#[test] | ||
|
@@ -698,7 +709,7 @@ mod mpf { | |
#[should_panic] | ||
fn test_div_zero() { | ||
let x = Mpf::new(0); | ||
&x / &x; | ||
let _ = &x / &x; | ||
} | ||
|
||
#[test] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the pattern we want is