pub mod typ;
pub mod mpz;
pub mod mpf;
pub mod mpq;
pub mod randstate;
pub mod gmp;
use crate::prim::{typ::*, gmp::*};
trait SNew {
fn new() -> Self;
}
pub trait AsPtr {
fn as_ptr(&self) -> mp_r { self as *const Self as mp_r }
}
pub trait AsPtrMut {
fn as_ptr_mut(&mut self) -> mp_t { self as *mut Self as mp_t }
}
#[macro_export]
macro_rules! to_u8z {
($s:expr) => { (String::from($s)+"\0").as_bytes() }
}
pub use to_u8z;
#[macro_export]
macro_rules! term_z {
($u:expr) => { (String::from_utf8($u.to_vec()).unwrap()+"\0").as_bytes() }
}
pub use term_z;
pub fn u8zlen(p: *mut u8) -> usize {
let mut l = 0usize;
unsafe {
loop { if std::slice::from_raw_parts_mut(p, l + 1)[l] == 0u8 { break; }
l += 1;
}
}
l
}
pub fn u8zvec(p: *mut u8, ff: bool) -> Option<Vec<u8>> {
unsafe {
if p == 0 as *mut u8 { None }
else {
let l = u8zlen(p);
let r = std::slice::from_raw_parts_mut(p, l).to_vec();
if ff {
let mut mp_free: FnPtrFree = __gmp_free_func; __gmp_get_memory_functions(
0 as *mut FnPtrAllocate, 0 as *mut FnPtrReallocate, &mut mp_free);
mp_free(p, l + 1);
}
Some(r)
}
}
}
#[inline]
pub fn gmp_printf<'a, T: AsPtr>(f: &str, a: &'a T) -> () {
gmp_printf_u8z(to_u8z!(f), a)
}
#[inline]
pub fn gmp_printf_u8z<'a, T: AsPtr>(f: &[u8], a: &'a T) -> () {
unsafe {
__gmp_printf(f as *const [u8] as mp_r,
a.as_ptr(), 0 as mp_r, 0 as mp_r, 0 as mp_r)
}
}
#[inline]
pub fn gmp_printf_1f<'a, T: AsPtr>(f: &str, p: int_t, a: &'a T) -> () {
gmp_printf_u8z_1f(to_u8z!(f), p, a)
}
#[inline]
pub fn gmp_printf_u8z_1f<'a, T: AsPtr>(f: &[u8], p: int_t, a: &'a T) -> () {
unsafe {
__gmp_printf(f as *const [u8] as mp_r,
p as mp_r, a.as_ptr(), 0 as mp_r, 0 as mp_r)
}
}
#[inline]
pub fn gmp_printf_2f<'a, T: AsPtr>(f: &str,
p: int_t, a: &'a T, q: int_t, b: &'a T) -> () {
gmp_printf_u8z_2f(to_u8z!(f), p, a, q, b)
}
#[inline]
pub fn gmp_printf_u8z_2f<'a, T: AsPtr>(f: &[u8],
p: int_t, a: &'a T, q: int_t, b: &'a T) -> () {
unsafe {
__gmp_printf(f as *const [u8] as mp_r,
p as mp_r, a.as_ptr(), q as mp_r, b.as_ptr())
}
}
#[inline]
pub fn mp_get_memory_functions(
alloc: &mut FnPtrAllocate,
realloc: &mut FnPtrReallocate,
free: &mut FnPtrFree) -> () {
unsafe { __gmp_get_memory_functions(alloc, realloc, free) }
}