Skip to content

Commit

Permalink
Remove the remaining AsRef<str> (#3669)
Browse files Browse the repository at this point in the history
Using &str instead of AsRef is better for compile times, binary size and code complexity.
  • Loading branch information
iequidoo committed Nov 28, 2022
1 parent e5be023 commit f6a502a
Show file tree
Hide file tree
Showing 16 changed files with 153 additions and 167 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

### Changes
- Refactor: Remove the remaining AsRef<str> #3669

### API-Changes
- Add Python API to send reactions #3762
Expand Down
2 changes: 1 addition & 1 deletion deltachat-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1659,7 +1659,7 @@ pub unsafe extern "C" fn dc_set_chat_profile_image(
let ctx = &*context;

block_on(async move {
chat::set_chat_profile_image(ctx, ChatId::new(chat_id), to_string_lossy(image))
chat::set_chat_profile_image(ctx, ChatId::new(chat_id), &to_string_lossy(image))
.await
.map(|_| 1)
.unwrap_or_log_default(ctx, "Failed to set profile image")
Expand Down
2 changes: 1 addition & 1 deletion deltachat-jsonrpc/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ impl CommandApi {
image_path: Option<String>,
) -> Result<()> {
let ctx = self.get_context(account_id).await?;
chat::set_chat_profile_image(&ctx, ChatId::new(chat_id), image_path.unwrap_or_default())
chat::set_chat_profile_image(&ctx, ChatId::new(chat_id), &image_path.unwrap_or_default())
.await
}

Expand Down
9 changes: 4 additions & 5 deletions src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2188,7 +2188,7 @@ pub async fn send_videochat_invitation(context: &Context, chat_id: ChatId) -> Re
let mut msg = Message::new(Viewtype::VideochatInvitation);
msg.param.set(Param::WebrtcRoom, &instance);
msg.text = Some(
stock_str::videochat_invite_msg_body(context, Message::parse_webrtc_instance(&instance).1)
stock_str::videochat_invite_msg_body(context, &Message::parse_webrtc_instance(&instance).1)
.await,
);
send_msg(context, chat_id, &mut msg).await
Expand Down Expand Up @@ -3005,7 +3005,7 @@ pub async fn set_chat_name(context: &Context, chat_id: ChatId, new_name: &str) -
pub async fn set_chat_profile_image(
context: &Context,
chat_id: ChatId,
new_image: impl AsRef<str>, // XXX use PathBuf
new_image: &str, // XXX use PathBuf
) -> Result<()> {
ensure!(!chat_id.is_special(), "Invalid chat ID");
let mut chat = Chat::load_from_db(context, chat_id).await?;
Expand All @@ -3023,13 +3023,12 @@ pub async fn set_chat_profile_image(
let mut msg = Message::new(Viewtype::Text);
msg.param
.set_int(Param::Cmd, SystemMessage::GroupImageChanged as i32);
if new_image.as_ref().is_empty() {
if new_image.is_empty() {
chat.param.remove(Param::ProfileImage);
msg.param.remove(Param::Arg);
msg.text = Some(stock_str::msg_grp_img_deleted(context, ContactId::SELF).await);
} else {
let mut image_blob =
BlobObject::new_from_path(context, Path::new(new_image.as_ref())).await?;
let mut image_blob = BlobObject::new_from_path(context, Path::new(new_image)).await?;
image_blob.recode_to_avatar_size(context).await?;
chat.param.set(Param::ProfileImage, image_blob.as_name());
msg.param.set(Param::Arg, image_blob.as_name());
Expand Down
20 changes: 12 additions & 8 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,20 +194,20 @@ pub enum Config {

impl Context {
pub async fn config_exists(&self, key: Config) -> Result<bool> {
Ok(self.sql.get_raw_config(key).await?.is_some())
Ok(self.sql.get_raw_config(key.as_ref()).await?.is_some())
}

/// Get a configuration key. Returns `None` if no value is set, and no default value found.
pub async fn get_config(&self, key: Config) -> Result<Option<String>> {
let value = match key {
Config::Selfavatar => {
let rel_path = self.sql.get_raw_config(key).await?;
let rel_path = self.sql.get_raw_config(key.as_ref()).await?;
rel_path.map(|p| get_abs_path(self, &p).to_string_lossy().into_owned())
}
Config::SysVersion => Some((*DC_VERSION_STR).clone()),
Config::SysMsgsizeMaxRecommended => Some(format!("{}", RECOMMENDED_FILE_SIZE)),
Config::SysConfigKeys => Some(get_config_keys_string()),
_ => self.sql.get_raw_config(key).await?,
_ => self.sql.get_raw_config(key.as_ref()).await?,
};

if value.is_some() {
Expand Down Expand Up @@ -297,26 +297,30 @@ impl Context {
Some(value) => {
let mut blob = BlobObject::new_from_path(self, value.as_ref()).await?;
blob.recode_to_avatar_size(self).await?;
self.sql.set_raw_config(key, Some(blob.as_name())).await?;
self.sql
.set_raw_config(key.as_ref(), Some(blob.as_name()))
.await?;
}
None => {
self.sql.set_raw_config(key, None).await?;
self.sql.set_raw_config(key.as_ref(), None).await?;
}
}
self.emit_event(EventType::SelfavatarChanged);
}
Config::DeleteDeviceAfter => {
let ret = self.sql.set_raw_config(key, value).await;
let ret = self.sql.set_raw_config(key.as_ref(), value).await;
// Interrupt ephemeral loop to delete old messages immediately.
self.interrupt_ephemeral_task().await;
ret?
}
Config::Displayname => {
let value = value.map(improve_single_line_input);
self.sql.set_raw_config(key, value.as_deref()).await?;
self.sql
.set_raw_config(key.as_ref(), value.as_deref())
.await?;
}
_ => {
self.sql.set_raw_config(key, value).await?;
self.sql.set_raw_config(key.as_ref(), value).await?;
}
}
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions src/configure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl Context {
self,
// We are using Anyhow's .context() and to show the
// inner error, too, we need the {:#}:
format!("{:#}", err),
&format!("{:#}", err),
)
.await
)
Expand Down Expand Up @@ -153,7 +153,7 @@ async fn on_configure_completed(
if !addr_cmp(&new_addr, &old_addr) {
let mut msg = Message::new(Viewtype::Text);
msg.text =
Some(stock_str::aeap_explanation_and_link(context, old_addr, new_addr).await);
Some(stock_str::aeap_explanation_and_link(context, &old_addr, &new_addr).await);
chat::add_device_msg(context, None, Some(&mut msg))
.await
.ok_or_log_msg(context, "Cannot add AEAP explanation");
Expand Down
10 changes: 5 additions & 5 deletions src/ephemeral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,13 @@ pub(crate) async fn stock_ephemeral_timer_changed(
Timer::Disabled => stock_str::msg_ephemeral_timer_disabled(context, from_id).await,
Timer::Enabled { duration } => match duration {
0..=59 => {
stock_str::msg_ephemeral_timer_enabled(context, timer.to_string(), from_id).await
stock_str::msg_ephemeral_timer_enabled(context, &timer.to_string(), from_id).await
}
60 => stock_str::msg_ephemeral_timer_minute(context, from_id).await,
61..=3599 => {
stock_str::msg_ephemeral_timer_minutes(
context,
format!("{}", (f64::from(duration) / 6.0).round() / 10.0),
&format!("{}", (f64::from(duration) / 6.0).round() / 10.0),
from_id,
)
.await
Expand All @@ -241,7 +241,7 @@ pub(crate) async fn stock_ephemeral_timer_changed(
3601..=86399 => {
stock_str::msg_ephemeral_timer_hours(
context,
format!("{}", (f64::from(duration) / 360.0).round() / 10.0),
&format!("{}", (f64::from(duration) / 360.0).round() / 10.0),
from_id,
)
.await
Expand All @@ -250,7 +250,7 @@ pub(crate) async fn stock_ephemeral_timer_changed(
86401..=604_799 => {
stock_str::msg_ephemeral_timer_days(
context,
format!("{}", (f64::from(duration) / 8640.0).round() / 10.0),
&format!("{}", (f64::from(duration) / 8640.0).round() / 10.0),
from_id,
)
.await
Expand All @@ -259,7 +259,7 @@ pub(crate) async fn stock_ephemeral_timer_changed(
_ => {
stock_str::msg_ephemeral_timer_weeks(
context,
format!("{}", (f64::from(duration) / 60480.0).round() / 10.0),
&format!("{}", (f64::from(duration) / 60480.0).round() / 10.0),
from_id,
)
.await
Expand Down
58 changes: 29 additions & 29 deletions src/login_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,73 +169,73 @@ impl LoginParam {
async fn from_database(context: &Context, prefix: &str) -> Result<Self> {
let sql = &context.sql;

let key = format!("{}addr", prefix);
let key = &format!("{}addr", prefix);
let addr = sql
.get_raw_config(key)
.await?
.unwrap_or_default()
.trim()
.to_string();

let key = format!("{}mail_server", prefix);
let key = &format!("{}mail_server", prefix);
let mail_server = sql.get_raw_config(key).await?.unwrap_or_default();

let key = format!("{}mail_port", prefix);
let key = &format!("{}mail_port", prefix);
let mail_port = sql.get_raw_config_int(key).await?.unwrap_or_default();

let key = format!("{}mail_user", prefix);
let key = &format!("{}mail_user", prefix);
let mail_user = sql.get_raw_config(key).await?.unwrap_or_default();

let key = format!("{}mail_pw", prefix);
let key = &format!("{}mail_pw", prefix);
let mail_pw = sql.get_raw_config(key).await?.unwrap_or_default();

let key = format!("{}mail_security", prefix);
let key = &format!("{}mail_security", prefix);
let mail_security = sql
.get_raw_config_int(key)
.await?
.and_then(num_traits::FromPrimitive::from_i32)
.unwrap_or_default();

let key = format!("{}imap_certificate_checks", prefix);
let key = &format!("{}imap_certificate_checks", prefix);
let imap_certificate_checks =
if let Some(certificate_checks) = sql.get_raw_config_int(key).await? {
num_traits::FromPrimitive::from_i32(certificate_checks).unwrap()
} else {
Default::default()
};

let key = format!("{}send_server", prefix);
let key = &format!("{}send_server", prefix);
let send_server = sql.get_raw_config(key).await?.unwrap_or_default();

let key = format!("{}send_port", prefix);
let key = &format!("{}send_port", prefix);
let send_port = sql.get_raw_config_int(key).await?.unwrap_or_default();

let key = format!("{}send_user", prefix);
let key = &format!("{}send_user", prefix);
let send_user = sql.get_raw_config(key).await?.unwrap_or_default();

let key = format!("{}send_pw", prefix);
let key = &format!("{}send_pw", prefix);
let send_pw = sql.get_raw_config(key).await?.unwrap_or_default();

let key = format!("{}send_security", prefix);
let key = &format!("{}send_security", prefix);
let send_security = sql
.get_raw_config_int(key)
.await?
.and_then(num_traits::FromPrimitive::from_i32)
.unwrap_or_default();

let key = format!("{}smtp_certificate_checks", prefix);
let key = &format!("{}smtp_certificate_checks", prefix);
let smtp_certificate_checks =
if let Some(certificate_checks) = sql.get_raw_config_int(key).await? {
num_traits::FromPrimitive::from_i32(certificate_checks).unwrap_or_default()
} else {
Default::default()
};

let key = format!("{}server_flags", prefix);
let key = &format!("{}server_flags", prefix);
let server_flags = sql.get_raw_config_int(key).await?.unwrap_or_default();
let oauth2 = matches!(server_flags & DC_LP_AUTH_FLAGS, DC_LP_AUTH_OAUTH2);

let key = format!("{}provider", prefix);
let key = &format!("{}provider", prefix);
let provider = sql
.get_raw_config(key)
.await?
Expand Down Expand Up @@ -275,58 +275,58 @@ impl LoginParam {

context.set_primary_self_addr(&self.addr).await?;

let key = format!("{}mail_server", prefix);
let key = &format!("{}mail_server", prefix);
sql.set_raw_config(key, Some(&self.imap.server)).await?;

let key = format!("{}mail_port", prefix);
let key = &format!("{}mail_port", prefix);
sql.set_raw_config_int(key, i32::from(self.imap.port))
.await?;

let key = format!("{}mail_user", prefix);
let key = &format!("{}mail_user", prefix);
sql.set_raw_config(key, Some(&self.imap.user)).await?;

let key = format!("{}mail_pw", prefix);
let key = &format!("{}mail_pw", prefix);
sql.set_raw_config(key, Some(&self.imap.password)).await?;

let key = format!("{}mail_security", prefix);
let key = &format!("{}mail_security", prefix);
sql.set_raw_config_int(key, self.imap.security as i32)
.await?;

let key = format!("{}imap_certificate_checks", prefix);
let key = &format!("{}imap_certificate_checks", prefix);
sql.set_raw_config_int(key, self.imap.certificate_checks as i32)
.await?;

let key = format!("{}send_server", prefix);
let key = &format!("{}send_server", prefix);
sql.set_raw_config(key, Some(&self.smtp.server)).await?;

let key = format!("{}send_port", prefix);
let key = &format!("{}send_port", prefix);
sql.set_raw_config_int(key, i32::from(self.smtp.port))
.await?;

let key = format!("{}send_user", prefix);
let key = &format!("{}send_user", prefix);
sql.set_raw_config(key, Some(&self.smtp.user)).await?;

let key = format!("{}send_pw", prefix);
let key = &format!("{}send_pw", prefix);
sql.set_raw_config(key, Some(&self.smtp.password)).await?;

let key = format!("{}send_security", prefix);
let key = &format!("{}send_security", prefix);
sql.set_raw_config_int(key, self.smtp.security as i32)
.await?;

let key = format!("{}smtp_certificate_checks", prefix);
let key = &format!("{}smtp_certificate_checks", prefix);
sql.set_raw_config_int(key, self.smtp.certificate_checks as i32)
.await?;

// The OAuth2 flag is either set for both IMAP and SMTP or not at all.
let key = format!("{}server_flags", prefix);
let key = &format!("{}server_flags", prefix);
let server_flags = match self.imap.oauth2 {
true => DC_LP_AUTH_OAUTH2,
false => DC_LP_AUTH_NORMAL,
};
sql.set_raw_config_int(key, server_flags).await?;

if let Some(provider) = self.provider {
let key = format!("{}provider", prefix);
let key = &format!("{}provider", prefix);
sql.set_raw_config(key, Some(provider.id)).await?;
}

Expand Down
4 changes: 2 additions & 2 deletions src/mimefactory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ impl<'a> MimeFactory<'a> {
}
}

let self_name = match context.get_config(Config::Displayname).await? {
let self_name = &match context.get_config(Config::Displayname).await? {
Some(name) => name,
None => context.get_config(Config::Addr).await?.unwrap_or_default(),
};
Expand Down Expand Up @@ -1259,7 +1259,7 @@ impl<'a> MimeFactory<'a> {
.truncated_text(32)
.to_string()
};
let p2 = stock_str::read_rcpt_mail_body(context, p1).await;
let p2 = stock_str::read_rcpt_mail_body(context, &p1).await;
let message_text = format!("{}\r\n", format_flowed(&p2));
message = message.child(
PartBuilder::new()
Expand Down
2 changes: 1 addition & 1 deletion src/peerstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ impl Peerstate {
let chats = Chatlist::try_load(context, 0, None, Some(contact_id)).await?;
let msg = match &change {
PeerstateChange::FingerprintChange => {
stock_str::contact_setup_changed(context, self.addr.clone()).await
stock_str::contact_setup_changed(context, &self.addr).await
}
PeerstateChange::Aeap(new_addr) => {
let old_contact = Contact::load_from_db(context, contact_id).await?;
Expand Down
Loading

0 comments on commit f6a502a

Please sign in to comment.