Skip to main content

Compress

Struct Compress 

Source
pub struct Compress { /* private fields */ }
Expand description

Compress responses for clients that ask.

App::new()?.middleware(Compress::default())

JSON is the best case compression has: a list of a hundred users is mostly the same twenty key names repeated a hundred times, and typically shrinks by 70–80%. The cost is CPU on the server, which is why small bodies are left alone — below a kilobyte the headers outweigh the saving — and why a body that is already compressed (an image, a zip, anything with a Content-Encoding) is never touched.

gzip is preferred over deflate when the client accepts both, because browsers historically disagreed about whether “deflate” meant the zlib format or a raw stream, and gzip never had that problem. When deflate is what is asked for, the zlib framing is sent, which is what every modern client means by it.

A strong ETag on the response is weakened, because the compressed bytes are a different representation of the same resource and a strong tag promises byte-for-byte identity. The validator still works; it just says so honestly.

Implementations§

Source§

impl Compress

Source

pub fn new() -> Self

Source

pub fn min_size(self, bytes: usize) -> Self

The size below which a body is not worth compressing.

Trait Implementations§

Source§

impl Clone for Compress

Source§

fn clone(&self) -> Compress

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for Compress

Source§

impl Debug for Compress

Source§

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

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

impl Default for Compress

Source§

fn default() -> Self

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

impl Middleware for Compress

Source§

fn handle(&self, request: Request, next: Next) -> BoxFuture<Response>

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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.