Skip to content

Commit

Permalink
Gateway: Add Songbird::iter (#166)
Browse files Browse the repository at this point in the history
This PR allows users to iterate over all existing `Call`s via the `Songbird::iter` method.

Closes #165.
  • Loading branch information
fee1-dead authored and FelixMcFelix committed Nov 20, 2023
1 parent 2de071f commit 5bc8430
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,16 @@ impl Songbird {
})
}

/// Creates an iterator for all [`Call`]s currently managed.
pub fn iter(&self) -> Iter<'_> {
Iter {
inner: self
.calls
.iter()
.map(|x| (*x.key(), Arc::clone(&x.value()))),
}
}

/// Sets a shared configuration for all drivers created from this
/// manager.
///
Expand Down Expand Up @@ -449,6 +459,40 @@ impl VoiceGatewayManager for Songbird {
}
}

type DashMapIter<'a> = dashmap::iter::Iter<'a, GuildId, Arc<Mutex<Call>>>;

/// An iterator over all [`Call`]s currently stored in the manager instance.
pub struct Iter<'a> {
inner: std::iter::Map<
DashMapIter<'a>,
fn(<DashMapIter<'a> as Iterator>::Item) -> (GuildId, Arc<Mutex<Call>>),
>,
}

impl<'a> Iterator for Iter<'a> {
type Item = (GuildId, Arc<Mutex<Call>>);

fn next(&mut self) -> Option<Self::Item> {
self.inner.next()
}

fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}

fn count(self) -> usize {
self.inner.count()
}

fn fold<B, F>(self, init: B, f: F) -> B
where
Self: Sized,
F: FnMut(B, Self::Item) -> B,
{
self.inner.fold(init, f)
}
}

#[inline]
fn shard_id(guild_id: u64, shard_count: u64) -> u64 {
(guild_id >> 22) % shard_count
Expand Down

0 comments on commit 5bc8430

Please sign in to comment.