pub struct AnonVec { /* private fields */ }Expand description
An Anonymously typed Vector.
Internally, AnonVec is a VecT 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
impl AnonVec
Sourcepub fn new<T>() -> Selfwhere
T: Any + 'static,
pub fn new<T>() -> Selfwhere
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);Sourcepub fn from_size(size: usize, typeid: TypeId) -> Self
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);Sourcepub fn uninit() -> Self
pub fn uninit() -> Self
Creates an Uninitialized Anonymously Typed Vector
MUST be initialized before access by calling init::
§Usage
use anon_vec::AnonVec;
let mut vec = AnonVec::uninit();
if vec.is_uninit() {
vec.init::<i32>();
}
// do stuff with anon_vecSourcepub fn init<T>(&mut self)where
T: Any + 'static,
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_vecSourcepub fn init_size(&mut self, size: usize, typeid: TypeId)
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_vecSourcepub fn get_ref<T>(&self, index: usize) -> &Twhere
T: Any + 'static,
pub fn get_ref<T>(&self, index: usize) -> &Twhere
T: Any + 'static,
Get a reference to the interior value at index as T.
Sourcepub fn get_mut<T>(&mut self, index: usize) -> &mut Twhere
T: Any + 'static,
pub fn get_mut<T>(&mut self, index: usize) -> &mut Twhere
T: Any + 'static,
Get a mutable reference to the interior value at index as T.
Sourcepub fn reserve_bytes(&mut self, additional: usize)
pub fn reserve_bytes(&mut self, additional: usize)
Reserves additional number of BYTES.
If you want to reserve size_of::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);Sourcepub fn reserve(&mut self, additional: usize)
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);Sourcepub fn push<T>(&mut self, val: T)where
T: Any + 'static,
pub fn push<T>(&mut self, val: T)where
T: Any + 'static,
Appends an element to the back of this AnonVec.
Sourcepub fn push_anon(&mut self, anon: Anon)
pub fn push_anon(&mut self, anon: Anon)
Appends an anonymous element to the back of this AnonVec.
Sourcepub fn insert<T>(&mut self, val: T, index: usize)
pub fn insert<T>(&mut self, val: T, index: usize)
Inserts an element at index, moving all elements after it to the right.
Sourcepub fn insert_anon(&mut self, anon: Anon, index: usize)
pub fn insert_anon(&mut self, anon: Anon, index: usize)
Inserts an anonymous element at index, moving all elements after it to the right.
Sourcepub fn remove_get<T>(&mut self, index: usize) -> T
pub fn remove_get<T>(&mut self, index: usize) -> T
Removes and returns the element at index.
Sourcepub fn remove_get_anon(&mut self, index: usize) -> Anon
pub fn remove_get_anon(&mut self, index: usize) -> Anon
Removes and returns the element at index as an anonymous type.
Sourcepub fn pop_anon(&mut self) -> Option<Anon>
pub fn pop_anon(&mut self) -> Option<Anon>
Pops off and returns the last element in the Vec as an Anon.
Sourcepub fn remove_swap(&mut self, index: usize)
pub fn remove_swap(&mut self, index: usize)
Remove the last element after copying it into index.
MUCH faster than remove, in certain situations.
Sourcepub fn iter_mut<T>(&mut self) -> AnonIterMut<T> ⓘ
pub fn iter_mut<T>(&mut self) -> AnonIterMut<T> ⓘ
Mutably Iterate over this AnonVec as T.