So, I've found myself in a bit of a sticky situation with a project I've been working on. I have a series of functions which pass around Vectors to do calculations on them, then hands the original vector along with the resulting vector to flutter to display.
When using 'pub extern "C" before the function, the compiler flags the structure Vector as not FFI safe.
example of code:
#[no_mangle]
pub extern "C" fn x_squared (x_vector: Vec<f64>, lower: f64, upper: f64) -> Vec<f64> {
let y_vector: Vec<f64> = x_vector.into_iter().map(|x| x * x).collect();
y_vector.into_iter().map(|y| if y >= lower && y <= upper { y } else { std::f64::NAN } ).collect()
}
#[no_mangle]
pub extern "C" fn coord_vector_maker (f: &dyn Fn(Vec<f64>, f64, f64) -> Vec<f64>, x_lower: f64, x_upper: f64, y_lower: f64, y_upper: f64, precision: f64) -> (Vec<f64>, Vec<f64>) {
(x_vector_maker(x_lower, x_upper, precision), f(x_vector_maker(x_lower, x_upper, precision), y_lower, y_upper))
}
#[no_mangle]
pub extern "C" fn x_vector_maker (x_lower: f64, x_upper: f64, precision: f64) -> Vec<f64> {
let mut x: f64 = x_lower;
let x_bound_bredth: f64 = &x_upper.ceil() - &x_lower.floor();
let capacity: usize = (x_bound_bredth * (1.0 / &precision) ).ceil() as usize;
let mut x_vector: Vec<f64> = Vec::with_capacity(capacity);
while x <= x_upper {
x_vector.push(x);
x = x + precision;
}
x_vector.shrink_to_fit();
x_vector
}
How do I fix this error (and any others that you might see)?
(Also, any suggestions on getting rid of that double while call would be fantastic)