From 47d2e1a26b1c691dc12bb78c054c3c602464da60 Mon Sep 17 00:00:00 2001 From: Gnome! Date: Mon, 11 Nov 2024 21:38:19 +0000 Subject: [PATCH] Swap CreateAttachment::data to Bytes (#3016) This exposes the `bytes` dependency but in turn allows for people to pass responses directly from reqwest to discord without cloning the data, as well as allowing reuploading cached data without cloning or leaking to get a static reference. The bytes dependency also has to be made non-optional, but this is fine as it was already due to `aformat` depending on it via `bytestring`. --- Cargo.toml | 4 +--- src/builder/create_attachment.rs | 10 ++++------ src/http/multipart.rs | 2 +- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a79d2d4ee07..348ced6dbd8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,13 +38,13 @@ strum = { version = "0.26", features = ["derive"] } to-arraystring = "0.2.0" extract_map = { version = "0.1.0", features = ["serde", "iter_mut"] } aformat = "0.1.3" +bytes = "1.5.0" # Optional dependencies fxhash = { version = "0.2.1", optional = true } chrono = { version = "0.4.31", default-features = false, features = ["clock", "serde"], optional = true } flate2 = { version = "1.0.28", optional = true } reqwest = { version = "0.12.2", default-features = false, features = ["multipart", "stream", "json"], optional = true } tokio-tungstenite = { version = "0.21.0", optional = true } -bytes = { version = "1.5.0", optional = true } percent-encoding = { version = "2.3.0", optional = true } mini-moka = { version = "0.10.2", optional = true } mime_guess = { version = "2.0.4", optional = true } @@ -124,14 +124,12 @@ tracing_instrument = ["tracing/attributes"] rustls_backend = [ "reqwest/rustls-tls", "tokio-tungstenite/rustls-tls-webpki-roots", - "bytes", ] # - Native TLS Backends native_tls_backend = [ "reqwest/native-tls", "tokio-tungstenite/native-tls", - "bytes", ] diff --git a/src/builder/create_attachment.rs b/src/builder/create_attachment.rs index 180a7a43ae5..60b57d092ed 100644 --- a/src/builder/create_attachment.rs +++ b/src/builder/create_attachment.rs @@ -1,6 +1,7 @@ use std::borrow::Cow; use std::path::Path; +use bytes::Bytes; use serde::ser::{Serialize, SerializeSeq, Serializer}; use tokio::fs::File; use tokio::io::AsyncReadExt; @@ -21,15 +22,12 @@ use crate::model::id::AttachmentId; pub struct CreateAttachment<'a> { pub filename: Cow<'static, str>, pub description: Option>, - pub data: Cow<'static, [u8]>, + pub data: Bytes, } impl<'a> CreateAttachment<'a> { /// Builds an [`CreateAttachment`] from the raw attachment data. - pub fn bytes( - data: impl Into>, - filename: impl Into>, - ) -> Self { + pub fn bytes(data: impl Into, filename: impl Into>) -> Self { CreateAttachment { data: data.into(), filename: filename.into(), @@ -85,7 +83,7 @@ impl<'a> CreateAttachment<'a> { filename: impl Into>, ) -> Result { let response = http.client.get(url).send().await?; - let data = response.bytes().await?.to_vec(); + let data = response.bytes().await?; Ok(CreateAttachment::bytes(data, filename)) } diff --git a/src/http/multipart.rs b/src/http/multipart.rs index 81b32c9c605..58d2f4a77b6 100644 --- a/src/http/multipart.rs +++ b/src/http/multipart.rs @@ -7,7 +7,7 @@ use crate::internal::prelude::*; impl CreateAttachment<'_> { fn into_part(self) -> Result { - let mut part = Part::bytes(self.data); + let mut part = Part::stream(self.data); part = guess_mime_str(part, &self.filename)?; part = part.file_name(self.filename); Ok(part)