Skip to main content

Brotli

Struct Brotli 

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

Brotli (RFC 7932): Content-Encoding: br.

Compresses at quality Brotli::DEFAULT_LEVEL (11) unless Brotli::set_level says otherwise, with a 4 MiB window (lgwin 22). Decompressing accepts what brotli_uncompress() accepts, including trailing bytes after the stream.

use utopia_compression::{Algorithm, Brotli};

let mut brotli = Brotli::new();
brotli.set_level(4)?;
let body = brotli.compress(b"This is a demo string")?;
assert_eq!(brotli.decompress(&body)?, b"This is a demo string");
assert_eq!(brotli.content_encoding(), "br");
use Utopia\Compression\Algorithms\Brotli;

$brotli = new Brotli();
$brotli->setLevel(4);
$body = $brotli->compress('This is a demo string');
echo $brotli->decompress($body), "\n"; // This is a demo string
echo $brotli->getContentEncoding(), "\n"; // br

The codec is C brotli 1.2.1, the library PHP’s ext-brotli links, called the way brotli_compress() calls it: the output is PHP’s, byte for byte, at every quality.

Requires the brotli feature. PHP: Utopia\Compression\Algorithms\Brotli.

Implementations§

Source§

impl Brotli

Source

pub const MIN_LEVEL: u32 = 0

The lowest quality. PHP: BROTLI_COMPRESS_LEVEL_MIN.

Source

pub const MAX_LEVEL: u32 = 11

The highest quality. PHP: BROTLI_COMPRESS_LEVEL_MAX.

Source

pub const DEFAULT_LEVEL: u32 = 11

The quality of a new instance. PHP: BROTLI_COMPRESS_LEVEL_DEFAULT.

Source

pub const fn new() -> Self

Brotli at quality 11 in generic mode.

PHP: new Brotli().

Source

pub const fn level(&self) -> u32

The quality, 0 to 11.

PHP: getLevel().

Source

pub fn set_level(&mut self, level: i64) -> Result<(), Error>

Sets the quality. Fails with Error::InvalidArgument outside 0 to 11.

PHP: setLevel($level).

Source

pub fn use_generic_mode(&mut self)

Tunes the encoding for any data (the default).

PHP: useGenericMode().

Source

pub fn use_text_mode(&mut self)

Meant to tune the encoding for UTF-8 text, but keeps generic mode, as PHP does.

PHP: useTextMode(), which sets BROTLI_GENERIC (a quirk kept for identical output).

Source

pub fn use_font_mode(&mut self)

Meant to tune the encoding for WOFF 2.0 fonts, but keeps generic mode, as PHP does.

PHP: useFontMode(), which sets BROTLI_GENERIC (a quirk kept for identical output).

Source

pub const fn mode(&self) -> Mode

The mode the encoder runs in.

Source

pub const fn is_supported() -> bool

Whether this build has Brotli (the brotli feature).

PHP: Brotli::isSupported(), whether brotli_compress() exists.

Source

pub fn compress_into<S: Sink + ?Sized>( &self, data: &[u8], out: &mut S, ) -> Result<(), Error>

Algorithm::compress, appended to out.

Source

pub fn compress_with<S: Sink + ?Sized>( data: &[u8], level: i64, mode: i64, out: &mut S, ) -> Result<(), Error>

Compresses data into out at level and mode (BROTLI_GENERIC, BROTLI_TEXT or BROTLI_FONT: 0, 1 or 2), unchecked, as brotli_compress($data, $level, $mode) takes them: either out of range fails with Error::Codec and PHP’s warning.

PHP: compress() on a subclass that set $level or $mode itself.

Source

pub fn decompress_into<S: Sink + ?Sized>( &self, data: &[u8], out: &mut S, ) -> Result<(), Error>

Algorithm::decompress, appended to out.

Trait Implementations§

Source§

impl Algorithm for Brotli

Source§

fn name(&self) -> &str

The algorithm’s name, such as gzip. Read more
Source§

fn content_encoding(&self) -> Cow<'_, str>

The token of the algorithm in Content-Encoding and Accept-Encoding headers: by default the name in lower case (ASCII only); br for Brotli. Read more
Source§

fn compress(&self, data: &[u8]) -> Result<Vec<u8>, Error>

Compresses data. Read more
Source§

fn decompress(&self, data: &[u8]) -> Result<Vec<u8>, Error>

Decompresses data. Fails with Error::Codec when data is not a valid stream of the algorithm. Read more
Source§

fn supported(&self) -> bool

Whether this build can run the algorithm. Read more
Source§

impl Clone for Brotli

Source§

fn clone(&self) -> Self

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 Brotli

Source§

impl Debug for Brotli

Source§

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

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

impl Default for Brotli

Source§

fn default() -> Self

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

impl Eq for Brotli

Source§

impl PartialEq for Brotli

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Brotli

Auto Trait Implementations§

§

impl Forget for Brotli

§

impl Freeze for Brotli

§

impl Move for Brotli

§

impl RefUnwindSafe for Brotli

§

impl Send for Brotli

§

impl Sync for Brotli

§

impl Unpin for Brotli

§

impl UnsafeUnpin for Brotli

§

impl UnwindSafe for Brotli

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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> Same for T

Source§

type Output = T

Should always be Self
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.