@@ -5,98 +5,10 @@ use super::Cache;
55/// This may be implemented on a type and used to update the cache via [`Cache::update`].
66///
77/// **Info**: You may not access the fields of the cache, as they are public for the crate only.
8- ///
9- /// # Examples
10- ///
11- /// Creating a custom struct implementation to update the cache with:
12- ///
13- /// ```rust,ignore
14- /// use std::collections::hash_map::Entry;
15- ///
16- /// use serde_json::json;
17- /// use serenity::cache::{Cache, CacheUpdate};
18- /// use serenity::model::id::UserId;
19- /// use serenity::model::user::User;
20- ///
21- /// // For example, an update to the user's record in the database was
22- /// // published to a pubsub channel.
23- /// struct DatabaseUserUpdate {
24- /// user_avatar: Option<String>,
25- /// user_discriminator: u16,
26- /// user_id: UserId,
27- /// user_is_bot: bool,
28- /// user_name: String,
29- /// }
30- ///
31- /// #[serenity::async_trait]
32- /// impl CacheUpdate for DatabaseUserUpdate {
33- /// // A copy of the old user's data, if it existed in the cache.
34- /// type Output = User;
35- ///
36- /// async fn update(&mut self, cache: &Cache) -> Option<Self::Output> {
37- /// // If an entry for the user already exists, update its fields.
38- /// match cache.users.entry(self.user_id) {
39- /// Entry::Occupied(entry) => {
40- /// let user = entry.get();
41- /// let old_user = user.clone();
42- ///
43- /// user.bot = self.user_is_bot;
44- /// user.discriminator = self.user_discriminator;
45- /// user.id = self.user_id;
46- ///
47- /// if user.avatar != self.user_avatar {
48- /// user.avatar = self.user_avatar.clone();
49- /// }
50- ///
51- /// if user.name != self.user_name {
52- /// user.name = self.user_name.clone();
53- /// }
54- ///
55- /// // Return the old copy for the user's sake.
56- /// Some(old_user)
57- /// },
58- /// Entry::Vacant(entry) => {
59- /// // We can convert a [`Value`] to a User for test
60- /// // purposes.
61- /// let user = from_value::<User>(json!({
62- /// "id": self.user_id,
63- /// "avatar": self.user_avatar.clone(),
64- /// "bot": self.user_is_bot,
65- /// "discriminator": self.user_discriminator,
66- /// "username": self.user_name.clone(),
67- /// })).expect("Error making user");
68- ///
69- /// entry.insert(user);
70- ///
71- /// // There was no old copy, so return None.
72- /// None
73- /// },
74- /// }
75- /// }
76- /// }
77- ///
78- /// # async fn run() {
79- /// // Create an instance of the cache.
80- /// let mut cache = Cache::new();
81- ///
82- /// // This is a sample pubsub message that you might receive from your
83- /// // database.
84- /// let mut update_message = DatabaseUserUpdate {
85- /// user_avatar: None,
86- /// user_discriminator: 6082,
87- /// user_id: UserId::new(379740138303127564),
88- /// user_is_bot: true,
89- /// user_name: "TofuBot".to_owned(),
90- /// };
91- ///
92- /// // Update the cache with the message.
93- /// cache.update(&mut update_message).await;
94- /// # }
95- /// ```
968pub trait CacheUpdate {
979 /// The return type of an update.
9810 ///
99- /// If there is nothing to return, specify this type as an unit ( `()`) .
11+ /// If there is nothing to return, specify this type to be the unit `()`.
10012 type Output ;
10113
10214 /// Updates the cache with the implementation.
0 commit comments