Skip to content

Commit

Permalink
Swap CreateAttachment::data to Bytes (serenity-rs#3016)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
GnomedDev authored and mkrasnitski committed Dec 8, 2024
1 parent 0969988 commit 19c89d1
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 10 deletions.
4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,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 }
Expand Down Expand Up @@ -134,14 +134,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",
]


Expand Down
10 changes: 4 additions & 6 deletions src/builder/create_attachment.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -21,15 +22,12 @@ use crate::model::id::AttachmentId;
pub struct CreateAttachment<'a> {
pub filename: Cow<'static, str>,
pub description: Option<Cow<'a, str>>,
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<Cow<'static, [u8]>>,
filename: impl Into<Cow<'static, str>>,
) -> Self {
pub fn bytes(data: impl Into<Bytes>, filename: impl Into<Cow<'static, str>>) -> Self {
CreateAttachment {
data: data.into(),
filename: filename.into(),
Expand Down Expand Up @@ -85,7 +83,7 @@ impl<'a> CreateAttachment<'a> {
filename: impl Into<Cow<'static, str>>,
) -> Result<Self> {
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))
}
Expand Down
2 changes: 1 addition & 1 deletion src/http/multipart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::internal::prelude::*;

impl CreateAttachment<'_> {
fn into_part(self) -> Result<Part> {
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)
Expand Down

0 comments on commit 19c89d1

Please sign in to comment.