Struct twilight_cache_inmemory::iter::InMemoryCacheIter
source · pub struct InMemoryCacheIter<'a>(/* private fields */);
Expand description
Interface to create iterators over various resources.
The created iterators will iterate over all entities of a resource across all channels and guilds.
The iteration order of all iterators are arbitrary.
Examples
Count the number of users in the cache whose username begins with “twi”:
use twilight_cache_inmemory::InMemoryCache;
let cache = InMemoryCache::new();
// later in the application...
let count = cache
.iter()
.users()
.filter(|user| user.name.starts_with("twi"))
.count();
println!("'twi' users: {count}");
Potential inefficiency
Resource iterators over the entire cache are inefficient when the goal is to
iterate over a resource in a specific guild. For example, when performing a
task such as iterating over the members of a specific guild, retrieving the
list of members via InMemoryCache::guild_members
and then calling
InMemoryCache::member
for each item is more efficient. That might look
like:
use twilight_cache_inmemory::InMemoryCache;
use twilight_model::id::Id;
let cache = InMemoryCache::new();
// later in the application...
let guild_id = Id::new(1);
let maybe_guild_members = cache.guild_members(guild_id);
if let Some(guild_members) = maybe_guild_members {
for user_id in guild_members.iter() {
if let Some(member) = cache.member(guild_id, *user_id) {
println!(
"member id {}'s nickname: {:?}",
member.user_id(),
member.nick(),
);
}
}
}
Implementations§
source§impl<'a> InMemoryCacheIter<'a>
impl<'a> InMemoryCacheIter<'a>
sourcepub const fn cache_ref(&'a self) -> &'a InMemoryCache
pub const fn cache_ref(&'a self) -> &'a InMemoryCache
Immutable reference to the underlying cache.
sourcepub fn channels(&self) -> ResourceIter<'a, Id<ChannelMarker>, Channel> ⓘ
pub fn channels(&self) -> ResourceIter<'a, Id<ChannelMarker>, Channel> ⓘ
Create an iterator over the channels in the cache.
sourcepub fn emojis(
&self
) -> ResourceIter<'a, Id<EmojiMarker>, GuildResource<CachedEmoji>> ⓘ
pub fn emojis( &self ) -> ResourceIter<'a, Id<EmojiMarker>, GuildResource<CachedEmoji>> ⓘ
Create an iterator over the emojis in the cache.
sourcepub fn guilds(&self) -> ResourceIter<'a, Id<GuildMarker>, CachedGuild> ⓘ
pub fn guilds(&self) -> ResourceIter<'a, Id<GuildMarker>, CachedGuild> ⓘ
Create an iterator over the guilds in the cache.
sourcepub fn integrations(
&self
) -> ResourceIter<'a, (Id<GuildMarker>, Id<IntegrationMarker>), GuildResource<GuildIntegration>> ⓘ
pub fn integrations( &self ) -> ResourceIter<'a, (Id<GuildMarker>, Id<IntegrationMarker>), GuildResource<GuildIntegration>> ⓘ
Create an iterator over the integrations in the cache.
sourcepub fn members(
&self
) -> ResourceIter<'a, (Id<GuildMarker>, Id<UserMarker>), CachedMember> ⓘ
pub fn members( &self ) -> ResourceIter<'a, (Id<GuildMarker>, Id<UserMarker>), CachedMember> ⓘ
Create an iterator over the members across all guilds in the cache.
sourcepub fn messages(&self) -> ResourceIter<'a, Id<MessageMarker>, CachedMessage> ⓘ
pub fn messages(&self) -> ResourceIter<'a, Id<MessageMarker>, CachedMessage> ⓘ
Create an iterator over the messages in the cache.
sourcepub fn presences(
&self
) -> ResourceIter<'a, (Id<GuildMarker>, Id<UserMarker>), CachedPresence> ⓘ
pub fn presences( &self ) -> ResourceIter<'a, (Id<GuildMarker>, Id<UserMarker>), CachedPresence> ⓘ
Create an iterator over the presences in the cache.
sourcepub fn roles(&self) -> ResourceIter<'a, Id<RoleMarker>, GuildResource<Role>> ⓘ
pub fn roles(&self) -> ResourceIter<'a, Id<RoleMarker>, GuildResource<Role>> ⓘ
Create an iterator over the roles in the cache.
sourcepub fn stage_instances(
&self
) -> ResourceIter<'a, Id<StageMarker>, GuildResource<StageInstance>> ⓘ
pub fn stage_instances( &self ) -> ResourceIter<'a, Id<StageMarker>, GuildResource<StageInstance>> ⓘ
Create an iterator over the stage instances in the cache.
sourcepub fn stickers(
&self
) -> ResourceIter<'a, Id<StickerMarker>, GuildResource<CachedSticker>> ⓘ
pub fn stickers( &self ) -> ResourceIter<'a, Id<StickerMarker>, GuildResource<CachedSticker>> ⓘ
Create an iterator over the stickers in the cache.
sourcepub fn users(&self) -> ResourceIter<'a, Id<UserMarker>, User> ⓘ
pub fn users(&self) -> ResourceIter<'a, Id<UserMarker>, User> ⓘ
Create an iterator over the users in the cache.
sourcepub fn voice_states(
&self
) -> ResourceIter<'a, (Id<GuildMarker>, Id<UserMarker>), CachedVoiceState> ⓘ
pub fn voice_states( &self ) -> ResourceIter<'a, (Id<GuildMarker>, Id<UserMarker>), CachedVoiceState> ⓘ
Create an iterator over the voice states in the cache.