mirror of
https://github.com/chatmail/core.git
synced 2026-05-07 17:06:35 +03:00
Debloat the binary by using less AsRef arguments
Using `impl AsRef<str>` as the argument instead of `&str` makes it possible to call the function with `&str`, `String` and other types that implement `AsRef` trait. The cost of it is that compiled binary contains mulitple versions of the same function, one for each variant of types. If function contains multiple generic `impl AsRef` arguments, the number of versions possibly compiled into binary grows exponentially with the number of arguments. Simple way to avoid it is to call `.as_ref()` on the caller side to convert the argument to `&str`. In most cases even adding a `&` and relying on `Deref` coercion is sufficient. This patch changes many functions that accepted `impl AsRef<str>` and `impl AsRef<Path>` to accept `&str` and `&Path` instead. In some places `.clone()` calls are removed. Calling `.clone()` on `String` and passing `String` to a function accepting `impl AsRef<str>` is completely unnecessary as `&str` reference could be passed instead. There is no clippy warning against it yet, but changing argument type to `&str` allowed to find these cases. The result of debloating is not impressive, several hundred kilobytes are saved, which is about 3% of the `.so` binary, but the code is cleaner too.
This commit is contained in:
@@ -271,7 +271,7 @@ pub unsafe extern "C" fn dc_get_oauth2_url(
|
||||
let redirect = to_string_lossy(redirect);
|
||||
|
||||
block_on(async move {
|
||||
match oauth2::dc_get_oauth2_url(&ctx, addr, redirect).await {
|
||||
match oauth2::dc_get_oauth2_url(&ctx, &addr, &redirect).await {
|
||||
Some(res) => res.strdup(),
|
||||
None => ptr::null_mut(),
|
||||
}
|
||||
@@ -1188,7 +1188,7 @@ pub unsafe extern "C" fn dc_search_msgs(
|
||||
|
||||
block_on(async move {
|
||||
let arr = dc_array_t::from(
|
||||
ctx.search_msgs(chat_id, to_string_lossy(query))
|
||||
ctx.search_msgs(chat_id, &to_string_lossy(query))
|
||||
.await
|
||||
.unwrap_or_log_default(ctx, "Failed search_msgs")
|
||||
.iter()
|
||||
@@ -1237,7 +1237,7 @@ pub unsafe extern "C" fn dc_create_group_chat(
|
||||
};
|
||||
|
||||
block_on(async move {
|
||||
chat::create_group_chat(&ctx, protect, to_string_lossy(name))
|
||||
chat::create_group_chat(&ctx, protect, &to_string_lossy(name))
|
||||
.await
|
||||
.log_err(ctx, "Failed to create group chat")
|
||||
.map(|id| id.to_u32())
|
||||
@@ -1312,7 +1312,7 @@ pub unsafe extern "C" fn dc_set_chat_name(
|
||||
let ctx = &*context;
|
||||
|
||||
block_on(async move {
|
||||
chat::set_chat_name(&ctx, ChatId::new(chat_id), to_string_lossy(name))
|
||||
chat::set_chat_name(&ctx, ChatId::new(chat_id), &to_string_lossy(name))
|
||||
.await
|
||||
.map(|_| 1)
|
||||
.unwrap_or_log_default(&ctx, "Failed to set chat name")
|
||||
@@ -1642,7 +1642,7 @@ pub unsafe extern "C" fn dc_create_contact(
|
||||
let name = to_string_lossy(name);
|
||||
|
||||
block_on(async move {
|
||||
Contact::create(&ctx, name, to_string_lossy(addr))
|
||||
Contact::create(&ctx, &name, &to_string_lossy(addr))
|
||||
.await
|
||||
.unwrap_or(0)
|
||||
})
|
||||
@@ -1660,7 +1660,7 @@ pub unsafe extern "C" fn dc_add_address_book(
|
||||
let ctx = &*context;
|
||||
|
||||
block_on(async move {
|
||||
match Contact::add_address_book(&ctx, to_string_lossy(addr_book)).await {
|
||||
match Contact::add_address_book(&ctx, &to_string_lossy(addr_book)).await {
|
||||
Ok(cnt) => cnt as libc::c_int,
|
||||
Err(_) => 0,
|
||||
}
|
||||
@@ -1827,7 +1827,7 @@ pub unsafe extern "C" fn dc_imex(
|
||||
|
||||
if let Some(param1) = to_opt_string_lossy(param1) {
|
||||
spawn(async move {
|
||||
imex::imex(&ctx, what, ¶m1)
|
||||
imex::imex(&ctx, what, param1.as_ref())
|
||||
.await
|
||||
.log_err(ctx, "IMEX failed")
|
||||
});
|
||||
@@ -1848,7 +1848,7 @@ pub unsafe extern "C" fn dc_imex_has_backup(
|
||||
let ctx = &*context;
|
||||
|
||||
block_on(async move {
|
||||
match imex::has_backup(&ctx, to_string_lossy(dir)).await {
|
||||
match imex::has_backup(&ctx, to_string_lossy(dir).as_ref()).await {
|
||||
Ok(res) => res.strdup(),
|
||||
Err(err) => {
|
||||
// do not bubble up error to the user,
|
||||
@@ -1929,7 +1929,7 @@ pub unsafe extern "C" fn dc_check_qr(
|
||||
let ctx = &*context;
|
||||
|
||||
block_on(async move {
|
||||
let lot = qr::check_qr(&ctx, to_string_lossy(qr)).await;
|
||||
let lot = qr::check_qr(&ctx, &to_string_lossy(qr)).await;
|
||||
Box::into_raw(Box::new(lot))
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user