Skip to content

Commit

Permalink
Switch to Into<String> when accepting string parameters (serenity-r…
Browse files Browse the repository at this point in the history
…s#1903)

This changes instances of `ToString` to `Into<String>` whenever ownership of
the `String` is necessary. In case it is not, it's simply changed to `&str` to
lessen the effects of monomorphization on compile times.
  • Loading branch information
GnomedDev authored and nickelc committed Jun 17, 2022
1 parent 371b045 commit 228f3c6
Show file tree
Hide file tree
Showing 40 changed files with 238 additions and 266 deletions.
2 changes: 1 addition & 1 deletion examples/e06_sample_bot_structure/src/commands/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub async fn multiply(ctx: &Context, msg: &Message, mut args: Args) -> CommandRe

let product = one * two;

msg.channel_id.say(&ctx.http, product).await?;
msg.channel_id.say(&ctx.http, product.to_string()).await?;

Ok(())
}
6 changes: 3 additions & 3 deletions examples/e13_parallel_loops/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ async fn log_system_load(ctx: Arc<Context>) {
.send_message(&ctx, |m| {
m.embed(|e| {
e.title("System Resource Load")
.field("CPU Load Average", format!("{:.2}%", cpu_load.one * 10.0), false)
.field("CPU Load Average", &format!("{:.2}%", cpu_load.one * 10.0), false)
.field(
"Memory Usage",
format!(
&format!(
"{:.2} MB Free out of {:.2} MB",
mem_use.free as f32 / 1000.0,
mem_use.total as f32 / 1000.0
Expand All @@ -104,7 +104,7 @@ async fn set_status_to_current_time(ctx: Arc<Context>) {
let current_time = Utc::now();
let formatted_time = current_time.to_rfc2822();

ctx.set_activity(Activity::playing(&formatted_time)).await;
ctx.set_activity(Activity::playing(formatted_time)).await;
}

#[tokio::main]
Expand Down
2 changes: 1 addition & 1 deletion examples/e17_message_components/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl Sound {
let mut b = CreateButton::default();
b.custom_id(self.to_string().to_ascii_lowercase());
b.emoji(self.emoji());
b.label(self);
b.label(self.to_string());
b.style(ButtonStyle::Primary);
b
}
Expand Down
8 changes: 4 additions & 4 deletions src/builder/add_member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ impl AddMember {
/// Sets the OAuth2 access token for this request.
///
/// Requires the access token to have the `guilds.join` scope granted.
pub fn access_token(&mut self, access_token: impl ToString) -> &mut Self {
self.0.insert("access_token", Value::from(access_token.to_string()));
pub fn access_token(&mut self, access_token: impl Into<String>) -> &mut Self {
self.0.insert("access_token", Value::String(access_token.into()));
self
}

Expand All @@ -23,8 +23,8 @@ impl AddMember {
/// Requires the [Manage Nicknames] permission.
///
/// [Manage Nicknames]: crate::model::permissions::Permissions::MANAGE_NICKNAMES
pub fn nickname(&mut self, nickname: impl ToString) -> &mut Self {
self.0.insert("nick", Value::from(nickname.to_string()));
pub fn nickname(&mut self, nickname: impl Into<String>) -> &mut Self {
self.0.insert("nick", Value::String(nickname.into()));
self
}

Expand Down
2 changes: 1 addition & 1 deletion src/builder/create_allowed_mentions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};
use crate::json::prelude::*;
use crate::model::id::{RoleId, UserId};

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum ParseValue {
#[serde(rename = "everyone")]
Everyone,
Expand Down
36 changes: 20 additions & 16 deletions src/builder/create_application_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ impl CreateApplicationCommandOption {
/// Sets the name of the option.
///
/// **Note**: Must be between 1 and 32 lowercase characters, matching `r"^[\w-]{1,32}$"`.
pub fn name<D: ToString>(&mut self, name: D) -> &mut Self {
self.0.insert("name", Value::from(name.to_string()));
pub fn name(&mut self, name: impl Into<String>) -> &mut Self {
self.0.insert("name", Value::String(name.into()));
self
}

Expand All @@ -54,8 +54,8 @@ impl CreateApplicationCommandOption {
/// Sets the description for the option.
///
/// **Note**: Must be between 1 and 100 characters.
pub fn description<D: ToString>(&mut self, description: D) -> &mut Self {
self.0.insert("description", Value::from(description.to_string()));
pub fn description(&mut self, description: impl Into<String>) -> &mut Self {
self.0.insert("description", Value::String(description.into()));
self
}

Expand Down Expand Up @@ -101,9 +101,9 @@ impl CreateApplicationCommandOption {
/// Adds an optional int-choice.
///
/// **Note**: There can be no more than 25 choices set. Name must be between 1 and 100 characters. Value must be between -2^53 and 2^53.
pub fn add_int_choice<D: ToString>(&mut self, name: D, value: i32) -> &mut Self {
pub fn add_int_choice(&mut self, name: impl Into<String>, value: i32) -> &mut Self {
let choice = json!({
"name": name.to_string(),
"name": name.into(),
"value" : value
});
self.add_choice(choice)
Expand All @@ -130,10 +130,14 @@ impl CreateApplicationCommandOption {
/// Adds an optional string-choice.
///
/// **Note**: There can be no more than 25 choices set. Name must be between 1 and 100 characters. Value must be up to 100 characters.
pub fn add_string_choice<D: ToString, E: ToString>(&mut self, name: D, value: E) -> &mut Self {
pub fn add_string_choice(
&mut self,
name: impl Into<String>,
value: impl Into<String>,
) -> &mut Self {
let choice = json!({
"name": name.to_string(),
"value": value.to_string()
"name": name.into(),
"value": value.into()
});
self.add_choice(choice)
}
Expand All @@ -159,9 +163,9 @@ impl CreateApplicationCommandOption {
/// Adds an optional number-choice.
///
/// **Note**: There can be no more than 25 choices set. Name must be between 1 and 100 characters. Value must be between -2^53 and 2^53.
pub fn add_number_choice<D: ToString>(&mut self, name: D, value: f64) -> &mut Self {
pub fn add_number_choice(&mut self, name: impl Into<String>, value: f64) -> &mut Self {
let choice = json!({
"name": name.to_string(),
"name": name.into(),
"value" : value
});
self.add_choice(choice)
Expand Down Expand Up @@ -287,8 +291,8 @@ impl CreateApplicationCommand {
/// Specifies the name of the application command.
///
/// **Note**: Must be between 1 and 32 lowercase characters, matching `r"^[\w-]{1,32}$"`. Two global commands of the same app cannot have the same name. Two guild-specific commands of the same app cannot have the same name.
pub fn name<D: ToString>(&mut self, name: D) -> &mut Self {
self.0.insert("name", Value::from(name.to_string()));
pub fn name(&mut self, name: impl Into<String>) -> &mut Self {
self.0.insert("name", Value::String(name.into()));
self
}

Expand Down Expand Up @@ -320,7 +324,7 @@ impl CreateApplicationCommand {

/// Specifies the default permissions required to execute the command.
pub fn default_member_permissions(&mut self, permissions: Permissions) -> &mut Self {
self.0.insert("default_member_permissions", Value::from(permissions.bits().to_string()));
self.0.insert("default_member_permissions", Value::String(permissions.bits().to_string()));

self
}
Expand All @@ -335,8 +339,8 @@ impl CreateApplicationCommand {
/// Specifies the description of the application command.
///
/// **Note**: Must be between 1 and 100 characters long.
pub fn description<D: ToString>(&mut self, description: D) -> &mut Self {
self.0.insert("description", Value::from(description.to_string()));
pub fn description(&mut self, description: impl Into<String>) -> &mut Self {
self.0.insert("description", Value::String(description.into()));
self
}

Expand Down
8 changes: 4 additions & 4 deletions src/builder/create_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ impl CreateChannel {
/// Specify how to call this new channel.
///
/// **Note**: Must be between 2 and 100 characters long.
pub fn name<D: ToString>(&mut self, name: D) -> &mut Self {
self.0.insert("name", Value::from(name.to_string()));
pub fn name(&mut self, name: impl Into<String>) -> &mut Self {
self.0.insert("name", Value::String(name.into()));

self
}
Expand All @@ -38,8 +38,8 @@ impl CreateChannel {
/// Set an interesting topic.
///
/// **Note**: Must be between 0 and 1000 characters long.
pub fn topic<D: ToString>(&mut self, topic: D) -> &mut Self {
self.0.insert("topic", Value::from(topic.to_string()));
pub fn topic(&mut self, topic: impl Into<String>) -> &mut Self {
self.0.insert("topic", Value::String(topic.into()));

self
}
Expand Down
66 changes: 33 additions & 33 deletions src/builder/create_components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,20 +161,20 @@ impl CreateButton {
}

/// The label of the button.
pub fn label<D: ToString>(&mut self, label: D) -> &mut Self {
self.0.insert("label", Value::from(label.to_string()));
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.0.insert("label", Value::String(label.into()));
self
}

/// Sets the custom id of the button, a developer-defined identifier.
pub fn custom_id<D: ToString>(&mut self, id: D) -> &mut Self {
self.0.insert("custom_id", Value::from(id.to_string()));
pub fn custom_id(&mut self, id: impl Into<String>) -> &mut Self {
self.0.insert("custom_id", Value::String(id.into()));
self
}

/// The url for url style button.
pub fn url<D: ToString>(&mut self, url: D) -> &mut Self {
self.0.insert("url", Value::from(url.to_string()));
pub fn url(&mut self, url: impl Into<String>) -> &mut Self {
self.0.insert("url", Value::String(url.into()));
self
}

Expand All @@ -188,18 +188,18 @@ impl CreateButton {

match emoji {
ReactionType::Unicode(u) => {
map.insert("name".to_string(), Value::from(u));
map.insert("name".into(), Value::from(u));
},
ReactionType::Custom {
animated,
id,
name,
} => {
map.insert("animated".to_string(), Value::from(animated));
map.insert("id".to_string(), Value::from(id.to_string()));
map.insert("animated".into(), Value::from(animated));
map.insert("id".into(), Value::String(id.to_string()));

if let Some(name) = name {
map.insert("name".to_string(), Value::from(name));
map.insert("name".into(), Value::String(name));
}
},
};
Expand Down Expand Up @@ -230,14 +230,14 @@ pub struct CreateSelectMenu(pub HashMap<&'static str, Value>);

impl CreateSelectMenu {
/// The placeholder of the select menu.
pub fn placeholder<D: ToString>(&mut self, label: D) -> &mut Self {
self.0.insert("placeholder", Value::from(label.to_string()));
pub fn placeholder(&mut self, label: impl Into<String>) -> &mut Self {
self.0.insert("placeholder", Value::String(label.into()));
self
}

/// Sets the custom id of the select menu, a developer-defined identifier.
pub fn custom_id<D: ToString>(&mut self, id: D) -> &mut Self {
self.0.insert("custom_id", Value::from(id.to_string()));
pub fn custom_id(&mut self, id: impl Into<String>) -> &mut Self {
self.0.insert("custom_id", Value::String(id.into()));
self
}

Expand Down Expand Up @@ -329,27 +329,27 @@ pub struct CreateSelectMenuOption(pub HashMap<&'static str, Value>);

impl CreateSelectMenuOption {
/// Creates an option.
pub fn new<L: ToString, V: ToString>(label: L, value: V) -> Self {
pub fn new(label: impl Into<String>, value: impl Into<String>) -> Self {
let mut opt = Self::default();
opt.label(label).value(value);
opt
}

/// Sets the label of this option.
pub fn label<D: ToString>(&mut self, label: D) -> &mut Self {
self.0.insert("label", Value::from(label.to_string()));
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.0.insert("label", Value::String(label.into()));
self
}

/// Sets the value of this option.
pub fn value<D: ToString>(&mut self, value: D) -> &mut Self {
self.0.insert("value", Value::from(value.to_string()));
pub fn value(&mut self, value: impl Into<String>) -> &mut Self {
self.0.insert("value", Value::String(value.into()));
self
}

/// Sets the description shown on this option.
pub fn description<D: ToString>(&mut self, description: D) -> &mut Self {
self.0.insert("description", Value::from(description.to_string()));
pub fn description(&mut self, description: impl Into<String>) -> &mut Self {
self.0.insert("description", Value::String(description.into()));
self
}

Expand All @@ -363,18 +363,18 @@ impl CreateSelectMenuOption {

match emoji {
ReactionType::Unicode(u) => {
map.insert("name".to_string(), Value::from(u));
map.insert("name".into(), Value::String(u));
},
ReactionType::Custom {
animated,
id,
name,
} => {
map.insert("animated".to_string(), Value::from(animated));
map.insert("id".to_string(), Value::from(id.to_string()));
map.insert("animated".into(), Value::from(animated));
map.insert("id".into(), Value::String(id.to_string()));

if let Some(name) = name {
map.insert("name".to_string(), Value::from(name));
map.insert("name".into(), Value::from(name));
}
},
};
Expand All @@ -398,8 +398,8 @@ pub struct CreateInputText(pub HashMap<&'static str, Value>);

impl CreateInputText {
/// Sets the custom id of the input text, a developer-defined identifier.
pub fn custom_id<D: ToString>(&mut self, id: D) -> &mut Self {
self.0.insert("custom_id", Value::from(id.to_string()));
pub fn custom_id(&mut self, id: impl Into<String>) -> &mut Self {
self.0.insert("custom_id", Value::from(id.into()));
self
}

Expand All @@ -410,14 +410,14 @@ impl CreateInputText {
}

/// Sets the label of this input text.
pub fn label<D: ToString>(&mut self, label: D) -> &mut Self {
self.0.insert("label", Value::from(label.to_string()));
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.0.insert("label", Value::String(label.into()));
self
}

/// Sets the placeholder of this input text.
pub fn placeholder<D: ToString>(&mut self, label: D) -> &mut Self {
self.0.insert("placeholder", Value::from(label.to_string()));
pub fn placeholder(&mut self, label: impl Into<String>) -> &mut Self {
self.0.insert("placeholder", Value::String(label.into()));
self
}

Expand All @@ -434,8 +434,8 @@ impl CreateInputText {
}

/// Sets the value of this input text.
pub fn value<D: ToString>(&mut self, value: D) -> &mut Self {
self.0.insert("value", Value::from(value.to_string()));
pub fn value(&mut self, value: impl Into<String>) -> &mut Self {
self.0.insert("value", Value::String(value.into()));
self
}

Expand Down
Loading

0 comments on commit 228f3c6

Please sign in to comment.