Skip to main content

Sink

Trait Sink 

Source
pub unsafe trait Sink {
    // Required methods
    fn len(&self) -> usize;
    fn capacity(&self) -> usize;
    fn grow(&mut self, capacity: usize) -> bool;
    fn as_mut_ptr(&mut self) -> *mut u8;
    unsafe fn set_len(&mut self, len: usize);

    // Provided methods
    fn is_empty(&self) -> bool { ... }
    fn reserve(&mut self, capacity: usize) -> bool { ... }
    fn allocator(&self) -> Option<Allocator> { ... }
}
Expand description

A growable byte buffer an algorithm writes its output into, in place.

Vec<u8> is one. A caller with its own string type implements it so the output is written once, where it ends up: the PHP extension writes straight into a PHP string, which a Vec would cost a second copy of (and twice the memory, for a moment).

The *_into methods of each algorithm (Gzip::compress_into, …) append to the sink; Algorithm::compress is compress_into a new Vec. On an error, what the sink holds is unspecified.

§Safety

Sink::as_mut_ptr points to Sink::capacity writable bytes, the first Sink::len of them the output so far; Sink::grow and Sink::reserve keep those bytes (the pointer may change).

Required Methods§

Source

fn len(&self) -> usize

How many bytes were written.

Source

fn capacity(&self) -> usize

How many bytes fit without growing.

Source

fn grow(&mut self, capacity: usize) -> bool

Makes room for capacity bytes in all (nothing to do when there is). false when the memory is not there, which the algorithm reports as its codec would. The algorithms ask for what PHP’s function would have at that point, so a sink that grows to exactly that (reallocating in place, as PHP’s allocator does for a large block) holds no more.

Source

fn as_mut_ptr(&mut self) -> *mut u8

The start of the buffer.

Source

unsafe fn set_len(&mut self, len: usize)

Records that the first len bytes are written.

§Safety

len <= capacity(), and the first len bytes were written.

Provided Methods§

Source

fn is_empty(&self) -> bool

Whether nothing was written.

Source

fn reserve(&mut self, capacity: usize) -> bool

Sink::grow to capacity when that is cheap to do: a guess at the final size (a gzip trailer’s length, a bound) that saves growing step by step. Declining (false) is never an error; the algorithm then grows as it needs to.

Source

fn allocator(&self) -> Option<Allocator>

The allocator for the codec’s working state during the call; None (the default) for the C library’s own, malloc().

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl Sink for Vec<u8>

Source§

fn grow(&mut self, capacity: usize) -> bool

Amortized, as a Vec grows: the algorithms grow step by step, as PHP’s buffers do.

Source§

fn len(&self) -> usize

Source§

fn capacity(&self) -> usize

Source§

fn as_mut_ptr(&mut self) -> *mut u8

Source§

unsafe fn set_len(&mut self, len: usize)

Implementors§