pub struct Builder { /* private fields */ }
Expand description
HTTP响应的构建器。
Implementations§
Source§impl Builder
impl Builder
Sourcepub fn new() -> Builder
pub fn new() -> Builder
创建构建器的默认实例以构建响应。
§例子
use puzz_http::response::Builder;
let response = Builder::new()
.status(200)
.body(())
.unwrap();
Sourcepub fn status<T>(self, status: T) -> Builder
pub fn status<T>(self, status: T) -> Builder
设置响应的状态码。
默认情况下,这是200
。
§例子
use puzz_http::Response;
let response = Response::builder()
.status(200)
.body(())
.unwrap();
Sourcepub fn status_ref(&self) -> Option<&StatusCode>
pub fn status_ref(&self) -> Option<&StatusCode>
Sourcepub fn version(self, version: Version) -> Builder
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();
Sourcepub fn version_ref(&self) -> Option<&Version>
pub fn version_ref(&self) -> Option<&Version>
Sourcepub fn header<K, V>(self, key: K, value: V) -> Builderwhere
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<Error>,
HeaderValue: TryFrom<V>,
<HeaderValue as TryFrom<V>>::Error: Into<Error>,
pub fn header<K, V>(self, key: K, value: V) -> Builderwhere
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<Error>,
HeaderValue: TryFrom<V>,
<HeaderValue as TryFrom<V>>::Error: Into<Error>,
将标头追加到响应中。
此函数将提供的键值对追加到响应内部的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();
Sourcepub fn headers_ref(&self) -> Option<&HeaderMap>
pub fn headers_ref(&self) -> Option<&HeaderMap>
Sourcepub fn headers_mut(&mut self) -> Option<&mut HeaderMap>
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" );
Sourcepub fn extension<T>(self, extension: T) -> Builderwhere
T: 'static,
pub fn extension<T>(self, extension: T) -> Builderwhere
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"));
Sourcepub fn extensions_ref(&self) -> Option<&Extensions>
pub fn extensions_ref(&self) -> Option<&Extensions>
Sourcepub fn extensions_mut(&mut self) -> Option<&mut Extensions>
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));
Trait Implementations§
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> 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