qfall_math/integer/poly_over_z/
unsafe_functions.rs

1// Copyright © 2025 Niklas Siemer
2//
3// This file is part of qFALL-math.
4//
5// qFALL-math is free software: you can redistribute it and/or modify it under
6// the terms of the Mozilla Public License Version 2.0 as published by the
7// Mozilla Foundation. See <https://mozilla.org/en-US/MPL/2.0/>.
8
9//! This module contains public functions that enable access to underlying
10//! [FLINT](https://flintlib.org/) structs. Therefore, they require to be unsafe.
11
12use super::PolyOverZ;
13use crate::macros::unsafe_passthrough::{unsafe_getter, unsafe_setter};
14use flint_sys::fmpz_poly::{fmpz_poly_clear, fmpz_poly_struct};
15
16unsafe_getter!(PolyOverZ, poly, fmpz_poly_struct);
17unsafe_setter!(PolyOverZ, poly, fmpz_poly_struct, fmpz_poly_clear);
18
19#[cfg(test)]
20mod test_get_fmpz_poly_struct {
21    use super::PolyOverZ;
22    use flint_sys::{fmpz::fmpz, fmpz_poly::fmpz_poly_set_fmpz};
23
24    /// Checks availability of the getter for [`PolyOverZ::poly`]
25    /// and its ability to be modified.
26    #[test]
27    #[allow(unused_mut)]
28    fn availability_and_modification() {
29        let mut poly = PolyOverZ::from(1);
30
31        let mut fmpz_poly = unsafe { poly.get_fmpz_poly_struct() };
32
33        unsafe { fmpz_poly_set_fmpz(fmpz_poly, &fmpz(2)) };
34
35        assert_eq!(PolyOverZ::from(2), poly);
36    }
37}
38
39#[cfg(test)]
40mod test_set_fmpz_poly_struct {
41    use super::PolyOverZ;
42    use flint_sys::fmpz_poly::fmpz_poly_init;
43    use std::mem::MaybeUninit;
44
45    /// Checks availability of the setter for [`PolyOverZ::poly`]
46    /// and its ability to modify [`PolyOverZ`].
47    #[test]
48    #[allow(unused_mut)]
49    fn availability_and_modification() {
50        let mut poly = PolyOverZ::from(1);
51        let mut flint_struct = MaybeUninit::uninit();
52        let flint_struct = unsafe {
53            fmpz_poly_init(flint_struct.as_mut_ptr());
54            flint_struct.assume_init()
55        };
56
57        unsafe { poly.set_fmpz_poly_struct(flint_struct) };
58
59        assert_eq!(PolyOverZ::default(), poly);
60    }
61}