mirror of
https://github.com/chatmail/core.git
synced 2026-04-18 22:16:30 +03:00
Make the callback optional again
The C API allows passing a NULL pointer is for the callback function. However when calling the callback nothing checks for this null pointer and thus things fail badly. Even worse since the C API is defined using an "fn pointer" rather than a "*-ptr" or raw pointer to the function rust does not realise this can be invalid and therefore the typechecker does not catch this even though there are no unsafe casts. Fix this by making the callback an Option in rust, this can be easily checked when calling. Also add a Context.call_cb() function which simplifies calling the callback, hides the weird syntax due to the function pointer and makes the call a little easier. Finally it also means the option checking is only needed in one place. For the C API this needs to check if this is a NULL pointer or not, this is implicitly done by rust using the "nullable pointer optimisation": https://doc.rust-lang.org/nomicon/ffi.html#the-nullable-pointer-optimization
This commit is contained in:
@@ -577,12 +577,7 @@ pub unsafe fn dc_delete_msgs(context: &Context, msg_ids: *const uint32_t, msg_cn
|
||||
}
|
||||
|
||||
if 0 != msg_cnt {
|
||||
((*context).cb)(
|
||||
context,
|
||||
Event::MSGS_CHANGED,
|
||||
0i32 as uintptr_t,
|
||||
0i32 as uintptr_t,
|
||||
);
|
||||
context.call_cb(Event::MSGS_CHANGED, 0i32 as uintptr_t, 0i32 as uintptr_t);
|
||||
dc_job_kill_action(context, 105i32);
|
||||
dc_job_add(context, 105i32, 0i32, 0 as *const libc::c_char, 10i32);
|
||||
};
|
||||
@@ -645,12 +640,7 @@ pub unsafe fn dc_markseen_msgs(context: &Context, msg_ids: *const uint32_t, msg_
|
||||
}
|
||||
|
||||
if 0 != send_event {
|
||||
((*context).cb)(
|
||||
context,
|
||||
Event::MSGS_CHANGED,
|
||||
0i32 as uintptr_t,
|
||||
0i32 as uintptr_t,
|
||||
);
|
||||
context.call_cb(Event::MSGS_CHANGED, 0i32 as uintptr_t, 0i32 as uintptr_t);
|
||||
}
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
@@ -1288,8 +1278,7 @@ pub unsafe fn dc_set_msg_failed(context: &Context, msg_id: uint32_t, error: *con
|
||||
sqlite3_bind_text(stmt, 2i32, (*(*msg).param).packed, -1i32, None);
|
||||
sqlite3_bind_int(stmt, 3i32, msg_id as libc::c_int);
|
||||
sqlite3_step(stmt);
|
||||
((*context).cb)(
|
||||
context,
|
||||
context.call_cb(
|
||||
Event::MSG_FAILED,
|
||||
(*msg).chat_id as uintptr_t,
|
||||
msg_id as uintptr_t,
|
||||
|
||||
Reference in New Issue
Block a user