Skip to content

Commit 9cc8253

Browse files
committed
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`.
1 parent 4b19a5f commit 9cc8253

File tree

3 files changed

+6
-10
lines changed

3 files changed

+6
-10
lines changed

Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ strum = { version = "0.26", features = ["derive"] }
3838
to-arraystring = "0.2.0"
3939
extract_map = { version = "0.1.0", features = ["serde", "iter_mut"] }
4040
aformat = "0.1.3"
41+
bytes = "1.5.0"
4142
# Optional dependencies
4243
fxhash = { version = "0.2.1", optional = true }
4344
chrono = { version = "0.4.31", default-features = false, features = ["clock", "serde"], optional = true }
4445
flate2 = { version = "1.0.28", optional = true }
4546
reqwest = { version = "0.12.2", default-features = false, features = ["multipart", "stream", "json"], optional = true }
4647
tokio-tungstenite = { version = "0.21.0", optional = true }
47-
bytes = { version = "1.5.0", optional = true }
4848
percent-encoding = { version = "2.3.0", optional = true }
4949
mini-moka = { version = "0.10.2", optional = true }
5050
mime_guess = { version = "2.0.4", optional = true }
@@ -124,14 +124,12 @@ tracing_instrument = ["tracing/attributes"]
124124
rustls_backend = [
125125
"reqwest/rustls-tls",
126126
"tokio-tungstenite/rustls-tls-webpki-roots",
127-
"bytes",
128127
]
129128

130129
# - Native TLS Backends
131130
native_tls_backend = [
132131
"reqwest/native-tls",
133132
"tokio-tungstenite/native-tls",
134-
"bytes",
135133
]
136134

137135

src/builder/create_attachment.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::borrow::Cow;
22
use std::path::Path;
33

4+
use bytes::Bytes;
45
use serde::ser::{Serialize, SerializeSeq, Serializer};
56
use tokio::fs::File;
67
use tokio::io::AsyncReadExt;
@@ -21,15 +22,12 @@ use crate::model::id::AttachmentId;
2122
pub struct CreateAttachment<'a> {
2223
pub filename: Cow<'static, str>,
2324
pub description: Option<Cow<'a, str>>,
24-
pub data: Cow<'static, [u8]>,
25+
pub data: Bytes,
2526
}
2627

2728
impl<'a> CreateAttachment<'a> {
2829
/// Builds an [`CreateAttachment`] from the raw attachment data.
29-
pub fn bytes(
30-
data: impl Into<Cow<'static, [u8]>>,
31-
filename: impl Into<Cow<'static, str>>,
32-
) -> Self {
30+
pub fn bytes(data: impl Into<Bytes>, filename: impl Into<Cow<'static, str>>) -> Self {
3331
CreateAttachment {
3432
data: data.into(),
3533
filename: filename.into(),
@@ -85,7 +83,7 @@ impl<'a> CreateAttachment<'a> {
8583
filename: impl Into<Cow<'static, str>>,
8684
) -> Result<Self> {
8785
let response = http.client.get(url).send().await?;
88-
let data = response.bytes().await?.to_vec();
86+
let data = response.bytes().await?;
8987

9088
Ok(CreateAttachment::bytes(data, filename))
9189
}

src/http/multipart.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::internal::prelude::*;
77

88
impl CreateAttachment<'_> {
99
fn into_part(self) -> Result<Part> {
10-
let mut part = Part::bytes(self.data);
10+
let mut part = Part::stream(self.data);
1111
part = guess_mime_str(part, &self.filename)?;
1212
part = part.file_name(self.filename);
1313
Ok(part)

0 commit comments

Comments
 (0)