pub struct Response<T>where
T: Serialize,{ /* private fields */ }
Implementations§
source§impl Response<()>
impl Response<()>
sourcepub fn builder() -> Builder
pub fn builder() -> Builder
Creates a new builder-style object to manufacture a Response
This method returns an instance of Builder
which can be used to
create a Response
.
§Examples
use webparse::*;
let response = Response::builder()
.status(200)
.header("X-Custom-Foo", "Bar")
.body(())
.unwrap();
pub fn text() -> Builder
pub fn json() -> Builder
pub fn status404() -> Builder
pub fn status503() -> Builder
pub fn status502() -> Builder
pub fn status500() -> Builder
source§impl<T: Serialize> Response<T>
impl<T: Serialize> Response<T>
sourcepub fn new(body: T) -> Response<T>
pub fn new(body: T) -> Response<T>
Creates a new blank Response
with the body
The component ports of this response will be set to their default, e.g. the ok status, no headers, etc.
§Examples
use webparse::*;
let response = Response::new("hello world");
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(*response.body(), "hello world");
sourcepub fn from_parts(parts: Parts, body: T) -> Response<T>
pub fn from_parts(parts: Parts, body: T) -> Response<T>
Creates a new Response
with the given head and body
§Examples
use webparse::*;
let response = Response::new("hello world");
let (mut parts, body) = response.into_parts();
parts.status = StatusCode::BAD_REQUEST;
let response = Response::from_parts(parts, body);
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_eq!(*response.body(), "hello world");
pub fn is_partial(&self) -> bool
sourcepub fn status(&self) -> StatusCode
pub fn status(&self) -> StatusCode
Returns the StatusCode
.
§Examples
use webparse::*;
let response: Response<()> = Response::default();
assert_eq!(response.status(), StatusCode::OK);
sourcepub fn status_mut(&mut self) -> &mut StatusCode
pub fn status_mut(&mut self) -> &mut StatusCode
Returns a mutable reference to the associated StatusCode
.
§Examples
use webparse::*;
let mut response: Response<()> = Response::default();
*response.status_mut() = StatusCode::CREATED;
assert_eq!(response.status(), StatusCode::CREATED);
sourcepub fn version(&self) -> Version
pub fn version(&self) -> Version
Returns a reference to the associated version.
§Examples
use webparse::*;
let response: Response<()> = Response::default();
assert_eq!(response.version(), Version::HTTP11);
sourcepub fn version_mut(&mut self) -> &mut Version
pub fn version_mut(&mut self) -> &mut Version
Returns a mutable reference to the associated version.
§Examples
use webparse::*;
let mut response: Response<()> = Response::default();
*response.version_mut() = Version::HTTP2;
assert_eq!(response.version(), Version::HTTP2);
sourcepub fn headers(&self) -> &HeaderMap
pub fn headers(&self) -> &HeaderMap
Returns a reference to the associated header field map.
§Examples
use webparse::*;
let response: Response<()> = Response::default();
assert!(response.headers().is_empty());
sourcepub fn headers_mut(&mut self) -> &mut HeaderMap
pub fn headers_mut(&mut self) -> &mut HeaderMap
Returns a mutable reference to the associated header field map.
§Examples
use webparse::*;
let mut response: Response<()> = Response::default();
response.headers_mut().insert(HeaderName::HOST, HeaderValue::from_static("world"));
assert!(!response.headers().is_empty());
sourcepub fn extensions(&self) -> &Extensions
pub fn extensions(&self) -> &Extensions
Returns a reference to the associated extensions.
§Examples
use webparse::*;
let response: Response<()> = Response::default();
assert!(response.extensions().get::<i32>().is_none());
pub fn extensions_mut(&mut self) -> &mut Extensions
sourcepub fn body(&self) -> &T
pub fn body(&self) -> &T
Returns a reference to the associated HTTP body.
§Examples
use webparse::*;
let response: Response<String> = Response::default();
assert!(response.body().is_empty());
sourcepub fn body_mut(&mut self) -> &mut T
pub fn body_mut(&mut self) -> &mut T
Returns a mutable reference to the associated HTTP body.
§Examples
use webparse::*;
let mut response: Response<String> = Response::default();
response.body_mut().push_str("hello world");
assert!(!response.body().is_empty());
pub fn headers_body(&mut self) -> (&HeaderMap, &T)
pub fn headers_body_mut(&mut self) -> (&mut HeaderMap, &mut T)
sourcepub fn into_body(self) -> T
pub fn into_body(self) -> T
Consumes the response, returning just the body.
§Examples
use webparse::Response;
let response = Response::new("hello");
let body = response.into_body();
assert_eq!(body, "hello");
sourcepub fn into_parts(self) -> (Parts, T)
pub fn into_parts(self) -> (Parts, T)
Consumes the response returning the head and body parts.
§Examples
use webparse::*;
let response: Response<()> = Response::default();
let (parts, body) = response.into_parts();
assert_eq!(parts.status, StatusCode::OK);
sourcepub fn map<F, U: Serialize>(self, f: F) -> Response<U>where
F: FnOnce(T) -> U,
pub fn map<F, U: Serialize>(self, f: F) -> Response<U>where
F: FnOnce(T) -> U,
Consumes the response returning a new response with body mapped to the return type of the passed in function.
§Examples
use webparse::*;
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());
pub fn httpdata(&mut self) -> WebResult<Vec<u8>>
pub fn into<B: Serialize>(self, body: B) -> (Response<B>, T)
pub fn into_type<B: From<T> + Serialize>(self) -> Response<B>
pub fn into_binary(self) -> Response<Binary>
sourcepub fn get_body_len(&self) -> isize
pub fn get_body_len(&self) -> isize
获取返回的body长度, 如果为0则表示未写入信息