Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
14 changes: 9 additions & 5 deletions Cargo.toml
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"
2 changes: 1 addition & 1 deletion src/ffi.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use mpz::*;
use crate::mpz::*;

#[link(name = "gmp")]
extern "C" {
Expand Down
3 changes: 0 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
#![warn(deprecated)]
#![allow(non_camel_case_types)]

extern crate libc;
extern crate num_traits;

mod ffi;
pub mod mpz;
pub mod mpq;
Expand Down
9 changes: 5 additions & 4 deletions src/mpf.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use libc::{c_double, c_int, c_long, c_ulong, c_void,c_char, free};
use libc::free;
use std::os::raw::{c_double, c_int, c_long, c_ulong, c_void, c_char};
use std;
use std::mem::uninitialized;
use std::mem::MaybeUninit;
use std::cmp;
use std::cmp::Ordering::{self, Greater, Less, Equal};
use std::ops::{Div, DivAssign, Mul, MulAssign, Add, AddAssign, Sub, SubAssign, Neg};
Expand Down Expand Up @@ -81,7 +82,7 @@ impl Mpf {

pub fn new(precision: usize) -> Mpf {
unsafe {
let mut mpf = uninitialized();
let mut mpf = MaybeUninit::uninit().assume_init();

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

        let mut mpf = MaybeUninit::uninit();
        unsafe {
            __gmpf_init2(mpf.as_mut_ptr(), precision as c_ulong);
            let mpf = mpf.assume_init();
            Mpf { mpf }
        }

__gmpf_init2(&mut mpf, precision as c_ulong);
Mpf { mpf: mpf }
}
Expand Down Expand Up @@ -201,7 +202,7 @@ impl Mpf {
impl Clone for Mpf {
fn clone(&self) -> Mpf {
unsafe {
let mut mpf = uninitialized();
let mut mpf = MaybeUninit::uninit().assume_init();
__gmpf_init_set(&mut mpf, &self.mpf);
Mpf { mpf: mpf }
}
Expand Down
18 changes: 9 additions & 9 deletions src/mpq.rs
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};

Choose a reason for hiding this comment

The 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};
Expand Down Expand Up @@ -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 }
}
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -206,7 +206,7 @@ impl Error for ParseMpqError {
"invalid rational number"
}

fn cause(&self) -> Option<&'static Error> {
fn cause(&self) -> Option<&'static dyn Error> {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, seems you were using Clippy to steer this too :)

None
}
}
Expand Down
40 changes: 28 additions & 12 deletions src/mpz.rs
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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 }
}
Expand Down Expand Up @@ -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 })
Expand Down Expand Up @@ -256,6 +259,19 @@ impl Mpz {
}
}

pub fn div_rem(&self, other: &Mpz) -> (Mpz, Mpz) {

Choose a reason for hiding this comment

The 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?

Copy link
Author

Choose a reason for hiding this comment

The 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.
Expand Down Expand Up @@ -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 }
}
Expand All @@ -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)
}
}

Expand All @@ -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 }
}
Expand Down
16 changes: 8 additions & 8 deletions src/rand.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use libc::{c_int, c_ulong, c_void};
use super::mpz::{mpz_struct, Mpz, mpz_ptr, mpz_srcptr, mp_bitcnt_t};
use std::mem::uninitialized;
use std::os::raw::{c_int, c_ulong, c_void};
use crate::mpz::{mpz_struct, Mpz, mpz_ptr, mpz_srcptr, mp_bitcnt_t};
use std::mem::MaybeUninit;

#[repr(C)]
pub struct gmp_randstate_struct {
Expand Down Expand Up @@ -41,31 +41,31 @@ impl Drop for RandState {
impl RandState {
pub fn new() -> RandState {
unsafe {
let mut state: gmp_randstate_struct = uninitialized();
let mut state: gmp_randstate_struct = MaybeUninit::uninit().assume_init();
__gmp_randinit_default(&mut state);
RandState { state: state }
}
}

pub fn new_mt() -> RandState {
unsafe {
let mut state: gmp_randstate_struct = uninitialized();
let mut state: gmp_randstate_struct = MaybeUninit::uninit().assume_init();
__gmp_randinit_mt(&mut state);
RandState { state: state }
}
}

pub fn new_lc_2exp(a: Mpz, c: u64, m2exp: u64) -> RandState {
unsafe {
let mut state: gmp_randstate_struct = uninitialized();
let mut state: gmp_randstate_struct = MaybeUninit::uninit().assume_init();
__gmp_randinit_lc_2exp(&mut state, a.inner(), c as c_ulong, m2exp as c_ulong);
RandState { state: state }
}
}

pub fn new_lc_2exp_size(size: u64) -> RandState {
unsafe {
let mut state: gmp_randstate_struct = uninitialized();
let mut state: gmp_randstate_struct = MaybeUninit::uninit().assume_init();
__gmp_randinit_lc_2exp_size(&mut state, size as c_ulong);
RandState { state: state }
}
Expand Down Expand Up @@ -101,7 +101,7 @@ impl RandState {
impl Clone for RandState {
fn clone(&self) -> RandState {
unsafe {
let mut state: gmp_randstate_struct = uninitialized();
let mut state: gmp_randstate_struct = MaybeUninit::uninit().assume_init();
__gmp_randinit_set(&mut state, &self.state);
RandState { state: state }
}
Expand Down
27 changes: 19 additions & 8 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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);

Choose a reason for hiding this comment

The 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]
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -698,7 +709,7 @@ mod mpf {
#[should_panic]
fn test_div_zero() {
let x = Mpf::new(0);
&x / &x;
let _ = &x / &x;
}

#[test]
Expand Down