Skip to content

Commit

Permalink
Add experimental typesize support
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev committed Nov 17, 2023
1 parent 45c4b0d commit 85fcddf
Show file tree
Hide file tree
Showing 46 changed files with 342 additions and 26 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ mime_guess = { version = "2.0.4", optional = true }
dashmap = { version = "5.5.3", features = ["serde"], optional = true }
parking_lot = { version = "0.12.1", optional = true }
ed25519-dalek = { version = "2.0.0", optional = true }
typesize = { version = "0.1.0", optional = true, features = ["url", "time", "serde_json", "secrecy", "dashmap"] }
# serde feature only allows for serialisation,
# Serenity workspace crates
command_attr = { version = "0.4.2", path = "./command_attr", optional = true }
Expand Down Expand Up @@ -110,13 +111,15 @@ voice = ["client", "model"]
# Enables unstable tokio features to give explicit names to internally spawned tokio tasks
tokio_task_builder = ["tokio/tracing"]
interactions_endpoint = ["ed25519-dalek"]
# Uses chrono for Timestamp, instead of time
chrono = ["dep:chrono", "typesize?/chrono"]

# This enables all parts of the serenity codebase
# (Note: all feature-gated APIs to be documented should have their features listed here!)
full = ["default", "collector", "unstable_discord_api", "voice", "voice_model", "interactions_endpoint"]

# Enables simd accelerated parsing.
simd_json = ["simd-json"]
simd_json = ["simd-json", "typesize?/simd_json"]

# Enables temporary caching in functions that retrieve data via the HTTP API.
temp_cache = ["cache", "mini-moka"]
Expand Down
51 changes: 51 additions & 0 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,28 @@ use wrappers::{BuildHasher, MaybeMap, ReadOnlyMapRef};

type MessageCache = DashMap<ChannelId, HashMap<MessageId, Message>, BuildHasher>;

#[non_exhaustive]
#[derive(Clone, Copy, Debug)]
pub struct CacheInfo {
/// The amount of cached values in the given Cache.
pub count: usize,
/// An estimate of the total memory usage of this Cache.
pub size: usize,
}

#[non_exhaustive]
#[derive(Clone, Copy, Debug)]
pub struct CacheStats {
pub channels: CacheInfo,
pub private_channels: CacheInfo,
pub guilds: CacheInfo,
pub unavailable_guilds: CacheInfo,
pub users: CacheInfo,
pub presences: CacheInfo,
pub messages: CacheInfo,
pub message_queue: CacheInfo,
}

struct NotSend;

enum CacheRefInner<'a, K, V> {
Expand Down Expand Up @@ -916,6 +938,35 @@ impl Cache {
e.update(self)
}

#[cfg(feature = "typesize")]
pub fn get_statistics(&self) -> CacheStats {
use typesize::TypeSize;

fn get_stat<K: Eq + Hash + TypeSize, V: TypeSize>(map: &MaybeMap<K, V>) -> CacheInfo {
CacheInfo {
count: map.len(),
size: map.get_size(),
}
}

CacheStats {
channels: get_stat(&self.channels),
private_channels: get_stat(&self.private_channels),
guilds: get_stat(&self.guilds),
unavailable_guilds: get_stat(&self.unavailable_guilds),
users: get_stat(&self.users),
presences: get_stat(&self.presences),
messages: CacheInfo {
count: self.messages.len(),
size: self.messages.get_size(),
},
message_queue: CacheInfo {
count: self.message_queue.len(),
size: self.message_queue.get_size(),
},
}
}

pub(crate) fn update_user_entry(&self, user: &User) {
if let Some(users) = &self.users.0 {
match users.entry(user.id) {
Expand Down
9 changes: 9 additions & 0 deletions src/cache/wrappers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use std::hash::Hash;
use dashmap::mapref::multiple::RefMulti;
use dashmap::mapref::one::{Ref, RefMut};
use dashmap::DashMap;
#[cfg(feature = "typesize")]
use typesize::TypeSize;

#[derive(Debug)]
/// A wrapper around Option<DashMap<K, V>> to ease disabling specific cache fields.
Expand Down Expand Up @@ -43,6 +45,13 @@ impl<K: Eq + Hash, V> MaybeMap<K, V> {
}
}

#[cfg(feature = "typesize")]
impl<K: Eq + Hash + TypeSize, V: TypeSize> TypeSize for MaybeMap<K, V> {
fn extra_size(&self) -> usize {
self.0.as_ref().map(DashMap::extra_size).unwrap_or_default()
}
}

#[derive(Clone, Copy, Debug)]
/// A wrapper around a reference to a MaybeMap, allowing for public inspection of the underlying
/// map without allowing mutation of internal cache fields, which could cause issues.
Expand Down
7 changes: 4 additions & 3 deletions src/internal/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,11 @@ macro_rules! bitflags {
)*
}
) => {
$(#[$outer])*
$vis struct $BitFlags($T);

bitflags::bitflags! {
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
$(#[$outer])*
$vis struct $BitFlags: $T {
impl $BitFlags: $T {
$(
$(#[$inner $($args)*])*
const $Flag = $value;
Expand Down
8 changes: 8 additions & 0 deletions src/model/application/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use crate::model::Permissions;
/// The base command model that belongs to an application.
///
/// [Discord docs](https://discord.com/developers/docs/interactions/application-commands#application-command-object-application-command-structure).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub struct Command {
Expand Down Expand Up @@ -222,6 +223,7 @@ enum_number! {
///
/// [Discord docs](https://discord.com/developers/docs/interactions/application-commands#application-command-object-application-command-types).
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[serde(from = "u8", into = "u8")]
#[non_exhaustive]
pub enum CommandType {
Expand All @@ -235,6 +237,7 @@ enum_number! {
/// The parameters for an [`Command`].
///
/// [Discord docs](https://discord.com/developers/docs/interactions/application-commands#application-command-object-application-command-option-structure).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub struct CommandOption {
Expand Down Expand Up @@ -296,6 +299,7 @@ enum_number! {
///
/// [Discord docs](https://discord.com/developers/docs/interactions/application-commands#application-command-object-application-command-option-type).
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[serde(from = "u8", into = "u8")]
#[non_exhaustive]
pub enum CommandOptionType {
Expand All @@ -317,6 +321,7 @@ enum_number! {
/// The only valid values a user can pick in an [`CommandOption`].
///
/// [Discord docs](https://discord.com/developers/docs/interactions/application-commands#application-command-object-application-command-option-choice-structure).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub struct CommandOptionChoice {
Expand All @@ -332,6 +337,7 @@ pub struct CommandOptionChoice {
/// An [`Command`] permission.
///
/// [Discord docs](https://discord.com/developers/docs/interactions/application-commands#application-command-permissions-object-guild-application-command-permissions-structure).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub struct CommandPermissions {
Expand All @@ -348,6 +354,7 @@ pub struct CommandPermissions {
/// The [`CommandPermission`] data.
///
/// [Discord docs](https://discord.com/developers/docs/interactions/application-commands#application-command-permissions-object-application-command-permissions-structure).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub struct CommandPermission {
Expand All @@ -365,6 +372,7 @@ enum_number! {
///
/// [Discord docs](https://discord.com/developers/docs/interactions/application-commands#application-command-permissions-object-application-command-permission-type).
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[serde(from = "u8", into = "u8")]
#[non_exhaustive]
pub enum CommandPermissionType {
Expand Down
5 changes: 5 additions & 0 deletions src/model/application/command_interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use crate::utils::{CreateQuickModal, QuickModalResponse};
/// An interaction when a user invokes a slash command.
///
/// [Discord docs](https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-object).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(remote = "Self")]
#[non_exhaustive]
Expand Down Expand Up @@ -265,6 +266,7 @@ impl Serialize for CommandInteraction {
/// The command data payload.
///
/// [Discord docs](https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-object-interaction-data-structure).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub struct CommandData {
Expand Down Expand Up @@ -476,6 +478,7 @@ pub enum ResolvedTarget<'a> {
/// [`CommandDataOption`]s.
///
/// [Discord docs](https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-object-resolved-data-structure).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[non_exhaustive]
pub struct CommandDataResolved {
Expand Down Expand Up @@ -508,6 +511,7 @@ pub struct CommandDataResolved {
/// Their resolved objects can be found on [`CommandData::resolved`].
///
/// [Discord docs](https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-object-application-command-interaction-data-option-structure).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct CommandDataOption {
Expand Down Expand Up @@ -632,6 +636,7 @@ impl Serialize for CommandDataOption {
/// The value of an [`CommandDataOption`].
///
/// [Discord docs](https://discord.com/developers/docs/interactions/application-commands#application-command-object-application-command-option-type).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum CommandDataOptionValue {
Expand Down
10 changes: 10 additions & 0 deletions src/model/application/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::model::utils::{default_true, deserialize_val};
enum_number! {
/// The type of a component
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[serde(from = "u8", into = "u8")]
#[non_exhaustive]
pub enum ComponentType {
Expand All @@ -27,6 +28,7 @@ enum_number! {
/// An action row.
///
/// [Discord docs](https://discord.com/developers/docs/interactions/message-components#action-rows).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub struct ActionRow {
Expand All @@ -41,6 +43,7 @@ pub struct ActionRow {
/// A component which can be inside of an [`ActionRow`].
///
/// [Discord docs](https://discord.com/developers/docs/interactions/message-components#component-object-component-types).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum ActionRowComponent {
Expand Down Expand Up @@ -97,6 +100,7 @@ impl From<SelectMenu> for ActionRowComponent {
}
}

#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
#[serde(untagged)]
pub enum ButtonKind {
Expand Down Expand Up @@ -142,6 +146,7 @@ impl Serialize for ButtonKind {
/// A button component.
///
/// [Discord docs](https://discord.com/developers/docs/interactions/message-components#button-object-button-structure).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
#[non_exhaustive]
pub struct Button {
Expand All @@ -165,6 +170,7 @@ pub struct Button {
enum_number! {
/// The style of a button.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[serde(from = "u8", into = "u8")]
#[non_exhaustive]
pub enum ButtonStyle {
Expand All @@ -180,6 +186,7 @@ enum_number! {
/// A select menu component.
///
/// [Discord docs](https://discord.com/developers/docs/interactions/message-components#select-menu-object-select-menu-structure).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub struct SelectMenu {
Expand Down Expand Up @@ -212,6 +219,7 @@ pub struct SelectMenu {
/// A select menu component options.
///
/// [Discord docs](https://discord.com/developers/docs/interactions/message-components#select-menu-object-select-option-structure).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub struct SelectMenuOption {
Expand All @@ -231,6 +239,7 @@ pub struct SelectMenuOption {
/// An input text component for modal interactions
///
/// [Discord docs](https://discord.com/developers/docs/interactions/message-components#text-inputs-text-input-structure).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
#[non_exhaustive]
pub struct InputText {
Expand Down Expand Up @@ -275,6 +284,7 @@ enum_number! {
///
/// [Discord docs](https://discord.com/developers/docs/interactions/message-components#text-inputs-text-input-styles).
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[serde(from = "u8", into = "u8")]
#[non_exhaustive]
pub enum InputTextStyle {
Expand Down
3 changes: 3 additions & 0 deletions src/model/application/component_interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::utils::{CreateQuickModal, QuickModalResponse};
/// An interaction triggered by a message component.
///
/// [Discord docs](https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-object-interaction-structure).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(remote = "Self")]
#[non_exhaustive]
Expand Down Expand Up @@ -240,6 +241,7 @@ impl Serialize for ComponentInteraction {
}
}

#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug)]
pub enum ComponentInteractionDataKind {
Button,
Expand Down Expand Up @@ -323,6 +325,7 @@ impl Serialize for ComponentInteractionDataKind {
/// A message component interaction data, provided by [`ComponentInteraction::data`]
///
/// [Discord docs](https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-object-message-component-data-structure).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub struct ComponentInteractionData {
Expand Down
5 changes: 4 additions & 1 deletion src/model/application/interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::model::utils::deserialize_val;
use crate::model::Permissions;

/// [Discord docs](https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-object)
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum Interaction {
Expand Down Expand Up @@ -246,6 +247,7 @@ enum_number! {
///
/// [Discord docs](https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-object-interaction-type).
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[serde(from = "u8", into = "u8")]
#[non_exhaustive]
pub enum InteractionType {
Expand All @@ -263,7 +265,7 @@ bitflags! {
///
/// [Discord docs](https://discord.com/developers/docs/resources/channel#message-object-message-flags)
/// ([only some are valid in this context](https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-response-object-messages))
#[derive(Default)]
#[derive(Copy, Clone, Default, Debug, Eq, Hash, PartialEq)]
pub struct InteractionResponseFlags: u64 {
/// Do not include any embeds when serializing this message.
const SUPPRESS_EMBEDS = 1 << 2;
Expand All @@ -278,6 +280,7 @@ bitflags! {
/// [`Message`]: crate::model::channel::Message
///
/// [Discord docs](https://discord.com/developers/docs/interactions/receiving-and-responding#message-interaction-object).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub struct MessageInteraction {
Expand Down
4 changes: 3 additions & 1 deletion src/model/application/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use super::Permissions;
/// Partial information about the given application.
///
/// Discord docs: [application field of Ready](https://discord.com/developers/docs/topics/gateway-events#ready-ready-event-fields)
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub struct PartialCurrentApplicationInfo {
Expand Down Expand Up @@ -129,7 +130,8 @@ bitflags! {
/// The flags of the application.
///
/// [Discord docs](https://discord.com/developers/docs/resources/application#application-object-application-flags).
#[derive(Default)]
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Copy, Clone, Default, Debug, Eq, Hash, PartialEq)]
pub struct ApplicationFlags: u64 {
/// Indicates if an app uses the Auto Moderation API
const APPLICATION_AUTO_MODERATION_RULE_CREATE_BADGE = 1 << 6;
Expand Down
Loading

0 comments on commit 85fcddf

Please sign in to comment.