pub struct Response<T>
where T: Serialize,
{ /* private fields */ }

Implementations§

source§

impl Response<()>

source

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

pub fn text() -> Builder

source

pub fn json() -> Builder

source

pub fn status404() -> Builder

source

pub fn status503() -> Builder

source

pub fn status502() -> Builder

source

pub fn status500() -> Builder

source§

impl<T: Serialize> Response<T>

source

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

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

pub fn is_partial(&self) -> bool

source

pub fn status(&self) -> StatusCode

Returns the StatusCode.

§Examples
use webparse::*;
let response: Response<()> = Response::default();
assert_eq!(response.status(), StatusCode::OK);
source

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

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

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

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

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

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

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

source

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

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

pub fn headers_body(&mut self) -> (&HeaderMap, &T)

source

pub fn headers_body_mut(&mut self) -> (&mut HeaderMap, &mut T)

source

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

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

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

pub fn httpdata(&mut self) -> WebResult<Vec<u8>>

source

pub fn into<B: Serialize>(self, body: B) -> (Response<B>, T)

source

pub fn into_type<B: From<T> + Serialize>(self) -> Response<B>

source

pub fn into_binary(self) -> Response<Binary>

source

pub fn get_body_len(&self) -> isize

获取返回的body长度, 如果为0则表示未写入信息

source

pub fn encode_header<B: Buf + BufMut>( &mut self, buffer: &mut B ) -> WebResult<usize>

source

pub fn parse_buffer<B: Buf>(&mut self, buffer: &mut B) -> WebResult<usize>

source

pub fn replace_body(&mut self, body: T)

source

pub fn replace_clone(&mut self, body: T) -> Response<T>

Trait Implementations§

source§

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

source§

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

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

impl<T: Default + Serialize> Default for Response<T>

source§

fn default() -> Self

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

impl<T> Display for Response<T>
where T: Serialize + Display,

source§

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

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

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

source§

fn serialize<B: Buf + BufMut>(&mut self, buffer: &mut B) -> WebResult<usize>

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for Response<T>

§

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

§

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

§

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<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> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

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

§

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

§

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.