actix_http/body/size.rs
1/// Body size hint.
2#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3pub enum BodySize {
4 /// Implicitly empty body.
5 ///
6 /// Will omit the Content-Length header. Used for responses to certain methods (e.g., `HEAD`) or
7 /// with particular status codes (e.g., 204 No Content). Consumers that read this as a body size
8 /// hint are allowed to make optimizations that skip reading or writing the payload.
9 None,
10
11 /// Known size body.
12 ///
13 /// Will write `Content-Length: N` header.
14 Sized(u64),
15
16 /// Unknown size body.
17 ///
18 /// Will not write Content-Length header. Can be used with chunked Transfer-Encoding.
19 Stream,
20}
21
22impl BodySize {
23 /// Equivalent to `BodySize::Sized(0)`;
24 pub const ZERO: Self = Self::Sized(0);
25
26 /// Returns true if size hint indicates omitted or empty body.
27 ///
28 /// Streams will return false because it cannot be known without reading the stream.
29 ///
30 /// ```
31 /// # use actix_http::body::BodySize;
32 /// assert!(BodySize::None.is_eof());
33 /// assert!(BodySize::Sized(0).is_eof());
34 ///
35 /// assert!(!BodySize::Sized(64).is_eof());
36 /// assert!(!BodySize::Stream.is_eof());
37 /// ```
38 pub fn is_eof(&self) -> bool {
39 matches!(self, BodySize::None | BodySize::Sized(0))
40 }
41}