diff options
| author | Zeyla Hellyer <[email protected]> | 2017-05-04 12:54:58 -0700 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2017-05-04 12:54:58 -0700 |
| commit | 4cdaf3a3187125971bdc9e5c5e52b36d70f563c2 (patch) | |
| tree | ba4ee4bf1d9d6f9c3a8122c5ee99657c26ddd99b | |
| parent | Add missing Member::kick shortcut (diff) | |
| download | serenity-4cdaf3a3187125971bdc9e5c5e52b36d70f563c2.tar.xz serenity-4cdaf3a3187125971bdc9e5c5e52b36d70f563c2.zip | |
Accept references on Into<Id>
By accepting references, users don't have to either pass in the entirity
of an instance or clone it.
| -rw-r--r-- | src/model/channel/channel_id.rs | 24 | ||||
| -rw-r--r-- | src/model/channel/message.rs | 7 | ||||
| -rw-r--r-- | src/model/guild/emoji.rs | 7 | ||||
| -rw-r--r-- | src/model/guild/guild_id.rs | 28 | ||||
| -rw-r--r-- | src/model/guild/role.rs | 7 | ||||
| -rw-r--r-- | src/model/user.rs | 21 |
6 files changed, 94 insertions, 0 deletions
diff --git a/src/model/channel/channel_id.rs b/src/model/channel/channel_id.rs index 79a6fa4..66bbbf3 100644 --- a/src/model/channel/channel_id.rs +++ b/src/model/channel/channel_id.rs @@ -508,6 +508,17 @@ impl From<Channel> for ChannelId { } } +impl<'a> From<&'a Channel> for ChannelId { + /// Gets the Id of a `Channel`. + fn from(channel: &Channel) -> ChannelId { + match *channel { + Channel::Group(ref group) => group.read().unwrap().channel_id, + Channel::Guild(ref ch) => ch.read().unwrap().id, + Channel::Private(ref ch) => ch.read().unwrap().id, + } + } +} + impl From<PrivateChannel> for ChannelId { /// Gets the Id of a private channel. fn from(private_channel: PrivateChannel) -> ChannelId { @@ -515,12 +526,25 @@ impl From<PrivateChannel> for ChannelId { } } +impl<'a> From<&'a PrivateChannel> for ChannelId { + /// Gets the Id of a private channel. + fn from(private_channel: &PrivateChannel) -> ChannelId { + private_channel.id + } +} + impl From<GuildChannel> for ChannelId { /// Gets the Id of a guild channel. fn from(public_channel: GuildChannel) -> ChannelId { public_channel.id } } +impl<'a> From<&'a GuildChannel> for ChannelId { + /// Gets the Id of a guild channel. + fn from(public_channel: &GuildChannel) -> ChannelId { + public_channel.id + } +} impl Display for ChannelId { fn fmt(&self, f: &mut Formatter) -> FmtResult { diff --git a/src/model/channel/message.rs b/src/model/channel/message.rs index 13178ba..520953a 100644 --- a/src/model/channel/message.rs +++ b/src/model/channel/message.rs @@ -506,6 +506,13 @@ impl From<Message> for MessageId { } } +impl<'a> From<&'a Message> for MessageId { + /// Gets the Id of a `Message`. + fn from(message: &Message) -> MessageId { + message.id + } +} + /// A representation of a reaction to a message. /// /// Multiple of the same [reaction type] are sent into one `MessageReaction`, diff --git a/src/model/guild/emoji.rs b/src/model/guild/emoji.rs index 54a70d3..4c2b6fc 100644 --- a/src/model/guild/emoji.rs +++ b/src/model/guild/emoji.rs @@ -127,3 +127,10 @@ impl From<Emoji> for EmojiId { emoji.id } } + +impl<'a> From<&'a Emoji> for EmojiId { + /// Gets the Id of an `Emoji`. + fn from(emoji: &Emoji) -> EmojiId { + emoji.id + } +} diff --git a/src/model/guild/guild_id.rs b/src/model/guild/guild_id.rs index f219659..6e5cb3a 100644 --- a/src/model/guild/guild_id.rs +++ b/src/model/guild/guild_id.rs @@ -607,6 +607,13 @@ impl From<PartialGuild> for GuildId { } } +impl<'a> From<&'a PartialGuild> for GuildId { + /// Gets the Id of a partial guild. + fn from(guild: &PartialGuild) -> GuildId { + guild.id + } +} + impl From<GuildInfo> for GuildId { /// Gets the Id of Guild information struct. fn from(guild_info: GuildInfo) -> GuildId { @@ -614,6 +621,13 @@ impl From<GuildInfo> for GuildId { } } +impl<'a> From<&'a GuildInfo> for GuildId { + /// Gets the Id of Guild information struct. + fn from(guild_info: &GuildInfo) -> GuildId { + guild_info.id + } +} + impl From<InviteGuild> for GuildId { /// Gets the Id of Invite Guild struct. fn from(invite_guild: InviteGuild) -> GuildId { @@ -621,9 +635,23 @@ impl From<InviteGuild> for GuildId { } } +impl<'a> From<&'a InviteGuild> for GuildId { + /// Gets the Id of Invite Guild struct. + fn from(invite_guild: &InviteGuild) -> GuildId { + invite_guild.id + } +} + impl From<Guild> for GuildId { /// Gets the Id of Guild. fn from(live_guild: Guild) -> GuildId { live_guild.id } } + +impl<'a> From<&'a Guild> for GuildId { + /// Gets the Id of Guild. + fn from(live_guild: &Guild) -> GuildId { + live_guild.id + } +} diff --git a/src/model/guild/role.rs b/src/model/guild/role.rs index d4e1da8..58f91de 100644 --- a/src/model/guild/role.rs +++ b/src/model/guild/role.rs @@ -199,3 +199,10 @@ impl From<Role> for RoleId { role.id } } + +impl<'a> From<&'a Role> for RoleId { + /// Gets the Id of a role. + fn from(role: &Role) -> RoleId { + role.id + } +} diff --git a/src/model/user.rs b/src/model/user.rs index 040effa..8e38c44 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -604,6 +604,13 @@ impl From<CurrentUser> for UserId { } } +impl<'a> From<&'a CurrentUser> for UserId { + /// Gets the Id of a `CurrentUser` struct. + fn from(current_user: &CurrentUser) -> UserId { + current_user.id + } +} + impl From<Member> for UserId { /// Gets the Id of a `Member`. fn from(member: Member) -> UserId { @@ -611,6 +618,13 @@ impl From<Member> for UserId { } } +impl<'a> From<&'a Member> for UserId { + /// Gets the Id of a `Member`. + fn from(member: &Member) -> UserId { + member.user.read().unwrap().id + } +} + impl From<User> for UserId { /// Gets the Id of a `User`. fn from(user: User) -> UserId { @@ -618,6 +632,13 @@ impl From<User> for UserId { } } +impl<'a> From<&'a User> for UserId { + /// Gets the Id of a `User`. + fn from(user: &User) -> UserId { + user.id + } +} + impl fmt::Display for UserId { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Display::fmt(&self.0, f) |