diff options
| author | Zeyla Hellyer <[email protected]> | 2017-10-10 20:08:11 -0700 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2017-10-10 20:08:11 -0700 |
| commit | 93e0a4215c915b98cf433ac6d0bcfbc60f0168ec (patch) | |
| tree | 727111506d1f89cd8a511b8b79c102131222421f /src/client/mod.rs | |
| parent | Resume on resumable session invalidations (diff) | |
| download | serenity-93e0a4215c915b98cf433ac6d0bcfbc60f0168ec.tar.xz serenity-93e0a4215c915b98cf433ac6d0bcfbc60f0168ec.zip | |
Switch to parking_lot::{Mutex, RwLock}
Switch to the `parking_lot` crate's implementations of
`std::sync::Mutex` and `std::sync::RwLock`, which are more efficient.
A writeup on why `parking_lot` is more efficient can be read here:
<https://github.com/Amanieu/parking_lot>
Upgrade path:
Modify `mutex.lock().unwrap()` usage to `mutex.lock()` (not needing to
unwrap or handle a result), and
`rwlock.read().unwrap()`/`rwlock.write().unwrap()` usage to
`rwlock.read()` and `rwlock.write()`.
For example, modify:
```rust
use serenity::CACHE;
println!("{}", CACHE.read().unwrap().user.id);
```
to:
```rust
use serenity::CACHE;
println!("{}", CACHE.read().user.id);
```
Diffstat (limited to 'src/client/mod.rs')
| -rw-r--r-- | src/client/mod.rs | 16 |
1 files changed, 8 insertions, 8 deletions
diff --git a/src/client/mod.rs b/src/client/mod.rs index f2fb297..c17e284 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -39,7 +39,7 @@ pub use CACHE; use self::bridge::gateway::{ShardId, ShardManager, ShardRunnerInfo}; use self::dispatch::dispatch; -use std::sync::{self, Arc}; +use std::sync::Arc; use std::sync::atomic::{AtomicBool, Ordering, ATOMIC_BOOL_INIT}; use parking_lot::Mutex; use std::collections::HashMap; @@ -192,7 +192,7 @@ pub struct Client<H: EventHandler + Send + Sync + 'static> { /// [`Event::Ready`]: ../model/event/enum.Event.html#variant.Ready /// [`on_ready`]: #method.on_ready event_handler: Arc<H>, - #[cfg(feature = "framework")] framework: Arc<sync::Mutex<Option<Box<Framework + Send>>>>, + #[cfg(feature = "framework")] framework: Arc<Mutex<Option<Box<Framework + Send>>>>, /// A HashMap of all shards instantiated by the Client. /// /// The key is the shard ID and the value is the shard itself. @@ -250,7 +250,7 @@ pub struct Client<H: EventHandler + Send + Sync + 'static> { /// Defaults to 5 threads, which should suffice small bots. Consider /// increasing this number as your bot grows. pub threadpool: ThreadPool, - token: Arc<sync::Mutex<String>>, + token: Arc<Mutex<String>>, } impl<H: EventHandler + Send + Sync + 'static> Client<H> { @@ -291,7 +291,7 @@ impl<H: EventHandler + Send + Sync + 'static> Client<H> { }; http::set_token(&token); - let locked = Arc::new(sync::Mutex::new(token)); + let locked = Arc::new(Mutex::new(token)); let name = "serenity client".to_owned(); let threadpool = ThreadPool::with_name(name, 5); @@ -300,7 +300,7 @@ impl<H: EventHandler + Send + Sync + 'static> Client<H> { Client { data: Arc::new(Mutex::new(ShareMap::custom())), event_handler: Arc::new(handler), - framework: Arc::new(sync::Mutex::new(None)), + framework: Arc::new(Mutex::new(None)), shard_runners: Arc::new(Mutex::new(HashMap::new())), threadpool, token: locked, @@ -417,7 +417,7 @@ impl<H: EventHandler + Send + Sync + 'static> Client<H> { /// [framework docs]: ../framework/index.html #[cfg(feature = "framework")] pub fn with_framework<F: Framework + Send + 'static>(&mut self, f: F) { - self.framework = Arc::new(sync::Mutex::new(Some(Box::new(f)))); + self.framework = Arc::new(Mutex::new(Some(Box::new(f)))); } /// Establish the connection and start listening for events. @@ -755,12 +755,12 @@ impl<H: EventHandler + Send + Sync + 'static> Client<H> { { let user = http::get_current_user()?; - if let Some(ref mut framework) = *self.framework.lock().unwrap() { + if let Some(ref mut framework) = *self.framework.lock() { framework.update_current_user(user.id, user.bot); } } - let gateway_url = Arc::new(sync::Mutex::new(url)); + let gateway_url = Arc::new(Mutex::new(url)); let mut manager = ShardManager::new( shard_data[0], |