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/voice/connection.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/voice/connection.rs')
| -rw-r--r-- | src/voice/connection.rs | 15 |
1 files changed, 5 insertions, 10 deletions
diff --git a/src/voice/connection.rs b/src/voice/connection.rs index 3b31488..47b448c 100644 --- a/src/voice/connection.rs +++ b/src/voice/connection.rs @@ -1,4 +1,5 @@ use byteorder::{BigEndian, LittleEndian, ReadBytesExt, WriteBytesExt}; +use parking_lot::Mutex; use opus::{ packet as opus_packet, Application as CodingMode, @@ -11,7 +12,7 @@ use std::collections::HashMap; use std::io::Write; use std::net::{SocketAddr, ToSocketAddrs, UdpSocket}; use std::sync::mpsc::{self, Receiver as MpscReceiver, Sender as MpscSender}; -use std::sync::{Arc, Mutex}; +use std::sync::Arc; use std::thread::{self, Builder as ThreadBuilder, JoinHandle}; use std::time::Duration; use super::audio::{AudioReceiver, AudioSource, AudioType, HEADER_LEN, SAMPLE_RATE}; @@ -223,10 +224,7 @@ impl Connection { // Send the voice websocket keepalive if it's time if self.keepalive_timer.check() { - self.client - .lock() - .unwrap() - .send_json(&payload::build_keepalive())?; + self.client.lock().send_json(&payload::build_keepalive())?; } // Send UDP keepalive if it's time @@ -357,10 +355,7 @@ impl Connection { self.speaking = speaking; - self.client - .lock() - .unwrap() - .send_json(&payload::build_speaking(speaking)) + self.client.lock().send_json(&payload::build_speaking(speaking)) } } @@ -452,7 +447,7 @@ fn start_threads(client: Arc<Mutex<Client>>, udp: &UdpSocket) -> Result<ThreadIt let ws_thread = ThreadBuilder::new() .name(format!("{} WS", thread_name)) .spawn(move || loop { - while let Ok(Some(msg)) = client.lock().unwrap().recv_json(VoiceEvent::decode) { + while let Ok(Some(msg)) = client.lock().recv_json(VoiceEvent::decode) { if tx_clone.send(ReceiverStatus::Websocket(msg)).is_ok() { return; } |