Try to minimize cloning

This commit is contained in:
jairinhohw 2020-03-25 04:26:17 -03:00
parent cb68f1f7aa
commit 178b4ee70d
3 changed files with 11 additions and 8 deletions

View file

@ -57,7 +57,7 @@ impl FormBuilder {
self,
name: N,
file_name: String,
data: Vec<u8>,
data: Cow<'static, [u8]>,
) -> Self
where
N: Into<Cow<'a, str>>,
@ -77,7 +77,7 @@ impl FormBuilder {
pub(crate) enum FormValue {
File(PathBuf),
Memory { file_name: String, data: Vec<u8> },
Memory { file_name: String, data: Cow<'static, [u8]> },
Str(String),
}
@ -176,7 +176,7 @@ impl IntoFormValue for InputFile {
InputFile::File(path) => Some(FormValue::File(path.clone())),
InputFile::Memory { file_name, data } => Some(FormValue::Memory {
file_name: file_name.clone(),
data: data.clone(),
data: data.clone().into(),
}),
InputFile::Url(url) => Some(FormValue::Str(url.clone())),
InputFile::FileId(file_id) => Some(FormValue::Str(file_id.clone())),

View file

@ -1,4 +1,4 @@
use std::path::PathBuf;
use std::{borrow::Cow, path::PathBuf};
use bytes::{Bytes, BytesMut};
use reqwest::{multipart::Part, Body};
@ -35,6 +35,9 @@ pub async fn file_to_part(path_to_file: PathBuf) -> Part {
Part::stream(Body::wrap_stream(file)).file_name(file_name)
}
pub fn file_from_memory_to_part(data: Vec<u8>, name: String) -> Part {
pub fn file_from_memory_to_part(
data: Cow<'static, [u8]>,
name: String,
) -> Part {
Part::bytes(data).file_name(name)
}

View file

@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use std::{borrow::Cow, path::PathBuf};
/// This object represents the contents of a file to be uploaded.
///
@ -8,7 +8,7 @@ use std::path::PathBuf;
#[derive(Clone, Debug, Eq, Hash, PartialEq, Deserialize)]
pub enum InputFile {
File(PathBuf),
Memory { file_name: String, data: Vec<u8> },
Memory { file_name: String, data: Cow<'static, [u8]> },
Url(String),
FileId(String),
}
@ -24,7 +24,7 @@ impl InputFile {
pub fn memory<S, D>(file_name: S, data: D) -> Self
where
S: Into<String>,
D: Into<Vec<u8>>,
D: Into<Cow<'static, [u8]>>,
{
Self::Memory { file_name: file_name.into(), data: data.into() }
}