Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions twilight-gateway/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ pub const API_VERSION: u8 = 10;
///
/// Panics if the bucket id is greater than or equal to the total number of
/// buckets.
#[inline]
#[track_caller]
pub fn bucket(
bucket_id: u16,
Expand All @@ -115,6 +116,10 @@ pub fn bucket(
let len = q + u32::from(bucket_id < r);
let start = bucket_id * q + r.min(bucket_id);

// Compiler hint deduced from mathematical proof.
if shards < start + len {
std::process::abort();
}
(start..start + len).map(move |id| ShardId::new(id, shards))
}

Expand Down
2 changes: 2 additions & 0 deletions twilight-model/src/gateway/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ impl ShardId {
///
/// Panics if the shard number is greater than or equal to the total number
/// of shards.
#[inline]
pub const fn new(number: u32, total: u32) -> Self {
assert!(number < total, "number must be less than total");
Self {
Expand All @@ -137,6 +138,7 @@ impl ShardId {

/// Create a new shard identifier if the shard indexes are valid.
#[allow(clippy::missing_panics_doc)]
#[inline]
pub const fn new_checked(number: u32, total: u32) -> Option<Self> {
if number < total {
Some(Self {
Expand Down
Loading