qfall_math/rational/mat_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::MatQ;
13use crate::macros::unsafe_passthrough::{unsafe_getter, unsafe_setter};
14use flint_sys::fmpq_mat::{fmpq_mat_clear, fmpq_mat_struct};
15
16unsafe_getter!(MatQ, matrix, fmpq_mat_struct);
17
18unsafe_setter!(MatQ, matrix, fmpq_mat_struct, fmpq_mat_clear);
19
20#[cfg(test)]
21mod test_get_fmpq_mat_struct {
22    use super::MatQ;
23    use crate::{rational::Q, traits::MatrixGetEntry};
24    use flint_sys::{fmpq::fmpq_set, fmpq_mat::fmpq_mat_entry};
25    use std::str::FromStr;
26
27    /// Checks availability of the getter for [`MatQ::matrix`]
28    /// and its ability to be modified.
29    #[test]
30    #[allow(unused_mut)]
31    fn availability_and_modification() {
32        let mut mat = MatQ::from_str("[[1]]").unwrap();
33        let mut value = Q::from(2);
34
35        let mut fmpq_mat = unsafe { mat.get_fmpq_mat_struct() };
36
37        unsafe {
38            let entry = fmpq_mat_entry(fmpq_mat, 0, 0);
39            fmpq_set(entry, value.get_fmpq())
40        };
41
42        assert_eq!(value, mat.get_entry(0, 0).unwrap());
43    }
44}
45
46#[cfg(test)]
47mod test_set_fmpq_mat_struct {
48    use super::MatQ;
49    use crate::{rational::Q, traits::MatrixGetEntry};
50    use flint_sys::fmpq_mat::fmpq_mat_init;
51    use std::{mem::MaybeUninit, str::FromStr};
52
53    /// Checks availability of the setter for [`MatQ::matrix`]
54    /// and its ability to modify [`MatQ`].
55    #[test]
56    #[allow(unused_mut)]
57    fn availability_and_modification() {
58        let mut mat = MatQ::from_str("[[1]]").unwrap();
59        let mut flint_struct = MaybeUninit::uninit();
60        let flint_struct = unsafe {
61            fmpq_mat_init(flint_struct.as_mut_ptr(), 1, 1);
62            flint_struct.assume_init()
63        };
64
65        unsafe { mat.set_fmpq_mat_struct(flint_struct) };
66
67        assert_eq!(Q::ZERO, mat.get_entry(0, 0).unwrap());
68    }
69}