Struct dyn_buf::VecBuf[][src]

pub struct VecBuf { /* fields omitted */ }
Expand description

VecBuf is an effective dynamic buffer v1 implement, which tries to reduce reallocation and mem-moving.

If commit and consume speed matches(len() is always less than min_write), this buffer would allocate only once.

Example

use dyn_buf::VecBuf;
let mut buf = VecBuf::new(10);

let t = buf.prepare(4);
assert_eq!(t.len(), 4);

// write data to prepared buffer
t[..3].copy_from_slice(&[1, 2, 3]);
assert_eq!(buf.data(), &[]);

buf.commit(3);
assert_eq!(buf.data(), &[1, 2, 3]);

buf.consume(2);
assert_eq!(buf.data(), &[3]);

let t = buf.prepare_at_least(2);
assert!(t.len() >= 2);
t[..2].copy_from_slice(&[4, 5]);
assert_eq!(buf.data(), &[3]);

buf.commit(2);
assert_eq!(buf.data(), &[3, 4, 5]);

buf.consume(2);
assert_eq!(buf.data(), &[5]);

assert_eq!(buf.into_vec(), vec![5]);

Implementations

Returns the number of readable bytes.

Returns true if buffer contains no readable bytes.

Returns the maximum number of bytes, both readable and writable, that can be held by self without requiring reallocation.

Returns a constant buffer sequence u that represents the readable bytes.

Returns a mutable buffer sequence u representing the writable bytes, and where buffer_size(u) == amt.

The dynamic buffer reallocates memory as required. All constant or mutable buffer sequences previously obtained using data() or prepare() are invalidated.

Panic: length_error if size() + amt exceeds max_size().

Returns a mutable buffer sequence u representing the writable bytes, and where buffer_size(u) >= amt.

The dynamic buffer reallocates memory as required. All constant or mutable buffer sequences previously obtained using data() or prepare() are invalidated.

Panic: length_error if size() + amt exceeds max_size().

Appends amt bytes from the start of the writable bytes to the end of the readable bytes.

The remainder of the writable bytes are discarded. If amt is greater than the number of writable bytes, all writable bytes are appended to the readable bytes. All constant or mutable buffer sequences previously obtained using data() or prepare() are invalidated.

Removes amt bytes from beginning of the readable bytes.

If amt is greater than the number of readable bytes, all readable bytes are removed. All constant or mutable buffer sequences previously obtained using data() or prepare() are invalidated.

create a VecBuf with min_write

prepare_at_least will return a buffer at least min_write bytes.

Example

 use dyn_buf::VecBuf;
 let min_write = 5;
 let mut buf = VecBuf::new(min_write);
 let t = buf.prepare_at_least(0);
 assert!(t.len() >= min_write);
 t[..2].copy_from_slice(&[1,2]);
 buf.commit(2);
 let t = buf.prepare_at_least(0);
 assert!(t.len() >= min_write);

Reserves capacity for at least amt bytes to be wrote

Grow capacity at least amt bytes

Write all bytes of buf into self

Converts self into a vector without clones or allocation. The resulting vector contains all the readable bytes

Trait Implementations

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

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.