aboutsummaryrefslogtreecommitdiff
path: root/src/cache/mod.rs
diff options
context:
space:
mode:
authorZeyla Hellyer <[email protected]>2018-02-04 07:50:53 -0800
committerZeyla Hellyer <[email protected]>2018-02-04 07:54:31 -0800
commita9966371def331cd848f642e222627ee9decf354 (patch)
tree316c75854ddea79230f98b66708c3f815b836227 /src/cache/mod.rs
parentPartially revert the video url change (diff)
downloadserenity-a9966371def331cd848f642e222627ee9decf354.tar.xz
serenity-a9966371def331cd848f642e222627ee9decf354.zip
Rewrite the library to use Futures
Rewrites the library to use Futures. This rewrites all of the gateway, client, cache, most model methods, HTTP, and in a later commit the framework and voice. HTTP has been mostly rewritten to be ergonomic so that it can be used by the user, and has been upgraded to hyper v0.11. The gateway now uses `tokio-tungstenite` v0.4. The client isn't yet in a working state. The models now have a `client` optionally attached to them to make use of `http` and `cache`-utilizing methods. The client isn't needed, in the case that someone is using the library just to deserialize models. The cache is now built around `Rc`s and `RefCell`s, instead of `Arc`s and `RwLock`s. Refer to example 01 for a working example. Much of the library still doesn't work.
Diffstat (limited to 'src/cache/mod.rs')
-rw-r--r--src/cache/mod.rs62
1 files changed, 32 insertions, 30 deletions
diff --git a/src/cache/mod.rs b/src/cache/mod.rs
index 8cd402e..462cc38 100644
--- a/src/cache/mod.rs
+++ b/src/cache/mod.rs
@@ -43,11 +43,11 @@
//! [`http`]: ../http/index.html
use model::prelude::*;
-use parking_lot::RwLock;
+use std::cell::RefCell;
use std::collections::hash_map::Entry;
use std::collections::{HashMap, HashSet};
use std::default::Default;
-use std::sync::Arc;
+use std::rc::Rc;
mod cache_update;
@@ -77,21 +77,21 @@ pub struct Cache {
/// [`Event::GuildDelete`]: ../model/event/struct.GuildDeleteEvent.html
/// [`Event::GuildUnavailable`]: ../model/event/struct.GuildUnavailableEvent.html
/// [`Guild`]: ../model/guild/struct.Guild.html
- pub channels: HashMap<ChannelId, Arc<RwLock<GuildChannel>>>,
+ pub channels: HashMap<ChannelId, Rc<RefCell<GuildChannel>>>,
/// A map of channel categories.
- pub categories: HashMap<ChannelId, Arc<RwLock<ChannelCategory>>>,
+ pub categories: HashMap<ChannelId, Rc<RefCell<ChannelCategory>>>,
/// A map of the groups that the current user is in.
///
/// For bot users this will always be empty, except for in [special cases].
///
/// [special cases]: index.html#special-cases-in-the-cache
- pub groups: HashMap<ChannelId, Arc<RwLock<Group>>>,
+ pub groups: HashMap<ChannelId, Rc<RefCell<Group>>>,
/// A map of guilds with full data available. This includes data like
/// [`Role`]s and [`Emoji`]s that are not available through the REST API.
///
/// [`Emoji`]: ../model/guild/struct.Emoji.html
/// [`Role`]: ../model/guild/struct.Role.html
- pub guilds: HashMap<GuildId, Arc<RwLock<Guild>>>,
+ pub guilds: HashMap<GuildId, Rc<RefCell<Guild>>>,
/// A map of notes that a user has made for individual users.
///
/// An empty note is equivalent to having no note, and creating an empty
@@ -102,10 +102,10 @@ pub struct Cache {
/// A map of users' presences. This is updated in real-time. Note that
/// status updates are often "eaten" by the gateway, and this should not
/// be treated as being entirely 100% accurate.
- pub presences: HashMap<UserId, Presence>,
+ pub presences: HashMap<UserId, Rc<RefCell<Presence>>>,
/// A map of direct message channels that the current user has open with
/// other users.
- pub private_channels: HashMap<ChannelId, Arc<RwLock<PrivateChannel>>>,
+ pub private_channels: HashMap<ChannelId, Rc<RefCell<PrivateChannel>>>,
/// The total number of shards being used by the bot.
pub shard_count: u64,
/// A list of guilds which are "unavailable". Refer to the documentation for
@@ -154,7 +154,7 @@ pub struct Cache {
/// [`GuildSyncEvent`]: ../model/event/struct.GuildSyncEvent.html
/// [`PresenceUpdateEvent`]: ../model/event/struct.PresenceUpdateEvent.html
/// [`ReadyEvent`]: ../model/event/struct.ReadyEvent.html
- pub users: HashMap<UserId, Arc<RwLock<User>>>,
+ pub users: HashMap<UserId, Rc<RefCell<User>>>,
}
impl Cache {
@@ -212,7 +212,7 @@ impl Cache {
let mut total = 0;
for guild in self.guilds.values() {
- let guild = guild.read();
+ let guild = guild.borrow();
let members = guild.members.len() as u64;
@@ -319,15 +319,15 @@ impl Cache {
let id = id.into();
if let Some(channel) = self.channels.get(&id) {
- return Some(Channel::Guild(Arc::clone(channel)));
+ return Some(Channel::Guild(Rc::clone(channel)));
}
if let Some(private_channel) = self.private_channels.get(&id) {
- return Some(Channel::Private(Arc::clone(private_channel)));
+ return Some(Channel::Private(Rc::clone(private_channel)));
}
if let Some(group) = self.groups.get(&id) {
- return Some(Channel::Group(Arc::clone(group)));
+ return Some(Channel::Group(Rc::clone(group)));
}
None
@@ -361,7 +361,7 @@ impl Cache {
/// # }
/// ```
#[inline]
- pub fn guild<G: Into<GuildId>>(&self, id: G) -> Option<Arc<RwLock<Guild>>> {
+ pub fn guild<G: Into<GuildId>>(&self, id: G) -> Option<Rc<RefCell<Guild>>> {
self.guilds.get(&id.into()).cloned()
}
@@ -418,7 +418,7 @@ impl Cache {
/// [`Guild`]: ../model/guild/struct.Guild.html
/// [`channel`]: #method.channel
#[inline]
- pub fn guild_channel<C: Into<ChannelId>>(&self, id: C) -> Option<Arc<RwLock<GuildChannel>>> {
+ pub fn guild_channel<C: Into<ChannelId>>(&self, id: C) -> Option<Rc<RefCell<GuildChannel>>> {
self.channels.get(&id.into()).cloned()
}
@@ -452,7 +452,7 @@ impl Cache {
/// # }
/// ```
#[inline]
- pub fn group<C: Into<ChannelId>>(&self, id: C) -> Option<Arc<RwLock<Group>>> {
+ pub fn group<C: Into<ChannelId>>(&self, id: C) -> Option<Rc<RefCell<Group>>> {
self.groups.get(&id.into()).cloned()
}
@@ -501,10 +501,13 @@ impl Cache {
/// [`Client::on_message`]: ../client/struct.Client.html#method.on_message
/// [`Guild`]: ../model/guild/struct.Guild.html
/// [`members`]: ../model/guild/struct.Guild.html#structfield.members
- pub fn member<G, U>(&self, guild_id: G, user_id: U) -> Option<Member>
- where G: Into<GuildId>, U: Into<UserId> {
+ pub fn member<G: Into<GuildId>, U: Into<UserId>>(
+ &self,
+ guild_id: G,
+ user_id: U,
+ ) -> Option<Rc<RefCell<Member>>> {
self.guilds.get(&guild_id.into()).and_then(|guild| {
- guild.read().members.get(&user_id.into()).cloned()
+ guild.borrow().members.get(&user_id.into()).cloned()
})
}
@@ -543,7 +546,7 @@ impl Cache {
#[inline]
pub fn private_channel<C: Into<ChannelId>>(&self,
channel_id: C)
- -> Option<Arc<RwLock<PrivateChannel>>> {
+ -> Option<Rc<RefCell<PrivateChannel>>> {
self.private_channels.get(&channel_id.into()).cloned()
}
@@ -575,11 +578,11 @@ impl Cache {
/// # try_main().unwrap();
/// # }
/// ```
- pub fn role<G, R>(&self, guild_id: G, role_id: R) -> Option<Role>
- where G: Into<GuildId>, R: Into<RoleId> {
+ pub fn role<G: Into<GuildId>, R: Into<RoleId>>(&self, guild_id: G, role_id: R)
+ -> Option<Rc<RefCell<Role>>> {
self.guilds
.get(&guild_id.into())
- .and_then(|g| g.read().roles.get(&role_id.into()).cloned())
+ .and_then(|g| g.borrow().roles.get(&role_id.into()).cloned())
}
/// Retrieves a `User` from the cache's [`users`] map, if it exists.
@@ -611,14 +614,13 @@ impl Cache {
/// # }
/// ```
#[inline]
- pub fn user<U: Into<UserId>>(&self, user_id: U) -> Option<Arc<RwLock<User>>> {
+ pub fn user<U: Into<UserId>>(&self, user_id: U) -> Option<Rc<RefCell<User>>> {
self.users.get(&user_id.into()).cloned()
}
#[inline]
- pub fn categories<C: Into<ChannelId>>(&self,
- channel_id: C)
- -> Option<Arc<RwLock<ChannelCategory>>> {
+ pub fn category<C: Into<ChannelId>>(&self, channel_id: C)
+ -> Option<Rc<RefCell<ChannelCategory>>> {
self.categories.get(&channel_id.into()).cloned()
}
@@ -630,10 +632,10 @@ impl Cache {
pub(crate) fn update_user_entry(&mut self, user: &User) {
match self.users.entry(user.id) {
Entry::Vacant(e) => {
- e.insert(Arc::new(RwLock::new(user.clone())));
+ e.insert(Rc::new(RefCell::new(user.clone())));
},
- Entry::Occupied(mut e) => {
- e.get_mut().write().clone_from(user);
+ Entry::Occupied(e) => {
+ e.get().borrow_mut().clone_from(user);
},
}
}