-
Notifications
You must be signed in to change notification settings - Fork 84
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
Conversation
src/body.rs
Outdated
/// req.set_body(Body::from_bytes(input, Some(len))); | ||
/// ``` | ||
pub fn from_bytes( | ||
bytes: Vec<u8>, |
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.
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?
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.
This should probably take a Vec<u8>
since it needs to keep ownership.
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.
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>, |
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.
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>, |
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.
This should probably take a Vec<u8>
since it needs to keep ownership.
I think I addressed all the comments so far and added |
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.
This is 💯, thanks so much!
This adds
Body::from_bytes
,Body.into_bytes
,Response.body_bytes
andRequest.body_bytes
to make it easier for users to work with bytes directly.cc http-rs/tide#436 (comment)