From 9900b20bf5cd4036cd8d8ba28bdcd852f2c89d2f Mon Sep 17 00:00:00 2001 From: acdenisSK Date: Sun, 16 Jul 2017 01:45:09 +0200 Subject: Add a close handle for closing the shards in another thread --- src/client/mod.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/client/mod.rs b/src/client/mod.rs index 3024eea..428d773 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -56,6 +56,15 @@ use ::framework::Framework; static HANDLE_STILL: AtomicBool = ATOMIC_BOOL_INIT; +#[derive(Clone)] +pub struct CloseHandle; + +impl CloseHandle { + pub fn close(self) { + HANDLE_STILL.store(false, Ordering::Relaxed); + } +} + /// The Client is the way to be able to start sending authenticated requests /// over the REST API, as well as initializing a WebSocket connection through /// [`Shard`]s. Refer to the [documentation on using sharding][sharding docs] @@ -595,9 +604,9 @@ impl Client { self.start_connection([range[0], range[1], total_shards], http::get_gateway()?.url) } - /// Closes all of the shards that are running. - pub fn close(&self) { - HANDLE_STILL.store(false, Ordering::Relaxed); + /// Returns a thread-safe handle for closing shards. + pub fn close_handle(&self) -> CloseHandle { + CloseHandle } // Shard data layout is: @@ -696,7 +705,7 @@ impl Client { impl Drop for Client { fn drop(&mut self) { - self.close(); + self.close_handle().close(); } } -- cgit v1.2.3