Skip to main content

Compressor

Struct Compressor 

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

Compresses messages for permessage-deflate.

§Examples

use compress_lib::message::{Compressor, MessageConfig};

let mut tx = Compressor::new(MessageConfig::new())?;
let mut wire = Vec::new();
tx.compress_message(b"Hello", &mut wire)?;
assert_eq!(wire, [0xF2, 0x48, 0xCD, 0xC9, 0xC9, 0x07, 0x00]); // RFC 7692 7.2.3.1

Implementations§

Source§

impl Compressor

Source

pub fn new(config: MessageConfig) -> Result<Self, Error>

A compressor for one direction of a connection.

§Errors

ErrorKind::InvalidWindowBits outside 8 to 15.

§Examples
use compress_lib::message::{Compressor, MessageConfig};

assert!(Compressor::new(MessageConfig::new().with_window_bits(8)).is_ok());
assert!(Compressor::new(MessageConfig::new().with_window_bits(16)).is_err());
Source

pub fn compress_message( &mut self, message: &[u8], out: &mut Vec<u8>, ) -> Result<usize, Error>

Compresses one message, appending the payload (without the trailing 00 00 FF FF) to out. Returns the payload length. An empty message compresses to the single byte 00.

The payload can be longer than the message for incompressible data; the WebSocket layer may then send the message uncompressed instead (with takeover the compressor’s window has advanced either way, so call reset and tell the peer, or keep sending compressed).

§Errors

ErrorKind::AllocationFailed when out cannot grow.

§Examples
use compress_lib::message::{Compressor, MessageConfig};

let mut tx = Compressor::new(MessageConfig::new())?;
let mut wire = Vec::new();
assert_eq!(tx.compress_message(b"", &mut wire)?, 1);
assert_eq!(wire, [0x00]);
Source

pub fn wipe(&mut self)

Zeroes the compressor’s window, hash chains, and buffers (they hold plaintext of earlier messages), keeping the allocations; see Deflater::wipe for what “zeroes” can promise without unsafe.

§Examples
use compress_lib::message::{Compressor, MessageConfig};

let mut tx = Compressor::new(MessageConfig::new().with_context_takeover(true))?;
tx.compress_message(b"token=secret", &mut Vec::new())?;
tx.wipe();
Source

pub fn reset(&mut self)

Forgets the window (for a connection that restarts its context).

§Examples
use compress_lib::message::{Compressor, MessageConfig};

let mut tx = Compressor::new(MessageConfig::new().with_context_takeover(true))?;
tx.reset();
Source

pub fn memory_in_use(&self) -> usize

Heap bytes held.

§Examples
use compress_lib::message::{Compressor, MessageConfig};

let small = Compressor::new(MessageConfig::new().with_window_bits(9))?;
let big = Compressor::new(MessageConfig::new())?;
assert!(small.memory_in_use() < big.memory_in_use());

Trait Implementations§

Source§

impl Debug for Compressor

Source§

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

Formats the value using the given formatter. Read more

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> 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, 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.