Skip to content

Commit f148966

Browse files
committed
future: optimise cass_future_error_message()
The function always allocates a new string for the error message when called the first time on a future in order to return a pointer to that allocated string to the caller. This is unnecessary when the future's error kind is already the string form. This commit performs a small refactor that makes allocation avoided for those two cases with errors given in a string form. Credits to @Lorak-mmk for the suggestion.
1 parent a1ca3f7 commit f148966

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

scylla-rust-wrapper/src/future.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -552,13 +552,13 @@ pub unsafe extern "C" fn cass_future_error_message(
552552
};
553553

554554
let value = future.waited_result();
555-
let msg = future.err_string.get_or_init(|| match value {
556-
Ok(CassResultValue::QueryError(err)) => err.msg(),
557-
Err((_, s)) => s.msg(),
558-
_ => "".to_string(),
559-
});
555+
let msg = match value {
556+
Ok(CassResultValue::QueryError(err)) => future.err_string.get_or_init(|| err.msg()),
557+
Err((_, s)) => s,
558+
_ => "",
559+
};
560560

561-
unsafe { write_str_to_c(msg.as_str(), message, message_length) };
561+
unsafe { write_str_to_c(msg, message, message_length) };
562562
}
563563

564564
#[unsafe(no_mangle)]

0 commit comments

Comments
 (0)