diff options
| author | Zeyla Hellyer <[email protected]> | 2018-07-04 21:28:22 -0700 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2018-07-04 21:32:17 -0700 |
| commit | 7b9764cf1097b0620d871fabe67b5593f0cd4a4a (patch) | |
| tree | 5b9f3eac6e9c57ac255c73bd1eea07669838f32d /src/model | |
| parent | Fix dead doc-links and add missing ones. (#347) (diff) | |
| download | serenity-7b9764cf1097b0620d871fabe67b5593f0cd4a4a.tar.xz serenity-7b9764cf1097b0620d871fabe67b5593f0cd4a4a.zip | |
Monomorphize all functions
This commit monomorphizes all functions, turning functions like:
```rust
fn foo<T: Into<Bar>>(baz: T) {
baz = baz.into();
// function here
}
```
Into functions like:
```rust
fn foo<T: Into<Bar>>(baz: T) {
_foo(baz.into())
}
fn _foo(baz: Bar) {
// function here
}
```
This avoids binary bloat and improves build times, by reducing the amount of
code duplication.
Diffstat (limited to 'src/model')
| -rw-r--r-- | src/model/channel/channel_id.rs | 86 | ||||
| -rw-r--r-- | src/model/channel/embed.rs | 8 | ||||
| -rw-r--r-- | src/model/channel/group.rs | 10 | ||||
| -rw-r--r-- | src/model/channel/guild_channel.rs | 5 | ||||
| -rw-r--r-- | src/model/channel/message.rs | 17 | ||||
| -rw-r--r-- | src/model/channel/reaction.rs | 14 | ||||
| -rw-r--r-- | src/model/guild/guild_id.rs | 129 | ||||
| -rw-r--r-- | src/model/guild/member.rs | 21 | ||||
| -rw-r--r-- | src/model/guild/mod.rs | 32 | ||||
| -rw-r--r-- | src/model/invite.rs | 5 | ||||
| -rw-r--r-- | src/model/user.rs | 8 |
11 files changed, 279 insertions, 56 deletions
diff --git a/src/model/channel/channel_id.rs b/src/model/channel/channel_id.rs index f939be8..e3034f7 100644 --- a/src/model/channel/channel_id.rs +++ b/src/model/channel/channel_id.rs @@ -88,7 +88,15 @@ impl ChannelId { #[inline] pub fn create_reaction<M, R>(&self, message_id: M, reaction_type: R) -> Result<()> where M: Into<MessageId>, R: Into<ReactionType> { - http::create_reaction(self.0, message_id.into().0, &reaction_type.into()) + self._create_reaction(message_id.into(), &reaction_type.into()) + } + + fn _create_reaction( + &self, + message_id: MessageId, + reaction_type: &ReactionType, + ) -> Result<()> { + http::create_reaction(self.0, message_id.0, reaction_type) } /// Deletes this channel, returning the channel on a successful deletion. @@ -107,7 +115,11 @@ impl ChannelId { /// [Manage Messages]: permissions/constant.MANAGE_MESSAGES.html #[inline] pub fn delete_message<M: Into<MessageId>>(&self, message_id: M) -> Result<()> { - http::delete_message(self.0, message_id.into().0) + self._delete_message(message_id.into()) + } + + fn _delete_message(&self, message_id: MessageId) -> Result<()> { + http::delete_message(self.0, message_id.0) } /// Deletes all messages by Ids from the given vector in the given channel. @@ -133,6 +145,11 @@ impl ChannelId { .into_iter() .map(|message_id| message_id.as_ref().0) .collect::<Vec<u64>>(); + + self._delete_messages(ids) + } + + fn _delete_messages(&self, ids: Vec<u64>) -> Result<()> { let len = ids.len(); if len == 0 || len > 100 { @@ -168,17 +185,31 @@ impl ChannelId { /// /// [`Reaction`]: struct.Reaction.html /// [Manage Messages]: permissions/constant.MANAGE_MESSAGES.html + #[inline] pub fn delete_reaction<M, R>(&self, message_id: M, user_id: Option<UserId>, reaction_type: R) -> Result<()> where M: Into<MessageId>, R: Into<ReactionType> { + self._delete_reaction( + message_id.into(), + user_id.into(), + &reaction_type.into(), + ) + } + + fn _delete_reaction( + &self, + message_id: MessageId, + user_id: Option<UserId>, + reaction_type: &ReactionType, + ) -> Result<()> { http::delete_reaction( self.0, - message_id.into().0, + message_id.0, user_id.map(|uid| uid.0), - &reaction_type.into(), + reaction_type, ) } @@ -229,8 +260,14 @@ impl ChannelId { /// [`Message`]: struct.Message.html /// [`the limit`]: ../builder/struct.EditMessage.html#method.content #[cfg(feature = "utils")] + #[inline] pub fn edit_message<F, M>(&self, message_id: M, f: F) -> Result<Message> where F: FnOnce(EditMessage) -> EditMessage, M: Into<MessageId> { + self._edit_message(message_id.into(), f) + } + + fn _edit_message<F>(&self, message_id: MessageId, f: F) -> Result<Message> + where F: FnOnce(EditMessage) -> EditMessage { let msg = f(EditMessage::default()); if let Some(content) = msg.0.get(&"content") { @@ -243,7 +280,7 @@ impl ChannelId { let map = utils::vecmap_to_json_map(msg.0); - http::edit_message(self.0, message_id.into().0, &Value::Object(map)) + http::edit_message(self.0, message_id.0, &Value::Object(map)) } /// Search the cache for the channel with the Id. @@ -277,7 +314,11 @@ impl ChannelId { /// [Read Message History]: permissions/constant.READ_MESSAGE_HISTORY.html #[inline] pub fn message<M: Into<MessageId>>(&self, message_id: M) -> Result<Message> { - http::get_message(self.0, message_id.into().0) + self._message(message_id.into()) + } + + fn _message(&self, message_id: MessageId) -> Result<Message> { + http::get_message(self.0, message_id.0) .map(|mut msg| { msg.transform_content(); @@ -350,7 +391,11 @@ impl ChannelId { /// [`Message`]: struct.Message.html #[inline] pub fn pin<M: Into<MessageId>>(&self, message_id: M) -> Result<()> { - http::pin_message(self.0, message_id.into().0) + self._pin(message_id.into()) + } + + fn _pin(&self, message_id: MessageId) -> Result<()> { + http::pin_message(self.0, message_id.0) } /// Gets the list of [`Message`]s which are pinned to the channel. @@ -379,14 +424,29 @@ impl ChannelId { ) -> Result<Vec<User>> where M: Into<MessageId>, R: Into<ReactionType>, U: Into<Option<UserId>> { + self._reaction_users( + message_id.into(), + &reaction_type.into(), + limit, + after.into(), + ) + } + + fn _reaction_users( + &self, + message_id: MessageId, + reaction_type: &ReactionType, + limit: Option<u8>, + after: Option<UserId>, + ) -> Result<Vec<User>> { let limit = limit.map_or(50, |x| if x > 100 { 100 } else { x }); http::get_reaction_users( self.0, - message_id.into().0, - &reaction_type.into(), + message_id.0, + reaction_type, limit, - after.into().map(|x| x.0), + after.map(|x| x.0), ) } @@ -529,7 +589,11 @@ impl ChannelId { /// [Manage Messages]: permissions/constant.MANAGE_MESSAGES.html #[inline] pub fn unpin<M: Into<MessageId>>(&self, message_id: M) -> Result<()> { - http::unpin_message(self.0, message_id.into().0) + self._unpin(message_id.into()) + } + + fn _unpin(&self, message_id: MessageId) -> Result<()> { + http::unpin_message(self.0, message_id.0) } /// Retrieves the channel's webhooks. diff --git a/src/model/channel/embed.rs b/src/model/channel/embed.rs index 8837a8d..bc2d0da 100644 --- a/src/model/channel/embed.rs +++ b/src/model/channel/embed.rs @@ -134,9 +134,13 @@ impl EmbedField { /// [`value`]: #structfield.value pub fn new<T, U>(name: T, value: U, inline: bool) -> Self where T: Into<String>, U: Into<String> { + Self::_new(name.into(), value.into(), inline) + } + + fn _new(name: String, value: String, inline: bool) -> Self { Self { - name: name.into(), - value: value.into(), + name: name, + value: value, inline, } } diff --git a/src/model/channel/group.rs b/src/model/channel/group.rs index 2fc062b..4baa654 100644 --- a/src/model/channel/group.rs +++ b/src/model/channel/group.rs @@ -53,9 +53,12 @@ impl Group { /// user. /// /// [`http::add_group_recipient`]: ../http/fn.add_group_recipient.html + #[inline] pub fn add_recipient<U: Into<UserId>>(&self, user: U) -> Result<()> { - let user = user.into(); + self._add_recipient(user.into()) + } + fn _add_recipient(&self, user: UserId) -> Result<()> { // If the group already contains the recipient, do nothing. if self.recipients.contains_key(&user) { return Ok(()); @@ -261,9 +264,12 @@ impl Group { /// the group, then nothing is done. /// /// **Note**: This is only available to the group owner. + #[inline] pub fn remove_recipient<U: Into<UserId>>(&self, user: U) -> Result<()> { - let user = user.into(); + self._remove_recipient(user.into()) + } + fn _remove_recipient(&self, user: UserId) -> Result<()> { // If the group does not contain the recipient already, do nothing. if !self.recipients.contains_key(&user) { return Ok(()); diff --git a/src/model/channel/guild_channel.rs b/src/model/channel/guild_channel.rs index 533aaf2..2f54fa1 100644 --- a/src/model/channel/guild_channel.rs +++ b/src/model/channel/guild_channel.rs @@ -520,7 +520,12 @@ impl GuildChannel { /// [Attach Files]: permissions/constant.ATTACH_FILES.html /// [Send Messages]: permissions/constant.SEND_MESSAGES.html #[cfg(feature = "cache")] + #[inline] pub fn permissions_for<U: Into<UserId>>(&self, user_id: U) -> Result<Permissions> { + self._permissions_for(user_id.into()) + } + + fn _permissions_for(&self, user_id: UserId) -> Result<Permissions> { self.guild() .ok_or_else(|| Error::Model(ModelError::GuildNotFound)) .map(|g| g.read().permissions_in(self.id, user_id)) diff --git a/src/model/channel/message.rs b/src/model/channel/message.rs index 65cecd6..be17772 100644 --- a/src/model/channel/message.rs +++ b/src/model/channel/message.rs @@ -32,7 +32,7 @@ pub struct Message { pub channel_id: ChannelId, /// The Id of the [`Guild`] that the message was sent in. This value will /// only be present if this message was received over the gateway. - /// + /// /// [`Guild`]: ../guild/struct.Guild.html pub guild_id: Option<GuildId>, /// The content of the message. @@ -434,7 +434,12 @@ impl Message { /// [`Emoji`]: struct.Emoji.html /// [Add Reactions]: permissions/constant.ADD_REACTIONS.html /// [permissions]: permissions + #[inline] pub fn react<R: Into<ReactionType>>(&self, reaction_type: R) -> Result<()> { + self._react(&reaction_type.into()) + } + + fn _react(&self, reaction_type: &ReactionType) -> Result<()> { #[cfg(feature = "cache")] { let req = Permissions::ADD_REACTIONS; @@ -444,7 +449,7 @@ impl Message { } } - http::create_reaction(self.channel_id.0, self.id.0, &reaction_type.into()) + http::create_reaction(self.channel_id.0, self.id.0, reaction_type) } /// Replies to the user, mentioning them prior to the content in the form @@ -498,9 +503,13 @@ impl Message { /// Checks whether the message mentions passed [`UserId`]. /// /// [`UserId`]: ../../model/id/struct.UserId.html + #[inline] pub fn mentions_user_id<I: Into<UserId>>(&self, id: I) -> bool { - let user_id_to_find = id.into(); - self.mentions.iter().any(|mentioned_user| mentioned_user.id.0 == user_id_to_find.0) + self._mentions_user_id(id.into()) + } + + fn _mentions_user_id(&self, id: UserId) -> bool { + self.mentions.iter().any(|mentioned_user| mentioned_user.id.0 == id.0) } /// Checks whether the message mentions passed [`User`]. diff --git a/src/model/channel/reaction.rs b/src/model/channel/reaction.rs index 88f687f..0f65e8b 100644 --- a/src/model/channel/reaction.rs +++ b/src/model/channel/reaction.rs @@ -151,18 +151,28 @@ impl Reaction { /// [`User`]: struct.User.html /// [Read Message History]: permissions/constant.READ_MESSAGE_HISTORY.html /// [permissions]: permissions + #[inline] pub fn users<R, U>(&self, reaction_type: R, limit: Option<u8>, after: Option<U>) -> Result<Vec<User>> where R: Into<ReactionType>, U: Into<UserId> { + self._users(&reaction_type.into(), limit, after.map(Into::into)) + } + + fn _users( + &self, + reaction_type: &ReactionType, + limit: Option<u8>, + after: Option<UserId>, + ) -> Result<Vec<User>> { http::get_reaction_users( self.channel_id.0, self.message_id.0, - &reaction_type.into(), + reaction_type, limit.unwrap_or(50), - after.map(|u| u.into().0), + after.map(|u| u.0), ) } } diff --git a/src/model/guild/guild_id.rs b/src/model/guild/guild_id.rs index c07a40f..a209369 100644 --- a/src/model/guild/guild_id.rs +++ b/src/model/guild/guild_id.rs @@ -48,20 +48,24 @@ impl GuildId { /// [`Guild::ban`]: struct.Guild.html#method.ban /// [`User`]: struct.User.html /// [Ban Members]: permissions/constant.BAN_MEMBERS.html + #[inline] pub fn ban<U, BO>(&self, user: U, ban_options: &BO) -> Result<()> where U: Into<UserId>, BO: BanOptions { - let dmd = ban_options.dmd(); + self._ban(user.into(), (ban_options.dmd(), ban_options.reason())) + } + + fn _ban(&self, user: UserId, ban_options: (u8, &str)) -> Result<()> { + let (dmd, reason) = ban_options; + if dmd > 7 { return Err(Error::Model(ModelError::DeleteMessageDaysAmount(dmd))); } - let reason = ban_options.reason(); - if reason.len() > 512 { return Err(Error::ExceededLimit(reason.to_string(), 512)); } - http::ban_user(self.0, user.into().0, dmd, &*reason) + http::ban_user(self.0, user.0, dmd, reason) } /// Gets a list of the guild's bans. @@ -113,12 +117,22 @@ impl GuildId { /// [`GuildChannel`]: struct.GuildChannel.html /// [`http::create_channel`]: ../http/fn.create_channel.html /// [Manage Channels]: permissions/constant.MANAGE_CHANNELS.html + #[inline] pub fn create_channel<C>(&self, name: &str, kind: ChannelType, category: C) -> Result<GuildChannel> where C: Into<Option<ChannelId>> { + self._create_channel(name, kind, category.into()) + } + + fn _create_channel( + &self, + name: &str, + kind: ChannelType, + category: Option<ChannelId>, + ) -> Result<GuildChannel> { let map = json!({ "name": name, "type": kind as u8, - "parent_id": category.into().map(|c| c.0) + "parent_id": category.map(|c| c.0) }); http::create_channel(self.0, &map) @@ -141,6 +155,7 @@ impl GuildId { /// [`Guild::create_emoji`]: struct.Guild.html#method.create_emoji /// [`utils::read_image`]: ../utils/fn.read_image.html /// [Manage Emojis]: permissions/constant.MANAGE_EMOJIS.html + #[inline] pub fn create_emoji(&self, name: &str, image: &str) -> Result<Emoji> { let map = json!({ "name": name, @@ -155,9 +170,17 @@ impl GuildId { /// Requires the [Manage Guild] permission. /// /// [Manage Guild]: permissions/constant.MANAGE_GUILD.html + #[inline] pub fn create_integration<I>(&self, integration_id: I, kind: &str) -> Result<()> where I: Into<IntegrationId> { - let integration_id = integration_id.into(); + self._create_integration(integration_id.into(), kind) + } + + fn _create_integration( + &self, + integration_id: IntegrationId, + kind: &str, + ) -> Result<()> { let map = json!({ "id": integration_id.0, "type": kind, @@ -206,7 +229,11 @@ impl GuildId { /// [Manage Emojis]: permissions/constant.MANAGE_EMOJIS.html #[inline] pub fn delete_emoji<E: Into<EmojiId>>(&self, emoji_id: E) -> Result<()> { - http::delete_emoji(self.0, emoji_id.into().0) + self._delete_emoji(emoji_id.into()) + } + + fn _delete_emoji(&self, emoji_id: EmojiId) -> Result<()> { + http::delete_emoji(self.0, emoji_id.0) } /// Deletes an integration by Id from the guild. @@ -216,7 +243,11 @@ impl GuildId { /// [Manage Guild]: permissions/constant.MANAGE_GUILD.html #[inline] pub fn delete_integration<I: Into<IntegrationId>>(&self, integration_id: I) -> Result<()> { - http::delete_guild_integration(self.0, integration_id.into().0) + self._delete_integration(integration_id.into()) + } + + fn _delete_integration(&self, integration_id: IntegrationId) -> Result<()> { + http::delete_guild_integration(self.0, integration_id.0) } /// Deletes a [`Role`] by Id from the guild. @@ -231,7 +262,11 @@ impl GuildId { /// [Manage Roles]: permissions/constant.MANAGE_ROLES.html #[inline] pub fn delete_role<R: Into<RoleId>>(&self, role_id: R) -> Result<()> { - http::delete_role(self.0, role_id.into().0) + self._delete_role(role_id.into()) + } + + fn _delete_role(&self, role_id: RoleId) -> Result<()> { + http::delete_role(self.0, role_id.0) } /// Edits the current guild with new data where specified. @@ -260,12 +295,17 @@ impl GuildId { /// [`Emoji`]: struct.Emoji.html /// [`Emoji::edit`]: struct.Emoji.html#method.edit /// [Manage Emojis]: permissions/constant.MANAGE_EMOJIS.html + #[inline] pub fn edit_emoji<E: Into<EmojiId>>(&self, emoji_id: E, name: &str) -> Result<Emoji> { + self._edit_emoji(emoji_id.into(), name) + } + + fn _edit_emoji(&self, emoji_id: EmojiId, name: &str) -> Result<Emoji> { let map = json!({ "name": name, }); - http::edit_emoji(self.0, emoji_id.into().0, &map) + http::edit_emoji(self.0, emoji_id.0, &map) } /// Edits the properties of member of the guild, such as muting or @@ -284,9 +324,14 @@ impl GuildId { #[inline] pub fn edit_member<F, U>(&self, user_id: U, f: F) -> Result<()> where F: FnOnce(EditMember) -> EditMember, U: Into<UserId> { + self._edit_member(user_id.into(), f) + } + + fn _edit_member<F>(&self, user_id: UserId, f: F) -> Result<()> + where F: FnOnce(EditMember) -> EditMember { let map = utils::vecmap_to_json_map(f(EditMember::default()).0); - http::edit_member(self.0, user_id.into().0, &map) + http::edit_member(self.0, user_id.0, &map) } /// Edits the current user's nickname for the guild. @@ -320,9 +365,14 @@ impl GuildId { #[inline] pub fn edit_role<F, R>(&self, role_id: R, f: F) -> Result<Role> where F: FnOnce(EditRole) -> EditRole, R: Into<RoleId> { + self._edit_role(role_id.into(), f) + } + + fn _edit_role<F>(&self, role_id: RoleId, f: F) -> Result<Role> + where F: FnOnce(EditRole) -> EditRole { let map = utils::vecmap_to_json_map(f(EditRole::default()).0); - http::edit_role(self.0, role_id.into().0, &map) + http::edit_role(self.0, role_id.0, &map) } /// Edits the order of [`Role`]s @@ -342,7 +392,15 @@ impl GuildId { #[inline] pub fn edit_role_position<R>(&self, role_id: R, position: u64) -> Result<Vec<Role>> where R: Into<RoleId> { - http::edit_role_position(self.0, role_id.into().0, position) + self._edit_role_position(role_id.into(), position) + } + + fn _edit_role_position( + &self, + role_id: RoleId, + position: u64, + ) -> Result<Vec<Role>> { + http::edit_role_position(self.0, role_id.0, position) } @@ -392,7 +450,11 @@ impl GuildId { /// [`Member`]: struct.Member.html #[inline] pub fn member<U: Into<UserId>>(&self, user_id: U) -> Result<Member> { - http::get_member(self.0, user_id.into().0) + self._member(user_id.into()) + } + + fn _member(&self, user_id: UserId) -> Result<Member> { + http::get_member(self.0, user_id.0) } /// Gets a list of the guild's members. @@ -405,7 +467,11 @@ impl GuildId { #[inline] pub fn members<U>(&self, limit: Option<u64>, after: Option<U>) -> Result<Vec<Member>> where U: Into<UserId> { - http::get_guild_members(self.0, limit, after.map(|x| x.into().0)) + self._members(limit, after.map(Into::into)) + } + + fn _members(&self, limit: Option<u64>, after: Option<UserId>) -> Result<Vec<Member>> { + http::get_guild_members(self.0, limit, after.map(|x| x.0)) } /// Moves a member to a specific voice channel. @@ -413,15 +479,24 @@ impl GuildId { /// Requires the [Move Members] permission. /// /// [Move Members]: permissions/constant.MOVE_MEMBERS.html + #[inline] pub fn move_member<C, U>(&self, user_id: U, channel_id: C) -> Result<()> where C: Into<ChannelId>, U: Into<UserId> { + self._move_member(user_id.into(), channel_id.into()) + } + + fn _move_member( + &self, + user_id: UserId, + channel_id: ChannelId, + ) -> Result<()> { let mut map = Map::new(); map.insert( "channel_id".to_string(), - Value::Number(Number::from(channel_id.into().0)), + Value::Number(Number::from(channel_id.0)), ); - http::edit_member(self.0, user_id.into().0, &map) + http::edit_member(self.0, user_id.0, &map) } /// Gets the number of [`Member`]s that would be pruned with the given @@ -447,8 +522,13 @@ impl GuildId { /// Although not required, you should specify all channels' positions, /// regardless of whether they were updated. Otherwise, positioning can /// sometimes get weird. + #[inline] pub fn reorder_channels<It>(&self, channels: It) -> Result<()> where It: IntoIterator<Item = (ChannelId, u64)> { + self._reorder_channels(channels.into_iter().collect()) + } + + fn _reorder_channels(&self, channels: Vec<(ChannelId, u64)>) -> Result<()> { let items = channels.into_iter().map(|(id, pos)| json!({ "id": id, "position": pos, @@ -503,7 +583,14 @@ impl GuildId { /// [Manage Guild]: permissions/constant.MANAGE_GUILD.html #[inline] pub fn start_integration_sync<I: Into<IntegrationId>>(&self, integration_id: I) -> Result<()> { - http::start_integration_sync(self.0, integration_id.into().0) + self._start_integration_sync(integration_id.into()) + } + + fn _start_integration_sync( + &self, + integration_id: IntegrationId, + ) -> Result<()> { + http::start_integration_sync(self.0, integration_id.0) } /// Starts a prune of [`Member`]s. @@ -532,7 +619,11 @@ impl GuildId { /// [Ban Members]: permissions/constant.BAN_MEMBERS.html #[inline] pub fn unban<U: Into<UserId>>(&self, user_id: U) -> Result<()> { - http::remove_ban(self.0, user_id.into().0) + self._unban(user_id.into()) + } + + fn _unban(&self, user_id: UserId) -> Result<()> { + http::remove_ban(self.0, user_id.0) } /// Retrieve's the guild's vanity URL. diff --git a/src/model/guild/member.rs b/src/model/guild/member.rs index c95c07f..8101700 100644 --- a/src/model/guild/member.rs +++ b/src/model/guild/member.rs @@ -85,9 +85,13 @@ impl Member { /// [`Role`]: struct.Role.html /// [Manage Roles]: permissions/constant.MANAGE_ROLES.html #[cfg(feature = "cache")] + #[inline] pub fn add_role<R: Into<RoleId>>(&mut self, role_id: R) -> Result<()> { - let role_id = role_id.into(); + self._add_role(role_id.into()) + } + #[cfg(feature = "cache")] + fn _add_role(&mut self, role_id: RoleId) -> Result<()> { if self.roles.contains(&role_id) { return Ok(()); } @@ -140,14 +144,17 @@ impl Member { /// /// [Ban Members]: permissions/constant.BAN_MEMBERS.html #[cfg(feature = "cache")] + #[inline] pub fn ban<BO: BanOptions>(&self, ban_options: &BO) -> Result<()> { - let dmd = ban_options.dmd(); + self._ban(ban_options.dmd(), ban_options.reason()) + } + + #[cfg(feature = "cache")] + fn _ban(&self, dmd: u8, reason: &str) -> Result<()> { if dmd > 7 { return Err(Error::Model(ModelError::DeleteMessageDaysAmount(dmd))); } - let reason = ban_options.reason(); - if reason.len() > 512 { return Err(Error::ExceededLimit(reason.to_string(), 512)); } @@ -368,9 +375,13 @@ impl Member { /// [`Role`]: struct.Role.html /// [Manage Roles]: permissions/constant.MANAGE_ROLES.html #[cfg(feature = "cache")] + #[inline] pub fn remove_role<R: Into<RoleId>>(&mut self, role_id: R) -> Result<()> { - let role_id = role_id.into(); + self._remove_role(role_id.into()) + } + #[cfg(feature = "cache")] + fn _remove_role(&mut self, role_id: RoleId) -> Result<()> { if !self.roles.contains(&role_id) { return Ok(()); } diff --git a/src/model/guild/mod.rs b/src/model/guild/mod.rs index 9e4d8ef..4fc7079 100644 --- a/src/model/guild/mod.rs +++ b/src/model/guild/mod.rs @@ -229,9 +229,12 @@ impl Guild { /// [`Guild::ban`]: struct.Guild.html#method.ban /// [`User`]: struct.User.html /// [Ban Members]: permissions/constant.BAN_MEMBERS.html + #[inline] pub fn ban<U: Into<UserId>, BO: BanOptions>(&self, user: U, options: &BO) -> Result<()> { - let user = user.into(); + self._ban(user.into(), options) + } + fn _ban<BO: BanOptions>(&self, user: UserId, options: &BO) -> Result<()> { #[cfg(feature = "cache")] { let req = Permissions::BAN_MEMBERS; @@ -669,11 +672,18 @@ impl Guild { /// If both user IDs are the same, `None` is returned. If one of the users /// is the guild owner, their ID is returned. #[cfg(feature = "cache")] + #[inline] pub fn greater_member_hierarchy<T, U>(&self, lhs_id: T, rhs_id: U) -> Option<UserId> where T: Into<UserId>, U: Into<UserId> { - let lhs_id = lhs_id.into(); - let rhs_id = rhs_id.into(); + self._greater_member_hierarchy(lhs_id.into(), rhs_id.into()) + } + #[cfg(feature = "cache")] + fn _greater_member_hierarchy( + &self, + lhs_id: UserId, + rhs_id: UserId, + ) -> Option<UserId> { // Check that the IDs are the same. If they are, neither is greater. if lhs_id == rhs_id { return None; @@ -1106,10 +1116,13 @@ impl Guild { /// Calculate a [`Member`]'s permissions in the guild. /// /// [`Member`]: struct.Member.html + #[inline] pub fn member_permissions<U>(&self, user_id: U) -> Permissions where U: Into<UserId> { - let user_id = user_id.into(); + self._member_permissions(user_id.into()) + } + fn _member_permissions(&self, user_id: UserId) -> Permissions { if user_id == self.owner_id { return Permissions::all(); } @@ -1179,17 +1192,22 @@ impl Guild { /// Calculate a [`User`]'s permissions in a given channel in the guild. /// /// [`User`]: struct.User.html + #[inline] pub fn permissions_in<C, U>(&self, channel_id: C, user_id: U) -> Permissions where C: Into<ChannelId>, U: Into<UserId> { - let user_id = user_id.into(); + self._permissions_in(channel_id.into(), user_id.into()) + } + fn _permissions_in( + &self, + channel_id: ChannelId, + user_id: UserId, + ) -> Permissions { // The owner has all permissions in all cases. if user_id == self.owner_id { return Permissions::all(); } - let channel_id = channel_id.into(); - // Start by retrieving the @everyone role's permissions. let everyone = match self.roles.get(&RoleId(self.id.0)) { Some(everyone) => everyone, diff --git a/src/model/invite.rs b/src/model/invite.rs index b93b231..2bbd5d9 100644 --- a/src/model/invite.rs +++ b/src/model/invite.rs @@ -70,8 +70,11 @@ impl Invite { /// [permission]: permissions/index.html pub fn create<C, F>(channel_id: C, f: F) -> Result<RichInvite> where C: Into<ChannelId>, F: FnOnce(CreateInvite) -> CreateInvite { - let channel_id = channel_id.into(); + Self::_create(channel_id.into(), f) + } + fn _create<F>(channel_id: ChannelId, f: F) -> Result<RichInvite> + where F: FnOnce(CreateInvite) -> CreateInvite { #[cfg(feature = "cache")] { let req = Permissions::CREATE_INVITE; diff --git a/src/model/user.rs b/src/model/user.rs index 330f319..48891d3 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -580,10 +580,12 @@ impl User { // no-cache would warn on guild_id. pub fn has_role<G, R>(&self, guild: G, role: R) -> bool where G: Into<GuildContainer>, R: Into<RoleId> { - let role_id = role.into(); + self._has_role(guild.into(), role.into()) + } + fn _has_role(&self, guild: GuildContainer, role: RoleId) -> bool { match guild.into() { - GuildContainer::Guild(guild) => guild.roles.contains_key(&role_id), + GuildContainer::Guild(guild) => guild.roles.contains_key(&role), GuildContainer::Id(_guild_id) => { feature_cache! {{ CACHE.read() @@ -591,7 +593,7 @@ impl User { .get(&_guild_id) .map(|g| { g.read().members.get(&self.id) - .map(|m| m.roles.contains(&role_id)) + .map(|m| m.roles.contains(&role)) .unwrap_or(false) }) .unwrap_or(false) |