Skip to content

Commit b72aa0f

Browse files
TaKO8Kiweiznich
authored andcommitted
refactor: splitup the unsafe block
1 parent 8b19ced commit b72aa0f

1 file changed

Lines changed: 36 additions & 38 deletions

File tree

  • diesel/src/sqlite/connection

diesel/src/sqlite/connection/raw.rs

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -257,46 +257,44 @@ extern "C" fn run_custom_function<F>(
257257
static NULL_DATA_ERR: &str = "An unknown error occurred. sqlite3_user_data returned a null pointer. This should never happen.";
258258
static NULL_CONN_ERR: &str = "An unknown error occurred. sqlite3_context_db_handle returned a null pointer. This should never happen.";
259259

260-
unsafe {
261-
let data_ptr = ffi::sqlite3_user_data(ctx);
262-
let data_ptr = data_ptr as *mut F;
263-
let f = match data_ptr.as_mut() {
264-
Some(f) => f,
265-
None => {
266-
context_error_str(ctx, NULL_DATA_ERR);
267-
return;
268-
}
269-
};
260+
let data_ptr = unsafe { ffi::sqlite3_user_data(ctx) };
261+
let data_ptr = data_ptr as *mut F;
262+
let f = match unsafe { data_ptr.as_mut() } {
263+
Some(f) => f,
264+
None => {
265+
unsafe { context_error_str(ctx, NULL_DATA_ERR) };
266+
return;
267+
}
268+
};
270269

271-
let args = slice::from_raw_parts(value_ptr, num_args as _);
272-
let conn = match NonNull::new(ffi::sqlite3_context_db_handle(ctx)) {
273-
Some(conn) => RawConnection {
274-
internal_connection: conn,
275-
},
276-
None => {
277-
context_error_str(ctx, NULL_DATA_ERR);
278-
return;
279-
}
280-
};
270+
let args = unsafe { slice::from_raw_parts(value_ptr, num_args as _) };
271+
let conn = match unsafe { NonNull::new(ffi::sqlite3_context_db_handle(ctx)) } {
272+
Some(conn) => RawConnection {
273+
internal_connection: conn,
274+
},
275+
None => {
276+
unsafe { context_error_str(ctx, NULL_DATA_ERR) };
277+
return;
278+
}
279+
};
281280

282-
let mut f = std::panic::AssertUnwindSafe(f);
283-
let result = std::panic::catch_unwind(move || {
284-
use std::ops::DerefMut as _;
285-
let result = f.deref_mut()(&conn, args);
286-
mem::forget(conn);
287-
result
288-
});
289-
290-
match result {
291-
Ok(Ok(value)) => value.result_of(ctx),
292-
Ok(Err(e)) => {
293-
let msg = e.to_string();
294-
context_error_str(ctx, &msg);
295-
}
296-
Err(_) => {
297-
let msg = format!("{} panicked", std::any::type_name::<F>());
298-
context_error_str(ctx, &msg);
299-
}
281+
let mut f = std::panic::AssertUnwindSafe(f);
282+
let result = std::panic::catch_unwind(move || {
283+
use std::ops::DerefMut as _;
284+
let result = f.deref_mut()(&conn, args);
285+
mem::forget(conn);
286+
result
287+
});
288+
289+
match result {
290+
Ok(Ok(value)) => value.result_of(ctx),
291+
Ok(Err(e)) => {
292+
let msg = e.to_string();
293+
unsafe { context_error_str(ctx, &msg) };
294+
}
295+
Err(_) => {
296+
let msg = format!("{} panicked", std::any::type_name::<F>());
297+
unsafe { context_error_str(ctx, &msg) };
300298
}
301299
}
302300
}

0 commit comments

Comments
 (0)