Skip to main content

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