-
-
Notifications
You must be signed in to change notification settings - Fork 132
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?
Conversation
Instead of taking `&'a [u8]` for attachments, which could cause lifetime issues when using functions like `fn create_attachment<'a>() -> Attachment<'a>`, `Cow<'a, u8>` can be used to support both creating attachments from owned and borrowed bytes. This is a followup idea to: #1886
Why the direct commit without discussing? |
I don't see why
Wanted to run CI on my diff, could and maybe should've done it on a private branch (though a single commit is easily revertable). |
Please revert that and request changes, if you want to use |
cb5703f
to
5d23287
Compare
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.
This can be slightly modified to work directly with &'a [u8]
, see the diff I sent in https://discord.com/channels/745809834183753828/1020775082123071508
I have chosen to use |
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.
Should update examples to utilize &'static [u8]
when possible
@@ -21,18 +24,18 @@ use serde::{Deserialize, Serialize}; | |||
/// .to_vec(); |
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.
/// .to_vec(); |
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
Cow::from is not smart enough for &'static [u8]
data (needed for it to work without .to_vec()
)
/// let mut attachment = Attachment::from_bytes(filename, Cow::from(file_content), id); | |
/// let mut attachment = Attachment::from_bytes(filename, Cow::Borrowed(file_content), id); |
/// 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 comment
The reason will be displayed to describe this comment to others. Learn more.
/// let file_content = b"Apples\nGrapes\nLemonade".to_vec(); | |
/// let file_content = b"Apples\nGrapes\nLemonade"; |
/// use twilight_model::http::attachment::Attachment; | ||
/// | ||
/// let filename = "grocerylist.txt".to_owned(); | ||
/// let file_content = b"Apples\nGrapes\nLemonade".to_vec(); | ||
/// 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 comment
The reason will be displayed to describe this comment to others. Learn more.
/// let attachment = Attachment::from_bytes(filename, Cow::from(file_content), id); | |
/// let attachment = Attachment::from_bytes(filename, Cow::Borrowed(file_content), id); |
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
file: "file data".as_bytes().into(), | |
file: Cow::Borrowed(b"file data"), |
Triage: deferring updating + merging to the major version after this next one, 0.16 |
Instead of taking
&'a [u8]
for attachments, which could cause lifetime issues when using functions likefn create_attachment<'a>() -> Attachment<'a>
,Cow<'a, u8>
can be used to support both creating attachments from owned and borrowed bytes.This is a followup idea to: #1886