Skip to content

Commit

Permalink
Use CacheRwLock in CacheAndHttp (#658)
Browse files Browse the repository at this point in the history
Use `CacheRwLock` in `CacheAndHttp`.
Replacing the normal `Arc<RwLock<Cache>>` with the
API-friendly `CacheRwLock`.
Implement `CacheHttp` for `CacheAndHttp`.
Also takes `Arc` and `&T` into account.
  • Loading branch information
Lakelezz authored and arqunis committed Dec 13, 2019
1 parent 5dbe078 commit 28a91c6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
6 changes: 6 additions & 0 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,12 @@ impl AsRef<CacheRwLock> for CacheRwLock {
}
}

impl Default for CacheRwLock {
fn default() -> Self {
Self(Arc::new(RwLock::new(Cache::default())))
}
}

impl Deref for CacheRwLock {
type Target = Arc<RwLock<Cache>>;

Expand Down
4 changes: 2 additions & 2 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ impl Client {

let cache_and_http = Arc::new(CacheAndHttp {
#[cfg(feature = "cache")]
cache: Arc::new(RwLock::new(Cache::default())),
cache: CacheRwLock::default(),
#[cfg(feature = "cache")]
update_cache_timeout: None,
#[cfg(feature = "http")]
Expand Down Expand Up @@ -475,7 +475,7 @@ impl Client {
)));

let cache_and_http = Arc::new(CacheAndHttp {
cache: Arc::new(RwLock::new(Cache::default())),
cache: CacheRwLock::default(),
update_cache_timeout: duration,
#[cfg(feature = "http")]
http: Arc::new(http),
Expand Down
28 changes: 28 additions & 0 deletions src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ use std::{
use crate::cache::CacheRwLock;
#[cfg(feature = "client")]
use crate::client::Context;
#[cfg(feature = "client")]
use crate::CacheAndHttp;
#[cfg(feature = "client")]
use std::sync::Arc;

/// This trait will be required by functions that need [`Http`] and can
/// optionally use a [`CacheRwLock`] to potentially avoid REST-requests.
Expand Down Expand Up @@ -105,6 +109,30 @@ impl CacheHttp for &&mut Context {
fn cache(&self) -> Option<&CacheRwLock> { Some(&self.cache) }
}

#[cfg(feature = "client")]
impl CacheHttp for CacheAndHttp {
#[cfg(feature = "http")]
fn http(&self) -> &Http { &self.http }
#[cfg(feature = "cache")]
fn cache(&self) -> Option<&CacheRwLock> { Some(&self.cache) }
}

#[cfg(feature = "client")]
impl CacheHttp for &CacheAndHttp {
#[cfg(feature = "http")]
fn http(&self) -> &Http { &self.http }
#[cfg(feature = "cache")]
fn cache(&self) -> Option<&CacheRwLock> { Some(&self.cache) }
}

#[cfg(feature = "client")]
impl CacheHttp for Arc<CacheAndHttp> {
#[cfg(feature = "http")]
fn http(&self) -> &Http { &self.http }
#[cfg(feature = "cache")]
fn cache(&self) -> Option<&CacheRwLock> { Some(&self.cache) }
}

#[cfg(all(feature = "cache", feature = "http"))]
impl CacheHttp for (&CacheRwLock, &Http) {
fn cache(&self) -> Option<&CacheRwLock> { Some(&self.0) }
Expand Down
6 changes: 2 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@ pub use crate::error::{Error, Result};
pub use crate::client::Client;

#[cfg(feature = "cache")]
use crate::cache::Cache;
#[cfg(feature = "cache")]
use parking_lot::RwLock;
use crate::cache::CacheRwLock;
#[cfg(feature = "cache")]
use std::time::Duration;
#[cfg(any(feature = "client", feature = "http"))]
Expand All @@ -105,7 +103,7 @@ use crate::http::Http;
#[derive(Default)]
pub struct CacheAndHttp {
#[cfg(feature = "cache")]
pub cache: Arc<RwLock<Cache>>,
pub cache: CacheRwLock,
#[cfg(feature = "cache")]
pub update_cache_timeout: Option<Duration>,
#[cfg(feature = "http")]
Expand Down

0 comments on commit 28a91c6

Please sign in to comment.