mirror of
https://github.com/openharmony/third_party_rust_http.git
synced 2026-07-19 14:43:33 -04:00
Add a map to Request and Response (#151)
For ease of use, it's nice to be able to map using a closure from one type to another. This adds that ability to both the `Request` and `Response` objects.
This commit is contained in:
committed by
Carl Lerche
parent
20709808a4
commit
5f02b4d21b
@@ -638,6 +638,27 @@ impl<T> Request<T> {
|
||||
pub fn into_parts(self) -> (Parts, T) {
|
||||
(self.head, self.body)
|
||||
}
|
||||
|
||||
/// Consumes the request returning a new request with body mapped to the
|
||||
/// return type of the passed in function.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// # use http::*;
|
||||
/// let request = Request::builder().body("some string").unwrap();
|
||||
/// let mapped_request: Request<&[u8]> = request.map(|b| {
|
||||
/// assert_eq!(b, "some string");
|
||||
/// b.as_bytes()
|
||||
/// });
|
||||
/// assert_eq!(mapped_request.body(), &"some string".as_bytes());
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn map<F, U>(self, f: F) -> Request<U>
|
||||
where F: FnOnce(T) -> U
|
||||
{
|
||||
Request { body: f(self.body), head: self.head }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Default> Default for Request<T> {
|
||||
@@ -906,3 +927,18 @@ impl Default for Builder {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn it_can_map_a_body_from_one_type_to_another() {
|
||||
let request= Request::builder().body("some string").unwrap();
|
||||
let mapped_request = request.map(|s| {
|
||||
assert_eq!(s, "some string");
|
||||
123u32
|
||||
});
|
||||
assert_eq!(mapped_request.body(), &123u32);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -439,6 +439,27 @@ impl<T> Response<T> {
|
||||
pub fn into_parts(self) -> (Parts, T) {
|
||||
(self.head, self.body)
|
||||
}
|
||||
|
||||
/// Consumes the response returning a new response with body mapped to the
|
||||
/// return type of the passed in function.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// # use http::*;
|
||||
/// let response = Response::builder().body("some string").unwrap();
|
||||
/// let mapped_response: Response<&[u8]> = response.map(|b| {
|
||||
/// assert_eq!(b, "some string");
|
||||
/// b.as_bytes()
|
||||
/// });
|
||||
/// assert_eq!(mapped_response.body(), &"some string".as_bytes());
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn map<F, U>(self, f: F) -> Response<U>
|
||||
where F: FnOnce(T) -> U
|
||||
{
|
||||
Response { body: f(self.body), head: self.head }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Default> Default for Response<T> {
|
||||
@@ -676,3 +697,18 @@ impl Default for Builder {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn it_can_map_a_body_from_one_type_to_another() {
|
||||
let response = Response::builder().body("some string").unwrap();
|
||||
let mapped_response = response.map(|s| {
|
||||
assert_eq!(s, "some string");
|
||||
123u32
|
||||
});
|
||||
assert_eq!(mapped_response.body(), &123u32);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user