-
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
Conversation
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.
Looks good. Just a question I have and some comment changes
#[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); | ||
} | ||
} |
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.
How does it work for multiple references?
You say that the function only works if no other references exist/are used in the comment. But is there no way to rewrite the function such that it does? Or is it just too much for us to implement right now?
I tried this, but it did not work:
/// Checks if setter works for all references.
#[test]
#[allow(unused_mut)]
fn counter_works() {
let mut modulus_1 = Modulus::from(3);
let mut modulus_2 = Modulus::from(3);
let mut modulus_3 = modulus_1.clone();
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_1.set_fmpz_mod_ctx(flint_struct) };
assert_eq!(Modulus::from(2), modulus_1);
assert_eq!(Modulus::from(3), modulus_2);
assert_eq!(Modulus::from(2), modulus_3);
}
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.
I would have loved to implement this. Unfortunately, you can't change the object that a Reference Counter (Rc), which we implement our Modulus objects with, retrospectively.
As I wasn't able to find out which objects point to the Modulus object (I believe that the data-structure internally only keeps a reference from the object pointing to the Rc, which would be absolutely sensible), it could be possible that this is impossible if you don't want to do a search for a reference to that Rc and replace the pointer. Also in this case, I wouldn't know how to do it and it seems very inefficient.
Thus, I settled for this solution for now.
src/integer_mod_q/modulus_polynomial_ring_zq/unsafe_functions.rs
Outdated
Show resolved
Hide resolved
Co-authored-by: Marcel583 <[email protected]>
Description
This PR implements...
Testing
Checklist: