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