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>

source

pub const fn cache_ref(&'a self) -> &'a InMemoryCache

Immutable reference to the underlying cache.

source

pub fn channels(&self) -> ResourceIter<'a, Id<ChannelMarker>, Channel>

Create an iterator over the channels in the cache.

source

pub fn emojis( &self ) -> ResourceIter<'a, Id<EmojiMarker>, GuildResource<CachedEmoji>>

Create an iterator over the emojis in the cache.

source

pub fn guilds(&self) -> ResourceIter<'a, Id<GuildMarker>, CachedGuild>

Create an iterator over the guilds in the cache.

source

pub fn integrations( &self ) -> ResourceIter<'a, (Id<GuildMarker>, Id<IntegrationMarker>), GuildResource<GuildIntegration>>

Create an iterator over the integrations in the cache.

source

pub fn members( &self ) -> ResourceIter<'a, (Id<GuildMarker>, Id<UserMarker>), CachedMember>

Create an iterator over the members across all guilds in the cache.

source

pub fn messages(&self) -> ResourceIter<'a, Id<MessageMarker>, CachedMessage>

Create an iterator over the messages in the cache.

source

pub fn presences( &self ) -> ResourceIter<'a, (Id<GuildMarker>, Id<UserMarker>), CachedPresence>

Create an iterator over the presences in the cache.

source

pub fn roles(&self) -> ResourceIter<'a, Id<RoleMarker>, GuildResource<Role>>

Create an iterator over the roles in the cache.

source

pub fn stage_instances( &self ) -> ResourceIter<'a, Id<StageMarker>, GuildResource<StageInstance>>

Create an iterator over the stage instances in the cache.

source

pub fn stickers( &self ) -> ResourceIter<'a, Id<StickerMarker>, GuildResource<CachedSticker>>

Create an iterator over the stickers in the cache.

source

pub fn users(&self) -> ResourceIter<'a, Id<UserMarker>, User>

Create an iterator over the users in the cache.

source

pub fn voice_states( &self ) -> ResourceIter<'a, (Id<GuildMarker>, Id<UserMarker>), CachedVoiceState>

Create an iterator over the voice states in the cache.

Trait Implementations§

source§

impl<'a> Debug for InMemoryCacheIter<'a>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for InMemoryCacheIter<'a>

§

impl<'a> Send for InMemoryCacheIter<'a>

§

impl<'a> Sync for InMemoryCacheIter<'a>

§

impl<'a> Unpin for InMemoryCacheIter<'a>

§

impl<'a> !UnwindSafe for InMemoryCacheIter<'a>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.