Skip to content

Commit b68e252

Browse files
committed
Add assert_fail!() convenience function
This encompasses the pattern of eprintln!() + std::process::abort() and adds a request to open an issue if the message is observed.
1 parent 1568d9f commit b68e252

File tree

1 file changed

+18
-18
lines changed
  • diesel/src/sqlite/connection

1 file changed

+18
-18
lines changed

diesel/src/sqlite/connection/raw.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ use crate::result::*;
1515
use crate::serialize::ToSql;
1616
use crate::sql_types::HasSqlType;
1717

18+
/// For use in FFI function, which cannot unwind.
19+
/// Print the message, ask to open an issue at Github and [`abort`](std::process::abort).
20+
macro_rules! assert_fail {
21+
($fmt:expr $(,$args:tt)*) => {
22+
eprint!(concat!(
23+
$fmt,
24+
"If you see this message, please open an issue at https://github.com/diesel-rs/diesel/issues/new.\n",
25+
"Source location: {}:{}\n",
26+
), $($args,)* file!(), line!());
27+
std::process::abort()
28+
};
29+
}
30+
1831
#[allow(missing_debug_implementations, missing_copy_implementations)]
1932
pub struct RawConnection {
2033
pub(crate) internal_connection: NonNull<ffi::sqlite3>,
@@ -328,8 +341,7 @@ extern "C" fn run_aggregator_step_function<ArgsSqlType, RetSqlType, Args, Ret, A
328341
if let &mut OptionalAggregator::Some(ref mut agg) = a_ptr {
329342
agg
330343
} else {
331-
eprintln!("We've written the aggregator to the aggregate context, but it could not be retrieved");
332-
std::process::abort();
344+
assert_fail!("We've written the aggregator to the aggregate context, but it could not be retrieved.");
333345
}
334346
}
335347
None => {
@@ -382,8 +394,7 @@ extern "C" fn run_aggregator_final_function<ArgsSqlType, RetSqlType, Args, Ret,
382394
match std::mem::replace(a, OptionalAggregator::None) {
383395
OptionalAggregator::Some(agg) => agg,
384396
OptionalAggregator::None => {
385-
eprintln!("We've written to the aggregator in the xStep callback. If xStep was never called, then ffi::sqlite_aggregate_context() would have returned a NULL pointer");
386-
std::process::abort();
397+
assert_fail!("We've written to the aggregator in the xStep callback. If xStep was never called, then ffi::sqlite_aggregate_context() would have returned a NULL pointer.");
387398
}
388399
}
389400
});
@@ -427,26 +438,15 @@ where
427438
{
428439
let user_ptr = user_ptr as *const F;
429440
let f = unsafe { user_ptr.as_ref() }.unwrap_or_else(|| {
430-
eprintln!(
431-
"An unknown error occurred. user_ptr is a null pointer. This should never happen."
432-
);
433-
std::process::abort();
441+
assert_fail!("An unknown error occurred. user_ptr is a null pointer. This should never happen.");
434442
});
435443

436444
for (ptr, len, side) in &[(rhs_ptr, rhs_len, "rhs"), (lhs_ptr, lhs_len, "lhs")] {
437445
if *len < 0 {
438-
eprintln!(
439-
"An unknown error occurred. {}_len is negative. This should never happen.",
440-
side
441-
);
442-
std::process::abort();
446+
assert_fail!("An unknown error occurred. {}_len is negative. This should never happen.", side);
443447
}
444448
if ptr.is_null() {
445-
eprintln!(
446-
"An unknown error occurred. {}_ptr is a null pointer. This should never happen.",
447-
side
448-
);
449-
std::process::abort();
449+
assert_fail!("An unknown error occurred. {}_ptr is a null pointer. This should never happen.", side);
450450
}
451451
}
452452

0 commit comments

Comments
 (0)