Struct Response

Source
pub struct Response<T> { /* private fields */ }
Expand description

一个HTTP响应。

HTTP响应由头部和可选的正文组成。正文是泛型的,允许任意类型来表示HTTP响应的正文。

Implementations§

Source§

impl Response<()>

Source

pub fn builder() -> Builder

创建新的构建器以构建响应。

§例子
use puzz_http::Response;

let response = Response::builder()
    .status(200)
    .header("X-Custom-Foo", "Bar")
    .body(())
    .unwrap();
Source§

impl<T> Response<T>

Source

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");
Source

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");
Source

pub fn status(&self) -> StatusCode

获取响应的状态码。

§例子
use puzz_http::{Response, StatusCode};

let response: Response<()> = Response::default();

assert_eq!(response.status(), StatusCode::OK);
Source

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);
Source

pub fn version(&self) -> Version

获取响应的版本的引用。

§例子
use puzz_http::{Response, Version};

let response: Response<()> = Response::default();

assert_eq!(response.version(), Version::HTTP_11);
Source

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);
Source

pub fn headers(&self) -> &HeaderMap

获取响应的标头集的引用。

§例子
use puzz_http::Response;

let response: Response<()> = Response::default();

assert!(response.headers().is_empty());
Source

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());
Source

pub fn extensions(&self) -> &Extensions

获取响应的扩展的引用。

§例子
use puzz_http::Response;

let response: Response<()> = Response::default();

assert!(response.extensions().get::<i32>().is_none());
Source

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"));
Source

pub fn body(&self) -> &T

获取响应的正文的引用。

§例子
use puzz_http::Response;

let response: Response<String> = Response::default();

assert!(response.body().is_empty());
Source

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());
Source

pub fn into_body(self) -> T

消耗响应,只返回响应的正文。

§例子
use puzz_http::Response;

let response = Response::new(10);
let body = response.into_body();

assert_eq!(body, 10);
Source

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);
Source

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§

Source§

impl<B> Body for Response<B>
where B: Body,

Source§

type Error = <B as Body>::Error

正文产生的错误。
Source§

fn poll_next( self: Pin<&mut Response<B>>, cx: &mut Context<'_>, ) -> Poll<Option<Result<Bytes, <Response<B> as Body>::Error>>>

尝试提取正文的下一个数据,如果正文耗尽则返回None
Source§

fn size_hint(&self) -> SizeHint

返回正文剩余长度的界限。
Source§

impl<T> Debug for Response<T>
where T: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<T> Default for Response<T>
where T: Default,

Source§

fn default() -> Response<T>

Returns the “default value” for a type. Read more

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<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<B> BodyExt for B
where B: Body,

Source§

fn next(&mut self) -> Next<'_, Self>
where Self: Unpin,

Source§

fn map_err<F, E>(self, f: F) -> MapErr<Self, F>
where Self: Sized, F: FnMut(Self::Error) -> E,

Source§

fn stream(self) -> BodyStream<Self>
where Self: Sized,

Source§

fn boxed(self) -> BoxBody
where Self: Sized + 'static, Self::Error: Into<Box<dyn Error>>,

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.