pub struct VecCopy<V = ()>where
V: ?Sized,{ /* private fields */ }
Expand description
Buffer of untyped Copy
values.
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
The data representing a type is 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.
It is an error to share this type between independently compiled binaries since TypeId
s
are not stable, and thus reinterpreting the values may not work as expected.
Implementations§
source§impl<V> VecCopy<V>
impl<V> VecCopy<V>
sourcepub fn with_type<T: CopyElem>() -> Selfwhere
V: VTable<T>,
pub fn with_type<T: CopyElem>() -> Selfwhere
V: VTable<T>,
Construct an empty VecCopy
with a specific type.
sourcepub fn with_capacity<T: CopyElem>(n: usize) -> Selfwhere
V: VTable<T>,
pub fn with_capacity<T: CopyElem>(n: usize) -> Selfwhere
V: VTable<T>,
Construct an empty VecCopy
with a capacity for a given number of typed elements. For
setting byte capacity use with_byte_capacity
.
sourcepub fn with_size<T: CopyElem>(n: usize, def: T) -> Selfwhere
V: VTable<T>,
pub fn with_size<T: CopyElem>(n: usize, def: T) -> Selfwhere
V: VTable<T>,
Construct a typed VecCopy
with a given size and filled with the specified default
value.
Examples
use dync::VecCopy;
let buf: VecCopy = 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]);
sourcepub fn from_vec<T: CopyElem>(vec: Vec<T>) -> Selfwhere
V: VTable<T>,
pub fn from_vec<T: CopyElem>(vec: Vec<T>) -> Selfwhere
V: VTable<T>,
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 = 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);
sourcepub fn from_slice<T: CopyElem>(slice: &[T]) -> Selfwhere
V: VTable<T>,
pub fn from_slice<T: CopyElem>(slice: &[T]) -> Selfwhere
V: VTable<T>,
Construct a VecCopy
from a given slice by copying the data.
source§impl<V: ?Sized> VecCopy<V>
impl<V: ?Sized> VecCopy<V>
sourcepub fn with_type_from<'a>(other: impl Into<Meta<VTableRef<'a, V>>>) -> Selfwhere
V: Clone + 'a,
pub fn with_type_from<'a>(other: impl Into<Meta<VTableRef<'a, V>>>) -> Selfwhere
V: Clone + 'a,
Construct a VecCopy
with the same type as the given buffer without copying its data.
sourcepub unsafe fn from_raw_parts(data: VecVoid, vtable: Ptr<V>) -> VecCopy<V>
pub unsafe fn from_raw_parts(data: VecVoid, vtable: Ptr<V>) -> VecCopy<V>
Construct a SliceCopy
from raw bytes and type metadata.
Safety
Almost exclusively the only inputs that are safe here are the ones returned by
into_raw_parts
.
This function should not be used other than in internal APIs. It exists to enable the
into_dyn
macro until CoerceUsize
is stabilized.
sourcepub fn into_raw_parts(self) -> (VecVoid, Ptr<V>)
pub fn into_raw_parts(self) -> (VecVoid, Ptr<V>)
Convert this collection into its raw components.
This function exists mainly to enable the into_dyn
macro until CoerceUnsized
is
stabilized.
sourcepub fn upcast<U: From<V>>(self) -> VecCopy<U>where
V: Clone,
pub fn upcast<U: From<V>>(self) -> VecCopy<U>where
V: Clone,
Upcast the VecCopy
into a more general base VecCopy
.
This function converts the underlying virtual function table into a subset of the existing
pub fn upcast_with<U>(self, f: impl FnOnce(V) -> U) -> VecCopy<U>where
V: Clone,
sourcepub fn resize<T: CopyElem>(
&mut self,
new_len: usize,
value: T
) -> Option<&mut Self>
pub fn resize<T: CopyElem>( &mut self, new_len: usize, value: T ) -> Option<&mut Self>
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
.
sourcepub fn copy_from_slice<T: CopyElem>(&mut self, slice: &[T]) -> Option<&mut Self>
pub fn copy_from_slice<T: CopyElem>(&mut self, slice: &[T]) -> Option<&mut Self>
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
.
sourcepub fn fill<T: CopyElem>(&mut self, def: T) -> Option<&mut Self>
pub fn fill<T: CopyElem>(&mut self, def: T) -> Option<&mut Self>
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 = VecCopy::from_vec(vec.clone()); // Convert into buffer
buf.fill(0u8);
assert_eq!(buf.into_vec::<u8>().unwrap(), vec![0u8, 0, 0, 0, 0]);
sourcepub fn push_as<T: Any>(&mut self, element: T) -> Option<&mut Self>
pub fn push_as<T: Any>(&mut self, element: T) -> Option<&mut Self>
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.
sourcepub fn check<T: Any>(self) -> Option<Self>
pub fn check<T: Any>(self) -> Option<Self>
Check if the current buffer contains elements of the specified type. Returns Some(self)
if the type matches and None
otherwise.
sourcepub fn check_ref<T: Any>(&self) -> Option<&Self>
pub fn check_ref<T: Any>(&self) -> Option<&Self>
Check if the current buffer contains elements of the specified type. Returns None
if the
check fails, otherwise a reference to self is returned.
sourcepub fn check_mut<T: Any>(&mut self) -> Option<&mut Self>
pub fn check_mut<T: Any>(&mut self) -> Option<&mut Self>
Check if the current buffer contains elements of the specified type. Same as check_ref
but consumes and produces a mut reference to self.
sourcepub fn element_type_id(&self) -> TypeId
pub fn element_type_id(&self) -> TypeId
Get the TypeId
of data stored within this buffer.
sourcepub fn iter_as<T: Any>(&self) -> Option<Iter<'_, T>>
pub fn iter_as<T: Any>(&self) -> Option<Iter<'_, T>>
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 = VecCopy::from(vec.clone()); // Convert into buffer
for (i, &val) in buf.iter_as::<f32>().unwrap().enumerate() {
assert_eq!(val, vec[i]);
}
sourcepub fn iter_mut_as<T: Any>(&mut self) -> Option<IterMut<'_, T>>
pub fn iter_mut_as<T: Any>(&mut self) -> Option<IterMut<'_, T>>
Return an iterator to a mutable slice representing typed data.
Returns None
if the given type T
doesn’t match the internal.
sourcepub fn append_copy_to_vec<'a, T: CopyElem>(
&self,
vec: &'a mut Vec<T>
) -> Option<&'a mut Vec<T>>
pub fn append_copy_to_vec<'a, T: CopyElem>( &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.
sourcepub fn copy_into_vec<T: CopyElem>(&self) -> Option<Vec<T>>
pub fn copy_into_vec<T: CopyElem>(&self) -> Option<Vec<T>>
Copies contents of self
into the given Vec
.
sourcepub fn into_vec<T: Any>(self) -> Option<Vec<T>>
pub fn into_vec<T: Any>(self) -> Option<Vec<T>>
Convert this vector in to a Vec<T>
.
This is like using the Into
trait, but it helps the compiler
determine the type T
automatically.
This function returns None
if T
is not the same as the T
that
this vector was created with.
sourcepub fn as_slice_as<T: Any>(&self) -> Option<&[T]>
pub fn as_slice_as<T: Any>(&self) -> Option<&[T]>
Convert this buffer into a typed slice.
Returns None
if the given type T
doesn’t match the internal.
sourcepub fn as_mut_slice_as<T: Any>(&mut self) -> Option<&mut [T]>
pub fn as_mut_slice_as<T: Any>(&mut self) -> Option<&mut [T]>
Convert this buffer into a typed mutable slice.
Returs None
if the given type T
doesn’t match the internal.
sourcepub fn get_as<T: CopyElem>(&self, i: usize) -> Option<T>
pub fn get_as<T: CopyElem>(&self, i: usize) -> Option<T>
Get i
’th element of the buffer by value.
sourcepub fn get_ref_as<T: Any>(&self, i: usize) -> Option<&T>
pub fn get_ref_as<T: Any>(&self, i: usize) -> Option<&T>
Get a const
reference to the i
’th element of the buffer.
sourcepub fn get_mut_as<T: Any>(&mut self, i: usize) -> Option<&mut T>
pub fn get_mut_as<T: Any>(&mut self, i: usize) -> Option<&mut T>
Get a mutable reference to the i
’th element of the buffer.
sourcepub fn append(&mut self, buf: &mut VecCopy<V>) -> Option<&mut Self>
pub fn append(&mut self, buf: &mut VecCopy<V>) -> Option<&mut Self>
Move elements from buf
to this buffer.
The given buffer must have the same underlying type as self
.
sourcepub fn rotate_left(&mut self, mid: usize)
pub fn rotate_left(&mut self, mid: usize)
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 = VecCopy::from_vec(vec![1u32,2,3,4,5]);
buf.rotate_left(3);
assert_eq!(buf.as_slice_as::<u32>().unwrap(), &[4,5,1,2,3]);
sourcepub fn rotate_right(&mut self, k: usize)
pub fn rotate_right(&mut self, k: usize)
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 = VecCopy::from_vec(vec![1u32,2,3,4,5]);
buf.rotate_right(3);
assert_eq!(buf.as_slice_as::<u32>().unwrap(), &[3,4,5,1,2]);
sourcepub fn get_ref(&self, i: usize) -> CopyValueRef<'_, V>
pub fn get_ref(&self, i: usize) -> CopyValueRef<'_, V>
Get a reference to a value stored in this container at index i
.
sourcepub fn get_mut(&mut self, i: usize) -> CopyValueMut<'_, V>
pub fn get_mut(&mut self, i: usize) -> CopyValueMut<'_, V>
Get a mutable reference to a value stored in this container at index i
.
sourcepub fn iter(&self) -> impl Iterator<Item = CopyValueRef<'_, V>>
pub fn iter(&self) -> impl Iterator<Item = CopyValueRef<'_, V>>
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 = VecCopy::from(vec.clone()); // Convert into buffer
for (i, val) in buf.iter().enumerate() {
assert_eq!(val.downcast::<f32>().unwrap(), &vec[i]);
}
sourcepub fn iter_mut(&mut self) -> impl Iterator<Item = CopyValueMut<'_, V>>
pub fn iter_mut(&mut self) -> impl Iterator<Item = CopyValueMut<'_, V>>
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 = VecCopy::from(vec.clone()); // Convert into buffer
for (i, val) in buf.iter_mut().enumerate() {
val.copy(CopyValueRef::new(&100.0f32));
}
assert_eq!(buf.into_vec::<f32>().unwrap(), vec![100.0f32; 5]);
sourcepub fn push<U>(&mut self, value: CopyValueRef<'_, U>) -> Option<&mut Self>
pub fn push<U>(&mut self, value: CopyValueRef<'_, U>) -> Option<&mut Self>
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, return None
.
Note that it is not necessary for vtables of the value and this vector to match. IF the types coincide, we know that either of the vtables is valid, so we just stick with the one we already have in the container.
Panics
This function panics if the size of the given value doesn’t match the size of the stored value.
pub fn as_slice(&self) -> SliceCopy<'_, V>
pub fn as_mut_slice(&mut self) -> SliceCopyMut<'_, V>
source§impl<V: ?Sized> VecCopy<V>
impl<V: ?Sized> VecCopy<V>
sourcepub unsafe fn get_unchecked_ref<T: Any>(&self, i: usize) -> &T
pub unsafe fn get_unchecked_ref<T: Any>(&self, i: usize) -> &T
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 i
th 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
and that i
is strictly
less than the length of this vector, otherwise this function will cause undefined behavior.
sourcepub unsafe fn get_unchecked_mut<T: Any>(&mut self, i: usize) -> &mut T
pub unsafe fn get_unchecked_mut<T: Any>(&mut self, i: usize) -> &mut T
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 i
th 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
and that i
is strictly
less than the length of this vector, otherwise this function will cause undefined behavior.
Trait Implementations§
source§impl<'a, V: ?Sized + 'a> Extend<CopyValueRef<'a, V>> for VecCopy<V>
impl<'a, V: ?Sized + 'a> Extend<CopyValueRef<'a, V>> for VecCopy<V>
source§fn extend<T: IntoIterator<Item = CopyValueRef<'a, V>>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = CopyValueRef<'a, V>>>(&mut self, iter: T)
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)source§impl<T, V: ?Sized> From<VecCopy<V>> for Option<Vec<T>>where
T: CopyElem,
impl<T, V: ?Sized> From<VecCopy<V>> for Option<Vec<T>>where
T: CopyElem,
Convert a VecCopy
to a Option<Vec<T>>
.
source§impl<'a, V: Clone + ?Sized + 'a> FromIterator<CopyValueRef<'a, V>> for VecCopy<V>
impl<'a, V: Clone + ?Sized + 'a> FromIterator<CopyValueRef<'a, V>> for VecCopy<V>
source§fn from_iter<T: IntoIterator<Item = CopyValueRef<'a, V>>>(iter: T) -> Self
fn from_iter<T: IntoIterator<Item = CopyValueRef<'a, V>>>(iter: T) -> Self
Construct a VecCopy
type from a non-empty iterator.
Panics
This function will panic if the given iterator is empty.
This is because we don’t know the element types until we see one since
the types are erased in both CopyValueRef
and VecCopy
.
Auto Trait Implementations§
impl<V: ?Sized> RefUnwindSafe for VecCopy<V>where
V: RefUnwindSafe,
impl<V = ()> !Send for VecCopy<V>
impl<V = ()> !Sync for VecCopy<V>
impl<V: ?Sized> Unpin for VecCopy<V>
impl<V: ?Sized> UnwindSafe for VecCopy<V>where
V: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneBytes for Twhere
T: Clone + 'static,
impl<T> CloneBytes for Twhere
T: Clone + 'static,
unsafe fn clone_bytes(src: &[MaybeUninit<u8>]) -> Box<[MaybeUninit<u8>]>
unsafe fn clone_from_bytes(dst: &mut [MaybeUninit<u8>], src: &[MaybeUninit<u8>])
unsafe fn clone_into_raw_bytes( src: &[MaybeUninit<u8>], dst: &mut [MaybeUninit<u8>] )
source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.