From ce53aace34c8b63e86d6c74127f8d4be95a187f3 Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Tue, 30 Jan 2024 11:00:28 -0500 Subject: [PATCH] parker wanted associated type --- extern/bindings/src/lib.rs | 74 +++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 36 deletions(-) diff --git a/extern/bindings/src/lib.rs b/extern/bindings/src/lib.rs index ce1892a..cf2ec2c 100644 --- a/extern/bindings/src/lib.rs +++ b/extern/bindings/src/lib.rs @@ -19,40 +19,42 @@ pub struct Bruh { weights_capacity: usize, } -fn create_bruh(result: Result) -> Bruh { - 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, +impl Bruh { + pub fn from_result(result: Result) -> 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() + Err(error) => { + let error = match error { + ShapiroWilkError::TooFew => -1, + ShapiroWilkError::NoDifference => -2, + ShapiroWilkError::CannotMakeDistribution => -3, + }; + Bruh { + error, + ..Default::default() + } } } } @@ -65,19 +67,19 @@ fn create_bruh(result: Result) -> Bruh { 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); - create_bruh(result) + 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); - create_bruh(result) + 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); - create_bruh(result) + Bruh::from_result(result) } \ No newline at end of file