AnonVec

Struct AnonVec 

Source
pub struct AnonVec { /* private fields */ }
Expand description

An Anonymously typed Vector.

Internally, AnonVec is a Vec. When pushing to an AnonVec, T is converted to *const u8. When getting from an AnonVec, *const u8 is converted to T.

§Usage

Anon Vec is intended for use in data systems where the type or size of the values stored cannot be known at compile-time. It is a more lax approach to Box<dyn Any>.

use anon_vec::AnonVec;
 
let mut anon = AnonVec::new::<i32>();
anon.push::<i32>(5);
anon.push::<i32>(10);
anon.push::<i32>(15);
 
let x = anon.get_ref::<i32>(1);

AnonVec can also work with Anon values.

use anon_vec::{AnonVec, Anon};
use std::mem::size_of;
use std::any::TypeId;
 
// Create AnonVec using the size and typeid.
let mut vec = AnonVec::from_size(size_of::<i32>(), TypeId::of::<i32>());
vec.push_anon(Anon::new::<i32>(5));
vec.push_anon(Anon::new::<i32>(10));
vec.push_anon(Anon::new::<i32>(15));
 
// move index 1 out and into `anon`.
let anon: Anon = vec.remove_get_anon(1);
 
let x: &i32 = anon.cast_ref::<i32>();

Implementations§

Source§

impl AnonVec

Source

pub fn new<T>() -> Self
where T: Any + 'static,

Creates a new Anonymously Typed Vector in-place.

§Usage
use anon_vec::AnonVec;
 
let mut anon = AnonVec::new::<i32>();
anon.push::<i32>(5);
Source

pub fn from_size(size: usize, typeid: TypeId) -> Self

Creates a new Anonymously Typed Vector using the size and TypeId of the value to be stored.

§Usage
use anon_vec::AnonVec;
use std::mem::size_of;
use std::any::TypeId;
 
let mut anon = AnonVec::from_size(size_of::<i32>(), TypeId::of::<i32>());
anon.push::<i32>(5);
Source

pub fn uninit() -> Self

Creates an Uninitialized Anonymously Typed Vector

MUST be initialized before access by calling init::. If you can’t call init::, call init_size instead.

§Usage
use anon_vec::AnonVec;
 
let mut vec = AnonVec::uninit();
 
if vec.is_uninit() {
    vec.init::<i32>();
}
 
// do stuff with anon_vec
Source

pub fn init<T>(&mut self)
where T: Any + 'static,

Initializes a previously uninitialized AnonVec.

§Usage
use anon_vec::AnonVec;
 
let mut vec = AnonVec::uninit();
 
if vec.is_uninit() {
    vec.init::<i32>();
}
 
// do stuff with anon_vec
Source

pub fn init_size(&mut self, size: usize, typeid: TypeId)

Initializes a previously uninitialized AnonVec.

§Usage
use anon_vec::AnonVec;
use std::mem::size_of;
use std::any::TypeId;
 
let mut vec = AnonVec::uninit();
 
if vec.is_uninit() {
    vec.init_size(size_of::<i32>(), TypeId::of::<i32>());
}
 
// do stuff with anon_vec
Source

pub fn typeid(&self) -> TypeId

The TypeId associated with this AnonVec.

Source

pub fn size(&self) -> usize

The size, in bytes, each element of this AnonVec holds.

Source

pub fn len(&self) -> usize

The number of elements this AnonVec holds. (as T)

Source

pub fn is_empty(&self) -> bool

Whether or not this AnonVec has a length of 0.

Source

pub fn is_uninit(&self) -> bool

Whether or not the size of this AnonVec is 0.

Source

pub fn get_ref<T>(&self, index: usize) -> &T
where T: Any + 'static,

Get a reference to the interior value at index as T.

Source

pub fn get_mut<T>(&mut self, index: usize) -> &mut T
where T: Any + 'static,

Get a mutable reference to the interior value at index as T.

Source

pub fn reserve_bytes(&mut self, additional: usize)

Reserves additional number of BYTES. If you want to reserve size_of::, use reserve instead.

Reserves capacity for at least additional more elements to be inserted in the given Vec<T>. The collection may reserve more space to speculatively avoid frequent reallocations. After calling reserve, capacity will be greater than or equal to self.len() + additional. Does nothing if capacity is already sufficient.

§Panics

Panics if the new capacity exceeds isize::MAX bytes.

§Examples
let mut vec = vec![1];
vec.reserve(10);
assert!(vec.capacity() >= 11);
Source

pub fn reserve(&mut self, additional: usize)

Reserves capacity for at least additional more elements to be inserted in the given Vec<T>. The collection may reserve more space to speculatively avoid frequent reallocations. After calling reserve, capacity will be greater than or equal to self.len() + additional. Does nothing if capacity is already sufficient.

§Panics

Panics if the new capacity exceeds isize::MAX bytes.

§Examples
let mut vec = vec![1];
vec.reserve(10);
assert!(vec.capacity() >= 11);
Source

pub fn push<T>(&mut self, val: T)
where T: Any + 'static,

Appends an element to the back of this AnonVec.

Source

pub fn push_anon(&mut self, anon: Anon)

Appends an anonymous element to the back of this AnonVec.

Source

pub fn insert<T>(&mut self, val: T, index: usize)

Inserts an element at index, moving all elements after it to the right.

Source

pub fn insert_anon(&mut self, anon: Anon, index: usize)

Inserts an anonymous element at index, moving all elements after it to the right.

Source

pub fn remove(&mut self, index: usize)

Removes an element at index.

Source

pub fn remove_get<T>(&mut self, index: usize) -> T
where T: Any + Clone + 'static,

Removes and returns the element at index.

Source

pub fn remove_get_anon(&mut self, index: usize) -> Anon

Removes and returns the element at index as an anonymous type.

Source

pub fn pop<T>(&mut self) -> Option<T>
where T: Any + Clone + 'static,

Pops off and returns the last element in the Vec.

Source

pub fn pop_anon(&mut self) -> Option<Anon>

Pops off and returns the last element in the Vec as an Anon.

Source

pub fn remove_swap(&mut self, index: usize)

Remove the last element after copying it into index. MUCH faster than remove, in certain situations.

Source

pub fn iter<T>(&self) -> AnonIter<T>

Immutably Iterate over this AnonVec as T.

Source

pub fn iter_mut<T>(&mut self) -> AnonIterMut<T>

Mutably Iterate over this AnonVec as T.

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.