aboutsummaryrefslogtreecommitdiff
path: root/src/ext/cache
diff options
context:
space:
mode:
authorAustin Hellyer <[email protected]>2016-11-26 11:33:28 -0800
committerAustin Hellyer <[email protected]>2016-11-26 11:33:28 -0800
commit23f1dc823d658ee66a51388b742b0d06c94560c9 (patch)
tree6b4ccf467d31d45f1e8d6834fdda5bbb18c865f8 /src/ext/cache
parentRemove the 'extras' feature flag (diff)
downloadserenity-23f1dc823d658ee66a51388b742b0d06c94560c9.tar.xz
serenity-23f1dc823d658ee66a51388b742b0d06c94560c9.zip
Make Cache::get_channel return a reference
Additionally, allow it to search the groups' and private channels' maps in the cache. As these are usually O(1) operations, it's cheap to allow, in comparison to the current unoptimized method of getting a guild's channel.
Diffstat (limited to 'src/ext/cache')
-rw-r--r--src/ext/cache/mod.rs27
1 files changed, 23 insertions, 4 deletions
diff --git a/src/ext/cache/mod.rs b/src/ext/cache/mod.rs
index 0552a14..a8d62c9 100644
--- a/src/ext/cache/mod.rs
+++ b/src/ext/cache/mod.rs
@@ -114,13 +114,21 @@ impl Cache {
self.calls.get(&group_id.into())
}
- pub fn get_channel<C: Into<ChannelId>>(&self, id: C) -> Option<Channel> {
+ pub fn get_channel<C: Into<ChannelId>>(&self, id: C) -> Option<ChannelRef> {
let id = id.into();
+ if let Some(private_channel) = self.private_channels.get(&id) {
+ return Some(ChannelRef::Private(private_channel));
+ }
+
+ if let Some(group) = self.groups.get(&id) {
+ return Some(ChannelRef::Group(group));
+ }
+
for guild in self.guilds.values() {
for channel in guild.channels.values() {
if channel.id == id {
- return Some(Channel::Guild(channel.clone()));
+ return Some(ChannelRef::Guild(channel));
}
}
}
@@ -849,16 +857,27 @@ fn update_presence(presences: &mut HashMap<UserId, Presence>,
}
/// A reference to a private channel, guild's channel, or group.
-#[derive(Debug, Clone, Copy)]
+#[derive(Clone, Copy, Debug)]
pub enum ChannelRef<'a> {
/// A group's channel
Group(&'a Group),
/// A guild channel and its guild
- Guild(&'a Guild, &'a GuildChannel),
+ Guild(&'a GuildChannel),
/// A private channel
Private(&'a PrivateChannel),
}
+impl<'a> ChannelRef<'a> {
+ /// Clones the inner value of the variant.
+ pub fn clone_inner(&self) -> Channel {
+ match *self {
+ ChannelRef::Group(group) => Channel::Group(group.clone()),
+ ChannelRef::Guild(channel) => Channel::Guild(channel.clone()),
+ ChannelRef::Private(private) => Channel::Private(private.clone()),
+ }
+ }
+}
+
fn opt_modify<T: Clone>(dest: &mut T, src: &Option<T>) {
if let Some(val) = src.as_ref() {
dest.clone_from(val);