-
Notifications
You must be signed in to change notification settings - Fork 2
Add unsafe
Setters
#491
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Add unsafe
Setters
#491
Changes from all commits
964dd0b
9e069bb
58dc582
f4274ab
3a107c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -11,10 +11,52 @@ | |||||||||||||||||
|
||||||||||||||||||
use super::Modulus; | ||||||||||||||||||
use crate::macros::unsafe_passthrough::unsafe_getter_mod; | ||||||||||||||||||
use flint_sys::fmpz_mod::fmpz_mod_ctx; | ||||||||||||||||||
use flint_sys::fmpz_mod::{fmpz_mod_ctx, fmpz_mod_ctx_clear}; | ||||||||||||||||||
|
||||||||||||||||||
unsafe_getter_mod!(Modulus, modulus, fmpz_mod_ctx); | ||||||||||||||||||
|
||||||||||||||||||
impl Modulus { | ||||||||||||||||||
/// Returns a mutable reference to the field `modulus` of type [`Modulus`]. | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function returns nothing. It sets the value
Suggested change
|
||||||||||||||||||
/// | ||||||||||||||||||
/// Parameters: | ||||||||||||||||||
/// - `flint_struct`: value to set the attribute to | ||||||||||||||||||
/// | ||||||||||||||||||
/// **WARNING:** The returned struct is part of [`flint_sys`]. | ||||||||||||||||||
/// Any changes to this object are unsafe and may introduce memory leaks. | ||||||||||||||||||
/// Please be aware that most moduli are shared across multiple instances and all | ||||||||||||||||||
/// modifications of this struct will affect any other instance with a reference to this object. | ||||||||||||||||||
Comment on lines
+24
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
/// | ||||||||||||||||||
/// This function is a passthrough to enable users of this library to use [`flint_sys`] | ||||||||||||||||||
/// and with that [FLINT](https://flintlib.org/) functions that might not be covered in our library yet. | ||||||||||||||||||
/// If this is the case, please consider contributing to this open-source project | ||||||||||||||||||
/// by opening a Pull Request at [qfall_math](https://github.com/qfall/math) | ||||||||||||||||||
/// to provide this feature in the future. | ||||||||||||||||||
/// | ||||||||||||||||||
/// # Safety | ||||||||||||||||||
/// Ensure that the old `modulus` does not share any memory with any moduli | ||||||||||||||||||
/// that might be used in the future. The memory of the old `modulus` is freed | ||||||||||||||||||
/// using this function. | ||||||||||||||||||
/// | ||||||||||||||||||
/// Any [`flint_sys`] struct and function is part of a FFI to the C-library `FLINT`. | ||||||||||||||||||
/// As `FLINT` is a C-library, it does not provide all memory safety features | ||||||||||||||||||
/// that Rust and our Wrapper provide. | ||||||||||||||||||
/// Thus, using functions of [`flint_sys`] can introduce memory leaks. | ||||||||||||||||||
pub unsafe fn set_fmpz_mod_ctx(&mut self, flint_struct: fmpz_mod_ctx) { | ||||||||||||||||||
let modulus = std::rc::Rc::<fmpz_mod_ctx>::get_mut(&mut self.modulus).unwrap(); | ||||||||||||||||||
|
||||||||||||||||||
// free memory of old modulus before new values of modulus are copied | ||||||||||||||||||
unsafe { fmpz_mod_ctx_clear(modulus) }; | ||||||||||||||||||
|
||||||||||||||||||
modulus.add_fxn = flint_struct.add_fxn; | ||||||||||||||||||
modulus.mod_ = flint_struct.mod_; | ||||||||||||||||||
modulus.mul_fxn = flint_struct.mul_fxn; | ||||||||||||||||||
modulus.n = flint_struct.n; | ||||||||||||||||||
modulus.n_limbs = flint_struct.n_limbs; | ||||||||||||||||||
modulus.ninv_limbs = flint_struct.ninv_limbs; | ||||||||||||||||||
modulus.sub_fxn = flint_struct.sub_fxn; | ||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
#[cfg(test)] | ||||||||||||||||||
mod test_get_fmpz_mod_ctx { | ||||||||||||||||||
use super::Modulus; | ||||||||||||||||||
|
@@ -33,3 +75,28 @@ mod test_get_fmpz_mod_ctx { | |||||||||||||||||
assert_eq!(Modulus::from(2), modulus); | ||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
#[cfg(test)] | ||||||||||||||||||
mod test_set_fmpz_mod_ctx { | ||||||||||||||||||
use super::Modulus; | ||||||||||||||||||
use flint_sys::{fmpz::fmpz, fmpz_mod::fmpz_mod_ctx_init}; | ||||||||||||||||||
use std::mem::MaybeUninit; | ||||||||||||||||||
|
||||||||||||||||||
/// Checks availability of the setter for [`Modulus::modulus`] | ||||||||||||||||||
/// and its ability to modify [`Modulus`]. | ||||||||||||||||||
#[test] | ||||||||||||||||||
#[allow(unused_mut)] | ||||||||||||||||||
fn availability_and_modification() { | ||||||||||||||||||
let mut modulus = Modulus::from(3); | ||||||||||||||||||
|
||||||||||||||||||
let mut flint_struct = MaybeUninit::uninit(); | ||||||||||||||||||
let mut flint_struct = unsafe { | ||||||||||||||||||
fmpz_mod_ctx_init(flint_struct.as_mut_ptr(), &fmpz(2)); | ||||||||||||||||||
flint_struct.assume_init() | ||||||||||||||||||
}; | ||||||||||||||||||
|
||||||||||||||||||
unsafe { modulus.set_fmpz_mod_ctx(flint_struct) }; | ||||||||||||||||||
|
||||||||||||||||||
assert_eq!(Modulus::from(2), modulus); | ||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
Comment on lines
+79
to
+102
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does it work for multiple references? /// Checks if setter works for all references.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But you check the getter here. So the initial comment should be correct