qfall_math/rational/poly_over_q/
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::PolyOverQ;
13use crate::macros::unsafe_passthrough::{unsafe_getter, unsafe_setter};
14use flint_sys::fmpq_poly::{fmpq_poly_clear, fmpq_poly_struct};
15
16unsafe_getter!(PolyOverQ, poly, fmpq_poly_struct);
17
18unsafe_setter!(PolyOverQ, poly, fmpq_poly_struct, fmpq_poly_clear);
19
20#[cfg(test)]
21mod test_get_fmpq_poly_struct {
22    use super::PolyOverQ;
23    use crate::rational::Q;
24    use flint_sys::fmpq_poly::fmpq_poly_set_coeff_fmpq;
25
26    /// Checks availability of the getter for [`PolyOverQ::poly`]
27    /// and its ability to be modified.
28    #[test]
29    #[allow(unused_mut)]
30    fn availability_and_modification() {
31        let mut poly = PolyOverQ::from(1);
32        let mut value = Q::from(2);
33
34        let mut fmpq_poly = unsafe { poly.get_fmpq_poly_struct() };
35
36        unsafe { fmpq_poly_set_coeff_fmpq(fmpq_poly, 0, value.get_fmpq()) };
37
38        assert_eq!(PolyOverQ::from(2), poly);
39    }
40}
41
42#[cfg(test)]
43mod test_set_fmpq_poly_struct {
44    use super::PolyOverQ;
45    use flint_sys::fmpq_poly::fmpq_poly_init;
46    use std::mem::MaybeUninit;
47
48    /// Checks availability of the setter for [`PolyOverQ::poly`]
49    /// and its ability to modify [`PolyOverQ`].
50    #[test]
51    #[allow(unused_mut)]
52    fn availability_and_modification() {
53        let mut poly = PolyOverQ::from(1);
54        let mut flint_struct = MaybeUninit::uninit();
55        let flint_struct = unsafe {
56            fmpq_poly_init(flint_struct.as_mut_ptr());
57            flint_struct.assume_init()
58        };
59
60        unsafe { poly.set_fmpq_poly_struct(flint_struct) };
61
62        assert_eq!(PolyOverQ::from(0), poly);
63    }
64}