-
-
Notifications
You must be signed in to change notification settings - Fork 136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(http,model,util,validate)!: use Cow<'a, [u8]>
for attachments
#1923
base: old-next
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,5 +1,7 @@ | ||||||
//! Models used when sending attachments to Discord. | ||||||
|
||||||
use std::borrow::Cow; | ||||||
|
||||||
use serde::{Deserialize, Serialize}; | ||||||
|
||||||
/// Attachments used in messages. | ||||||
|
@@ -10,6 +12,7 @@ use serde::{Deserialize, Serialize}; | |||||
/// description for screen readers: | ||||||
/// | ||||||
/// ``` | ||||||
/// use std::borrow::Cow; | ||||||
/// use twilight_model::http::attachment::Attachment; | ||||||
/// | ||||||
/// let filename = "twilight_sparkle.json".to_owned(); | ||||||
|
@@ -21,18 +24,18 @@ use serde::{Deserialize, Serialize}; | |||||
/// .to_vec(); | ||||||
/// let id = 1; | ||||||
/// | ||||||
/// let mut attachment = Attachment::from_bytes(filename, file_content, id); | ||||||
/// let mut attachment = Attachment::from_bytes(filename, Cow::from(file_content), id); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cow::from is not smart enough for
Suggested change
|
||||||
/// attachment.description("Raw data about Twilight Sparkle".to_owned()); | ||||||
/// ``` | ||||||
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)] | ||||||
pub struct Attachment { | ||||||
pub struct Attachment<'a> { | ||||||
/// Description of the attachment, useful for screen readers and users | ||||||
/// requiring alt text. | ||||||
#[serde(skip_serializing_if = "Option::is_none")] | ||||||
pub description: Option<String>, | ||||||
/// Content of the file. | ||||||
#[serde(skip)] | ||||||
pub file: Vec<u8>, | ||||||
pub file: Cow<'a, [u8]>, | ||||||
/// Name of the file. | ||||||
/// | ||||||
/// Examples may be "twilight_sparkle.png", "cat.jpg", or "logs.txt". | ||||||
|
@@ -46,23 +49,24 @@ pub struct Attachment { | |||||
pub id: u64, | ||||||
} | ||||||
|
||||||
impl Attachment { | ||||||
impl<'a> Attachment<'a> { | ||||||
/// Create an attachment from a filename and bytes. | ||||||
/// | ||||||
/// # Examples | ||||||
/// | ||||||
/// Create an attachment with a grocery list named "grocerylist.txt": | ||||||
/// | ||||||
/// ``` | ||||||
/// use std::borrow::Cow; | ||||||
/// use twilight_model::http::attachment::Attachment; | ||||||
/// | ||||||
/// let filename = "grocerylist.txt".to_owned(); | ||||||
/// let file_content = b"Apples\nGrapes\nLemonade".to_vec(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
/// let id = 1; | ||||||
/// | ||||||
/// let attachment = Attachment::from_bytes(filename, file_content, id); | ||||||
/// let attachment = Attachment::from_bytes(filename, Cow::from(file_content), id); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
/// ``` | ||||||
pub const fn from_bytes(filename: String, file: Vec<u8>, id: u64) -> Self { | ||||||
pub const fn from_bytes(filename: String, file: Cow<'a, [u8]>, id: u64) -> Self { | ||||||
Self { | ||||||
description: None, | ||||||
file, | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -14,7 +14,7 @@ use serde_repr::{Deserialize_repr, Serialize_repr}; | |||||
/// | ||||||
/// [Discord Docs/Interaction Object]: https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-object-interaction-structure | ||||||
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] | ||||||
pub struct InteractionResponse { | ||||||
pub struct InteractionResponse<'a> { | ||||||
/// Type of the response. | ||||||
#[serde(rename = "type")] | ||||||
pub kind: InteractionResponseType, | ||||||
|
@@ -31,18 +31,18 @@ pub struct InteractionResponse { | |||||
/// [`Modal`]: InteractionResponseType::Modal | ||||||
/// [`UpdateMessage`]: InteractionResponseType::UpdateMessage | ||||||
#[serde(skip_serializing_if = "Option::is_none")] | ||||||
pub data: Option<InteractionResponseData>, | ||||||
pub data: Option<InteractionResponseData<'a>>, | ||||||
} | ||||||
|
||||||
/// Data included in an interaction response. | ||||||
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)] | ||||||
pub struct InteractionResponseData { | ||||||
pub struct InteractionResponseData<'a> { | ||||||
/// Allowed mentions of the response. | ||||||
#[serde(skip_serializing_if = "Option::is_none")] | ||||||
pub allowed_mentions: Option<AllowedMentions>, | ||||||
/// List of attachments on the response. | ||||||
#[serde(skip_serializing_if = "Option::is_none")] | ||||||
pub attachments: Option<Vec<Attachment>>, | ||||||
pub attachments: Option<Vec<Attachment<'a>>>, | ||||||
/// List of autocomplete alternatives. | ||||||
/// | ||||||
/// Can only be used with | ||||||
|
@@ -126,7 +126,7 @@ mod tests { | |||||
tts | ||||||
); | ||||||
assert_impl_all!( | ||||||
InteractionResponseData: Clone, | ||||||
InteractionResponseData<'_>: Clone, | ||||||
Debug, | ||||||
Deserialize<'static>, | ||||||
PartialEq, | ||||||
|
@@ -187,7 +187,7 @@ mod tests { | |||||
data: Some(InteractionResponseData { | ||||||
attachments: Some(Vec::from([Attachment { | ||||||
description: None, | ||||||
file: "file data".into(), | ||||||
file: "file data".as_bytes().into(), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
filename: "filename.jpg".into(), | ||||||
id: 1, | ||||||
}])), | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't encourage needless allocations.