GP_Image_Test/extern/bindings/src/lib.rs

85 lines
2.5 KiB
Rust

#![crate_type="cdylib"]
use stattest::test::{ShapiroWilkError, ShapiroWilkStatus, ShapiroWilkTest};
#[no_mangle]
pub extern "C" fn test() {
println!("Hello");
}
#[repr(C)]
#[derive(Default)]
pub struct Bruh {
error: i8,
estimate: f64,
p_value: f64,
weights: Option<std::ptr::NonNull<f64>>,
weights_len: usize,
weights_capacity: usize,
}
impl Bruh {
pub fn from_result(result: Result<ShapiroWilkTest, ShapiroWilkError>) -> Self {
match result {
Ok(results) => {
let (weights, weights_len, weights_capacity) = {
let ptr = results.weights.as_ptr();
let len = results.weights.len();
let capacity = results.weights.capacity();
std::mem::forget(results.weights);
(std::ptr::NonNull::new(ptr.cast_mut()), len, capacity)
};
Bruh {
error: match results.status {
ShapiroWilkStatus::Ok => 0,
ShapiroWilkStatus::TooMany => 1,
},
estimate: results.estimate,
p_value: results.p_value,
weights,
weights_capacity,
weights_len,
}
}
Err(error) => {
let error = match error {
ShapiroWilkError::TooFew => -1,
ShapiroWilkError::NoDifference => -2,
ShapiroWilkError::CannotMakeDistribution => -3,
};
Bruh {
error,
..Default::default()
}
}
}
}
}
/// # Safety
/// data must be a valid unique ptr to len f64's
/// requires input data to be sorted
#[no_mangle]
pub extern "C-unwind" fn willbert(data: *const f64, len: usize) -> Bruh {
let slice = unsafe { std::slice::from_raw_parts(data, len) };
let result = ShapiroWilkTest::new_sorted(slice);
Bruh::from_result(result)
}
#[no_mangle]
pub extern "C-unwind" fn willbert_unsorted(data: *mut f64, len: usize) -> Bruh {
let slice = unsafe { std::slice::from_raw_parts_mut(data, len) };
let result = ShapiroWilkTest::new(slice);
Bruh::from_result(result)
}
#[no_mangle]
pub extern "C-unwind" fn willbert_unsorted_copy(data: *const f64, len: usize) -> Bruh {
let slice = unsafe { std::slice::from_raw_parts(data, len) };
let result = ShapiroWilkTest::new_copy(slice);
Bruh::from_result(result)
}