feat(http,model,util,validate)!: use Cow<'a, [u8]> for attachments#1923
feat(http,model,util,validate)!: use Cow<'a, [u8]> for attachments#1923
Cow<'a, [u8]> for attachments#1923Conversation
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
vilgotf
left a comment
There was a problem hiding this comment.
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 |
vilgotf
left a comment
There was a problem hiding this comment.
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.
Shouldn't encourage needless allocations.
| /// .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.
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); |
| /// 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.
| /// let file_content = b"Apples\nGrapes\nLemonade".to_vec(); | |
| /// let file_content = b"Apples\nGrapes\nLemonade"; |
| /// 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.
| /// let attachment = Attachment::from_bytes(filename, Cow::from(file_content), id); | |
| /// let attachment = Attachment::from_bytes(filename, Cow::Borrowed(file_content), id); |
| attachments: Some(Vec::from([Attachment { | ||
| description: None, | ||
| file: "file data".into(), | ||
| file: "file data".as_bytes().into(), |
There was a problem hiding this comment.
| 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