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