Update unsafety of C function pointers for new bindgen

This commit is contained in:
John Gallagher 2015-12-13 08:40:51 -05:00
parent e94a5b8411
commit b883ab651d
2 changed files with 23 additions and 25 deletions

View File

@ -407,26 +407,24 @@ impl InnerConnection {
where F: FnMut(&Context) -> Result<T>, where F: FnMut(&Context) -> Result<T>,
T: ToResult T: ToResult
{ {
extern "C" fn call_boxed_closure<F, T>(ctx: *mut sqlite3_context, unsafe extern "C" fn call_boxed_closure<F, T>(ctx: *mut sqlite3_context,
argc: c_int, argc: c_int,
argv: *mut *mut sqlite3_value) argv: *mut *mut sqlite3_value)
where F: FnMut(&Context) -> Result<T>, where F: FnMut(&Context) -> Result<T>,
T: ToResult T: ToResult
{ {
unsafe { let ctx = Context {
let ctx = Context { ctx: ctx,
ctx: ctx, args: slice::from_raw_parts(argv, argc as usize),
args: slice::from_raw_parts(argv, argc as usize), };
}; let boxed_f: *mut F = mem::transmute(ffi::sqlite3_user_data(ctx.ctx));
let boxed_f: *mut F = mem::transmute(ffi::sqlite3_user_data(ctx.ctx)); assert!(!boxed_f.is_null(), "Internal error - null function pointer");
assert!(!boxed_f.is_null(), "Internal error - null function pointer"); match (*boxed_f)(&ctx) {
match (*boxed_f)(&ctx) { Ok(r) => r.set_result(ctx.ctx),
Ok(r) => r.set_result(ctx.ctx), Err(e) => {
Err(e) => { ffi::sqlite3_result_error_code(ctx.ctx, e.code);
ffi::sqlite3_result_error_code(ctx.ctx, e.code); if let Ok(cstr) = str_to_cstring(&e.message) {
if let Ok(cstr) = str_to_cstring(&e.message) { ffi::sqlite3_result_error(ctx.ctx, cstr.as_ptr(), -1);
ffi::sqlite3_result_error(ctx.ctx, cstr.as_ptr(), -1);
}
} }
} }
} }

View File

@ -67,9 +67,9 @@ impl Connection {
/// There can only be a single tracer defined for each database connection. /// There can only be a single tracer defined for each database connection.
/// Setting a new tracer clears the old one. /// Setting a new tracer clears the old one.
pub fn trace(&mut self, trace_fn: Option<fn(&str)>) { pub fn trace(&mut self, trace_fn: Option<fn(&str)>) {
extern "C" fn trace_callback(p_arg: *mut c_void, z_sql: *const c_char) { unsafe extern "C" fn trace_callback(p_arg: *mut c_void, z_sql: *const c_char) {
let trace_fn: fn(&str) = unsafe { mem::transmute(p_arg) }; let trace_fn: fn(&str) = mem::transmute(p_arg);
let c_slice = unsafe { CStr::from_ptr(z_sql).to_bytes() }; let c_slice = CStr::from_ptr(z_sql).to_bytes();
if let Ok(s) = str::from_utf8(c_slice) { if let Ok(s) = str::from_utf8(c_slice) {
trace_fn(s); trace_fn(s);
} }
@ -91,11 +91,11 @@ impl Connection {
/// There can only be a single profiler defined for each database connection. /// There can only be a single profiler defined for each database connection.
/// Setting a new profiler clears the old one. /// Setting a new profiler clears the old one.
pub fn profile(&mut self, profile_fn: Option<fn(&str, Duration)>) { pub fn profile(&mut self, profile_fn: Option<fn(&str, Duration)>) {
extern "C" fn profile_callback(p_arg: *mut c_void, unsafe extern "C" fn profile_callback(p_arg: *mut c_void,
z_sql: *const c_char, z_sql: *const c_char,
nanoseconds: u64) { nanoseconds: u64) {
let profile_fn: fn(&str, Duration) = unsafe { mem::transmute(p_arg) }; let profile_fn: fn(&str, Duration) = mem::transmute(p_arg);
let c_slice = unsafe { CStr::from_ptr(z_sql).to_bytes() }; let c_slice = CStr::from_ptr(z_sql).to_bytes();
if let Ok(s) = str::from_utf8(c_slice) { if let Ok(s) = str::from_utf8(c_slice) {
const NANOS_PER_SEC: u64 = 1_000_000_000; const NANOS_PER_SEC: u64 = 1_000_000_000;