Skip to main content

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