diff --git a/Examples/Primes/LibPrimes_component/Implementations/Rust/Interfaces/libprimes_interface_wrapper.rs b/Examples/Primes/LibPrimes_component/Implementations/Rust/Interfaces/libprimes_interface_wrapper.rs index 880f6e82..12baa7af 100644 --- a/Examples/Primes/LibPrimes_component/Implementations/Rust/Interfaces/libprimes_interface_wrapper.rs +++ b/Examples/Primes/LibPrimes_component/Implementations/Rust/Interfaces/libprimes_interface_wrapper.rs @@ -46,8 +46,8 @@ pub fn libprimes_getversion(major : *mut u32, minor : *mut u32, micro : *mut u32 pub fn libprimes_getlasterror(instance : BaseHandle, error_message_buffer_size : usize, error_message_needed_chars : *mut usize, error_message_buffer : *mut u8, has_error : *mut u8) -> i32 { // Convert parameter instance to be used as an argument if instance.is_null() { return LIBPRIMES_ERROR_INVALIDPARAM; } - let _handle_instance = unsafe {&*instance}; - let _optional_instance = _handle_instance.as_base(); + let _handle_instance = unsafe {&mut *instance}; + let _optional_instance = _handle_instance.as_mut_base(); if _optional_instance.is_none() { return LIBPRIMES_ERROR_INVALIDPARAM; } let _instance = _optional_instance.unwrap(); diff --git a/Examples/Primes/LibPrimes_component/Implementations/Rust/Interfaces/libprimes_interfaces.rs b/Examples/Primes/LibPrimes_component/Implementations/Rust/Interfaces/libprimes_interfaces.rs index 08d8b6d3..fcdfa51d 100644 --- a/Examples/Primes/LibPrimes_component/Implementations/Rust/Interfaces/libprimes_interfaces.rs +++ b/Examples/Primes/LibPrimes_component/Implementations/Rust/Interfaces/libprimes_interfaces.rs @@ -222,21 +222,7 @@ pub trait Wrapper { // * @param[out] error_message - Message of the last error // * @param[return] has_error - Is there a last error to query // - fn get_last_error(instance : & dyn Base, error_message : &mut String) -> bool; - - // acquire_instance - // - // Acquire shared ownership of an Instance - // * @param[in] instance - Instance Handle - // - fn acquire_instance(instance : & dyn Base); - - // release_instance - // - // Releases shared ownership of an Instance - // * @param[in] instance - Instance Handle - // - fn release_instance(instance : & dyn Base); + fn get_last_error(instance : &mut dyn Base, error_message : &mut String) -> bool; // create_factorization_calculator // diff --git a/Examples/Primes/LibPrimes_component/Implementations/Rust/Stub/libprimes.rs b/Examples/Primes/LibPrimes_component/Implementations/Rust/Stub/libprimes.rs index 1fa0a5ef..ed68033e 100644 --- a/Examples/Primes/LibPrimes_component/Implementations/Rust/Stub/libprimes.rs +++ b/Examples/Primes/LibPrimes_component/Implementations/Rust/Stub/libprimes.rs @@ -15,6 +15,8 @@ Interface version: 1.2.0 use libprimes_interfaces::*; +use libprimes_sieve_calculator::CSieveCalculator; +use libprimes_factorization_calculator::CFactorizationCalculator; // Wrapper struct to implement the wrapper trait for global methods pub struct CWrapper; @@ -30,7 +32,9 @@ impl Wrapper for CWrapper { // * @param[out] micro - returns the micro version of this library // fn get_version(_major : &mut u32, _minor : &mut u32, _micro : &mut u32) { - unimplemented!(); + *_major = 1; + *_minor = 1; + *_micro = 1; } // get_last_error @@ -40,26 +44,8 @@ impl Wrapper for CWrapper { // * @param[out] error_message - Message of the last error // * @param[return] has_error - Is there a last error to query // - fn get_last_error(_instance : & dyn Base, _error_message : &mut String) -> bool { - unimplemented!(); - } - - // acquire_instance - // - // Acquire shared ownership of an Instance - // * @param[in] instance - Instance Handle - // - fn acquire_instance(_instance : & dyn Base) { - unimplemented!(); - } - - // release_instance - // - // Releases shared ownership of an Instance - // * @param[in] instance - Instance Handle - // - fn release_instance(_instance : & dyn Base) { - unimplemented!(); + fn get_last_error(_instance : &mut dyn Base, _error_message : &mut String) -> bool { + _instance.get_last_error_message(_error_message) } // create_factorization_calculator @@ -68,7 +54,7 @@ impl Wrapper for CWrapper { // * @param[return] instance - New FactorizationCalculator instance // fn create_factorization_calculator() -> Box { - unimplemented!(); + Box::new(CFactorizationCalculator::new()) } // create_sieve_calculator @@ -77,7 +63,7 @@ impl Wrapper for CWrapper { // * @param[return] instance - New SieveCalculator instance // fn create_sieve_calculator() -> Box { - unimplemented!(); + Box::new(CSieveCalculator::new()) } // set_journal diff --git a/Examples/Primes/LibPrimes_component/Implementations/Rust/Stub/libprimes_base.rs b/Examples/Primes/LibPrimes_component/Implementations/Rust/Stub/libprimes_base.rs index 4cb747bd..67541471 100644 --- a/Examples/Primes/LibPrimes_component/Implementations/Rust/Stub/libprimes_base.rs +++ b/Examples/Primes/LibPrimes_component/Implementations/Rust/Stub/libprimes_base.rs @@ -60,7 +60,15 @@ impl Base for CBase { // * @param[return] class_type_id - Class type as a 64 bits integer // fn class_type_id(&mut self) -> u64 { - unimplemented!(); + 0 } } + +impl CBase { + pub fn new() -> CBase { + CBase { + last_error : None + } + } +} \ No newline at end of file diff --git a/Examples/Primes/LibPrimes_component/Implementations/Rust/Stub/libprimes_calculator.rs b/Examples/Primes/LibPrimes_component/Implementations/Rust/Stub/libprimes_calculator.rs index f2167a55..762fadbe 100644 --- a/Examples/Primes/LibPrimes_component/Implementations/Rust/Stub/libprimes_calculator.rs +++ b/Examples/Primes/LibPrimes_component/Implementations/Rust/Stub/libprimes_calculator.rs @@ -19,7 +19,9 @@ use libprimes_base::CBase; // Stub struct to implement the Calculator trait pub struct CCalculator { - parent : CBase + parent : CBase, + value : u64, + progress_callback : ProgressCallback } // Implementation of parent traits via parent @@ -32,7 +34,7 @@ impl Base for CCalculator { // * @param[return] class_type_id - Class type as a 64 bits integer // fn class_type_id(&mut self) -> u64 { - self.parent.class_type_id() + 1 } // get_last_error_message @@ -72,7 +74,7 @@ impl Calculator for CCalculator { // * @param[return] value - The current value of this Calculator // fn get_value(&mut self) -> u64 { - unimplemented!(); + self.value } // set_value @@ -81,7 +83,7 @@ impl Calculator for CCalculator { // * @param[in] value - The value to be factorized // fn set_value(&mut self, _value : u64) { - unimplemented!(); + self.value = _value; } // calculate @@ -98,7 +100,29 @@ impl Calculator for CCalculator { // * @param[in] progress_callback - The progress callback // fn set_progress_callback(&mut self, _progress_callback : ProgressCallback) { - unimplemented!(); + self.progress_callback = _progress_callback } } + +unsafe extern "C" fn dummy_progress(_par : f32, _val : *mut u8) { + +} + +impl CCalculator { + pub fn new() -> CCalculator { + CCalculator { + parent : CBase::new(), + value : 0, + progress_callback : dummy_progress + } + } + + pub fn progress_abort(&self) -> bool { + let mut res : u8 = 0; + unsafe { + (self.progress_callback)(0_f32, &mut res as *mut u8) + } + res != 0 + } +} \ No newline at end of file diff --git a/Examples/Primes/LibPrimes_component/Implementations/Rust/Stub/libprimes_factorization_calculator.rs b/Examples/Primes/LibPrimes_component/Implementations/Rust/Stub/libprimes_factorization_calculator.rs index 149c9da5..2a850705 100644 --- a/Examples/Primes/LibPrimes_component/Implementations/Rust/Stub/libprimes_factorization_calculator.rs +++ b/Examples/Primes/LibPrimes_component/Implementations/Rust/Stub/libprimes_factorization_calculator.rs @@ -19,7 +19,8 @@ use libprimes_calculator::CCalculator; // Stub struct to implement the FactorizationCalculator trait pub struct CFactorizationCalculator { - parent : CCalculator + parent : CCalculator, + prime_factors : Vec } // Implementation of parent traits via parent @@ -49,7 +50,26 @@ impl Calculator for CFactorizationCalculator { // Performs the specific calculation of this Calculator // fn calculate(&mut self) { - self.parent.calculate() + self.prime_factors.clear(); + let mut val = self.get_value(); + let mut n = 0_u64; + while n < val { + if self.parent.progress_abort() { + return; + } + let mut prime_factor = PrimeFactor { + prime : n, + multiplicity : 0 + }; + while val % n == 0 { + val = val / n; + prime_factor.multiplicity += 1; + } + if prime_factor.multiplicity > 0 { + self.prime_factors.push(prime_factor) + } + n += 1 + } } // set_progress_callback @@ -69,7 +89,7 @@ impl Base for CFactorizationCalculator { // * @param[return] class_type_id - Class type as a 64 bits integer // fn class_type_id(&mut self) -> u64 { - self.parent.class_type_id() + 2 } // get_last_error_message @@ -109,7 +129,15 @@ impl FactorizationCalculator for CFactorizationCalculator { // * @param[out] prime_factors - The prime factors of this number // fn get_prime_factors(&mut self, _prime_factors : &mut Vec) { - unimplemented!(); + *_prime_factors = self.prime_factors.clone() } } +impl CFactorizationCalculator { + pub fn new() -> CFactorizationCalculator { + CFactorizationCalculator { + parent : CCalculator::new(), + prime_factors : Vec::new() + } + } +} \ No newline at end of file diff --git a/Examples/Primes/LibPrimes_component/Implementations/Rust/Stub/libprimes_sieve_calculator.rs b/Examples/Primes/LibPrimes_component/Implementations/Rust/Stub/libprimes_sieve_calculator.rs index fccd0480..7e2c515f 100644 --- a/Examples/Primes/LibPrimes_component/Implementations/Rust/Stub/libprimes_sieve_calculator.rs +++ b/Examples/Primes/LibPrimes_component/Implementations/Rust/Stub/libprimes_sieve_calculator.rs @@ -19,7 +19,8 @@ use libprimes_calculator::CCalculator; // Stub struct to implement the SieveCalculator trait pub struct CSieveCalculator { - parent : CCalculator + parent : CCalculator, + primes : Vec } // Implementation of parent traits via parent @@ -49,7 +50,25 @@ impl Calculator for CSieveCalculator { // Performs the specific calculation of this Calculator // fn calculate(&mut self) { - self.parent.calculate() + self.primes.clear(); + let val = self.get_value(); + let mut sieved : Vec = vec![false; (val+1) as usize]; + sieved[0] = true; + sieved[1] = true; + let val_sqrt = (val as f64).sqrt() as u64 + 1; + for i in 2_u64..val_sqrt { + if self.parent.progress_abort() { + return + } + if !sieved[i as usize] { + self.primes.push(i); + let mut mul : u64 = i*i; + while mul <= val { + sieved[mul as usize] = true; + mul *= i; + } + } + } } // set_progress_callback @@ -69,7 +88,7 @@ impl Base for CSieveCalculator { // * @param[return] class_type_id - Class type as a 64 bits integer // fn class_type_id(&mut self) -> u64 { - self.parent.class_type_id() + 3 } // get_last_error_message @@ -109,7 +128,15 @@ impl SieveCalculator for CSieveCalculator { // * @param[out] primes - The primes lower or equal to the sieve's value // fn get_primes(&mut self, _primes : &mut Vec) { - unimplemented!(); + *_primes = self.primes.clone() } } +impl CSieveCalculator { + pub fn new() -> CSieveCalculator { + CSieveCalculator { + parent : CCalculator::new(), + primes : Vec::new() + } + } +} \ No newline at end of file