Skip to content

Commit

Permalink
Put big model type fields behind Box (serenity-rs#2205)
Browse files Browse the repository at this point in the history
This helps reduce the stack size of most types.
  • Loading branch information
kangalio authored and mkrasnitski committed Feb 28, 2023
1 parent 07509d0 commit 6a0b143
Show file tree
Hide file tree
Showing 7 changed files with 305 additions and 8 deletions.
4 changes: 4 additions & 0 deletions examples/testing/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use serenity::model::prelude::interaction::application_command::*;
use serenity::model::prelude::*;
use serenity::prelude::*;

mod model_type_sizes;

const IMAGE_URL: &str = "https://raw.githubusercontent.com/serenity-rs/serenity/current/logo.png";
const IMAGE_URL_2: &str = "https://rustacean.net/assets/rustlogo.png";

Expand Down Expand Up @@ -71,6 +73,8 @@ async fn message(ctx: &Context, msg: Message) -> Result<(), serenity::Error> {
.add_existing_attachment(msg.attachments[0].id),
)
.await?;
} else if msg.content == "ranking" {
model_type_sizes::print_ranking();
} else if msg.content == "auditlog" {
// Test special characters in audit log reason
msg.channel_id
Expand Down
293 changes: 293 additions & 0 deletions examples/testing/src/model_type_sizes.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/model/application/interaction/application_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub struct ApplicationCommandInteraction {
///
/// **Note**: It is only present if the interaction is triggered in a guild.
#[serde(skip_serializing_if = "Option::is_none")]
pub member: Option<Member>,
pub member: Option<Box<Member>>,
/// The `user` object for the invoking user.
pub user: User,
/// A continuation token for responding to the interaction.
Expand Down Expand Up @@ -247,7 +247,7 @@ impl<'de> Deserialize<'de> for ApplicationCommandInteraction {
add_guild_id_to_resolved(&mut map, guild_id);
}

let member = remove_from_map_opt::<Member, _>(&mut map, "member")?;
let member = remove_from_map_opt::<Box<Member>, _>(&mut map, "member")?;
let user = remove_from_map_opt(&mut map, "user")?
.or_else(|| member.as_ref().map(|m| m.user.clone()))
.ok_or_else(|| DeError::custom("expected user or member"))?;
Expand Down
2 changes: 1 addition & 1 deletion src/model/application/interaction/message_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub struct MessageComponentInteraction {
pub version: u8,
/// The message this interaction was triggered by, if
/// it is a component.
pub message: Message,
pub message: Box<Message>,
/// Permissions the app or bot has within the channel the interaction was sent from.
pub app_permissions: Option<Permissions>,
/// The selected language of the invoking user.
Expand Down
2 changes: 1 addition & 1 deletion src/model/application/interaction/modal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub struct ModalSubmitInteraction {
/// **Note**: Does not exist if the modal interaction originates from
/// an application command interaction
#[serde(skip_serializing_if = "Option::is_none")]
pub message: Option<Message>,
pub message: Option<Box<Message>>,
/// Permissions the app or bot has within the channel the interaction was sent from.
pub app_permissions: Option<Permissions>,
/// The selected language of the invoking user.
Expand Down
6 changes: 3 additions & 3 deletions src/model/channel/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub struct Message {
/// Sent if the message is a response to an [`Interaction`].
///
/// [`Interaction`]: crate::model::application::interaction::Interaction
pub interaction: Option<MessageInteraction>,
pub interaction: Option<Box<MessageInteraction>>,
/// The thread that was started from this message, includes thread member object.
pub thread: Option<GuildChannel>,
/// The components of this message
Expand All @@ -130,7 +130,7 @@ pub struct Message {
pub guild_id: Option<GuildId>,
/// A partial amount of data about the user's member data, if this message
/// was sent in a guild.
pub member: Option<PartialMember>,
pub member: Option<Box<PartialMember>>,
}

#[cfg(feature = "model")]
Expand Down Expand Up @@ -546,7 +546,7 @@ impl Message {
message_id: self.id,
user_id,
guild_id: self.guild_id,
member: self.member.clone(),
member: self.member.as_deref().cloned(),
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils/custom_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl CustomMessage {
/// [author]: Self::author
#[inline]
pub fn member(&mut self, member: PartialMember) -> &mut Self {
self.msg.member = Some(member);
self.msg.member = Some(Box::new(member));

self
}
Expand Down

0 comments on commit 6a0b143

Please sign in to comment.