Type Alias Response

pub type Response<T = Body> = Response<T>;
Expand description

Type alias for http::Response whose body type defaults to Body, the most common body type used with rama.

Aliased Type§

struct Response<T = Body> { /* private fields */ }

Implementations

§

impl Response<()>

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
let response = Response::builder()
    .status(200)
    .header("X-Custom-Foo", "Bar")
    .body(())
    .unwrap();
§

impl<T> Response<T>

pub fn new(body: T) -> Response<T>

Creates a new blank Response with the body

The component parts of this response will be set to their default, e.g. the ok status, no headers, etc.

§Examples
let response = Response::new("hello world");

assert_eq!(response.status(), StatusCode::OK);
assert_eq!(*response.body(), "hello world");

pub fn from_parts(parts: Parts, body: T) -> Response<T>

Creates a new Response with the given head and body

§Examples
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");

pub fn status(&self) -> StatusCode

Returns the StatusCode.

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

pub fn status_mut(&mut self) -> &mut StatusCode

Returns a mutable reference to the associated StatusCode.

§Examples
let mut response: Response<()> = Response::default();
*response.status_mut() = StatusCode::CREATED;
assert_eq!(response.status(), StatusCode::CREATED);

pub fn version(&self) -> Version

Returns a reference to the associated version.

§Examples
let response: Response<()> = Response::default();
assert_eq!(response.version(), Version::HTTP_11);

pub fn version_mut(&mut self) -> &mut Version

Returns a mutable reference to the associated version.

§Examples
let mut response: Response<()> = Response::default();
*response.version_mut() = Version::HTTP_2;
assert_eq!(response.version(), Version::HTTP_2);

pub fn headers(&self) -> &HeaderMap

Returns a reference to the associated header field map.

§Examples
let response: Response<()> = Response::default();
assert!(response.headers().is_empty());

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

Returns a mutable reference to the associated header field map.

§Examples
let mut response: Response<()> = Response::default();
response.headers_mut().insert(HOST, HeaderValue::from_static("world"));
assert!(!response.headers().is_empty());

pub fn extensions(&self) -> &Extensions

Returns a reference to the associated extensions.

§Examples
let response: Response<()> = Response::default();
assert!(response.extensions().get::<i32>().is_none());

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

Returns a mutable reference to the associated extensions.

§Examples
let mut response: Response<()> = Response::default();
response.extensions_mut().insert("hello");
assert_eq!(response.extensions().get(), Some(&"hello"));

pub fn body(&self) -> &T

Returns a reference to the associated HTTP body.

§Examples
let response: Response<String> = Response::default();
assert!(response.body().is_empty());

pub fn body_mut(&mut self) -> &mut T

Returns a mutable reference to the associated HTTP body.

§Examples
let mut response: Response<String> = Response::default();
response.body_mut().push_str("hello world");
assert!(!response.body().is_empty());

pub fn into_body(self) -> T

Consumes the response, returning just the body.

§Examples
let response = Response::new(10);
let body = response.into_body();
assert_eq!(body, 10);

pub fn into_parts(self) -> (Parts, T)

Consumes the response returning the head and body parts.

§Examples
let response: Response<()> = Response::default();
let (parts, body) = response.into_parts();
assert_eq!(parts.status, StatusCode::OK);

pub fn map<F, U>(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
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

§

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

§

type Data = <B as Body>::Data

Values yielded by the Body.
§

type Error = <B as Body>::Error

The error type this Body might generate.
§

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

Attempt to pull out the next data buffer of this stream.
§

fn is_end_stream(&self) -> bool

Returns true when the end of stream has been reached. Read more
§

fn size_hint(&self) -> SizeHint

Returns the bounds on the remaining length of the stream. Read more
§

impl<Body> BodyExtractExt for Response<Body>
where Body: Body + Send + 'static, <Body as Body>::Data: Send + 'static, <Body as Body>::Error: Into<Box<dyn Error + Sync + Send>>,

§

async fn try_into_json<T>(self) -> Result<T, OpaqueError>
where T: DeserializeOwned + Send + 'static,

Try to deserialize the (contained) body as a JSON object.
§

async fn try_into_string(self) -> Result<String, OpaqueError>

Try to turn the (contained) body in an utf-8 string.
§

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

§

fn clone(&self) -> Response<T>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
§

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

§

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

Formats the value using the given formatter. Read more
§

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

§

fn default() -> Response<T>

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

impl<T> From<StaticResponseFactory<T>> for Response<Body>
where T: IntoResponse,

§

fn from(value: StaticResponseFactory<T>) -> Response<Body>

Converts to this type from the input type.
§

impl<Body> HeaderValueGetter for Response<Body>

§

fn header_str<K>(&self, key: K) -> Result<&str, HeaderValueErr>
where K: AsHeaderName + Copy,

Get a header value as a string.
§

fn header_bytes<K>(&self, key: K) -> Result<&[u8], HeaderValueErr>
where K: AsHeaderName + Copy,

Get a header value as a byte slice.
§

impl<B> IntoResponse for Response<B>
where B: Body<Data = Bytes> + Send + Sync + 'static, <B as Body>::Error: Into<Box<dyn Error + Sync + Send>>,

§

fn into_response(self) -> Response<Body>

Create a response.