Skip to content
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

Add conversions to and from bytes #100

Merged
merged 1 commit into from Apr 24, 2020
Merged

Conversation

phansch
Copy link
Contributor

@phansch phansch commented Apr 21, 2020

This adds Body::from_bytes, Body.into_bytes, Response.body_bytes and Request.body_bytes to make it easier for users to work with bytes directly.

cc http-rs/tide#436 (comment)

src/body.rs Outdated
/// req.set_body(Body::from_bytes(input, Some(len)));
/// ```
pub fn from_bytes(
bytes: Vec<u8>,
Copy link
Contributor Author

@phansch phansch Apr 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So.. I guess ideally this would be &[u8]? However, that conflicts with the 'static requirement of reader: Box<dyn BufRead + Unpin + Send + Sync + 'static>. I'm not really why Vec doesn't conflict with that requirement either?

Copy link
Member

@yoshuawuyts yoshuawuyts Apr 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably take a Vec<u8> since it needs to keep ownership.

Copy link
Member

@yoshuawuyts yoshuawuyts left a comment

Thanks for putting in the work @phansch! This is looking good overall; left a few notes.

It'd be fantastic if we could also perhaps implement an into_bytes method and expose the corresponding {Request,Response}::body_bytes methods. That way we'd have a bytes counterpart to the json API proposed in #102. But that's not a blocker for merging this (:

src/body.rs Outdated
/// ```
pub fn from_bytes(
bytes: Vec<u8>,
len: Option<usize>,
Copy link
Member

@yoshuawuyts yoshuawuyts Apr 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can infer the length of the body from the length of the vec.

src/body.rs Outdated
/// req.set_body(Body::from_bytes(input, Some(len)));
/// ```
pub fn from_bytes(
bytes: Vec<u8>,
Copy link
Member

@yoshuawuyts yoshuawuyts Apr 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably take a Vec<u8> since it needs to keep ownership.

@phansch phansch changed the title Add Body::from_bytes method Add conversions to and from bytes Apr 22, 2020
@phansch phansch marked this pull request as ready for review Apr 22, 2020
@phansch
Copy link
Contributor Author

@phansch phansch commented Apr 22, 2020

I think I addressed all the comments so far and added Body.into_bytes, Request.body_bytes and Response.body_bytes.

Copy link
Member

@yoshuawuyts yoshuawuyts left a comment

This is 💯, thanks so much!

@yoshuawuyts yoshuawuyts merged commit b9129e4 into http-rs:master Apr 24, 2020
3 checks passed
@phansch phansch deleted the from-bytes branch Apr 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants