-
Notifications
You must be signed in to change notification settings - Fork 315
own request Bytes & to_json #145
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
Changes from all commits
bcfa052
9b6eaa8
5c2b422
3320d61
ea1959b
c99bbc0
70ffb7d
6077bfd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ use crate::prelude::*; | |
use crate::responses::CreateSlugAttachmentResponse; | ||
use azure_core::prelude::*; | ||
use azure_core::{No, ToAssign, Yes}; | ||
use bytes::Bytes; | ||
use http::StatusCode; | ||
use std::convert::TryInto; | ||
use std::marker::PhantomData; | ||
|
@@ -13,7 +14,7 @@ where | |
ContentTypeSet: ToAssign, | ||
{ | ||
attachment_client: &'a AttachmentClient, | ||
body: Option<&'b [u8]>, | ||
body: Option<Bytes>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure about this change. I don't really know how CreateSlugAttachmentBuilder is used. If It is reused, this should be a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to the docs (https://docs.rs/bytes/1.0.1/bytes/) |
||
content_type: Option<ContentType<'b>>, | ||
if_match_condition: Option<IfMatchCondition<'b>>, | ||
user_agent: Option<UserAgent<'b>>, | ||
|
@@ -56,9 +57,12 @@ impl<'a, 'b, ContentTypeSet> CreateSlugAttachmentBuilder<'a, 'b, No, ContentType | |
where | ||
ContentTypeSet: ToAssign, | ||
{ | ||
pub fn body(self, body: &'b [u8]) -> CreateSlugAttachmentBuilder<'a, 'b, Yes, ContentTypeSet> { | ||
pub fn body( | ||
self, | ||
body: impl Into<Bytes>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rylev, like this with |
||
) -> CreateSlugAttachmentBuilder<'a, 'b, Yes, ContentTypeSet> { | ||
CreateSlugAttachmentBuilder { | ||
body: Some(body), | ||
body: Some(body.into()), | ||
attachment_client: self.attachment_client, | ||
content_type: self.content_type, | ||
if_match_condition: self.if_match_condition, | ||
|
@@ -111,9 +115,10 @@ impl<'a, 'b> CreateSlugAttachmentBuilder<'a, 'b, Yes, Yes> { | |
req = azure_core::headers::add_mandatory_header(&self.content_type.unwrap(), req); | ||
|
||
req = req.header("Slug", self.attachment_client.attachment_name()); | ||
req = req.header(http::header::CONTENT_LENGTH, self.body.unwrap().len()); | ||
let body = self.body.clone().unwrap(); | ||
req = req.header(http::header::CONTENT_LENGTH, body.len()); | ||
|
||
let req = req.body(self.body.unwrap())?; | ||
let req = req.body(body)?; | ||
|
||
debug!("req == {:#?}", req); | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem here is that byte array reference gets cloned with
to_vec
& then converts it to a string, only to have the string converted to bytes later. It is more efficient to own the bytes and send them without the conversions.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes you are right, the string conversion is not really necessary.
Also the idea to use ref counting here (by using
bytes
) is really good. 👍 👍