Struct InMemoryCacheIter

Source
pub struct InMemoryCacheIter<'a, CacheModels: CacheableModels>(/* 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::DefaultInMemoryCache;

let cache = DefaultInMemoryCache::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::DefaultInMemoryCache;
use twilight_model::id::Id;

let cache = DefaultInMemoryCache::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, CacheModels: CacheableModels> InMemoryCacheIter<'a, CacheModels>

Source

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

Immutable reference to the underlying cache.

Source

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

Create an iterator over the channels in the cache.

Source

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

Create an iterator over the emojis in the cache.

Source

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

Create an iterator over the guilds in the cache.

Source

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

Create an iterator over the integrations in the cache.

Source

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

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

Source

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

Create an iterator over the messages in the cache.

Source

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

Create an iterator over the presences in the cache.

Source

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

Create an iterator over the roles in the cache.

Source

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

Create an iterator over the stage instances in the cache.

Source

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

Create an iterator over the stickers in the cache.

Source

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

Create an iterator over the users in the cache.

Source

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

Create an iterator over the voice states in the cache.

Trait Implementations§

Source§

impl<'a, CacheModels: Debug + CacheableModels> Debug for InMemoryCacheIter<'a, CacheModels>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a, CacheModels> Freeze for InMemoryCacheIter<'a, CacheModels>

§

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

§

impl<'a, CacheModels> Send for InMemoryCacheIter<'a, CacheModels>
where <CacheModels as CacheableModels>::CurrentUser: Send, <CacheModels as CacheableModels>::Channel: Send + Sync, <CacheModels as CacheableModels>::Guild: Send + Sync, <CacheModels as CacheableModels>::Member: Send + Sync, <CacheModels as CacheableModels>::Message: Send + Sync, <CacheModels as CacheableModels>::Presence: Send + Sync, <CacheModels as CacheableModels>::User: Send + Sync, <CacheModels as CacheableModels>::VoiceState: Send + Sync, <CacheModels as CacheableModels>::Emoji: Send + Sync, <CacheModels as CacheableModels>::GuildIntegration: Send + Sync, <CacheModels as CacheableModels>::Role: Send + Sync, <CacheModels as CacheableModels>::GuildScheduledEvent: Send + Sync, <CacheModels as CacheableModels>::StageInstance: Send + Sync, <CacheModels as CacheableModels>::Sticker: Send + Sync,

§

impl<'a, CacheModels> Sync for InMemoryCacheIter<'a, CacheModels>
where <CacheModels as CacheableModels>::CurrentUser: Send, <CacheModels as CacheableModels>::Channel: Send + Sync, <CacheModels as CacheableModels>::Guild: Send + Sync, <CacheModels as CacheableModels>::Member: Send + Sync, <CacheModels as CacheableModels>::Message: Send + Sync, <CacheModels as CacheableModels>::Presence: Send + Sync, <CacheModels as CacheableModels>::User: Send + Sync, <CacheModels as CacheableModels>::VoiceState: Send + Sync, <CacheModels as CacheableModels>::Emoji: Send + Sync, <CacheModels as CacheableModels>::GuildIntegration: Send + Sync, <CacheModels as CacheableModels>::Role: Send + Sync, <CacheModels as CacheableModels>::GuildScheduledEvent: Send + Sync, <CacheModels as CacheableModels>::StageInstance: Send + Sync, <CacheModels as CacheableModels>::Sticker: Send + Sync,

§

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

§

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

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Source§

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.