diff options
| author | Zeyla Hellyer <[email protected]> | 2018-05-28 16:34:38 -0700 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2018-05-28 16:34:38 -0700 |
| commit | 6b5f3b98084b86b00e3f7e78b5eb9512e75e78a0 (patch) | |
| tree | 4011d56b63d88999eb8169e332c54f3eafe972ae /src/internal | |
| parent | Make Message Builder use &mut self instead of self (diff) | |
| parent | Futures shard manager #298 (WIP) (#300) (diff) | |
| download | serenity-6b5f3b98084b86b00e3f7e78b5eb9512e75e78a0.tar.xz serenity-6b5f3b98084b86b00e3f7e78b5eb9512e75e78a0.zip | |
Merge branch 'futures' into v0.6.x
Diffstat (limited to 'src/internal')
| -rw-r--r-- | src/internal/macros.rs | 97 | ||||
| -rw-r--r-- | src/internal/mod.rs | 7 | ||||
| -rw-r--r-- | src/internal/rwlock_ext.rs | 31 | ||||
| -rw-r--r-- | src/internal/timer.rs | 53 | ||||
| -rw-r--r-- | src/internal/ws_impl.rs | 47 |
5 files changed, 9 insertions, 226 deletions
diff --git a/src/internal/macros.rs b/src/internal/macros.rs index c4d2b6f..805a91c 100644 --- a/src/internal/macros.rs +++ b/src/internal/macros.rs @@ -1,48 +1,5 @@ //! A set of macros for easily working with internals. -#[cfg(feature = "http")] -macro_rules! request { - ($route:expr, $method:ident($body:expr), $url:expr, $($rest:tt)*) => {{ - let client = request_client!(); - - request($route, || client - .$method(&format!(api!($url), $($rest)*)) - .body(&$body))? - }}; - ($route:expr, $method:ident($body:expr), $url:expr) => {{ - let client = request_client!(); - - request($route, || client - .$method(api!($url)) - .body(&$body))? - }}; - ($route:expr, $method:ident, $url:expr, $($rest:tt)*) => {{ - let client = request_client!(); - - request($route, || client - .$method(&format!(api!($url), $($rest)*)))? - }}; - ($route:expr, $method:ident, $url:expr) => {{ - let client = request_client!(); - - request($route, || client - .$method(api!($url)))? - }}; -} - -#[cfg(feature = "http")] -macro_rules! request_client { - () => {{ - use hyper::net::HttpsConnector; - use hyper_native_tls::NativeTlsClient; - - let tc = NativeTlsClient::new()?; - let connector = HttpsConnector::new(tc); - - HyperClient::with_connector(connector) - }} -} - #[cfg(any(feature = "model", feature = "utils"))] macro_rules! cdn { ($e:expr) => { @@ -53,51 +10,6 @@ macro_rules! cdn { }; } -#[cfg(feature = "http")] -macro_rules! api { - ($e:expr) => { - concat!("https://discordapp.com/api/v6", $e) - }; - ($e:expr, $($rest:tt)*) => { - format!(api!($e), $($rest)*) - }; -} - -#[cfg(feature = "http")] -macro_rules! status { - ($e:expr) => { - concat!("https://status.discordapp.com/api/v2", $e) - } -} - -// Enable/disable check for cache -#[cfg(any(all(feature = "cache", any(feature = "client", feature = "model"))))] -macro_rules! feature_cache { - ($enabled:block else $disabled:block) => { - { - $enabled - } - } -} - -#[cfg(any(all(not(feature = "cache"), any(feature = "client", feature = "model"))))] -macro_rules! feature_cache { - ($enabled:block else $disabled:block) => { - { - $disabled - } - } -} - -#[cfg(all(feature = "client", not(feature = "framework")))] -macro_rules! feature_framework { - ($enabled:block else $disabled:block) => { - { - $disabled - } - } -} - macro_rules! enum_number { ($name:ident { $($variant:ident, )* }) => { impl ::serde::Serialize for $name { @@ -143,3 +55,12 @@ macro_rules! enum_number { } } } + +macro_rules! ftry { + ($code:expr) => { + match $code { + Ok(v) => v, + Err(why) => return Box::new(::futures::future::err(From::from(why))), + } + } +} diff --git a/src/internal/mod.rs b/src/internal/mod.rs index a0c8142..8943cd2 100644 --- a/src/internal/mod.rs +++ b/src/internal/mod.rs @@ -3,13 +3,6 @@ pub mod macros; pub mod prelude; -mod rwlock_ext; - -pub use self::rwlock_ext::RwLockExt; - -#[cfg(feature = "gateway")] -pub mod ws_impl; - #[cfg(feature = "voice")] mod timer; diff --git a/src/internal/rwlock_ext.rs b/src/internal/rwlock_ext.rs deleted file mode 100644 index 6235370..0000000 --- a/src/internal/rwlock_ext.rs +++ /dev/null @@ -1,31 +0,0 @@ -use parking_lot::RwLock as ParkingLotRwLock; -use std::sync::{Arc, RwLock}; - -pub trait RwLockExt<T> { - fn with<Y, F: Fn(&T) -> Y>(&self, f: F) -> Y; - fn with_mut<Y, F: FnMut(&mut T) -> Y>(&self, f: F) -> Y; -} - -impl<T> RwLockExt<T> for Arc<RwLock<T>> { - fn with<Y, F: Fn(&T) -> Y>(&self, f: F) -> Y { - let r = self.read().unwrap(); - f(&r) - } - - fn with_mut<Y, F: FnMut(&mut T) -> Y>(&self, mut f: F) -> Y { - let mut w = self.write().unwrap(); - f(&mut w) - } -} - -impl<T> RwLockExt<T> for Arc<ParkingLotRwLock<T>> { - fn with<Y, F: Fn(&T) -> Y>(&self, f: F) -> Y { - let r = self.read(); - f(&r) - } - - fn with_mut<Y, F: FnMut(&mut T) -> Y>(&self, mut f: F) -> Y { - let mut w = self.write(); - f(&mut w) - } -} diff --git a/src/internal/timer.rs b/src/internal/timer.rs deleted file mode 100644 index 82cec0d..0000000 --- a/src/internal/timer.rs +++ /dev/null @@ -1,53 +0,0 @@ -use chrono::{DateTime, Duration, Utc}; -use std::{ - time::Duration as StdDuration, - thread -}; - -#[derive(Debug)] -pub struct Timer { - due: DateTime<Utc>, - duration: Duration, -} - -impl Timer { - pub fn new(duration_in_ms: u64) -> Timer { - let duration = Duration::milliseconds(duration_in_ms as i64); - - Timer { - due: Utc::now() + duration, - duration, - } - } - - pub fn await(&mut self) { - let due_time = (self.due.timestamp() * 1000) + i64::from(self.due.timestamp_subsec_millis()); - let now_time = { - let now = Utc::now(); - - (now.timestamp() * 1000) + i64::from(now.timestamp_subsec_millis()) - }; - - if due_time > now_time { - let sleep_time = due_time - now_time; - - if sleep_time > 0 { - thread::sleep(StdDuration::from_millis(sleep_time as u64)); - } - } - - self.due = self.due + self.duration; - } - - pub fn check(&mut self) -> bool { - if Utc::now() >= self.due { - self.due = self.due + self.duration; - - true - } else { - false - } - } - - pub fn reset(&mut self) { self.due = Utc::now() + self.duration; } -} diff --git a/src/internal/ws_impl.rs b/src/internal/ws_impl.rs deleted file mode 100644 index 8c10264..0000000 --- a/src/internal/ws_impl.rs +++ /dev/null @@ -1,47 +0,0 @@ -use flate2::read::ZlibDecoder; -use gateway::GatewayError; -use internal::prelude::*; -use serde_json; -use websocket::{ - message::OwnedMessage, - sync::stream::{TcpStream, TlsStream}, - sync::Client as WsClient -}; - -pub trait ReceiverExt { - fn recv_json(&mut self) -> Result<Option<Value>>; -} - -pub trait SenderExt { - fn send_json(&mut self, value: &Value) -> Result<()>; -} - -impl ReceiverExt for WsClient<TlsStream<TcpStream>> { - fn recv_json(&mut self) -> Result<Option<Value>> { - Ok(match self.recv_message()? { - OwnedMessage::Binary(bytes) => { - serde_json::from_reader(ZlibDecoder::new(&bytes[..])).map(Some)? - }, - OwnedMessage::Close(data) => return Err(Error::Gateway(GatewayError::Closed(data))), - OwnedMessage::Text(payload) => { - serde_json::from_str(&payload).map(Some)? - }, - OwnedMessage::Ping(x) => { - self.send_message(&OwnedMessage::Pong(x)) - .map_err(Error::from)?; - - None - }, - OwnedMessage::Pong(_) => None, - }) - } -} - -impl SenderExt for WsClient<TlsStream<TcpStream>> { - fn send_json(&mut self, value: &Value) -> Result<()> { - serde_json::to_string(value) - .map(OwnedMessage::Text) - .map_err(Error::from) - .and_then(|m| self.send_message(&m).map_err(Error::from)) - } -} |