[−][src]Struct dync::VecCopy
Buffer of plain old data. The data is stored as an array of bytes (Vec<u8>).
VecCopy 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).
Safety
It is assumed that any Rust type has a valid representation in bytes. This library has an
inherently more relaxed requirement than crates like zerocopy or bytemuck since the
representative bytes cannot be modified or inspected by the safe API exposed by this library,
they can only be copied.
Further, the bytes representing a type are never interpreted as
anything other than a type with an identical TypeId, which are assumed to have an identical
memory layout throughout the execution of the program.
Although serializing is enabled via the serde feature, deserializing this type in a program
compiled into a different binary may cause undefined behavior. Deserializing this type should be
done using the same binary because TypeId values could change between Rust compiler versions,
architectures or other variables.
Methods
impl VecCopy[src]
pub fn with_type<T: Elem>() -> Self[src]
Construct an empty VecCopy with a specific type.
pub fn with_type_from(other: &VecCopy) -> Self[src]
Construct a VecCopy with the same type as the given buffer without copying its data.
pub fn with_capacity<T: Elem>(n: usize) -> Self[src]
Construct an empty VecCopy with a capacity for a given number of typed elements. For
setting byte capacity use with_byte_capacity.
pub fn with_size<T: Elem>(n: usize, def: T) -> Self[src]
Construct a typed VecCopy with a given size and filled with the specified default
value.
Examples
use dync::VecCopy; let buf = VecCopy::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]);
pub fn from_vec<T: Elem>(vec: Vec<T>) -> Self[src]
Construct a VecCopy from a given Vec<T> reusing the space already allocated by the
given vector.
Examples
use dync::VecCopy; let vec = vec![1u8, 3, 4, 1, 2]; let buf = VecCopy::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);
pub fn from_slice<T: Elem>(slice: &[T]) -> Self[src]
Construct a VecCopy from a given slice by copying the data.
pub fn resize<T: Elem>(&mut self, new_len: usize, value: T) -> Option<&mut Self>[src]
Resizes the buffer in-place to store new_len elements and returns an optional
mutable reference to Self.
If T does not correspond to the underlying element type, then None is returned and the
VecCopy is left unchanged.
This function has the similar properties to Vec::resize.
pub fn copy_from_slice<T: Elem>(&mut self, slice: &[T]) -> &mut Self[src]
Copy data from a given slice into the current buffer.
The VecCopy is extended if the given slice is larger than the number of elements
already stored in this VecCopy.
pub fn clear(&mut self)[src]
Clear the data buffer without destroying its type information.
pub fn fill<T: Elem>(&mut self, def: T) -> Option<&mut Self>[src]
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
use dync::VecCopy; let vec = vec![1u8, 3, 4, 1, 2]; let mut buf = VecCopy::from_vec(vec.clone()); // Convert into buffer buf.fill(0u8); assert_eq!(buf.into_vec::<u8>().unwrap(), vec![0u8, 0, 0, 0, 0]);
pub fn push<T: Any>(&mut self, element: T) -> Option<&mut Self>[src]
Add an element to this buffer.
If the type of the given element coincides with the type
stored by this buffer, then the modified buffer is returned via a mutable reference.
Otherwise, None is returned.
pub fn check<T: Any>(self) -> Option<Self>[src]
Check if the current buffer contains elements of the specified type. Returns Some(self)
if the type matches and None otherwise.
pub fn check_ref<T: Any>(&self) -> Option<&Self>[src]
Check if the current buffer contains elements of the specified type. Returns None if the
check fails, otherwise a reference to self is returned.
pub fn check_mut<T: Any>(&mut self) -> Option<&mut Self>[src]
Check if the current buffer contains elements of the specified type. Same as check_ref
but consumes and produces a mut reference to self.
pub fn element_type_id(&self) -> TypeId[src]
Get the TypeId of data stored within this buffer.
pub fn len(&self) -> usize[src]
Get the number of elements stored in this buffer.
pub fn is_empty(&self) -> bool[src]
Check if there are any elements stored in this buffer.
pub fn byte_capacity(&self) -> usize[src]
Get the byte capacity of this buffer.
pub fn element_size(&self) -> usize[src]
Get the size of the element type in bytes.
pub fn iter<T: Any>(&self) -> Option<Iter<T>>[src]
Return an iterator to a slice representing typed data.
Returs None if the given type T doesn't match the internal.
Examples
use dync::VecCopy; let vec = vec![1.0_f32, 23.0, 0.01, 42.0, 11.43]; let buf = VecCopy::from(vec.clone()); // Convert into buffer for (i, &val) in buf.iter::<f32>().unwrap().enumerate() { assert_eq!(val, vec[i]); }
pub fn iter_mut<T: Any>(&mut self) -> Option<IterMut<T>>[src]
Return an iterator to a mutable slice representing typed data.
Returns None if the given type T doesn't match the internal.
pub fn append_to_vec<'a, T: Elem>(
&self,
vec: &'a mut Vec<T>
) -> Option<&'a mut Vec<T>>[src]
&self,
vec: &'a mut Vec<T>
) -> Option<&'a mut Vec<T>>
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.
pub fn copy_into_vec<T: Elem>(&self) -> Option<Vec<T>>[src]
Copies contents of self into the given Vec.
pub fn into_vec<T: Any>(self) -> Option<Vec<T>>[src]
An alternative to using the Into trait. This function helps the compiler
determine the type T automatically.
pub fn as_slice<T: Any>(&self) -> Option<&[T]>[src]
Convert this buffer into a typed slice.
Returs None if the given type T doesn't match the internal.
pub fn as_mut_slice<T: Any>(&mut self) -> Option<&mut [T]>[src]
Convert this buffer into a typed mutable slice.
Returs None if the given type T doesn't match the internal.
pub fn get<T: Elem>(&self, i: usize) -> Option<T>[src]
Get i'th element of the buffer by value.
pub fn get_ref<T: Any>(&self, i: usize) -> Option<&T>[src]
Get a const reference to the i'th element of the buffer.
pub fn get_mut<T: Any>(&mut self, i: usize) -> Option<&mut T>[src]
Get a mutable reference to the i'th element of the buffer.
pub fn append(&mut self, buf: &mut VecCopy) -> Option<&mut Self>[src]
Move elements from buf to this buffer.
The given buffer must have the same underlying type as self.
pub fn rotate_left(&mut self, mid: usize)[src]
Rotates the slice in-place such that the first mid elements of the slice move to the end
while the last self.len() - mid elements move to the front. After calling rotate_left,
the element previously at index mid will become the first element in the slice.
Example
use dync::*; let mut buf = VecCopy::from_vec(vec![1u32,2,3,4,5]); buf.rotate_left(3); assert_eq!(buf.as_slice::<u32>().unwrap(), &[4,5,1,2,3]);
pub fn rotate_right(&mut self, k: usize)[src]
Rotates the slice in-place such that the first self.len() - k elements of the slice move
to the end while the last k elements move to the front. After calling rotate_right, the
element previously at index k will become the first element in the slice.
Example
use dync::*; let mut buf = VecCopy::from_vec(vec![1u32,2,3,4,5]); buf.rotate_right(3); assert_eq!(buf.as_slice::<u32>().unwrap(), &[3,4,5,1,2]);
pub fn value_ref(&self, i: usize) -> CopyValueRef[src]
Get a reference to a value stored in this container at index i.
pub fn value_mut(&mut self, i: usize) -> CopyValueMut[src]
Get a mutable reference to a value stored in this container at index i.
pub fn iter_value_ref<'a>(
&'a self
) -> impl Iterator<Item = CopyValueRef<'a>> + 'a[src]
&'a self
) -> impl Iterator<Item = CopyValueRef<'a>> + 'a
Return an iterator over untyped value references stored in this buffer.
In contrast to iter, this function defers downcasting on a per element basis.
As a result, this type of iteration is typically less efficient if a typed value is needed
for each element.
Examples
use dync::VecCopy; let vec = vec![1.0_f32, 23.0, 0.01, 42.0, 11.43]; let buf = VecCopy::from(vec.clone()); // Convert into buffer for (i, val) in buf.iter_value_ref().enumerate() { assert_eq!(val.downcast::<f32>().unwrap(), &vec[i]); }
pub fn iter_value_mut<'a>(
&'a mut self
) -> impl Iterator<Item = CopyValueMut<'a>> + 'a[src]
&'a mut self
) -> impl Iterator<Item = CopyValueMut<'a>> + 'a
Return an iterator over untyped value references stored in this buffer.
In contrast to iter, this function defers downcasting on a per element basis.
As a result, this type of iteration is typically less efficient if a typed value is needed
for each element.
Examples
use dync::*; let vec = vec![1.0_f32, 23.0, 0.01, 42.0, 11.43]; let mut buf = VecCopy::from(vec.clone()); // Convert into buffer for (i, val) in buf.iter_value_mut().enumerate() { val.copy(CopyValueRef::new(&100.0f32)); } assert_eq!(buf.into_vec::<f32>().unwrap(), vec![100.0f32; 5]);
pub fn push_value(&mut self, value: CopyValueRef) -> Option<&mut Self>[src]
Push a value to this VecCopy by reference and return a mutable reference to Self.
If the type of the value doesn't match the internal element type.
Panics
This function panics if the size of the given value doesn't match the size of the stored value.
impl VecCopy[src]
pub fn reserve_bytes(&mut self, additional: usize)[src]
Reserves capacity for at least additional more bytes to be inserted in this buffer.
pub unsafe fn get_unchecked<T: Elem>(&self, i: usize) -> T[src]
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.
Safety
It is assumed that that the buffer contains elements of type T, otherwise this function
will cause undefined behavior.
pub unsafe fn get_unchecked_ref<T: Elem>(&self, i: usize) -> &T[src]
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.
Safety
It is assumed that that the buffer contains elements of type T, otherwise this function
will cause undefined behavior.
pub unsafe fn get_unchecked_mut<T: Elem>(&mut self, i: usize) -> &mut T[src]
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.
Safety
It is assumed that that the buffer contains elements of type T, otherwise this function
will cause undefined behavior.
pub fn get_bytes(&self, i: usize) -> &[u8][src]
Get a const reference to the byte slice of the i'th element of the buffer.
pub unsafe fn get_bytes_mut(&mut self, i: usize) -> &mut [u8][src]
Get a mutable reference to the byte slice of the i'th element of the buffer.
Safety
This function is marked as unsafe since the returned bytes may be modified arbitrarily, which may potentially produce malformed values.
pub unsafe fn reinterpret_into_vec<T>(self) -> Vec<T>[src]
Move buffer data to a vector with a given type, reinterpreting the data type as required.
Safety
The underlying data must be correctly represented by a Vec<T>.
pub unsafe fn reinterpret_as_slice<T>(&self) -> &[T][src]
Borrow buffer data and reinterpret it as a slice of a given type.
Safety
The underlying data must be correctly represented by a &[T] when borrowed as&[u8].
pub unsafe fn reinterpret_as_mut_slice<T>(&mut self) -> &mut [T][src]
Mutably borrow buffer data and reinterpret it as a mutable slice of a given type.
Safety
The underlying data must be correctly represented by a &mut [T] when borrowed as&mut [u8].
pub unsafe fn reinterpret_iter<T>(&self) -> Iter<T>[src]
Borrow buffer data and iterate over reinterpreted underlying data.
Safety
Each underlying element must be correctly represented by a &T when borrowed as &[u8].
pub unsafe fn reinterpret_iter_mut<T>(&mut self) -> IterMut<T>[src]
Mutably borrow buffer data and mutably iterate over reinterpreted underlying data.
Safety
Each underlying element must be correctly represented by a &mut T when borrowed as &mut [u8].
pub fn as_bytes(&self) -> &[u8][src]
Peek at the internal representation of the data.
pub unsafe fn as_bytes_mut(&mut self) -> &mut [u8][src]
Get a mutable reference to the internal data representation.
Safety
This function is marked as unsafe since the returned bytes may be modified arbitrarily, which may potentially produce malformed values.
pub fn byte_chunks<'a>(&'a self) -> impl Iterator<Item = &'a [u8]> + 'a[src]
Iterate over chunks type sized chunks of bytes without interpreting them.
This avoids needing to know what type data you're dealing with. This type of iterator is useful for transferring data from one place to another for a generic buffer.
pub unsafe fn byte_chunks_mut<'a>(
&'a mut self
) -> impl Iterator<Item = &'a mut [u8]> + 'a[src]
&'a mut self
) -> impl Iterator<Item = &'a mut [u8]> + 'a
Mutably iterate over chunks type sized chunks of bytes without interpreting them. This avoids needing to know what type data you're dealing with. This type of iterator is useful for transferring data from one place to another for a generic buffer, or modifying the underlying untyped bytes (e.g. bit twiddling).
Safety
This function is marked as unsafe since the returned bytes may be modified arbitrarily, which may potentially produce malformed values.
pub unsafe fn push_bytes(&mut self, bytes: &[u8]) -> Option<&mut Self>[src]
Add bytes to this buffer.
If the size of the given slice coincides with the number of bytes occupied by the
underlying element type, then these bytes are added to the underlying data buffer and a
mutable reference to the buffer is returned.
Otherwise, None is returned, and the buffer remains unmodified.
Safety
It is assumed that that the given bytes slice is a valid representation of the element
types stored in this buffer. Otherwise this function will cause undefined behavior.
pub unsafe fn extend_bytes(&mut self, bytes: &[u8]) -> Option<&mut Self>[src]
Add bytes to this buffer.
If the size of the given slice is a multiple of the number of bytes occupied by the
underlying element type, then these bytes are added to the underlying data buffer and a
mutable reference to the buffer is returned.
Otherwise, None is returned and the buffer is unmodified.
Safety
It is assumed that that the given bytes slice is a valid representation of a contiguous
collection of elements with the same type as stored in this buffer. Otherwise this function
will cause undefined behavior.
pub unsafe fn append_bytes(&mut self, bytes: &mut Vec<u8>) -> Option<&mut Self>[src]
Move bytes to this buffer.
If the size of the given vector is a multiple of the number of bytes occupied by the
underlying element type, then these bytes are moved to the underlying data buffer and a
mutable reference to the buffer is returned.
Otherwise, None is returned and both the buffer and the input vector remain unmodified.
Safety
It is assumed that that the given bytes Vec is a valid representation of a contiguous
collection of elements with the same type as stored in this buffer. Otherwise this function
will cause undefined behavior.
Trait Implementations
impl Clone for VecCopy[src]
impl Debug for VecCopy[src]
impl<'a> Extend<CopyValueRef<'a>> for VecCopy[src]
fn extend<T: IntoIterator<Item = CopyValueRef<'a>>>(&mut self, iter: T)[src]
impl<'a, T> From<&'a [T]> for VecCopy where
T: Elem, [src]
T: Elem,
Convert a &[T] to a VecCopy.
impl<T> From<Vec<T>> for VecCopy where
T: Elem, [src]
T: Elem,
Convert a Vec<T> to a VecCopy.
impl<'a> FromIterator<CopyValueRef<'a>> for VecCopy[src]
fn from_iter<T: IntoIterator<Item = CopyValueRef<'a>>>(iter: T) -> Self[src]
impl Hash for VecCopy[src]
fn hash<__H: Hasher>(&self, state: &mut __H)[src]
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher, 1.3.0[src]
H: Hasher,
impl<T> Into<Option<Vec<T>>> for VecCopy where
T: Elem, [src]
T: Elem,
Convert a VecCopy to a Option<Vec<T>>.
impl PartialEq<VecCopy> for VecCopy[src]
impl StructuralPartialEq for VecCopy[src]
Auto Trait Implementations
impl RefUnwindSafe for VecCopy
impl Send for VecCopy
impl Sync for VecCopy
impl Unpin for VecCopy
impl UnwindSafe for VecCopy
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized, [src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized, [src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized, [src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T[src]
impl<T> From<T> for T[src]
impl<T, U> Into<U> for T where
U: From<T>, [src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone, [src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T[src]
fn clone_into(&self, target: &mut T)[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>, [src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>, [src]
U: TryFrom<T>,