actix_http/body/
none.rs

1use std::{
2    convert::Infallible,
3    pin::Pin,
4    task::{Context, Poll},
5};
6
7use bytes::Bytes;
8
9use super::{BodySize, MessageBody};
10
11/// Body type for responses that forbid payloads.
12///
13/// This is distinct from an "empty" response which _would_ contain a `Content-Length` header.
14/// For an "empty" body, use `()` or `Bytes::new()`.
15///
16/// For example, the HTTP spec forbids a payload to be sent with a `204 No Content` response.
17/// In this case, the payload (or lack thereof) is implicit from the status code, so a
18/// `Content-Length` header is not required.
19#[derive(Debug, Clone, Copy, Default)]
20#[non_exhaustive]
21pub struct None;
22
23impl None {
24    /// Constructs new "none" body.
25    #[inline]
26    pub fn new() -> Self {
27        None
28    }
29}
30
31impl MessageBody for None {
32    type Error = Infallible;
33
34    #[inline]
35    fn size(&self) -> BodySize {
36        BodySize::None
37    }
38
39    #[inline]
40    fn poll_next(
41        self: Pin<&mut Self>,
42        _cx: &mut Context<'_>,
43    ) -> Poll<Option<Result<Bytes, Self::Error>>> {
44        Poll::Ready(Option::None)
45    }
46
47    #[inline]
48    fn try_into_bytes(self) -> Result<Bytes, Self> {
49        Ok(Bytes::new())
50    }
51}