pub struct ShardId { /* private fields */ }
Expand description
Shard identifier to calculate if it receivies a given event.
A shard ID consist of two fields: number
and total
. These values do not
need to be unique, and are used by Discord for calculating which events to
send to which shard. Shards should in general share the same total
value
and have an unique number
value, but users may deviate from this when
resharding/migrating to a new set of shards.
§Advanced use
Incoming events are split by their originating guild and are received by the shard with the id calculated from the following formula:
number = (guild_id >> 22) % total
.
total
is in other words unrelated to the total number of shards and is
only used to specify the share of events a shard will receive. The formula
is independently calculated for all shards, which means that events may be
duplicated or lost if it’s determined that an event should be sent to
multiple or no shard.
It may be helpful to visualize the logic in code:
for shard in shards {
if shard.id().number() == (guild_id >> 22) % shard.id().total() {
unimplemented!("send event to shard");
}
}
Implementations§
Source§impl ShardId
impl ShardId
Sourcepub const ONE: ShardId
pub const ONE: ShardId
ID of a bot that has only one shard.
Should only be used by small bots in under one or two thousand guilds.
Sourcepub const fn new(number: u32, total: u32) -> ShardId
pub const fn new(number: u32, total: u32) -> ShardId
Create a new shard identifier.
The shard number is 0-indexed while the total number of shards is 1-indexed. A shard number of 7 with a total of 8 is therefore valid, whilst a shard number of 8 out of 8 total shards is invalid.
§Examples
Create a new shard with a shard number of 13 out of a total of 24 shards:
use twilight_model::gateway::ShardId;
let id = ShardId::new(13, 24);
§Panics
Panics if the shard number is greater than or equal to the total number of shards.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for ShardId
impl<'de> Deserialize<'de> for ShardId
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<ShardId, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<ShardId, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
Source§impl Serialize for ShardId
impl Serialize for ShardId
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
impl Copy for ShardId
impl Eq for ShardId
impl StructuralPartialEq for ShardId
Auto Trait Implementations§
impl Freeze for ShardId
impl RefUnwindSafe for ShardId
impl Send for ShardId
impl Sync for ShardId
impl Unpin for ShardId
impl UnwindSafe for ShardId
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.