qfall_math/rational/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::Q;
13use crate::macros::unsafe_passthrough::{unsafe_getter, unsafe_setter};
14use flint_sys::fmpq::{fmpq, fmpq_clear};
15
16unsafe_getter!(Q, value, fmpq);
17
18unsafe_setter!(Q, value, fmpq, fmpq_clear);
19
20#[cfg(test)]
21mod test_get_fmpq {
22    use super::Q;
23    use flint_sys::fmpz::fmpz;
24
25    /// Checks availability of the getter for [`Q::value`]
26    /// and its ability to be modified.
27    #[test]
28    #[allow(unused_mut)]
29    fn availability_and_modification() {
30        let mut rational = Q::from(1);
31
32        let mut fmpq_rat = unsafe { rational.get_fmpq() };
33
34        fmpq_rat.num = fmpz(2);
35
36        assert_eq!(Q::from(2), rational);
37    }
38}
39
40#[cfg(test)]
41mod test_set_fmpq {
42    use super::Q;
43    use flint_sys::{fmpq::fmpq, fmpz::fmpz};
44
45    /// Checks availability of the setter for [`Q::value`]
46    /// and its ability to modify [`Q`].
47    #[test]
48    #[allow(unused_mut)]
49    fn availability_and_modification() {
50        let mut rational = Q::from(1);
51        let flint_struct = fmpq {
52            num: fmpz(2),
53            den: fmpz(1),
54        };
55
56        unsafe { rational.set_fmpq(flint_struct) };
57
58        assert_eq!(Q::from(2), rational);
59    }
60}