Struct Builder

Source
pub struct Builder { /* private fields */ }
Expand description

HTTP响应的构建器。

Implementations§

Source§

impl Builder

Source

pub fn new() -> Builder

创建构建器的默认实例以构建响应。

§例子
use puzz_http::response::Builder;

let response = Builder::new()
    .status(200)
    .body(())
    .unwrap();
Source

pub fn status<T>(self, status: T) -> Builder

设置响应的状态码。

默认情况下,这是200

§例子
use puzz_http::Response;

let response = Response::builder()
    .status(200)
    .body(())
    .unwrap();
Source

pub fn status_ref(&self) -> Option<&StatusCode>

获取响应的状态码。

默认情况下,这是200。如果构建器有错误,则返回None

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

let mut req = Response::builder();

assert_eq!(req.status_ref().unwrap(), &StatusCode::OK);
Source

pub fn version(self, version: Version) -> Builder

设置响应的HTTP版本。

默认情况下,这是HTTP/1.1

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

let response = Response::builder()
    .version(Version::HTTP_2)
    .body(())
    .unwrap();
Source

pub fn version_ref(&self) -> Option<&Version>

获取响应的HTTP版本。

默认情况下,这是HTTP/1.1。如果构建器有错误,则返回None

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

let mut req = Response::builder();
assert_eq!(req.version_ref().unwrap(), &Version::HTTP_11);

req = req.version(Version::HTTP_2);
assert_eq!(req.version_ref().unwrap(), &Version::HTTP_2);
Source

pub fn header<K, V>(self, key: K, value: V) -> Builder

将标头追加到响应中。

此函数将提供的键值对追加到响应内部的HeaderMap中。本质上, 这相当于调用HeaderMap::append

§例子
use puzz_http::Response;

let response = Response::builder()
    .header("Content-Type", "text/html")
    .header("X-Custom-Foo", "bar")
    .header("content-length", 0)
    .body(())
    .unwrap();
Source

pub fn headers_ref(&self) -> Option<&HeaderMap>

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

如果构建器有错误,则返回None

§例子
use puzz_http::Response;

let res = Response::builder()
    .header("Accept", "text/html")
    .header("X-Custom-Foo", "bar");
let headers = res.headers_ref().unwrap();

assert_eq!( headers["Accept"], "text/html" );
assert_eq!( headers["X-Custom-Foo"], "bar" );
Source

pub fn headers_mut(&mut self) -> Option<&mut HeaderMap>

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

如果构建器有错误,则返回None

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

let mut res = Response::builder();

let headers = res.headers_mut().unwrap();
headers.insert("Accept", HeaderValue::from_static("text/html"));
headers.insert("X-Custom-Foo", HeaderValue::from_static("bar"));

let headers = res.headers_ref().unwrap();
assert_eq!( headers["Accept"], "text/html" );
assert_eq!( headers["X-Custom-Foo"], "bar" );
Source

pub fn extension<T>(self, extension: T) -> Builder
where T: 'static,

将一个类型添加到响应的扩展中。

§例子
use puzz_http::Response;

let response = Response::builder()
    .extension("My Extension")
    .body(())
    .unwrap();

assert_eq!(response.extensions().get::<&'static str>(),
           Some(&"My Extension"));
Source

pub fn extensions_ref(&self) -> Option<&Extensions>

获取响应的扩展的引用。

如果构建器有错误,则返回None

§例子
use puzz_http::Response;

let res = Response::builder().extension("My Extension").extension(5u32);
let extensions = res.extensions_ref().unwrap();

assert_eq!(extensions.get::<&'static str>(), Some(&"My Extension"));
assert_eq!(extensions.get::<u32>(), Some(&5u32));
Source

pub fn extensions_mut(&mut self) -> Option<&mut Extensions>

获取响应的扩展的可变引用。

如果构建器有错误,则返回None

§例子
use puzz_http::Response;

let mut res = Response::builder().extension("My Extension");
let mut extensions = res.extensions_mut().unwrap();
assert_eq!(extensions.get::<&'static str>(), Some(&"My Extension"));

extensions.insert(5u32);
assert_eq!(extensions.get::<u32>(), Some(&5u32));
Source

pub fn body<T>(self, body: T) -> Result<Response<T>, Error>

消耗构建器,使用给定的正文构建响应。

§错误

如果之前配置的任意一个参数发生错误,则在调用此函数时将错误返回。

§例子
use puzz_http::Response;

let response = Response::builder()
    .body(())
    .unwrap();

Trait Implementations§

Source§

impl Debug for Builder

Source§

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

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

impl Default for Builder

Source§

fn default() -> Builder

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

Auto Trait Implementations§

§

impl Freeze for Builder

§

impl !RefUnwindSafe for Builder

§

impl !Send for Builder

§

impl !Sync for Builder

§

impl Unpin for Builder

§

impl !UnwindSafe for Builder

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<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.