Skip to main content

PeerCache

Struct PeerCache 

Source
pub struct PeerCache {
    pub users: HashMap<i64, i64>,
    pub channels: HashMap<i64, i64>,
    pub chats: HashSet<i64>,
    pub channels_min: HashSet<i64>,
    pub min_contexts: HashMap<i64, (i64, i32)>,
    pub username_to_peer: HashMap<String, (i64, PeerType)>,
    pub phone_to_user: HashMap<String, i64>,
    /* private fields */
}
Expand description

All fields are pub so that save_session / connect can read/write them directly, and so that advanced callers can inspect the cache.

Fields§

§users: HashMap<i64, i64>

user_id -> access_hash (full users only, min=false)

§channels: HashMap<i64, i64>

channel_id -> access_hash (full channels only, min=false)

§chats: HashSet<i64>

Regular group chat IDs (Chat::Chat / ChatForbidden). Groups need no access_hash; track existence for peer validation.

§channels_min: HashSet<i64>

Channel IDs seen with min=true. These are real channels but have no valid access_hash. Stored separately so they are NEVER confused with regular groups. DO NOT put min channels in chats. A min channel must never become InputPeerChat - that causes fatal RPC failures.

§min_contexts: HashMap<i64, (i64, i32)>

user_id -> (peer_id, msg_id) for min users seen in a message context. Min users have an invalid access_hash; they must be referenced via InputPeerUserFromMessage using the peer and message where they appeared.

§username_to_peer: HashMap<String, (i64, PeerType)>

Reverse index: lowercase username → (id, PeerType). Populated by cache_user / cache_chat; always overwritten on update (usernames can change).

§phone_to_user: HashMap<String, i64>

Reverse index: E.164 phone → user_id.

Implementations§

Source§

impl PeerCache

Source

pub fn new(experimental: ExperimentalFeatures) -> Self

Create a new empty cache with the given experimental-feature flags.

Source

pub fn cache_user(&mut self, user: &User)

Source

pub fn cache_user_with_context( &mut self, user: &User, peer_id: i64, msg_id: i32, )

Cache a user that arrived in a message context.

For min users (access_hash is invalid), stores the peer+msg context so they can later be referenced via InputPeerUserFromMessage.

Uses latest-wins semantics: a newer message context replaces the stored one. Recent messages are less likely to have been deleted.

Source

pub fn cache_chat(&mut self, chat: &Chat)

Source

pub fn cache_users(&mut self, users: &[User])

Source

pub fn cache_chats(&mut self, chats: &[Chat])

Source

pub fn cache_input_peer(&mut self, ip: &InputPeer)

Store an already-resolved InputPeer’s access hash into the cache.

Called when a caller provides a PeerRef::Input so that the subsequent peer_to_input lookup succeeds without an RPC.

Source

pub fn invalidate_peer(&mut self, peer: &Peer)

Remove stale cache entries when Telegram rejects them with PEER_ID_INVALID, CHANNEL_INVALID, USER_ID_INVALID, or CHANNEL_PRIVATE. The caller should then retry the operation.

Source

pub fn peer_to_input(&self, peer: &Peer) -> Result<InputPeer, InvocationError>

Trait Implementations§

Source§

impl Default for PeerCache

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more