Skip to content

Commit

Permalink
Changes to appease borrowck. Also crashes rustc.
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixMcFelix committed Jun 21, 2018
1 parent c470eaa commit ee8bfa1
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 19 deletions.
4 changes: 2 additions & 2 deletions examples/dev_shard_manager/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ fn try_main(handle: Handle) -> Box<Future<Item = (), Error = ()>> {
queue: SimpleReconnectQueue::new(4),
};

let mut shard_manager = ShardManager::new(opts, handle.clone());
let mut shard_manager = ShardManager::new(opts);
let future = shard_manager.start()
.map_err(|e| println!("Error starting shard manager: {:?}", e));

handle.spawn(future);

let future = shard_manager.messages().for_each(move |(shard, message)| {
let mut shard = shard.borrow_mut();
let mut shard = shard.lock();

let event = shard.parse(message)
.expect("Could not parse shard stream message");
Expand Down
7 changes: 5 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,16 @@ impl StdError for Error {
Error::Json(ref inner) => inner.description(),
Error::Model(ref inner) => inner.description(),
Error::Num(ref inner) => inner.description(),
Error::SplitWebSocketSend(ref inner) => inner.description(),
Error::Url(ref inner) => inner,
Error::Uri(ref inner) => inner.description(),
#[cfg(feature = "client")]
Error::Client(ref inner) => inner.description(),
#[cfg(feature = "http")]
#[cfg(feature = "http_base")]
Error::Http(ref inner) => inner.description(),
#[cfg(feature = "http")]
Error::HttpCrate(ref inner) => inner.description(),
#[cfg(feature = "hyper")]
Error::Hyper(ref inner) => inner.description(),
#[cfg(feature = "voice")]
Error::Opus(ref inner) => inner.description(),
Expand All @@ -264,7 +267,7 @@ impl StdError for Error {

fn cause(&self) -> Option<&StdError> {
match *self {
#[cfg(feature = "http")]
#[cfg(feature = "hyper")]
Error::Hyper(ref inner) => Some(inner),
Error::Json(ref inner) => Some(inner),
Error::Io(ref inner) => Some(inner),
Expand Down
8 changes: 4 additions & 4 deletions src/gateway/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use std::collections::VecDeque;
pub trait ReconnectQueue {
type Error: 'static + Send;

fn push_back(&mut self, shard_id: u64) -> Box<Future<Item = (), Error = Self::Error>>;
fn push_back(&mut self, shard_id: u64) -> Box<Future<Item = (), Error = Self::Error> + Send>;

fn pop_front(&mut self) -> Box<Future<Item = Option<u64>, Error = Self::Error>>;
fn pop_front(&mut self) -> Box<Future<Item = Option<u64>, Error = Self::Error> + Send>;
}

pub struct SimpleReconnectQueue {
Expand All @@ -24,12 +24,12 @@ impl SimpleReconnectQueue {
impl ReconnectQueue for SimpleReconnectQueue {
type Error = ();

fn push_back(&mut self, shard_id: u64) -> Box<Future<Item = (), Error = Self::Error>> {
fn push_back(&mut self, shard_id: u64) -> Box<Future<Item = (), Error = Self::Error> + Send> {
self.queue.push_back(shard_id);
Box::new(future::ok(()))
}

fn pop_front(&mut self) -> Box<Future<Item = Option<u64>, Error = Self::Error>> {
fn pop_front(&mut self) -> Box<Future<Item = Option<u64>, Error = Self::Error> + Send> {
Box::new(future::ok(self.queue.pop_front()))
}
}
2 changes: 1 addition & 1 deletion src/gateway/shard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub struct Shard {

impl Shard {
pub fn new(token: String, shard_info: [u64; 2])
-> Box<Future<Item = Shard, Error = Error>> {
-> Box<Future<Item = Shard, Error = Error> + Send> {
let done = connect_async(Url::from_str(CONNECTION).unwrap())
.map(move |(duplex, _)| {
let (sink, stream) = duplex.split();
Expand Down
14 changes: 7 additions & 7 deletions src/gateway/shard_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub struct ShardManagerOptions<T: ReconnectQueue> {
pub type WrappedShard = Arc<Mutex<Shard>>;//Rc<RefCell<Shard>>;
pub type Message = (WrappedShard, TungsteniteMessage);
pub type MessageStream = UnboundedReceiver<Message>;
type ShardsMap = Rc<RefCell<HashMap<u64, WrappedShard>>>;
type ShardsMap = Arc<Mutex<HashMap<u64, WrappedShard>>>;//Rc<RefCell<HashMap<u64, WrappedShard>>>;

pub struct ShardManager<T: ReconnectQueue> {
pub queue: VecDeque<u64>,
Expand All @@ -83,7 +83,7 @@ impl<T: ReconnectQueue> ShardManager<T> {
Self {
queue: VecDeque::new(),
reconnect_queue: options.queue,
shards: Rc::new(RefCell::new(HashMap::new())),
shards: Arc::new(Mutex::new(HashMap::new())),
strategy: options.strategy,
token: options.token,
ws_uri: options.ws_uri,
Expand Down Expand Up @@ -202,7 +202,8 @@ fn process_queue(
let future = start_shard(token, shard_id, shards_total,
sender)
.map(move |shard| {
shards_map.borrow_mut().insert(shard_id.clone(), shard);
let mut map = shards_map.lock();
map.insert(shard_id.clone(), shard);
});

tokio::spawn(future);
Expand All @@ -217,7 +218,7 @@ fn start_shard(
shard_id: u64,
shards_total: u64,
sender: UnboundedSender<Message>,
) -> Box<Future<Item = WrappedShard, Error = ()>> {
) -> Box<Future<Item = WrappedShard, Error = ()> + Send> {
Box::new(Shard::new(token, [shard_id, shards_total])
.then(move |result| {
let shard = match result {
Expand All @@ -228,13 +229,12 @@ fn start_shard(
};

let sink = MessageSink {
shard,
shard: shard.clone(),
sender,
};

let messages = {
let shard_lock = shard.clone();
let shard = shard_lock.lock();
let mut shard = shard.lock();

shard.messages()
};
Expand Down
1 change: 1 addition & 0 deletions src/http/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ impl StdError for Error {
Error::BorrowMut(ref inner) => inner.description(),
Error::Canceled(ref inner) => inner.description(),
Error::Format(ref inner) => inner.description(),
Error::Http(ref inner) => inner.description(),
Error::Hyper(ref inner) => inner.description(),
Error::InvalidRequest(_) => "Received an unexpected status code",
Error::Io(ref inner) => inner.description(),
Expand Down
4 changes: 2 additions & 2 deletions src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1422,7 +1422,7 @@ impl Client {
let done = client.request(request)
.from_err()
.and_then(verify_status)
.and_then(|res| res.body().concat2().map_err(From::from))
.and_then(|res| res.into_body().concat2().map_err(From::from))
.and_then(|body| serde_json::from_slice(&body).map_err(From::from));

Box::new(done)
Expand Down Expand Up @@ -1562,7 +1562,7 @@ impl Client {
.and_then(move |_| client.request(request).map_err(From::from))
.from_err()
.and_then(verify_status)
.and_then(|res| res.body().concat2().map_err(From::from))
.and_then(|res| res.into_body().concat2().map_err(From::from))
.and_then(|body| serde_json::from_slice(&body).map_err(From::from)))
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ pub mod client;
pub mod framework;
#[cfg(feature = "gateway")]
pub mod gateway;
#[cfg(feature = "http")]
#[cfg(feature = "http_base")]
pub mod http;
#[cfg(feature = "utils")]
pub mod utils;
Expand Down

0 comments on commit ee8bfa1

Please sign in to comment.