Skip to content
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

Open
wants to merge 3 commits into
base: old-next
Choose a base branch
from

Conversation

itohatweb
Copy link
Member

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

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
@github-actions github-actions bot added c-http Affects the http crate c-model Affects the model crate c-util Affects the util crate c-validate Affects the validate crate m-breaking change Breaks the public API. t-feature Addition of a new feature labels Sep 17, 2022
@itohatweb
Copy link
Member Author

Why the direct commit without discussing?

@vilgotf
Copy link
Member

vilgotf commented Sep 22, 2022

I don't see why Cow is necessary?

Why the direct commit without discussing?

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).

@itohatweb
Copy link
Member Author

Please revert that and request changes, if you want to use &[U8] instead.

@vilgotf vilgotf force-pushed the itohatweb/feat/attachment-cow branch from cb5703f to 5d23287 Compare September 22, 2022 15:52
Copy link
Member

@vilgotf vilgotf left a 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

@itohatweb
Copy link
Member Author

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 Cow since users might want to create attachments in a separate function. Using &[u8] would break this functionality.
Reference: https://discord.com/channels/745809834183753828/1020775082123071508/1022591932163162112

Copy link
Member

@vilgotf vilgotf left a 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();
Copy link
Member

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.

Suggested change
/// .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);
Copy link
Member

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())

Suggested change
/// 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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// 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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// 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(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
file: "file data".as_bytes().into(),
file: Cow::Borrowed(b"file data"),

@zeylahellyer
Copy link
Member

zeylahellyer commented Feb 4, 2023

Triage: deferring updating + merging to the major version after this next one, 0.16

@zeylahellyer zeylahellyer added the w-do-not-merge PR is blocked or deferred label Feb 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c-http Affects the http crate c-model Affects the model crate c-util Affects the util crate c-validate Affects the validate crate m-breaking change Breaks the public API. t-feature Addition of a new feature w-do-not-merge PR is blocked or deferred
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants