Skip to main content

qfall_math/integer/poly_over_z/
unsafe_functions.rs

1// Copyright 2025 Jan 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 flint3_sys::{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 flint3_sys::{fmpz, 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        let value: fmpz = 2;
33
34        unsafe { fmpz_poly_set_fmpz(fmpz_poly, &value) };
35
36        assert_eq!(PolyOverZ::from(2), poly);
37    }
38}
39
40#[cfg(test)]
41mod test_set_fmpz_poly_struct {
42    use super::PolyOverZ;
43    use flint3_sys::fmpz_poly_init;
44    use std::mem::MaybeUninit;
45
46    /// Checks availability of the setter for [`PolyOverZ::poly`]
47    /// and its ability to modify [`PolyOverZ`].
48    #[test]
49    #[allow(unused_mut)]
50    fn availability_and_modification() {
51        let mut poly = PolyOverZ::from(1);
52        let mut flint_struct = MaybeUninit::uninit();
53        let flint_struct = unsafe {
54            fmpz_poly_init(flint_struct.as_mut_ptr());
55            flint_struct.assume_init()
56        };
57
58        unsafe { poly.set_fmpz_poly_struct(flint_struct) };
59
60        assert_eq!(PolyOverZ::default(), poly);
61    }
62}