parker wanted associated type

main
Brett 2024-01-30 11:00:28 -05:00
parent c67d9294d2
commit ce53aace34
1 changed files with 38 additions and 36 deletions

View File

@ -19,40 +19,42 @@ pub struct Bruh {
weights_capacity: usize, weights_capacity: usize,
} }
fn create_bruh(result: Result<ShapiroWilkTest, ShapiroWilkError>) -> Bruh { impl Bruh {
match result { pub fn from_result(result: Result<ShapiroWilkTest, ShapiroWilkError>) -> Self {
Ok(results) => { match result {
let (weights, weights_len, weights_capacity) = { Ok(results) => {
let ptr = results.weights.as_ptr(); let (weights, weights_len, weights_capacity) = {
let len = results.weights.len(); let ptr = results.weights.as_ptr();
let capacity = results.weights.capacity(); let len = results.weights.len();
let capacity = results.weights.capacity();
std::mem::forget(results.weights);
std::mem::forget(results.weights);
(std::ptr::NonNull::new(ptr.cast_mut()), len, capacity)
}; (std::ptr::NonNull::new(ptr.cast_mut()), len, capacity)
};
Bruh {
error: match results.status { Bruh {
ShapiroWilkStatus::Ok => 0, error: match results.status {
ShapiroWilkStatus::TooMany => 1, ShapiroWilkStatus::Ok => 0,
}, ShapiroWilkStatus::TooMany => 1,
estimate: results.estimate, },
p_value: results.p_value, estimate: results.estimate,
weights, p_value: results.p_value,
weights_capacity, weights,
weights_len, weights_capacity,
weights_len,
}
} }
} Err(error) => {
Err(error) => { let error = match error {
let error = match error { ShapiroWilkError::TooFew => -1,
ShapiroWilkError::TooFew => -1, ShapiroWilkError::NoDifference => -2,
ShapiroWilkError::NoDifference => -2, ShapiroWilkError::CannotMakeDistribution => -3,
ShapiroWilkError::CannotMakeDistribution => -3, };
}; Bruh {
Bruh { error,
error, ..Default::default()
..Default::default() }
} }
} }
} }
@ -65,19 +67,19 @@ fn create_bruh(result: Result<ShapiroWilkTest, ShapiroWilkError>) -> Bruh {
pub extern "C-unwind" fn willbert(data: *const f64, len: usize) -> Bruh { pub extern "C-unwind" fn willbert(data: *const f64, len: usize) -> Bruh {
let slice = unsafe { std::slice::from_raw_parts(data, len) }; let slice = unsafe { std::slice::from_raw_parts(data, len) };
let result = ShapiroWilkTest::new_sorted(slice); let result = ShapiroWilkTest::new_sorted(slice);
create_bruh(result) Bruh::from_result(result)
} }
#[no_mangle] #[no_mangle]
pub extern "C-unwind" fn willbert_unsorted(data: *mut f64, len: usize) -> Bruh { 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 slice = unsafe { std::slice::from_raw_parts_mut(data, len) };
let result = ShapiroWilkTest::new(slice); let result = ShapiroWilkTest::new(slice);
create_bruh(result) Bruh::from_result(result)
} }
#[no_mangle] #[no_mangle]
pub extern "C-unwind" fn willbert_unsorted_copy(data: *const f64, len: usize) -> Bruh { 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 slice = unsafe { std::slice::from_raw_parts(data, len) };
let result = ShapiroWilkTest::new_copy(slice); let result = ShapiroWilkTest::new_copy(slice);
create_bruh(result) Bruh::from_result(result)
} }