pub struct AnyVec { /* private fields */ }Expand description
A type-erased vector containing values of the same type.
This vector would be useful when you need to hold vectors of heterogeneous types in a single variable. Instead, this vector has methods that looks quite dirty and not easy to use. Most methods are unsafe because they take or return pointer instead of concrete type.
Implementations§
Source§impl AnyVec
impl AnyVec
Sourcepub fn new(tinfo: TypeInfo) -> Self
pub fn new(tinfo: TypeInfo) -> Self
Creates a new empty vector.
§Examples
use my_ecs::{tinfo, ds::AnyVec};
let v = AnyVec::new(tinfo!(i32));Sourcepub const fn item_size(&self) -> usize
pub const fn item_size(&self) -> usize
Returns size in bytes of the item.
§Examples
use my_ecs::{tinfo, ds::AnyVec};
let v = AnyVec::new(tinfo!(i32));
assert_eq!(v.item_size(), std::mem::size_of::<i32>());Sourcepub const fn is_zst(&self) -> bool
pub const fn is_zst(&self) -> bool
Returns whether the item is zero-sized type or not.
§Examples
use my_ecs::{tinfo, ds::AnyVec};
let v = AnyVec::new(tinfo!(i32));
assert!(!v.is_zst());
let v = AnyVec::new(tinfo!(()));
assert!(v.is_zst());Sourcepub const fn align(&self) -> usize
pub const fn align(&self) -> usize
Returns alignment in bytes of the item.
§Examples
use my_ecs::{tinfo, ds::AnyVec};
let v = AnyVec::new(tinfo!(i32));
assert_eq!(v.align(), std::mem::align_of::<i32>());Sourcepub const fn fn_clone(&self) -> FnCloneRaw
pub const fn fn_clone(&self) -> FnCloneRaw
Returns raw clone function pointer.
Sourcepub fn is_type_of<T: 'static>(&self) -> bool
pub fn is_type_of<T: 'static>(&self) -> bool
Sourcepub const fn len(&self) -> usize
pub const fn len(&self) -> usize
Returns number of item.
§Examples
use my_ecs::{tinfo, ds::AnyVec};
let mut v = AnyVec::new(tinfo!(i32));
unsafe { v.push(0_i32) };
assert_eq!(v.len(), 1);Sourcepub const fn is_empty(&self) -> bool
pub const fn is_empty(&self) -> bool
Returns true if the vector is empty.
§Examples
use my_ecs::{tinfo, ds::AnyVec};
let v = AnyVec::new(tinfo!(i32));
assert!(v.is_empty());Sourcepub const fn capacity(&self) -> usize
pub const fn capacity(&self) -> usize
Returns capacity in bytes of the vector.
§Examples
use my_ecs::{tinfo, ds::AnyVec};
let mut v = AnyVec::new(tinfo!(i32));
v.reserve(10);
assert!(v.capacity() >= 10);Sourcepub fn iter_mut_of<T: 'static>(&mut self) -> IterMut<'_, T> ⓘ
pub fn iter_mut_of<T: 'static>(&mut self) -> IterMut<'_, T> ⓘ
Sourcepub fn par_iter_of<T: Send + Sync + 'static>(&self) -> ParIter<'_, T>
pub fn par_iter_of<T: Send + Sync + 'static>(&self) -> ParIter<'_, T>
Returns parallel iterator visiting all items.
Parallel iterator implements rayon’s parallel iterator traits, so
that it can be split into multiple CPU cores then consumed at the same
time.
§Panics
Panics if the given type is not the same as the vector contains.
§Examples
use my_ecs::prelude::*;
use my_ecs::ds::AnyVec;
let mut v = AnyVec::new(tinfo!(i32));
unsafe {
v.push(1_i32);
v.push(2_i32);
}
let sum: i32 = v.par_iter_of::<i32>().sum();
assert_eq!(sum, 3);Sourcepub fn par_iter_mut_of<T: Send + Sync + 'static>(&mut self) -> ParIterMut<'_, T>
pub fn par_iter_mut_of<T: Send + Sync + 'static>(&mut self) -> ParIterMut<'_, T>
Returns mutable parallel iterator visiting all items.
Parallel iterator implements rayon’s parallel iterator traits, so
that it can be split into multiple CPU cores then consumed at the same
time.
§Panics
Panics if the given type is not the same as the vector contains.
§Examples
use my_ecs::prelude::*;
use my_ecs::ds::AnyVec;
let mut v = AnyVec::new(tinfo!(i32));
unsafe {
v.push(1_i32);
v.push(2_i32);
}
let sum: i32 = v.par_iter_mut_of::<i32>().map(|x| *x + 1).sum();
assert_eq!(sum, 5);Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves additional capacity more than or equal to the given value.
If capacity of the vector is already sufficient, nothing takes place.
Otherwise, allocates new memory or reallocates the old memory so that
the capacity will be greater than or equal to self.len() + additional.
This method deliberately allocates more memory than requested to avoid
frequent reallocation.
§Panics
Panics if total memory in bytes after calling this method will exceed
isize::MAX.
§Examples
use my_ecs::{tinfo, ds::AnyVec};
let mut v = AnyVec::new(tinfo!(i32));
v.reserve(10);
assert!(v.capacity() >= 10);Sourcepub fn reserve_exact(&mut self, additional: usize)
pub fn reserve_exact(&mut self, additional: usize)
Reserves additional capacity as much as the given value.
If capacity of the vector is already sufficient, nothing takes place.
Otherwise, allocates new memory or reallocates the old memory so that
the capacity will be equal to self.len() + additional exactly.
Note, however, that allocator may give a memory block that is greater
than requested for some reason. The exact size of memory block is
invisible from clients, but you can look into it using something like
libc::malloc_usable_size.
§Examples
use my_ecs::{tinfo, ds::AnyVec};
let mut v = AnyVec::new(tinfo!(i32));
v.reserve(10);
assert_eq!(v.capacity(), 10);Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks capacity of the vector as much as possible.
§Examples
use my_ecs::{tinfo, ds::AnyVec};
let mut v = AnyVec::new(tinfo!(i32));
assert_eq!(v.capacity(), 0);
v.reserve(10);
assert!(v.capacity() >= 10);
unsafe { v.push(1_i32) };
v.shrink_to_fit();
assert!(v.capacity() >= 1);
v.pop_drop();
v.shrink_to_fit();
assert_eq!(v.capacity(), 0);Sourcepub unsafe fn set_len(&mut self, new_len: usize)
pub unsafe fn set_len(&mut self, new_len: usize)
Sets length of the vector to the given value without any additional operations.
§Safety
new_lenmust be less than or equal toself.capacity().- If
new_lenis greater than the previous length, caller must initialize the extended range with proper values. - If
new_lenis less than the previous length, caller must drop abandoned values from the vector properly.
§Examples
use my_ecs::{tinfo, ds::AnyVec};
let mut v = AnyVec::new(tinfo!(i32));
unsafe {
v.push(0_i32);
v.set_len(0);
}Sourcepub unsafe fn set_raw_unchecked(&mut self, index: usize, src: NonNull<u8>)
pub unsafe fn set_raw_unchecked(&mut self, index: usize, src: NonNull<u8>)
Copies data as much as AnyVec::item_size from src address to the
memory pointed by index.
Note that the old stored value is dropped before the copy, which means the given index must be in bounds.
§Safety
indexmust be in bounds.srcmust point to a valid type that the vector contains.- Memory range pointed by
indexmust not need to be dropped. srcmust not be dropped after calling this method because it is moved into the vector.
§Examples
use my_ecs::{tinfo, ds::AnyVec};
use std::ptr::NonNull;
let mut v = AnyVec::new(tinfo!(i32));
unsafe {
v.push(0_i32);
let value = 1_i32;
let ptr = (&value as *const i32 as *const u8).cast_mut();
v.set_raw_unchecked(0, NonNull::new(ptr).unwrap());
assert_eq!(v.pop(), Some(1_i32));
}Sourcepub unsafe fn push_raw(&mut self, src: NonNull<u8>)
pub unsafe fn push_raw(&mut self, src: NonNull<u8>)
Copies data as much as self.item_size() from src address to the end
of the vector.
If the vector doesn’t have sufficient capacity for the appended value,
then the vector increases its capacity first by calling
AnyVec::reserve which allocates memory more than just one value,
then does the copy.
§Safety
srcmust point to a valid type that the vector contains.srcmust not be dropped after calling this method because it is moved into the vector.
§Examples
use my_ecs::{tinfo, ds::AnyVec};
use std::ptr::NonNull;
let mut v = AnyVec::new(tinfo!(i32));
let value = 0x01020304_i32;
unsafe {
let ptr = (&value as *const i32 as *const u8).cast_mut();
v.push_raw(NonNull::new(ptr).unwrap());
assert_eq!(v.pop(), Some(value));
}Sourcepub unsafe fn push_with<F>(&mut self, write: F)
pub unsafe fn push_with<F>(&mut self, write: F)
Writes a value to the end of the vector by calling the given function.
If the vector doesn’t have sufficient capacity for the appended value,
then the vector increases its capacity first by calling
AnyVec::reserve which allocates memory more than just one value,
then does the write operation.
§Safety
writemust write a valid type that the vector contains.
§Examples
use my_ecs::{tinfo, ds::AnyVec};
use std::ptr::{self, NonNull};
let mut v = AnyVec::new(tinfo!(i32));
let value = 0x01020304_i32;
unsafe {
v.push_with(|dst| {
ptr::write(dst as *mut i32, value);
});
assert_eq!(v.pop(), Some(value));
}Sourcepub unsafe fn pop_raw(&mut self, buf: *mut u8) -> Option<()>
pub unsafe fn pop_raw(&mut self, buf: *mut u8) -> Option<()>
Removes the last item from the vector and writes it to the given
buffer, then returns Some.
If removing is successful, caller becomes to own the item in the
buffer, so that caller must call drop() on it correctly.
Otherwise, returns None without change to the buffer.
§Safety
Undefined behavior if conditions below are not met.
bufmust have enough size to be copied an item.- When
Someis returned,bufmust be correctly handled as an item. For example, if an item should be dropped, caller must call drop() on it. - When
Noneis returned,bufmust be correctly handled as it was.
§Examples
use my_ecs::{tinfo, ds::AnyVec};
let mut v = AnyVec::new(tinfo!(i32));
unsafe { v.push(42_i32) };
assert_eq!(v.len(), 1);
let mut buf = 0_i32;
let res = unsafe { v.pop_raw(&mut buf as *mut i32 as *mut u8) };
assert!(res.is_some());
assert!(v.is_empty());
assert_eq!(buf, 42);Sourcepub fn pop_drop(&mut self) -> Option<()>
pub fn pop_drop(&mut self) -> Option<()>
Removes the last item from the vector then drops it immediately.
§Examples
use my_ecs::{tinfo, ds::AnyVec};
let mut v = AnyVec::new(tinfo!(i32));
unsafe {
v.push(0_i32);
assert_eq!(v.pop_drop(), Some(()));
}Sourcepub fn pop_forget(&mut self) -> Option<()>
pub fn pop_forget(&mut self) -> Option<()>
Removes the last item from the vector without calling drop function on it.
§Safety
Rust safety doesn’t include calling drop function. See
forget for more information. However, caller must
guarantee that not calling drop function is fine for the item.
§Examples
use my_ecs::{tinfo, ds::AnyVec};
let mut v = AnyVec::new(tinfo!(i32));
unsafe {
v.push(0_i32);
assert_eq!(v.pop_forget(), Some(()));
}Sourcepub unsafe fn swap_remove_raw(&mut self, index: usize, buf: *mut u8)
pub unsafe fn swap_remove_raw(&mut self, index: usize, buf: *mut u8)
Removes an item at the given index from the vector and writes it to the given buffer.
Therefore, the item is actually moved from the vector to the given buffer. So caller must take care of calling drop on it.
§Panics
Panics if the given index is out of bounds.
§Safety
Undefined behavior if conditions below are not met.
bufmust have enough size to be copied an item.- When
Someis returned,bufmust be correctly handled as an item. For example, if an item should be dropped, caller must call drop() on it. - When
Noneis returned,bufmust be correctly handled as it was.
§Examples
use my_ecs::{tinfo, ds::AnyVec};
let mut v = AnyVec::new(tinfo!(i32));
unsafe {
v.push(0_i32);
v.push(1_i32);
v.push(2_i32);
}
assert_eq!(v.len(), 3);
let mut buf = 3_i32;
unsafe { v.swap_remove_raw(0, &mut buf as *mut i32 as *mut u8) };
assert_eq!(buf, 0);
unsafe {
assert_eq!(v.pop::<i32>(), Some(1));
assert_eq!(v.pop::<i32>(), Some(2));
}Sourcepub unsafe fn swap_remove<T: 'static>(&mut self, index: usize) -> T
pub unsafe fn swap_remove<T: 'static>(&mut self, index: usize) -> T
Removes an item at the given index from the vector and returns it.
Then the last item of the vector is moved to the vacant slot.
§Panics
Panics if the given index is out of bounds.
§Safety
Tmust be the same type as the vector contains.
§Examples
use my_ecs::{tinfo, ds::AnyVec};
let mut v = AnyVec::new(tinfo!(i32));
unsafe {
v.push(0_i32);
v.push(1_i32);
v.push(2_i32);
assert_eq!(v.swap_remove::<i32>(0), 0);
assert_eq!(v.swap_remove::<i32>(0), 2);
assert_eq!(v.swap_remove::<i32>(0), 1);
}Sourcepub fn swap_remove_drop(&mut self, index: usize)
pub fn swap_remove_drop(&mut self, index: usize)
Removes an item at the given index from the vector and drops it immediately.
Then the last item of the vector is moved to the vacant slot.
§Panics
Panics if the given index is out of bounds.
§Examples
use my_ecs::{tinfo, ds::AnyVec};
let mut v = AnyVec::new(tinfo!(i32));
unsafe {
v.push(0_i32);
v.swap_remove_drop(0);
assert!(v.is_empty());
}Sourcepub fn swap_remove_forget(&mut self, index: usize)
pub fn swap_remove_forget(&mut self, index: usize)
Removes an item at the given index from the vector without calling drop function on it.
Then the last item of the vector is moved to the vacant slot.
§Panics
Panics if given index is out of bounds.
§Safety
Rust safety doesn’t include calling drop function. See
forget for more information. However, caller must
guarantee that not calling drop function is fine for the item.
§Examples
use my_ecs::{tinfo, ds::AnyVec};
let mut v = AnyVec::new(tinfo!(i32));
unsafe {
v.push(0_i32);
v.swap_remove_forget(0);
assert!(v.is_empty());
}Sourcepub fn get_raw(&self, index: usize) -> Option<NonNull<u8>>
pub fn get_raw(&self, index: usize) -> Option<NonNull<u8>>
Returns a pointer to an item at the given index.
§Examples
use my_ecs::{tinfo, ds::AnyVec};
let mut v = AnyVec::new(tinfo!(i32));
let value = 0x01020304_i32;
unsafe { v.push(value) };
let ptr = v.get_raw(0).unwrap().cast::<i32>().as_ptr().cast_const();
unsafe {
assert_eq!(std::ptr::read(ptr), value);
}Sourcepub unsafe fn get_raw_unchecked(&self, index: usize) -> NonNull<u8>
pub unsafe fn get_raw_unchecked(&self, index: usize) -> NonNull<u8>
Returns a pointer to an item at the given index.
§Safety
- Index must be in bounds.
§Examples
use my_ecs::{tinfo, ds::AnyVec};
let mut v = AnyVec::new(tinfo!(i32));
let value = 0x01020304_i32;
unsafe {
v.push(value);
let ptr = v.get_raw_unchecked(0)
.cast::<i32>()
.as_ptr()
.cast_const();
assert_eq!(std::ptr::read(ptr), value);
}Sourcepub fn resize<T>(&mut self, new_len: usize, value: T)where
T: Clone + 'static,
pub fn resize<T>(&mut self, new_len: usize, value: T)where
T: Clone + 'static,
Resizes the vector to the given length.
If the new length is greater than previous length of the vector, then the vector is extended with the given value. Otherwise, the vector is shrunk.
§Panics
Panics if T is not the same type as the vector contains.
§Examples
use my_ecs::{tinfo, ds::AnyVec};
let mut v = AnyVec::new(tinfo!(i32));
unsafe {
v.resize(2, 0_i32);
assert_eq!(v.pop(), Some(0_i32));
assert_eq!(v.pop(), Some(0_i32));
assert!(v.is_empty());
}Sourcepub unsafe fn resize_raw(&mut self, new_len: usize, val_ptr: NonNull<u8>)
pub unsafe fn resize_raw(&mut self, new_len: usize, val_ptr: NonNull<u8>)
Resizes the vector to the given length.
If the new length is greater than previous length of the vector, then the vector is extended with clones of a value pointed by the given pointer. Otherwise, the vector is shrunk.
§Panics
Panics if vector item is not Clone.
§Safety
val_ptrmust point to a valid type that the vector contains.
§Examples
use my_ecs::{tinfo, ds::AnyVec};
use std::ptr::NonNull;
let mut v = AnyVec::new(tinfo!(i32));
let value = 0x01020304_i32;
unsafe {
let ptr = (&value as *const i32 as *const u8).cast_mut();
v.resize_raw(2, NonNull::new(ptr).unwrap());
assert_eq!(v.pop(), Some(value));
assert_eq!(v.pop(), Some(value));
assert!(v.is_empty());
}Sourcepub fn resize_with<T, F>(&mut self, new_len: usize, f: F)where
T: 'static,
F: FnMut() -> T,
pub fn resize_with<T, F>(&mut self, new_len: usize, f: F)where
T: 'static,
F: FnMut() -> T,
Resizes the vector to the given length.
If the new length is greater than previous length of the vector, then the vector is extended with values the given function generates. In this case, generated values are appended in order. Otherwise, the vector is shrunk.
§Panics
Panics if T is not the same type as the vector contains.
§Examples
use my_ecs::{tinfo, ds::AnyVec};
let mut v = AnyVec::new(tinfo!(i32));
unsafe {
v.resize_with(2, || 0_i32);
assert_eq!(v.pop(), Some(0_i32));
assert_eq!(v.pop(), Some(0_i32));
assert!(v.is_empty());
}Sourcepub fn truncate(&mut self, len: usize)
pub fn truncate(&mut self, len: usize)
Shrinks the vector to the given length, and drops abandoned items.
If the given length is greater than or equal to the current length of the vector, nothing takes place.
§Examples
use my_ecs::{tinfo, ds::AnyVec};
let mut v = AnyVec::new(tinfo!(i32));
unsafe { v.resize(10, 0_i32) };
v.truncate(5);
assert_eq!(v.len(), 5);Sourcepub fn as_vec_mut<T: 'static>(&mut self) -> TypedAnyVec<'_, T>
pub fn as_vec_mut<T: 'static>(&mut self) -> TypedAnyVec<'_, T>
Creates TypedAnyVec which looks like Vec<T>.
You can call tons of useful methods on Vec by converting the vector.
§Panics
Panics if T it not the same type as the vector contains.
§Examples
use my_ecs::{tinfo, ds::AnyVec};
let mut v = AnyVec::new(tinfo!(i32));
{
let mut tv = v.as_vec_mut::<i32>();
tv.push(0);
tv.push(1);
}
unsafe {
assert_eq!(v.pop(), Some(1_i32));
assert_eq!(v.pop(), Some(0_i32));
}Sourcepub fn as_mut_slice<T: 'static>(&mut self) -> &mut [T]
pub fn as_mut_slice<T: 'static>(&mut self) -> &mut [T]
Trait Implementations§
Source§impl AsRawIter for AnyVec
impl AsRawIter for AnyVec
Source§fn par_iter(&self) -> ParRawIter
fn par_iter(&self) -> ParRawIter
ParRawIter. Read moreSource§unsafe fn iter_mut_of<T>(&mut self) -> IterMut<'_, T> ⓘ
unsafe fn iter_mut_of<T>(&mut self) -> IterMut<'_, T> ⓘ
Source§unsafe fn par_iter_of<T>(&self) -> ParIter<'_, T>
unsafe fn par_iter_of<T>(&self) -> ParIter<'_, T>
Source§unsafe fn par_iter_mut_of<T>(&mut self) -> ParIterMut<'_, T>
unsafe fn par_iter_mut_of<T>(&mut self) -> ParIterMut<'_, T>
Source§impl IntoIterator for &AnyVec
impl IntoIterator for &AnyVec
Source§impl<'data> IntoParallelRefIterator<'data> for AnyVec
impl<'data> IntoParallelRefIterator<'data> for AnyVec
Source§type Iter = ParRawIter
type Iter = ParRawIter
Source§type Item = SendSyncPtr<u8>
type Item = SendSyncPtr<u8>
&'data T reference type.impl Resource for AnyVec
impl Send for AnyVec
impl Sync for AnyVec
Auto Trait Implementations§
impl Freeze for AnyVec
impl RefUnwindSafe for AnyVec
impl Unpin for AnyVec
impl UnwindSafe for AnyVec
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more