Remove some AsRef<str> (#3354)

Using &str instead of AsRef is better for compile times, binary size and code complexity.
This commit is contained in:
Hocuri
2022-05-23 12:57:50 +02:00
committed by GitHub
parent 9c41f0fdb9
commit 9549aca48b
10 changed files with 25 additions and 36 deletions

View File

@@ -2,6 +2,9 @@
## Unreleased ## Unreleased
### Changes
- refactorings #3354
### Fixes ### Fixes
- do not unnecessarily SELECT folders if there are no operations planned on - do not unnecessarily SELECT folders if there are no operations planned on
them #3333 them #3333

View File

@@ -786,7 +786,7 @@ impl ChatId {
); );
let row = sql let row = sql
.query_row_optional( .query_row_optional(
query, &query,
paramsv![ paramsv![
self, self,
MessageState::OutPreparing, MessageState::OutPreparing,
@@ -3061,7 +3061,7 @@ pub async fn forward_msgs(context: &Context, msg_ids: &[MsgId], chat_id: ChatId)
let ids = context let ids = context
.sql .sql
.query_map( .query_map(
format!( &format!(
"SELECT id FROM msgs WHERE id IN({}) ORDER BY timestamp,id", "SELECT id FROM msgs WHERE id IN({}) ORDER BY timestamp,id",
sql::repeat_vars(msg_ids.len()) sql::repeat_vars(msg_ids.len())
), ),

View File

@@ -699,7 +699,7 @@ impl Contact {
context context
.sql .sql
.query_map( .query_map(
format!( &format!(
"SELECT c.id FROM contacts c \ "SELECT c.id FROM contacts c \
LEFT JOIN acpeerstates ps ON c.addr=ps.addr \ LEFT JOIN acpeerstates ps ON c.addr=ps.addr \
WHERE c.addr NOT IN ({}) WHERE c.addr NOT IN ({})
@@ -754,7 +754,7 @@ impl Contact {
context context
.sql .sql
.query_map( .query_map(
format!( &format!(
"SELECT id FROM contacts "SELECT id FROM contacts
WHERE addr NOT IN ({}) WHERE addr NOT IN ({})
AND id>? AND id>?

View File

@@ -2008,7 +2008,7 @@ async fn check_verified_properties(
let rows = context let rows = context
.sql .sql
.query_map( .query_map(
format!( &format!(
"SELECT c.addr, LENGTH(ps.verified_key_fingerprint) FROM contacts c \ "SELECT c.addr, LENGTH(ps.verified_key_fingerprint) FROM contacts c \
LEFT JOIN acpeerstates ps ON c.addr=ps.addr WHERE c.id IN({}) ", LEFT JOIN acpeerstates ps ON c.addr=ps.addr WHERE c.id IN({}) ",
sql::repeat_vars(to_ids.len()) sql::repeat_vars(to_ids.len())

View File

@@ -307,7 +307,7 @@ pub(crate) async fn start_ephemeral_timers_msgids(
let count = context let count = context
.sql .sql
.execute( .execute(
format!( &format!(
"UPDATE msgs SET ephemeral_timestamp = ? + ephemeral_timer "UPDATE msgs SET ephemeral_timestamp = ? + ephemeral_timer
WHERE (ephemeral_timestamp == 0 OR ephemeral_timestamp > ? + ephemeral_timer) AND ephemeral_timer > 0 WHERE (ephemeral_timestamp == 0 OR ephemeral_timestamp > ? + ephemeral_timer) AND ephemeral_timer > 0
AND id IN ({})", AND id IN ({})",

View File

@@ -910,7 +910,7 @@ impl Imap {
context context
.sql .sql
.execute( .execute(
format!( &format!(
"DELETE FROM imap WHERE id IN ({})", "DELETE FROM imap WHERE id IN ({})",
sql::repeat_vars(row_ids.len()) sql::repeat_vars(row_ids.len())
), ),
@@ -947,7 +947,7 @@ impl Imap {
context context
.sql .sql
.execute( .execute(
format!( &format!(
"DELETE FROM imap WHERE id IN ({})", "DELETE FROM imap WHERE id IN ({})",
sql::repeat_vars(row_ids.len()) sql::repeat_vars(row_ids.len())
), ),
@@ -989,7 +989,7 @@ impl Imap {
context context
.sql .sql
.execute( .execute(
format!( &format!(
"UPDATE imap SET target='' WHERE id IN ({})", "UPDATE imap SET target='' WHERE id IN ({})",
sql::repeat_vars(row_ids.len()) sql::repeat_vars(row_ids.len())
), ),
@@ -1106,7 +1106,7 @@ impl Imap {
context context
.sql .sql
.execute( .execute(
format!( &format!(
"DELETE FROM imap_markseen WHERE id IN ({})", "DELETE FROM imap_markseen WHERE id IN ({})",
sql::repeat_vars(rowid_set.len()) sql::repeat_vars(rowid_set.len())
), ),

View File

@@ -1283,7 +1283,7 @@ pub async fn markseen_msgs(context: &Context, msg_ids: Vec<MsgId>) -> Result<()>
let msgs = context let msgs = context
.sql .sql
.query_map( .query_map(
format!( &format!(
"SELECT "SELECT
m.id AS id, m.id AS id,
m.chat_id AS chat_id, m.chat_id AS chat_id,

View File

@@ -593,7 +593,7 @@ async fn send_mdn_msg_id(
); );
context context
.sql .sql
.execute(q, rusqlite::params_from_iter(additional_msg_ids)) .execute(&q, rusqlite::params_from_iter(additional_msg_ids))
.await?; .await?;
} }
Ok(()) Ok(())

View File

@@ -331,24 +331,16 @@ impl Sql {
} }
/// Execute the given query, returning the number of affected rows. /// Execute the given query, returning the number of affected rows.
pub async fn execute( pub async fn execute(&self, query: &str, params: impl rusqlite::Params) -> Result<usize> {
&self,
query: impl AsRef<str>,
params: impl rusqlite::Params,
) -> Result<usize> {
let conn = self.get_conn().await?; let conn = self.get_conn().await?;
let res = conn.execute(query.as_ref(), params)?; let res = conn.execute(query, params)?;
Ok(res) Ok(res)
} }
/// Executes the given query, returning the last inserted row ID. /// Executes the given query, returning the last inserted row ID.
pub async fn insert( pub async fn insert(&self, query: &str, params: impl rusqlite::Params) -> Result<i64> {
&self,
query: impl AsRef<str>,
params: impl rusqlite::Params,
) -> Result<i64> {
let conn = self.get_conn().await?; let conn = self.get_conn().await?;
conn.execute(query.as_ref(), params)?; conn.execute(query, params)?;
Ok(conn.last_insert_rowid()) Ok(conn.last_insert_rowid())
} }
@@ -357,7 +349,7 @@ impl Sql {
/// result of that function. /// result of that function.
pub async fn query_map<T, F, G, H>( pub async fn query_map<T, F, G, H>(
&self, &self,
sql: impl AsRef<str>, sql: &str,
params: impl rusqlite::Params, params: impl rusqlite::Params,
f: F, f: F,
mut g: G, mut g: G,
@@ -366,8 +358,6 @@ impl Sql {
F: FnMut(&rusqlite::Row) -> rusqlite::Result<T>, F: FnMut(&rusqlite::Row) -> rusqlite::Result<T>,
G: FnMut(rusqlite::MappedRows<F>) -> Result<H>, G: FnMut(rusqlite::MappedRows<F>) -> Result<H>,
{ {
let sql = sql.as_ref();
let conn = self.get_conn().await?; let conn = self.get_conn().await?;
let mut stmt = conn.prepare(sql)?; let mut stmt = conn.prepare(sql)?;
let res = stmt.query_map(params, f)?; let res = stmt.query_map(params, f)?;
@@ -385,11 +375,7 @@ impl Sql {
} }
/// Used for executing `SELECT COUNT` statements only. Returns the resulting count. /// Used for executing `SELECT COUNT` statements only. Returns the resulting count.
pub async fn count( pub async fn count(&self, query: &str, params: impl rusqlite::Params) -> anyhow::Result<usize> {
&self,
query: impl AsRef<str>,
params: impl rusqlite::Params,
) -> anyhow::Result<usize> {
let count: isize = self.query_row(query, params, |row| row.get(0)).await?; let count: isize = self.query_row(query, params, |row| row.get(0)).await?;
Ok(usize::try_from(count)?) Ok(usize::try_from(count)?)
} }
@@ -404,7 +390,7 @@ impl Sql {
/// Execute a query which is expected to return one row. /// Execute a query which is expected to return one row.
pub async fn query_row<T, F>( pub async fn query_row<T, F>(
&self, &self,
query: impl AsRef<str>, query: &str,
params: impl rusqlite::Params, params: impl rusqlite::Params,
f: F, f: F,
) -> Result<T> ) -> Result<T>
@@ -412,7 +398,7 @@ impl Sql {
F: FnOnce(&rusqlite::Row) -> rusqlite::Result<T>, F: FnOnce(&rusqlite::Row) -> rusqlite::Result<T>,
{ {
let conn = self.get_conn().await?; let conn = self.get_conn().await?;
let res = conn.query_row(query.as_ref(), params, f)?; let res = conn.query_row(query, params, f)?;
Ok(res) Ok(res)
} }
@@ -474,7 +460,7 @@ impl Sql {
/// Execute a query which is expected to return zero or one row. /// Execute a query which is expected to return zero or one row.
pub async fn query_row_optional<T, F>( pub async fn query_row_optional<T, F>(
&self, &self,
sql: impl AsRef<str>, sql: &str,
params: impl rusqlite::Params, params: impl rusqlite::Params,
f: F, f: F,
) -> anyhow::Result<Option<T>> ) -> anyhow::Result<Option<T>>

View File

@@ -199,7 +199,7 @@ impl Context {
pub(crate) async fn delete_sync_ids(&self, ids: String) -> Result<()> { pub(crate) async fn delete_sync_ids(&self, ids: String) -> Result<()> {
self.sql self.sql
.execute( .execute(
format!("DELETE FROM multi_device_sync WHERE id IN ({});", ids), &format!("DELETE FROM multi_device_sync WHERE id IN ({});", ids),
paramsv![], paramsv![],
) )
.await?; .await?;