pub struct Response<T> { /* private fields */ }
Expand description
一个HTTP响应。
HTTP响应由头部和可选的正文组成。正文是泛型的,允许任意类型来表示HTTP响应的正文。
Implementations§
Source§impl<T> Response<T>
impl<T> Response<T>
Sourcepub fn new(body: T) -> Response<T>
pub fn new(body: T) -> Response<T>
使用给定的正文创建一个空白的响应。
此响应的头部将被设置为默认值。
§例子
use puzz_http::{Response, StatusCode};
let response = Response::new("hello world");
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(*response.body(), "hello world");
Sourcepub fn from_head(head: Head, body: T) -> Response<T>
pub fn from_head(head: Head, body: T) -> Response<T>
使用给定的头部和正文创建响应。
§例子
use puzz_http::{Response, StatusCode};
let response = Response::new("hello world");
let (mut head, body) = response.into_head();
head.status = StatusCode::BAD_REQUEST;
let response = Response::from_head(head, body);
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_eq!(*response.body(), "hello world");
Sourcepub fn status(&self) -> StatusCode
pub fn status(&self) -> StatusCode
获取响应的状态码。
§例子
use puzz_http::{Response, StatusCode};
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
获取响应的状态码的可变引用。
§例子
use puzz_http::{Response, StatusCode};
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
获取响应的版本的引用。
§例子
use puzz_http::{Response, Version};
let response: Response<()> = Response::default();
assert_eq!(response.version(), Version::HTTP_11);
Sourcepub fn version_mut(&mut self) -> &mut Version
pub fn version_mut(&mut self) -> &mut Version
获取响应的版本的可变引用。
§例子
use puzz_http::{Response, Version};
let mut response: Response<()> = Response::default();
*response.version_mut() = Version::HTTP_2;
assert_eq!(response.version(), Version::HTTP_2);
Sourcepub fn headers(&self) -> &HeaderMap
pub fn headers(&self) -> &HeaderMap
获取响应的标头集的引用。
§例子
use puzz_http::Response;
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
获取响应的标头集的可变引用。
§例子
use puzz_http::Response;
use puzz_http::header::*;
let mut response: Response<()> = Response::default();
response.headers_mut().insert(HOST, HeaderValue::from_static("world"));
assert!(!response.headers().is_empty());
Sourcepub fn extensions(&self) -> &Extensions
pub fn extensions(&self) -> &Extensions
获取响应的扩展的引用。
§例子
use puzz_http::Response;
let response: Response<()> = Response::default();
assert!(response.extensions().get::<i32>().is_none());
Sourcepub fn extensions_mut(&mut self) -> &mut Extensions
pub fn extensions_mut(&mut self) -> &mut Extensions
获取响应的扩展的可变引用。
§例子
use puzz_http::Response;
let mut response: Response<()> = Response::default();
response.extensions_mut().insert("hello");
assert_eq!(response.extensions().get(), Some(&"hello"));
Sourcepub fn body(&self) -> &T
pub fn body(&self) -> &T
获取响应的正文的引用。
§例子
use puzz_http::Response;
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
获取响应的正文的可变引用。
§例子
use puzz_http::Response;
let mut response: Response<String> = Response::default();
response.body_mut().push_str("hello world");
assert!(!response.body().is_empty());
Sourcepub fn into_body(self) -> T
pub fn into_body(self) -> T
消耗响应,只返回响应的正文。
§例子
use puzz_http::Response;
let response = Response::new(10);
let body = response.into_body();
assert_eq!(body, 10);
Sourcepub fn into_head(self) -> (Head, T)
pub fn into_head(self) -> (Head, T)
消耗响应,返回响应的头部和正文。
§例子
use puzz_http::{Response, StatusCode};
let response: Response<()> = Response::default();
let (head, body) = response.into_head();
assert_eq!(head.status, StatusCode::OK);
Sourcepub fn map<F, U>(self, f: F) -> Response<U>where
F: FnOnce(T) -> U,
pub fn map<F, U>(self, f: F) -> Response<U>where
F: FnOnce(T) -> U,
消耗响应,返回带有给定正文的新响应,其正文为传入函数的返回值。
§例子
use puzz_http::Response;
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());
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for Response<T>where
T: Freeze,
impl<T> !RefUnwindSafe for Response<T>
impl<T> !Send for Response<T>
impl<T> !Sync for Response<T>
impl<T> Unpin for Response<T>where
T: Unpin,
impl<T> !UnwindSafe for Response<T>
Blanket Implementations§
Source§impl<B> BodyExt for Bwhere
B: Body,
impl<B> BodyExt for Bwhere
B: Body,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more