Skip to content

Commit

Permalink
Use method calls instead of match
Browse files Browse the repository at this point in the history
This simplifies the code, keeping error handling more concise
  • Loading branch information
nyurik authored and danielrh committed Mar 16, 2024
1 parent d66b801 commit 5f5504e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 56 deletions.
42 changes: 18 additions & 24 deletions src/ffi/compressor.rs
Expand Up @@ -71,7 +71,7 @@ pub unsafe extern "C" fn BrotliEncoderCreateInstance(
free_func: brotli_free_func,
opaque: *mut c_void,
) -> *mut BrotliEncoderState {
match catch_panic_cstate(|| {
catch_panic_cstate(|| {
let allocators = CAllocator {
alloc_func,
free_func,
Expand All @@ -98,13 +98,11 @@ pub unsafe extern "C" fn BrotliEncoderCreateInstance(
} else {
brotli_new_compressor_without_custom_alloc(to_box)
}
}) {
Ok(ret) => ret,
Err(err) => {
error_print(err);
core::ptr::null_mut()
}
}
})
.unwrap_or_else(|err| {
error_print(err);
core::ptr::null_mut()
})
}

#[no_mangle]
Expand Down Expand Up @@ -186,7 +184,7 @@ pub unsafe extern "C" fn BrotliEncoderCompress(
encoded_size: *mut usize,
encoded_buffer: *mut u8,
) -> i32 {
match catch_panic(|| {
catch_panic(|| {
let input_buf = slice_from_raw_parts_or_nil(input_buffer, input_size);
let encoded_buf = slice_from_raw_parts_or_nil_mut(encoded_buffer, *encoded_size);
let allocators = CAllocator {
Expand Down Expand Up @@ -234,13 +232,11 @@ pub unsafe extern "C" fn BrotliEncoderCompress(
encoded_buf,
&mut |_a, _b, _c, _d| (),
)
}) {
Ok(ret) => ret,
Err(panic_err) => {
error_print(panic_err);
0
}
}
})
.unwrap_or_else(|panic_err| {
error_print(panic_err);
0
})
}

#[no_mangle]
Expand Down Expand Up @@ -273,7 +269,7 @@ pub unsafe extern "C" fn BrotliEncoderCompressStream(
output_buf_ptr: *mut *mut u8,
total_out: *mut usize,
) -> i32 {
match catch_panic(|| {
catch_panic(|| {
let mut input_offset = 0usize;
let mut output_offset = 0usize;
let result;
Expand Down Expand Up @@ -332,13 +328,11 @@ pub unsafe extern "C" fn BrotliEncoderCompressStream(
}
}
result
}) {
Ok(ret) => ret,
Err(panic_err) => {
error_print(panic_err);
0
}
}
})
.unwrap_or_else(|panic_err| {
error_print(panic_err);
0
})
}

#[no_mangle]
Expand Down
56 changes: 24 additions & 32 deletions src/ffi/multicompress/mod.rs
Expand Up @@ -109,7 +109,7 @@ pub unsafe extern "C" fn BrotliEncoderCompressMulti(
return 0;
}
let num_threads = core::cmp::min(desired_num_threads, MAX_THREADS);
match compressor::catch_panic(|| {
compressor::catch_panic(|| {
let param_keys_slice = slice_from_raw_parts_or_nil(param_keys, num_params);
let param_values_slice = slice_from_raw_parts_or_nil(param_values, num_params);
let input_slice = slice_from_raw_parts_or_nil(input, input_size);
Expand Down Expand Up @@ -204,13 +204,11 @@ pub unsafe extern "C" fn BrotliEncoderCompressMulti(
}
Err(_err) => 0,
}
}) {
Ok(ret) => ret,
Err(panic_err) => {
error_print(panic_err);
0
}
}
})
.unwrap_or_else(|panic_err| {
error_print(panic_err);
0
})
}

#[repr(C)]
Expand Down Expand Up @@ -246,7 +244,7 @@ pub unsafe extern "C" fn BrotliEncoderCreateWorkPool(
free_func: brotli_free_func,
opaque: *mut c_void,
) -> *mut BrotliEncoderWorkPool {
match catch_panic_wstate(|| {
catch_panic_wstate(|| {
let allocators = CAllocator {
alloc_func,
free_func,
Expand All @@ -271,13 +269,11 @@ pub unsafe extern "C" fn BrotliEncoderCreateWorkPool(
} else {
brotli_new_work_pool_without_custom_alloc(to_box)
}
}) {
Ok(ret) => ret,
Err(err) => {
error_print(err);
core::ptr::null_mut()
}
}
})
.unwrap_or_else(|err| {
error_print(err);
core::ptr::null_mut()
})
}
#[cfg(feature = "std")]
unsafe fn free_work_pool_no_custom_alloc(_work_pool: *mut BrotliEncoderWorkPool) {
Expand Down Expand Up @@ -329,7 +325,7 @@ pub unsafe extern "C" fn BrotliEncoderCompressWorkPool(
return 0;
}
if work_pool.is_null() {
match compressor::catch_panic(|| {
return compressor::catch_panic(|| {
BrotliEncoderCompressMulti(
num_params,
param_keys,
Expand All @@ -343,16 +339,14 @@ pub unsafe extern "C" fn BrotliEncoderCompressWorkPool(
free_func,
alloc_opaque_per_thread,
)
}) {
Ok(ret) => return ret, // no panic
Err(panic_err) => {
error_print(panic_err); // print panic
return 0; // fail
}
}
})
.unwrap_or_else(|panic_err| {
error_print(panic_err); // print panic
0 // fail
});
}
let work_pool_wrapper = UnsafeUnwindBox(work_pool);
match compressor::catch_panic(|| {
compressor::catch_panic(|| {
let null_opaques = [core::ptr::null_mut::<c_void>(); MAX_THREADS];
let alloc_opaque = if alloc_opaque_per_thread.is_null() {
&null_opaques[..]
Expand Down Expand Up @@ -424,13 +418,11 @@ pub unsafe extern "C" fn BrotliEncoderCompressWorkPool(
}
Err(_err) => 0,
}
}) {
Ok(ret) => ret, // no panic
Err(panic_err) => {
error_print(panic_err); // print panic
0 // fail
}
}
})
.unwrap_or_else(|panic_err| {
error_print(panic_err); // print panic
0 // fail
})
}

#[cfg(all(feature = "std", not(feature = "pass-through-ffi-panics")))]
Expand Down

0 comments on commit 5f5504e

Please sign in to comment.