2020-05-05 06:24:13 +02:00
|
|
|
//! Module containing various utility types and functions.
|
2020-05-16 18:24:07 +02:00
|
|
|
//!
|
|
|
|
//! # Safety
|
|
|
|
//!
|
2020-05-17 16:19:49 +02:00
|
|
|
//! The `StaticVec` type has some `unsafe` blocks to handle conversions between `MaybeUninit` and regular types.
|
2020-05-14 12:27:22 +02:00
|
|
|
|
2020-05-05 06:24:13 +02:00
|
|
|
use crate::stdlib::{
|
|
|
|
any::TypeId,
|
2020-05-05 09:00:10 +02:00
|
|
|
fmt,
|
2020-05-05 06:24:13 +02:00
|
|
|
hash::{Hash, Hasher},
|
2020-05-10 15:25:47 +02:00
|
|
|
iter::FromIterator,
|
2020-05-05 06:24:13 +02:00
|
|
|
mem,
|
2020-05-17 16:19:49 +02:00
|
|
|
mem::MaybeUninit,
|
|
|
|
ops::{Drop, Index, IndexMut},
|
2020-05-05 06:24:13 +02:00
|
|
|
vec::Vec,
|
|
|
|
};
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_std"))]
|
2020-05-10 15:25:47 +02:00
|
|
|
use crate::stdlib::collections::hash_map::DefaultHasher;
|
2020-05-05 06:24:13 +02:00
|
|
|
|
|
|
|
#[cfg(feature = "no_std")]
|
|
|
|
use ahash::AHasher;
|
|
|
|
|
2020-05-07 18:19:08 +02:00
|
|
|
/// Calculate a `u64` hash key from a module-qualified function name and parameter types.
|
2020-05-05 06:24:13 +02:00
|
|
|
///
|
2020-05-07 18:19:08 +02:00
|
|
|
/// Module names are passed in via `&str` references from an iterator.
|
|
|
|
/// Parameter types are passed in via `TypeId` values from an iterator.
|
|
|
|
///
|
2020-05-10 15:25:47 +02:00
|
|
|
/// # Note
|
2020-05-07 18:19:08 +02:00
|
|
|
///
|
|
|
|
/// The first module name is skipped. Hashing starts from the _second_ module in the chain.
|
|
|
|
pub fn calc_fn_spec<'a>(
|
|
|
|
modules: impl Iterator<Item = &'a str>,
|
|
|
|
fn_name: &str,
|
2020-05-19 14:07:51 +02:00
|
|
|
num: usize,
|
2020-05-07 18:19:08 +02:00
|
|
|
params: impl Iterator<Item = TypeId>,
|
|
|
|
) -> u64 {
|
2020-05-05 06:24:13 +02:00
|
|
|
#[cfg(feature = "no_std")]
|
|
|
|
let mut s: AHasher = Default::default();
|
|
|
|
#[cfg(not(feature = "no_std"))]
|
|
|
|
let mut s = DefaultHasher::new();
|
|
|
|
|
2020-05-07 18:19:08 +02:00
|
|
|
// We always skip the first module
|
|
|
|
modules.skip(1).for_each(|m| m.hash(&mut s));
|
2020-05-05 06:24:13 +02:00
|
|
|
s.write(fn_name.as_bytes());
|
2020-05-19 14:07:51 +02:00
|
|
|
s.write_usize(num);
|
2020-05-05 06:24:13 +02:00
|
|
|
params.for_each(|t| t.hash(&mut s));
|
|
|
|
s.finish()
|
|
|
|
}
|
|
|
|
|
2020-05-17 16:19:49 +02:00
|
|
|
/// A type to hold a number of values in static storage for no-allocation, quick access.
|
|
|
|
/// If too many items are stored, it converts into using a `Vec`.
|
2020-05-05 06:24:13 +02:00
|
|
|
///
|
|
|
|
/// This is essentially a knock-off of the [`staticvec`](https://crates.io/crates/staticvec) crate.
|
|
|
|
/// This simplified implementation here is to avoid pulling in another crate.
|
2020-05-10 15:25:47 +02:00
|
|
|
///
|
2020-05-17 16:19:49 +02:00
|
|
|
/// # Implementation
|
|
|
|
///
|
|
|
|
/// A `StaticVec` holds data in _either one_ of two storages: 1) a fixed-size array of `MAX_STATIC_VEC`
|
|
|
|
/// items, and 2) a dynamic `Vec`. At any time, either one of them (or both) must be empty, depending on the
|
|
|
|
/// total number of items.
|
|
|
|
///
|
|
|
|
/// There is a `len` field containing the total number of items held by the `StaticVec`.
|
|
|
|
///
|
|
|
|
/// The fixed-size array (`list`) is not initialized (i.e. initialized with `MaybeUninit::uninit()`).
|
|
|
|
///
|
|
|
|
/// When `len <= MAX_STATIC_VEC`, all elements are stored in the fixed-size array.
|
|
|
|
/// Array slots `>= len` are `MaybeUninit::uninit()` while slots `< len` are considered actual data.
|
|
|
|
/// In this scenario, the `Vec` (`more`) is empty.
|
|
|
|
///
|
|
|
|
/// As soon as we try to push a new item into the `StaticVec` that makes the total number exceed
|
|
|
|
/// `MAX_STATIC_VEC`, all the items in the fixed-sized array are taken out, replaced with
|
|
|
|
/// `MaybeUninit::uninit()` (via `mem::replace`) and pushed into the `Vec`.
|
|
|
|
/// Then the new item is added to the `Vec`.
|
|
|
|
///
|
|
|
|
/// Therefore, if `len > MAX_STATIC_VEC`, then the fixed-size array (`list`) is considered
|
|
|
|
/// empty and uninitialized while all data resides in the `Vec` (`more`).
|
|
|
|
///
|
|
|
|
/// When popping an item off of the `StaticVec`, the reverse is true. When `len = MAX_STATIC_VEC + 1`,
|
|
|
|
/// after popping the item, all the items residing in the `Vec` are moved back to the fixed-size array (`list`).
|
|
|
|
/// The `Vec` will then be empty.
|
|
|
|
///
|
|
|
|
/// Therefore, if `len <= MAX_STATIC_VEC`, data is in the fixed-size array (`list`).
|
|
|
|
/// Otherwise, data is in the `Vec` (`more`).
|
|
|
|
///
|
2020-05-10 15:25:47 +02:00
|
|
|
/// # Safety
|
|
|
|
///
|
2020-05-16 18:24:07 +02:00
|
|
|
/// This type uses some unsafe code (mainly for uninitialized/unused array slots) for efficiency.
|
2020-05-14 05:21:56 +02:00
|
|
|
//
|
|
|
|
// TODO - remove unsafe code
|
2020-05-10 15:25:47 +02:00
|
|
|
pub struct StaticVec<T> {
|
2020-05-05 06:24:13 +02:00
|
|
|
/// Total number of values held.
|
|
|
|
len: usize,
|
2020-05-17 16:19:49 +02:00
|
|
|
/// Fixed-size storage for fast, no-allocation access.
|
|
|
|
list: [MaybeUninit<T>; MAX_STATIC_VEC],
|
2020-05-05 06:24:13 +02:00
|
|
|
/// Dynamic storage. For spill-overs.
|
|
|
|
more: Vec<T>,
|
|
|
|
}
|
|
|
|
|
2020-05-17 16:19:49 +02:00
|
|
|
/// Maximum slots of fixed-size storage for a `StaticVec`.
|
|
|
|
/// 4 slots should be enough for most cases.
|
|
|
|
const MAX_STATIC_VEC: usize = 4;
|
|
|
|
|
2020-05-16 18:24:07 +02:00
|
|
|
impl<T> Drop for StaticVec<T> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Default for StaticVec<T> {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
len: 0,
|
2020-05-17 16:19:49 +02:00
|
|
|
list: unsafe { mem::MaybeUninit::uninit().assume_init() },
|
2020-05-16 18:24:07 +02:00
|
|
|
more: Vec::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-12 12:48:25 +02:00
|
|
|
impl<T: PartialEq> PartialEq for StaticVec<T> {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
2020-05-17 16:19:49 +02:00
|
|
|
if self.len != other.len || self.more != other.more {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.len > MAX_STATIC_VEC {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
mem::transmute::<_, &[T; MAX_STATIC_VEC]>(&self.list)
|
|
|
|
== mem::transmute::<_, &[T; MAX_STATIC_VEC]>(&other.list)
|
|
|
|
}
|
2020-05-16 18:24:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Clone> Clone for StaticVec<T> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
let mut value: Self = Default::default();
|
|
|
|
value.len = self.len;
|
|
|
|
|
2020-05-17 16:19:49 +02:00
|
|
|
if self.is_fixed_storage() {
|
2020-05-16 18:24:07 +02:00
|
|
|
for x in 0..self.len {
|
|
|
|
let item: &T = unsafe { mem::transmute(self.list.get(x).unwrap()) };
|
2020-05-17 16:19:49 +02:00
|
|
|
value.list[x] = MaybeUninit::new(item.clone());
|
2020-05-16 18:24:07 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
value.more = self.more.clone();
|
|
|
|
}
|
|
|
|
|
|
|
|
value
|
2020-05-12 12:48:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Eq> Eq for StaticVec<T> {}
|
|
|
|
|
2020-05-10 15:25:47 +02:00
|
|
|
impl<T> FromIterator<T> for StaticVec<T> {
|
2020-05-10 10:56:17 +02:00
|
|
|
fn from_iter<X: IntoIterator<Item = T>>(iter: X) -> Self {
|
|
|
|
let mut vec = StaticVec::new();
|
|
|
|
|
|
|
|
for x in iter {
|
|
|
|
vec.push(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
vec
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 15:25:47 +02:00
|
|
|
impl<T> StaticVec<T> {
|
2020-05-05 06:24:13 +02:00
|
|
|
/// Create a new `StaticVec`.
|
|
|
|
pub fn new() -> Self {
|
2020-05-05 09:00:10 +02:00
|
|
|
Default::default()
|
2020-05-05 06:24:13 +02:00
|
|
|
}
|
2020-05-16 18:24:07 +02:00
|
|
|
/// Empty the `StaticVec`.
|
|
|
|
pub fn clear(&mut self) {
|
2020-05-17 16:19:49 +02:00
|
|
|
if self.is_fixed_storage() {
|
2020-05-16 18:24:07 +02:00
|
|
|
for x in 0..self.len {
|
2020-05-17 16:19:49 +02:00
|
|
|
self.extract_from_list(x);
|
2020-05-16 18:24:07 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.more.clear();
|
|
|
|
}
|
|
|
|
self.len = 0;
|
|
|
|
}
|
2020-05-17 16:19:49 +02:00
|
|
|
/// Extract a `MaybeUninit` into a concrete initialized type.
|
|
|
|
fn extract(value: MaybeUninit<T>) -> T {
|
|
|
|
unsafe { value.assume_init() }
|
|
|
|
}
|
|
|
|
/// Extract an item from the fixed-size array, replacing it with `MaybeUninit::uninit()`.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics if fixed-size storage is not used, or if the `index` is out of bounds.
|
|
|
|
fn extract_from_list(&mut self, index: usize) -> T {
|
|
|
|
if !self.is_fixed_storage() {
|
|
|
|
panic!("not fixed storage in StaticVec");
|
|
|
|
}
|
|
|
|
if index >= self.len {
|
|
|
|
panic!("index OOB in StaticVec");
|
|
|
|
}
|
|
|
|
Self::extract(mem::replace(
|
|
|
|
self.list.get_mut(index).unwrap(),
|
|
|
|
MaybeUninit::uninit(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
/// Set an item into the fixed-size array.
|
|
|
|
/// If `drop` is `true`, the original value is extracted then automatically dropped.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics if fixed-size storage is not used, or if the `index` is out of bounds.
|
|
|
|
fn set_into_list(&mut self, index: usize, value: T, drop: bool) {
|
|
|
|
if !self.is_fixed_storage() {
|
|
|
|
panic!("not fixed storage in StaticVec");
|
|
|
|
}
|
|
|
|
// Allow setting at most one slot to the right
|
|
|
|
if index > self.len {
|
|
|
|
panic!("index OOB in StaticVec");
|
|
|
|
}
|
|
|
|
let temp = mem::replace(self.list.get_mut(index).unwrap(), MaybeUninit::new(value));
|
|
|
|
if drop {
|
|
|
|
// Extract the original value - which will drop it automatically
|
|
|
|
Self::extract(temp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Move item in the fixed-size array into the `Vec`.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics if fixed-size storage is not used, or if the fixed-size storage is not full.
|
|
|
|
fn move_fixed_into_vec(&mut self, num: usize) {
|
|
|
|
if !self.is_fixed_storage() {
|
|
|
|
panic!("not fixed storage in StaticVec");
|
|
|
|
}
|
|
|
|
if self.len != num {
|
|
|
|
panic!("fixed storage is not full in StaticVec");
|
|
|
|
}
|
|
|
|
self.more.extend(
|
|
|
|
self.list
|
|
|
|
.iter_mut()
|
|
|
|
.take(num)
|
|
|
|
.map(|v| mem::replace(v, MaybeUninit::uninit()))
|
|
|
|
.map(Self::extract),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
/// Is data stored in fixed-size storage?
|
|
|
|
fn is_fixed_storage(&self) -> bool {
|
|
|
|
self.len <= MAX_STATIC_VEC
|
|
|
|
}
|
2020-05-05 06:24:13 +02:00
|
|
|
/// Push a new value to the end of this `StaticVec`.
|
|
|
|
pub fn push<X: Into<T>>(&mut self, value: X) {
|
2020-05-17 16:19:49 +02:00
|
|
|
if self.len == MAX_STATIC_VEC {
|
|
|
|
self.move_fixed_into_vec(MAX_STATIC_VEC);
|
2020-05-10 15:25:47 +02:00
|
|
|
self.more.push(value.into());
|
2020-05-17 16:19:49 +02:00
|
|
|
} else if self.is_fixed_storage() {
|
|
|
|
self.set_into_list(self.len, value.into(), false);
|
2020-05-16 18:24:07 +02:00
|
|
|
} else {
|
2020-05-05 06:24:13 +02:00
|
|
|
self.more.push(value.into());
|
2020-05-16 18:24:07 +02:00
|
|
|
}
|
|
|
|
self.len += 1;
|
|
|
|
}
|
|
|
|
/// Insert a new value to this `StaticVec` at a particular position.
|
2020-05-17 16:19:49 +02:00
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics if `index` is out of bounds.
|
2020-05-16 18:24:07 +02:00
|
|
|
pub fn insert<X: Into<T>>(&mut self, index: usize, value: X) {
|
|
|
|
if index > self.len {
|
|
|
|
panic!("index OOB in StaticVec");
|
|
|
|
}
|
|
|
|
|
2020-05-17 16:19:49 +02:00
|
|
|
if self.len == MAX_STATIC_VEC {
|
|
|
|
self.move_fixed_into_vec(MAX_STATIC_VEC);
|
2020-05-16 18:24:07 +02:00
|
|
|
self.more.insert(index, value.into());
|
2020-05-17 16:19:49 +02:00
|
|
|
} else if self.is_fixed_storage() {
|
|
|
|
// Move all items one slot to the right
|
2020-05-16 18:24:07 +02:00
|
|
|
for x in (index..self.len).rev() {
|
2020-05-17 16:19:49 +02:00
|
|
|
let orig_value = self.extract_from_list(x);
|
|
|
|
self.set_into_list(x + 1, orig_value, false);
|
2020-05-16 18:24:07 +02:00
|
|
|
}
|
2020-05-17 16:19:49 +02:00
|
|
|
self.set_into_list(index, value.into(), false);
|
2020-05-05 06:24:13 +02:00
|
|
|
} else {
|
2020-05-16 18:24:07 +02:00
|
|
|
self.more.insert(index, value.into());
|
2020-05-05 06:24:13 +02:00
|
|
|
}
|
|
|
|
self.len += 1;
|
|
|
|
}
|
|
|
|
/// Pop a value from the end of this `StaticVec`.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics if the `StaticVec` is empty.
|
|
|
|
pub fn pop(&mut self) -> T {
|
2020-05-17 16:19:49 +02:00
|
|
|
if self.is_empty() {
|
2020-05-16 18:24:07 +02:00
|
|
|
panic!("nothing to pop!");
|
|
|
|
}
|
|
|
|
|
2020-05-17 16:19:49 +02:00
|
|
|
let result = if self.is_fixed_storage() {
|
|
|
|
self.extract_from_list(self.len - 1)
|
2020-05-05 06:24:13 +02:00
|
|
|
} else {
|
2020-05-17 16:19:49 +02:00
|
|
|
let value = self.more.pop().unwrap();
|
2020-05-10 15:25:47 +02:00
|
|
|
|
|
|
|
// Move back to the fixed list
|
2020-05-17 16:19:49 +02:00
|
|
|
if self.more.len() == MAX_STATIC_VEC {
|
|
|
|
for index in (0..MAX_STATIC_VEC).rev() {
|
|
|
|
let item = self.more.pop().unwrap();
|
|
|
|
self.set_into_list(index, item, false);
|
2020-05-10 15:25:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-17 16:19:49 +02:00
|
|
|
value
|
|
|
|
};
|
|
|
|
|
|
|
|
self.len -= 1;
|
|
|
|
|
|
|
|
result
|
|
|
|
}
|
|
|
|
/// Remove a value from this `StaticVec` at a particular position.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics if `index` is out of bounds.
|
|
|
|
pub fn remove(&mut self, index: usize) -> T {
|
|
|
|
if index >= self.len {
|
|
|
|
panic!("index OOB in StaticVec");
|
|
|
|
}
|
|
|
|
|
|
|
|
let result = if self.is_fixed_storage() {
|
|
|
|
let value = self.extract_from_list(index);
|
|
|
|
|
|
|
|
// Move all items one slot to the left
|
|
|
|
for x in index..self.len - 1 {
|
|
|
|
let orig_value = self.extract_from_list(x + 1);
|
|
|
|
self.set_into_list(x, orig_value, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
value
|
|
|
|
} else {
|
|
|
|
let value = self.more.remove(index);
|
|
|
|
|
|
|
|
// Move back to the fixed list
|
|
|
|
if self.more.len() == MAX_STATIC_VEC {
|
|
|
|
for index in (0..MAX_STATIC_VEC).rev() {
|
|
|
|
let item = self.more.pop().unwrap();
|
|
|
|
self.set_into_list(index, item, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
value
|
2020-05-05 06:24:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
self.len -= 1;
|
|
|
|
|
|
|
|
result
|
|
|
|
}
|
|
|
|
/// Get the number of items in this `StaticVec`.
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.len
|
|
|
|
}
|
2020-05-16 18:24:07 +02:00
|
|
|
/// Is this `StaticVec` empty?
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.len == 0
|
|
|
|
}
|
2020-05-10 15:25:47 +02:00
|
|
|
/// Get a reference to the item at a particular index.
|
2020-05-05 06:24:13 +02:00
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
2020-05-17 16:19:49 +02:00
|
|
|
/// Panics if `index` is out of bounds.
|
|
|
|
pub fn get(&self, index: usize) -> &T {
|
2020-05-05 06:24:13 +02:00
|
|
|
if index >= self.len {
|
|
|
|
panic!("index OOB in StaticVec");
|
|
|
|
}
|
|
|
|
|
2020-05-16 18:24:07 +02:00
|
|
|
let list: &[T; MAX_STATIC_VEC] = unsafe { mem::transmute(&self.list) };
|
|
|
|
|
2020-05-17 16:19:49 +02:00
|
|
|
if self.is_fixed_storage() {
|
2020-05-16 18:24:07 +02:00
|
|
|
list.get(index).unwrap()
|
2020-05-05 06:24:13 +02:00
|
|
|
} else {
|
2020-05-10 15:25:47 +02:00
|
|
|
self.more.get(index).unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Get a mutable reference to the item at a particular index.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
2020-05-17 16:19:49 +02:00
|
|
|
/// Panics if `index` is out of bounds.
|
2020-05-10 15:25:47 +02:00
|
|
|
pub fn get_mut(&mut self, index: usize) -> &mut T {
|
|
|
|
if index >= self.len {
|
|
|
|
panic!("index OOB in StaticVec");
|
|
|
|
}
|
|
|
|
|
2020-05-16 18:24:07 +02:00
|
|
|
let list: &mut [T; MAX_STATIC_VEC] = unsafe { mem::transmute(&mut self.list) };
|
|
|
|
|
2020-05-17 16:19:49 +02:00
|
|
|
if self.is_fixed_storage() {
|
2020-05-16 18:24:07 +02:00
|
|
|
list.get_mut(index).unwrap()
|
2020-05-10 15:25:47 +02:00
|
|
|
} else {
|
|
|
|
self.more.get_mut(index).unwrap()
|
2020-05-05 06:24:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Get an iterator to entries in the `StaticVec`.
|
|
|
|
pub fn iter(&self) -> impl Iterator<Item = &T> {
|
2020-05-16 18:24:07 +02:00
|
|
|
let list: &[T; MAX_STATIC_VEC] = unsafe { mem::transmute(&self.list) };
|
|
|
|
|
2020-05-17 16:19:49 +02:00
|
|
|
if self.is_fixed_storage() {
|
2020-05-16 18:24:07 +02:00
|
|
|
list[..self.len].iter()
|
2020-05-05 06:24:13 +02:00
|
|
|
} else {
|
2020-05-16 18:24:07 +02:00
|
|
|
self.more.iter()
|
2020-05-10 15:25:47 +02:00
|
|
|
}
|
2020-05-05 06:24:13 +02:00
|
|
|
}
|
2020-05-07 04:00:10 +02:00
|
|
|
/// Get a mutable iterator to entries in the `StaticVec`.
|
|
|
|
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T> {
|
2020-05-16 18:24:07 +02:00
|
|
|
let list: &mut [T; MAX_STATIC_VEC] = unsafe { mem::transmute(&mut self.list) };
|
|
|
|
|
2020-05-17 16:19:49 +02:00
|
|
|
if self.is_fixed_storage() {
|
2020-05-16 18:24:07 +02:00
|
|
|
list[..self.len].iter_mut()
|
2020-05-07 04:00:10 +02:00
|
|
|
} else {
|
2020-05-16 18:24:07 +02:00
|
|
|
self.more.iter_mut()
|
2020-05-10 15:25:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-17 16:19:49 +02:00
|
|
|
impl<T: 'static> StaticVec<T> {
|
|
|
|
/// Get a mutable iterator to entries in the `StaticVec`.
|
|
|
|
pub fn into_iter(mut self) -> Box<dyn Iterator<Item = T>> {
|
|
|
|
if self.is_fixed_storage() {
|
|
|
|
let mut it = FixedStorageIterator {
|
|
|
|
data: unsafe { mem::MaybeUninit::uninit().assume_init() },
|
|
|
|
index: 0,
|
|
|
|
limit: self.len,
|
|
|
|
};
|
|
|
|
|
|
|
|
for x in 0..self.len {
|
|
|
|
it.data[x] = mem::replace(self.list.get_mut(x).unwrap(), MaybeUninit::uninit());
|
|
|
|
}
|
|
|
|
self.len = 0;
|
|
|
|
|
|
|
|
Box::new(it)
|
|
|
|
} else {
|
|
|
|
Box::new(Vec::from(self).into_iter())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An iterator that takes control of the fixed-size storage of a `StaticVec` and returns its values.
|
|
|
|
struct FixedStorageIterator<T> {
|
|
|
|
data: [MaybeUninit<T>; MAX_STATIC_VEC],
|
|
|
|
index: usize,
|
|
|
|
limit: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Iterator for FixedStorageIterator<T> {
|
|
|
|
type Item = T;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
if self.index >= self.limit {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
self.index += 1;
|
|
|
|
|
|
|
|
let value = mem::replace(
|
|
|
|
self.data.get_mut(self.index - 1).unwrap(),
|
|
|
|
MaybeUninit::uninit(),
|
|
|
|
);
|
|
|
|
|
|
|
|
unsafe { Some(value.assume_init()) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-16 18:24:07 +02:00
|
|
|
impl<T: Default> StaticVec<T> {
|
|
|
|
/// Get the item at a particular index, replacing it with the default.
|
2020-05-10 15:25:47 +02:00
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
2020-05-17 16:19:49 +02:00
|
|
|
/// Panics if `index` is out of bounds.
|
|
|
|
pub fn take(&mut self, index: usize) -> T {
|
2020-05-10 15:25:47 +02:00
|
|
|
if index >= self.len {
|
|
|
|
panic!("index OOB in StaticVec");
|
|
|
|
}
|
2020-05-07 04:00:10 +02:00
|
|
|
|
2020-05-17 16:19:49 +02:00
|
|
|
mem::take(if self.is_fixed_storage() {
|
2020-05-16 18:24:07 +02:00
|
|
|
unsafe { mem::transmute(self.list.get_mut(index).unwrap()) }
|
2020-05-10 15:25:47 +02:00
|
|
|
} else {
|
2020-05-16 18:24:07 +02:00
|
|
|
self.more.get_mut(index).unwrap()
|
|
|
|
})
|
2020-05-07 04:00:10 +02:00
|
|
|
}
|
2020-05-05 06:24:13 +02:00
|
|
|
}
|
2020-05-05 09:00:10 +02:00
|
|
|
|
2020-05-10 15:25:47 +02:00
|
|
|
impl<T: fmt::Debug> fmt::Debug for StaticVec<T> {
|
2020-05-05 09:00:10 +02:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "[ ")?;
|
|
|
|
self.iter().try_for_each(|v| write!(f, "{:?}, ", v))?;
|
|
|
|
write!(f, "]")
|
|
|
|
}
|
|
|
|
}
|
2020-05-10 15:25:47 +02:00
|
|
|
|
|
|
|
impl<T> AsRef<[T]> for StaticVec<T> {
|
|
|
|
fn as_ref(&self) -> &[T] {
|
2020-05-16 18:24:07 +02:00
|
|
|
let list: &[T; MAX_STATIC_VEC] = unsafe { mem::transmute(&self.list) };
|
|
|
|
|
2020-05-17 16:19:49 +02:00
|
|
|
if self.is_fixed_storage() {
|
2020-05-16 18:24:07 +02:00
|
|
|
&list[..self.len]
|
2020-05-10 15:25:47 +02:00
|
|
|
} else {
|
2020-05-16 18:24:07 +02:00
|
|
|
&self.more[..]
|
2020-05-10 15:25:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> AsMut<[T]> for StaticVec<T> {
|
|
|
|
fn as_mut(&mut self) -> &mut [T] {
|
2020-05-16 18:24:07 +02:00
|
|
|
let list: &mut [T; MAX_STATIC_VEC] = unsafe { mem::transmute(&mut self.list) };
|
|
|
|
|
2020-05-17 16:19:49 +02:00
|
|
|
if self.is_fixed_storage() {
|
2020-05-16 18:24:07 +02:00
|
|
|
&mut list[..self.len]
|
|
|
|
} else {
|
2020-05-10 15:25:47 +02:00
|
|
|
&mut self.more[..]
|
2020-05-16 18:24:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-17 16:19:49 +02:00
|
|
|
impl<T> Index<usize> for StaticVec<T> {
|
|
|
|
type Output = T;
|
|
|
|
|
|
|
|
fn index(&self, index: usize) -> &Self::Output {
|
|
|
|
self.get(index)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> IndexMut<usize> for StaticVec<T> {
|
|
|
|
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
|
|
|
|
self.get_mut(index)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-16 18:24:07 +02:00
|
|
|
impl<T> From<StaticVec<T>> for Vec<T> {
|
|
|
|
fn from(mut value: StaticVec<T>) -> Self {
|
2020-05-17 16:19:49 +02:00
|
|
|
if value.len <= MAX_STATIC_VEC {
|
|
|
|
value.move_fixed_into_vec(value.len);
|
2020-05-10 15:25:47 +02:00
|
|
|
}
|
2020-05-17 16:19:49 +02:00
|
|
|
value.len = 0;
|
|
|
|
|
|
|
|
let mut arr = Self::new();
|
|
|
|
arr.append(&mut value.more);
|
|
|
|
arr
|
2020-05-10 15:25:47 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-16 18:24:07 +02:00
|
|
|
|
|
|
|
impl<T> From<Vec<T>> for StaticVec<T> {
|
|
|
|
fn from(mut value: Vec<T>) -> Self {
|
|
|
|
let mut arr: Self = Default::default();
|
|
|
|
arr.len = value.len();
|
|
|
|
|
2020-05-17 16:19:49 +02:00
|
|
|
if arr.len <= MAX_STATIC_VEC {
|
2020-05-16 18:24:07 +02:00
|
|
|
for x in (0..arr.len).rev() {
|
2020-05-17 16:19:49 +02:00
|
|
|
arr.set_into_list(x, value.pop().unwrap(), false);
|
2020-05-16 18:24:07 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
arr.more = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
arr
|
|
|
|
}
|
|
|
|
}
|