pub struct AnyVec { /* private fields */ }Expand description
A type-erased vector that stores values of a single type.
AnyVec enables storing and retrieving vectors of any type that implements
Send + Sync without knowing the type at compile time. It uses a function pointer
for type-specific drop semantics and TypeId for runtime type verification.
§Type Erasure Pattern
Values are stored behind a trait object and retrieved via type-safe downcasting.
The get::<T>() method verifies the type at runtime before returning a slice reference.
§Smart Pointer Types
The module provides two smart pointer types for vector access:
AnyVecRef- Immutable reference to aVec<T>AnyVecMutRef- Mutable reference to aVec<T>
§Examples
use any_container::AnyVec;
let mut vec = AnyVec::new::<i32>();
assert!(vec.is_empty());
vec.get_mut::<i32>().unwrap().push(42);
assert_eq!(vec.len(), 1);
assert_eq!(vec.get::<i32>().unwrap(), &[42]);
assert!(vec.get::<f64>().is_none()); // Type mismatch returns NoneImplementations§
Source§impl AnyVec
impl AnyVec
Sourcepub fn new<T: 'static + Send + Sync>() -> Self
pub fn new<T: 'static + Send + Sync>() -> Self
Creates a new empty AnyVec for elements of type T.
Sourcepub fn new_with_capacity<T: 'static + Send + Sync>(capacity: usize) -> Self
pub fn new_with_capacity<T: 'static + Send + Sync>(capacity: usize) -> Self
Creates a new AnyVec with the specified capacity for elements of type T.
Sourcepub fn from_vec<T: 'static + Send + Sync>(vec: Vec<T>) -> Self
pub fn from_vec<T: 'static + Send + Sync>(vec: Vec<T>) -> Self
Creates a new AnyVec from a Vec<T>, consuming the vector and taking ownership of its memory.
Sourcepub fn elem_type_id(&self) -> TypeId
pub fn elem_type_id(&self) -> TypeId
Returns the type id of the elements stored in the AnyVec.
Sourcepub fn type_name(&self) -> &'static str
pub fn type_name(&self) -> &'static str
Returns the type name of the elements stored in the AnyVec.
Sourcepub fn get<T: 'static>(&self) -> Option<&[T]>
pub fn get<T: 'static>(&self) -> Option<&[T]>
Returns a reference to the elements in the AnyVec as a slice of type T.
Returns None if the stored type doesn’t match T.
Sourcepub unsafe fn get_unchecked<T: 'static>(&self) -> &[T]
pub unsafe fn get_unchecked<T: 'static>(&self) -> &[T]
Returns a reference to the elements in the AnyVec as a slice of type T.
§Safety
The caller must ensure that the type T matches the type of the elements stored in the
AnyVec. If the type is different, this can lead to undefined behaviour.
Sourcepub fn get_ref<T: 'static>(&self) -> Option<AnyVecRef<'_, T>>
pub fn get_ref<T: 'static>(&self) -> Option<AnyVecRef<'_, T>>
Returns a smart pointer, dereferencing to a Vec<T>, allowing access to the elements in
the AnyVec as a vector of type T.
§Note
In most cases, you want AnyVec::get or AnyVec::get_mut instead.
Sourcepub unsafe fn get_ref_unchecked<T: 'static>(&self) -> AnyVecRef<'_, T>
pub unsafe fn get_ref_unchecked<T: 'static>(&self) -> AnyVecRef<'_, T>
Returns a smart pointer, dereferencing to a Vec<T>, allowing access to the elements in
the AnyVec as a vector of type T.
§Note
In most cases, you want AnyVec::get_unchecked or AnyVec::get_mut_unchecked instead.
§Safety
The caller must ensure that the type T matches the type of the elements stored in the
AnyVec. If the type is different, this can lead to undefined behaviour.
Sourcepub fn get_mut<T: 'static>(&mut self) -> Option<AnyVecMutRef<'_, T>>
pub fn get_mut<T: 'static>(&mut self) -> Option<AnyVecMutRef<'_, T>>
Returns a smart pointer, dereferencing to a mutable Vec<T>, allowing access to the elements in
the AnyVec as a mutable vector of type T.
Sourcepub unsafe fn get_mut_unchecked<T: 'static>(&mut self) -> AnyVecMutRef<'_, T>
pub unsafe fn get_mut_unchecked<T: 'static>(&mut self) -> AnyVecMutRef<'_, T>
Returns a smart pointer, dereferencing to a mutable Vec<T>, allowing access to the elements in
the AnyVec as a mutable vector of type T.
§Safety
The caller must ensure that the type T matches the type of the elements stored in the
AnyVec. If the type is different, this can lead to undefined behaviour.
Sourcepub fn try_into_vec<T: 'static>(self) -> Result<Vec<T>, Self>
pub fn try_into_vec<T: 'static>(self) -> Result<Vec<T>, Self>
Consumes the AnyVec and returns the elements as a Vec<T>.
Sourcepub unsafe fn into_vec_unchecked<T: 'static>(self) -> Vec<T>
pub unsafe fn into_vec_unchecked<T: 'static>(self) -> Vec<T>
Consumes the AnyVec and returns the elements as a Vec<T>.
§Safety
The caller must ensure that the type T matches the type of the elements stored in the
AnyVec. If the type is different, this can lead to undefined behaviour.