Struct data_buffer::DataBuffer

source ·
pub struct DataBuffer { /* private fields */ }
Expand description

Buffer of plain old data (POD). The data is stored as an array of bytes (Vec<u8>). DataBuffer keeps track of the type stored within via an explicit TypeId member. This allows one to hide the type from the compiler and check it only when necessary. It is particularly useful when the type of data is determined at runtime (e.g. when parsing numeric data).

Implementations§

Construct an empty DataBuffer

Construct a typed DataBuffer with a given size and filled with the specified default value.

Examples
let buf = DataBuffer::with_size(8, 42usize); // Create buffer
let buf_vec: Vec<usize> = buf.into_vec().unwrap(); // Convert into `Vec`
assert_eq!(buf_vec, vec![42usize; 8]);

Construct a DataBuffer from a given Vec<T> reusing the space already allocated by the given vector.

Examples
let vec = vec![1u8, 3, 4, 1, 2];
let buf = DataBuffer::from_vec(vec.clone()); // Convert into buffer
let nu_vec: Vec<u8> = buf.into_vec().unwrap(); // Convert back into `Vec`
assert_eq!(vec, nu_vec);

Construct a DataBuffer from a given slice by cloning the data.

Copy data from a given slice into the current buffer.

Clear the data buffer and set length to zero.

Fill the current buffer with copies of the given value. The size of the buffer is left unchanged. If the given type doesn’t patch the internal type, None is returned, otherwise a mut reference to the modified buffer is returned.

Examples
let vec = vec![1u8, 3, 4, 1, 2];
let mut buf = DataBuffer::from_vec(vec.clone()); // Convert into buffer
buf.fill(0u8);
assert_eq!(buf.into_vec::<u8>().unwrap(), vec![0u8, 0, 0, 0, 0]);

Check if the current buffer contains elements of the specified type. Returns Some(self) if the type matches and None otherwise.

Check if the current buffer contains elements of the specified type. Returns None if the check fails, otherwise a reference to self is returned.

Check if the current buffer contains elements of the specified type. Same as check_ref but consumes and produces a mut reference to self.

Get the TypeId of data stored within this buffer.

Get the number of elements stored in this buffer.

Return an iterator to a slice representing typed data. Returs None if the given type T doesn’t match the internal.

Examples
let vec = vec![1.0_f32, 23.0, 0.01, 42.0, 11.43];
let buf = DataBuffer::from(vec.clone()); // Convert into buffer
for (i, &val) in buf.iter::<f32>().unwrap().enumerate() {
    assert_eq!(val, vec[i]);
}

Return an iterator to a mutable slice representing typed data. Returs None if the given type T doesn’t match the internal.

Append cloned items from this buffer to a given Vec<T>. Return the mutable reference Some(vec) if type matched the internal type and None otherwise.

Append copied items from this buffer to a given Vec<T>. Return the mutable reference Some(vec) if type matched the internal type and None otherwise. This may be faster than append_clone_to_vec.

Clones contents of self into the given Vec.

Copies contents of self into the given Vec.

An alternative to using the Into trait. This function helps the compiler determine the type T automatically.

Convert this buffer into a typed slice. Returs None if the given type T doesn’t match the internal.

Convert this buffer into a typed mutable slice. Returs None if the given type T doesn’t match the internal.

Get i’th element of the buffer by value.

Get a const reference to the i’th element of the buffer.

Get a mutable reference to the i’th element of the buffer.

Get i’th element of the buffer by value without checking type. This can be used to reinterpret the internal data as a different type. Note that if the size of the given type T doesn’t match the size of the internal type, i will really index the ith T sized chunk in the current buffer. See the implementation for details.

Get a const reference to the i’th element of the buffer. This can be used to reinterpret the internal data as a different type. Note that if the size of the given type T doesn’t match the size of the internal type, i will really index the ith T sized chunk in the current buffer. See the implementation for details.

Get a mutable reference to the i’th element of the buffer. This can be used to reinterpret the internal data as a different type. Note that if the size of the given type T doesn’t match the size of the internal type, i will really index the ith T sized chunk in the current buffer. See the implementation for details.

Move buffer data to a vector with a given type, reinterpreting the data type as required.

Borrow buffer data and reinterpret it as a slice of a given type.

Mutably borrow buffer data and reinterpret it as a mutable slice of a given type.

Borrow buffer data and iterate over reinterpreted underlying data.

Mutably borrow buffer data and mutably iterate over reinterpreted underlying data.

Peak at the internal representation of the data.

Get a mutable reference to the internal data representation.

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more

Convert a &[T] to a DataBuffer.

Converts to this type from the input type.

Convert a Vec<T> to a DataBuffer.

Converts to this type from the input type.
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more

Convert a DataBuffer to a Option<Vec<T>>.

Converts this type into the (usually inferred) input type.
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. 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

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
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.