pub struct ShardManager {
pub token: String,
pub total_shards: usize,
pub shards: Arc<RwLock<ShardMap>>,
/* private fields */
}
Expand description
The central hub for all shards, where shards are spawned and maintained.
Fields§
§token: String
The token used by this manager to spawn shards.
total_shards: usize
The total amount of shards that this manager will attempt to spawn.
shards: Arc<RwLock<ShardMap>>
A collection of shards that have been spawned.
Implementations§
Source§impl ShardManager
impl ShardManager
Sourcepub fn new(
token: String,
strategy: ShardStrategy,
) -> impl Future<Item = ShardManager, Error = Error>
pub fn new( token: String, strategy: ShardStrategy, ) -> impl Future<Item = ShardManager, Error = Error>
Creates a new cluster, with the provided Discord API token.
Examples found in repository?
examples/sharder.rs (line 12)
7fn main() {
8 let token = var("DISCORD_TOKEN").expect("Failed to parse Discord token");
9
10 // Here, we initialize a new shard manager, with the provided Discord token and strategy.
11 // We use the recommended sharding strategy, but a fixed shard strategy may also be chosen.
12 tokio::run(ShardManager::new(token, ShardStrategy::Recommended)
13 .map(|mut manager| {
14 // The start_spawn() method returns a tuple of streams: One for handling spawned shards, the other for handling shard events.
15 let (spawner, events) = manager.start_spawn();
16 // As demonstrated below, we consume the streams concurrently between threads.
17 tokio::spawn(spawner.for_each(|shard| {
18 println!("Shard {:?} has successfully spawned.", shard.lock().info);
19
20 Ok(())
21 }));
22 tokio::spawn(events.for_each(|event| {
23 if let Some(evt) = event.packet.t {
24 println!("Received event from Shard {:?}: {:?}", event.shard.lock().info, evt);
25 };
26
27 Ok(())
28 }));
29 })
30 .map_err(|err| {
31 eprintln!("Error occured: {:?}", err);
32 })
33 );
34}
Sourcepub fn start_spawn(&mut self) -> (Spawner, EventHandler)
pub fn start_spawn(&mut self) -> (Spawner, EventHandler)
Spawns shards up to the specified amount and identifies them with Discord.
Examples found in repository?
examples/sharder.rs (line 15)
7fn main() {
8 let token = var("DISCORD_TOKEN").expect("Failed to parse Discord token");
9
10 // Here, we initialize a new shard manager, with the provided Discord token and strategy.
11 // We use the recommended sharding strategy, but a fixed shard strategy may also be chosen.
12 tokio::run(ShardManager::new(token, ShardStrategy::Recommended)
13 .map(|mut manager| {
14 // The start_spawn() method returns a tuple of streams: One for handling spawned shards, the other for handling shard events.
15 let (spawner, events) = manager.start_spawn();
16 // As demonstrated below, we consume the streams concurrently between threads.
17 tokio::spawn(spawner.for_each(|shard| {
18 println!("Shard {:?} has successfully spawned.", shard.lock().info);
19
20 Ok(())
21 }));
22 tokio::spawn(events.for_each(|event| {
23 if let Some(evt) = event.packet.t {
24 println!("Received event from Shard {:?}: {:?}", event.shard.lock().info, evt);
25 };
26
27 Ok(())
28 }));
29 })
30 .map_err(|err| {
31 eprintln!("Error occured: {:?}", err);
32 })
33 );
34}
Auto Trait Implementations§
impl Freeze for ShardManager
impl !RefUnwindSafe for ShardManager
impl Send for ShardManager
impl Sync for ShardManager
impl Unpin for ShardManager
impl !UnwindSafe for ShardManager
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more