1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-06-02 21:39:26 +00:00
actix-web/actix-http/src/body/size.rs

42 lines
1.3 KiB
Rust
Raw Normal View History

2021-02-15 12:20:43 +00:00
/// Body size hint.
2021-12-11 16:05:08 +00:00
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2021-02-15 12:20:43 +00:00
pub enum BodySize {
2021-12-11 16:05:08 +00:00
/// Implicitly empty body.
2021-02-15 12:20:43 +00:00
///
2021-12-11 16:05:08 +00:00
/// Will omit the Content-Length header. Used for responses to certain methods (e.g., `HEAD`) or
/// with particular status codes (e.g., 204 No Content). Consumers that read this as a body size
/// hint are allowed to make optimizations that skip reading or writing the payload.
2021-02-15 12:20:43 +00:00
None,
/// Known size body.
///
/// Will write `Content-Length: N` header.
2021-02-15 12:20:43 +00:00
Sized(u64),
/// Unknown size body.
///
/// Will not write Content-Length header. Can be used with chunked Transfer-Encoding.
Stream,
}
impl BodySize {
2021-12-11 16:05:08 +00:00
/// Equivalent to `BodySize::Sized(0)`;
pub const ZERO: Self = Self::Sized(0);
2021-12-04 19:40:47 +00:00
/// Returns true if size hint indicates omitted or empty body.
2021-02-15 12:20:43 +00:00
///
/// Streams will return false because it cannot be known without reading the stream.
///
2021-02-15 12:20:43 +00:00
/// ```
/// # use actix_http::body::BodySize;
/// assert!(BodySize::None.is_eof());
/// assert!(BodySize::Sized(0).is_eof());
///
/// assert!(!BodySize::Sized(64).is_eof());
/// assert!(!BodySize::Stream.is_eof());
/// ```
pub fn is_eof(&self) -> bool {
matches!(self, BodySize::None | BodySize::Sized(0))
2021-02-15 12:20:43 +00:00
}
}