Struct data_buffer::DataBuffer[][src]

pub struct DataBuffer { /* fields omitted */ }

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

Methods

impl DataBuffer
[src]

Construct an empty DataBuffer

Construct a typed DataBuffer with a given size.

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 copying 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

impl Clone for DataBuffer
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl Debug for DataBuffer
[src]

Formats the value using the given formatter. Read more

impl PartialEq for DataBuffer
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl Hash for DataBuffer
[src]

Feeds this value into the given [Hasher]. Read more

Feeds a slice of this type into the given [Hasher]. Read more

impl<T> From<Vec<T>> for DataBuffer where
    T: Any
[src]

Convert a Vec<T> to a DataBuffer.

Performs the conversion.

impl<'a, T> From<&'a [T]> for DataBuffer where
    T: Any
[src]

Convert a &[T] to a DataBuffer.

Performs the conversion.

impl<T> Into<Option<Vec<T>>> for DataBuffer where
    T: Any + Clone
[src]

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

Performs the conversion.

Auto Trait Implementations

impl Send for DataBuffer

impl Sync for DataBuffer