Struct ShardManager

Source
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

Source

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}
Source

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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Err = <U as TryFrom<T>>::Err

Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Err>

Source§

impl<T> Erased for T

Source§

impl<T> ErasedDestructor for T
where T: 'static,