1pub mod typ;
5pub mod mpz;
6pub mod mpf;
7pub mod mpq;
8pub mod randstate;
9pub mod gmp;
10
11use crate::prim::{typ::*, gmp::*}; trait SNew {
15 fn new() -> Self;
17}
18
19pub trait AsPtr {
21 fn as_ptr(&self) -> mp_r { self as *const Self as mp_r }
23}
24
25pub trait AsPtrMut {
27 fn as_ptr_mut(&mut self) -> mp_t { self as *mut Self as mp_t }
29}
30
31#[macro_export]
33macro_rules! to_u8z {
34 ($s:expr) => { (String::from($s)+"\0").as_bytes() }
35}
36pub use to_u8z;
37
38#[macro_export]
40macro_rules! term_z {
41 ($u:expr) => { (String::from_utf8($u.to_vec()).unwrap()+"\0").as_bytes() }
42}
43pub use term_z;
44
45pub fn u8zlen(p: *mut u8) -> usize {
47 let mut l = 0usize;
48unsafe {
49 loop { if std::slice::from_raw_parts_mut(p, l + 1)[l] == 0u8 { break; }
51 l += 1;
52 }
53}
54 l
55}
56
57pub fn u8zvec(p: *mut u8, ff: bool) -> Option<Vec<u8>> {
59unsafe {
60 if p == 0 as *mut u8 { None }
61 else {
62 let l = u8zlen(p);
63 let r = std::slice::from_raw_parts_mut(p, l).to_vec();
64 if ff {
65 let mut mp_free: FnPtrFree = __gmp_free_func; __gmp_get_memory_functions(
67 0 as *mut FnPtrAllocate, 0 as *mut FnPtrReallocate, &mut mp_free);
68 mp_free(p, l + 1);
69 }
70 Some(r)
71 }
72}
73}
74
75#[inline]
77pub fn gmp_printf<'a, T: AsPtr>(f: &str, a: &'a T) -> () {
78 gmp_printf_u8z(to_u8z!(f), a)
79}
80
81#[inline]
83pub fn gmp_printf_u8z<'a, T: AsPtr>(f: &[u8], a: &'a T) -> () {
84 unsafe {
85 __gmp_printf(f as *const [u8] as mp_r,
86 a.as_ptr(), 0 as mp_r, 0 as mp_r, 0 as mp_r)
87 }
88}
89
90#[inline]
92pub fn gmp_printf_1f<'a, T: AsPtr>(f: &str, p: int_t, a: &'a T) -> () {
93 gmp_printf_u8z_1f(to_u8z!(f), p, a)
94}
95
96#[inline]
98pub fn gmp_printf_u8z_1f<'a, T: AsPtr>(f: &[u8], p: int_t, a: &'a T) -> () {
99 unsafe {
100 __gmp_printf(f as *const [u8] as mp_r,
101 p as mp_r, a.as_ptr(), 0 as mp_r, 0 as mp_r)
102 }
103}
104
105#[inline]
107pub fn gmp_printf_2f<'a, T: AsPtr>(f: &str,
108 p: int_t, a: &'a T, q: int_t, b: &'a T) -> () {
109 gmp_printf_u8z_2f(to_u8z!(f), p, a, q, b)
110}
111
112#[inline]
114pub fn gmp_printf_u8z_2f<'a, T: AsPtr>(f: &[u8],
115 p: int_t, a: &'a T, q: int_t, b: &'a T) -> () {
116 unsafe {
117 __gmp_printf(f as *const [u8] as mp_r,
118 p as mp_r, a.as_ptr(), q as mp_r, b.as_ptr())
119 }
120}
121
122#[inline]
124pub fn mp_get_memory_functions(
125 alloc: &mut FnPtrAllocate,
126 realloc: &mut FnPtrReallocate,
127 free: &mut FnPtrFree) -> () {
128 unsafe { __gmp_get_memory_functions(alloc, realloc, free) }
129}