Skip to main content

AnyVec

Struct AnyVec 

Source
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:

§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 None

Implementations§

Source§

impl AnyVec

Source

pub fn new<T: 'static + Send + Sync>() -> Self

Creates a new empty AnyVec for elements of type T.

Source

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.

Source

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.

Source

pub fn elem_type_id(&self) -> TypeId

Returns the type id of the elements stored in the AnyVec.

Source

pub fn type_name(&self) -> &'static str

Returns the type name of the elements stored in the AnyVec.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

§Note

If the smart pointer type is leaked, any values stored in the AnyVec will be leaked as well and new calls will return an empty vector.

To ensure that no values are lost, ensure that the destructor of the smart pointer can run.

Source

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.

§Note

If the smart pointer type is leaked, any values stored in the AnyVec will be leaked as well and new calls will return an empty vector.

To ensure that no values are lost, ensure that the destructor of the smart pointer can run.

Source

pub fn len(&self) -> usize

Returns the number of elements in the AnyVec.

Source

pub fn is_empty(&self) -> bool

Returns true if the AnyVec contains no elements.

Source

pub fn try_into_vec<T: 'static>(self) -> Result<Vec<T>, Self>

Consumes the AnyVec and returns the elements as a Vec<T>.

Source

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.

Trait Implementations§

Source§

impl Debug for AnyVec

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for AnyVec

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Send for AnyVec

Source§

impl Sync for AnyVec

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.