mirror of
https://github.com/openharmony/third_party_rust_tinyvec.git
synced 2026-07-18 12:25:35 -04:00
rename arrayish to array I guess
The "ish" part was always supposed to be temporary.
This commit is contained in:
@@ -1,16 +1,21 @@
|
||||
/// A trait for types that can be the backing store of an
|
||||
/// [`ArrayishVec`](ArrayishVec::<A>).
|
||||
/// [`ArrayVec`](ArrayVec::<A>).
|
||||
///
|
||||
/// You are not generally expected to need to implement this yourself. You can
|
||||
/// if you want I guess. Impls are provided for arrays of length `0..=32`, 33,
|
||||
/// and powers of 2 up to 4096. Additional lengths can probably be added upon
|
||||
/// request.
|
||||
/// An "array", for our purposes, has the following basic properties:
|
||||
/// * Owns some number of elements.
|
||||
/// * The element type can be generic, but must implement [`Default`].
|
||||
/// * The capacity is fixed based on the array type.
|
||||
/// * You can get a shared or mutable slice to the elements.
|
||||
///
|
||||
/// You are generally note expected to need to implement this yourself. It is
|
||||
/// already implemented for all the major array lengths. Additional lengths can
|
||||
/// probably be added upon request.
|
||||
///
|
||||
/// ## Safety Reminder
|
||||
///
|
||||
/// As a reminder, the `Arrayish` trait is 100% safe so unsafe code **must not**
|
||||
/// rely on an instance of the trait being correct.
|
||||
pub trait Arrayish {
|
||||
/// As a reminder, this trait is 100% safe, which means that `unsafe` code
|
||||
/// **must not** rely on an instance of the trait being correct to avoid UB.
|
||||
pub trait Array {
|
||||
/// The type of the items in the thing.
|
||||
type Item: Default;
|
||||
|
||||
@@ -30,9 +35,9 @@ pub trait Arrayish {
|
||||
fn slice_mut(&mut self) -> &mut [Self::Item];
|
||||
}
|
||||
|
||||
macro_rules! impl_arrayish_for_len {
|
||||
macro_rules! impl_array_for_len {
|
||||
($($len:expr),+ $(,)?) => {
|
||||
$(impl<T: Default> Arrayish for [T; $len] {
|
||||
$(impl<T: Default> Array for [T; $len] {
|
||||
type Item = T;
|
||||
const CAPACITY: usize = $len;
|
||||
#[inline(always)]
|
||||
@@ -47,7 +52,7 @@ macro_rules! impl_arrayish_for_len {
|
||||
}
|
||||
}
|
||||
|
||||
impl_arrayish_for_len! {
|
||||
impl_array_for_len! {
|
||||
0, /* The oft-forgotten 0-length array! */
|
||||
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
|
||||
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
|
||||
@@ -1,6 +1,6 @@
|
||||
use super::*;
|
||||
|
||||
/// Helper to make an `ArrayishVec`.
|
||||
/// Helper to make an `ArrayVec`.
|
||||
///
|
||||
/// You specify the backing array type, and optionally give all the elements you
|
||||
/// want to initially place into the array.
|
||||
@@ -19,13 +19,13 @@ use super::*;
|
||||
macro_rules! arr_vec {
|
||||
($array_type:ty) => {
|
||||
{
|
||||
let mut av: ArrayishVec<$array_type> = Default::default();
|
||||
let mut av: ArrayVec<$array_type> = Default::default();
|
||||
av
|
||||
}
|
||||
};
|
||||
($array_type:ty, $($elem:expr),*) => {
|
||||
{
|
||||
let mut av: ArrayishVec<$array_type> = Default::default();
|
||||
let mut av: ArrayVec<$array_type> = Default::default();
|
||||
$( av.push($elem); )*
|
||||
av
|
||||
}
|
||||
@@ -39,12 +39,12 @@ macro_rules! arr_vec {
|
||||
/// * All of the array memory is always initialized.
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Default)]
|
||||
pub struct ArrayishVec<A: Arrayish> {
|
||||
pub struct ArrayVec<A: Array> {
|
||||
len: usize,
|
||||
data: A,
|
||||
}
|
||||
|
||||
impl<A: Arrayish> Deref for ArrayishVec<A> {
|
||||
impl<A: Array> Deref for ArrayVec<A> {
|
||||
type Target = [A::Item];
|
||||
#[inline(always)]
|
||||
#[must_use]
|
||||
@@ -53,7 +53,7 @@ impl<A: Arrayish> Deref for ArrayishVec<A> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish> DerefMut for ArrayishVec<A> {
|
||||
impl<A: Array> DerefMut for ArrayVec<A> {
|
||||
#[inline(always)]
|
||||
#[must_use]
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
@@ -61,7 +61,7 @@ impl<A: Arrayish> DerefMut for ArrayishVec<A> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish, I: SliceIndex<[A::Item]>> Index<I> for ArrayishVec<A> {
|
||||
impl<A: Array, I: SliceIndex<[A::Item]>> Index<I> for ArrayVec<A> {
|
||||
type Output = <I as SliceIndex<[A::Item]>>::Output;
|
||||
#[inline(always)]
|
||||
#[must_use]
|
||||
@@ -70,7 +70,7 @@ impl<A: Arrayish, I: SliceIndex<[A::Item]>> Index<I> for ArrayishVec<A> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish, I: SliceIndex<[A::Item]>> IndexMut<I> for ArrayishVec<A> {
|
||||
impl<A: Array, I: SliceIndex<[A::Item]>> IndexMut<I> for ArrayVec<A> {
|
||||
#[inline(always)]
|
||||
#[must_use]
|
||||
fn index_mut(&mut self, index: I) -> &mut Self::Output {
|
||||
@@ -78,7 +78,7 @@ impl<A: Arrayish, I: SliceIndex<[A::Item]>> IndexMut<I> for ArrayishVec<A> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish> ArrayishVec<A> {
|
||||
impl<A: Array> ArrayVec<A> {
|
||||
/// Move all values from `other` into this vec.
|
||||
#[inline]
|
||||
pub fn append(&mut self, other: &mut Self) {
|
||||
@@ -123,7 +123,7 @@ impl<A: Arrayish> ArrayishVec<A> {
|
||||
self.deref()
|
||||
}
|
||||
|
||||
/// The capacity of the `ArrayishVec`.
|
||||
/// The capacity of the `ArrayVec`.
|
||||
///
|
||||
/// This is fixed based on the array type.
|
||||
#[inline(always)]
|
||||
@@ -181,7 +181,7 @@ impl<A: Arrayish> ArrayishVec<A> {
|
||||
/// ```rust
|
||||
/// use tinyvec::*;
|
||||
/// let mut av = arr_vec!([i32; 4], 1, 2, 3);
|
||||
/// let av2: ArrayishVec<[i32; 4]> = av.drain(1..).collect();
|
||||
/// let av2: ArrayVec<[i32; 4]> = av.drain(1..).collect();
|
||||
/// assert_eq!(av.as_slice(), &[1][..]);
|
||||
/// assert_eq!(av2.as_slice(), &[2, 3][..]);
|
||||
///
|
||||
@@ -192,7 +192,7 @@ impl<A: Arrayish> ArrayishVec<A> {
|
||||
pub fn drain<R: RangeBounds<usize>>(
|
||||
&mut self,
|
||||
range: R,
|
||||
) -> ArrayishVecDrain<'_, A> {
|
||||
) -> ArrayVecDrain<'_, A> {
|
||||
use core::ops::Bound;
|
||||
let start = match range.start_bound() {
|
||||
Bound::Included(x) => *x,
|
||||
@@ -206,17 +206,17 @@ impl<A: Arrayish> ArrayishVec<A> {
|
||||
};
|
||||
assert!(
|
||||
start <= end,
|
||||
"ArrayishVec::drain> Illegal range, {} to {}",
|
||||
"ArrayVec::drain> Illegal range, {} to {}",
|
||||
start,
|
||||
end
|
||||
);
|
||||
assert!(
|
||||
end <= self.len,
|
||||
"ArrayishVec::drain> Range ends at {} but length is only {}!",
|
||||
"ArrayVec::drain> Range ends at {} but length is only {}!",
|
||||
end,
|
||||
self.len
|
||||
);
|
||||
ArrayishVecDrain {
|
||||
ArrayVecDrain {
|
||||
parent: self,
|
||||
target_index: start,
|
||||
target_count: end - start,
|
||||
@@ -250,7 +250,7 @@ impl<A: Arrayish> ArrayishVec<A> {
|
||||
match Self::try_from_array_len(data, len) {
|
||||
Ok(out) => out,
|
||||
Err(_) => {
|
||||
panic!("ArrayishVec: length {} exceeds capacity {}!", len, A::CAPACITY)
|
||||
panic!("ArrayVec: length {} exceeds capacity {}!", len, A::CAPACITY)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -287,7 +287,7 @@ impl<A: Arrayish> ArrayishVec<A> {
|
||||
}
|
||||
Ordering::Greater => {
|
||||
panic!(
|
||||
"ArrayishVec::insert> index {} is out of bounds {}",
|
||||
"ArrayVec::insert> index {} is out of bounds {}",
|
||||
index, self.len
|
||||
);
|
||||
}
|
||||
@@ -336,13 +336,13 @@ impl<A: Arrayish> ArrayishVec<A> {
|
||||
|
||||
/// Place an element onto the end of the vec.
|
||||
///
|
||||
/// See also, [`try_push`](ArrayishVec::try_push)
|
||||
/// See also, [`try_push`](ArrayVec::try_push)
|
||||
/// ## Panics
|
||||
/// * If the length of the vec would overflow the capacity.
|
||||
#[inline(always)]
|
||||
pub fn push(&mut self, val: A::Item) {
|
||||
if self.try_push(val).is_err() {
|
||||
panic!("ArrayishVec: overflow!")
|
||||
panic!("ArrayVec: overflow!")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -499,7 +499,7 @@ impl<A: Arrayish> ArrayishVec<A> {
|
||||
// FIXME: should this just use drain into the output?
|
||||
if at > self.len {
|
||||
panic!(
|
||||
"ArrayishVec::split_off> at value {} exceeds length of {}",
|
||||
"ArrayVec::split_off> at value {} exceeds length of {}",
|
||||
at, self.len
|
||||
);
|
||||
}
|
||||
@@ -534,7 +534,7 @@ impl<A: Arrayish> ArrayishVec<A> {
|
||||
pub fn swap_remove(&mut self, index: usize) -> A::Item {
|
||||
assert!(
|
||||
index < self.len,
|
||||
"ArrayishVec::swap_remove> index {} is out of bounds {}",
|
||||
"ArrayVec::swap_remove> index {} is out of bounds {}",
|
||||
index,
|
||||
self.len
|
||||
);
|
||||
@@ -600,17 +600,17 @@ impl<A: Arrayish> ArrayishVec<A> {
|
||||
// LATER: try_remove ?
|
||||
}
|
||||
|
||||
/// Draining iterator for `ArrayishVecDrain`
|
||||
/// Draining iterator for `ArrayVecDrain`
|
||||
///
|
||||
/// See [`ArrayishVecDrain::drain`](ArrayishVecDrain::<A>::drain)
|
||||
pub struct ArrayishVecDrain<'p, A: Arrayish> {
|
||||
parent: &'p mut ArrayishVec<A>,
|
||||
/// See [`ArrayVecDrain::drain`](ArrayVecDrain::<A>::drain)
|
||||
pub struct ArrayVecDrain<'p, A: Array> {
|
||||
parent: &'p mut ArrayVec<A>,
|
||||
target_index: usize,
|
||||
target_count: usize,
|
||||
}
|
||||
// GoodFirstIssue: this entire type is correct but slow.
|
||||
// NIGHTLY: vec_drain_as_slice, https://github.com/rust-lang/rust/issues/58957
|
||||
impl<'p, A: Arrayish> Iterator for ArrayishVecDrain<'p, A> {
|
||||
impl<'p, A: Array> Iterator for ArrayVecDrain<'p, A> {
|
||||
type Item = A::Item;
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
@@ -623,14 +623,14 @@ impl<'p, A: Arrayish> Iterator for ArrayishVecDrain<'p, A> {
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<'p, A: Arrayish> Drop for ArrayishVecDrain<'p, A> {
|
||||
impl<'p, A: Array> Drop for ArrayVecDrain<'p, A> {
|
||||
#[inline]
|
||||
fn drop(&mut self) {
|
||||
for _ in self {}
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish> AsMut<[A::Item]> for ArrayishVec<A> {
|
||||
impl<A: Array> AsMut<[A::Item]> for ArrayVec<A> {
|
||||
#[inline(always)]
|
||||
#[must_use]
|
||||
fn as_mut(&mut self) -> &mut [A::Item] {
|
||||
@@ -638,7 +638,7 @@ impl<A: Arrayish> AsMut<[A::Item]> for ArrayishVec<A> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish> AsRef<[A::Item]> for ArrayishVec<A> {
|
||||
impl<A: Array> AsRef<[A::Item]> for ArrayVec<A> {
|
||||
#[inline(always)]
|
||||
#[must_use]
|
||||
fn as_ref(&self) -> &[A::Item] {
|
||||
@@ -646,7 +646,7 @@ impl<A: Arrayish> AsRef<[A::Item]> for ArrayishVec<A> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish> Borrow<[A::Item]> for ArrayishVec<A> {
|
||||
impl<A: Array> Borrow<[A::Item]> for ArrayVec<A> {
|
||||
#[inline(always)]
|
||||
#[must_use]
|
||||
fn borrow(&self) -> &[A::Item] {
|
||||
@@ -654,7 +654,7 @@ impl<A: Arrayish> Borrow<[A::Item]> for ArrayishVec<A> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish> BorrowMut<[A::Item]> for ArrayishVec<A> {
|
||||
impl<A: Array> BorrowMut<[A::Item]> for ArrayVec<A> {
|
||||
#[inline(always)]
|
||||
#[must_use]
|
||||
fn borrow_mut(&mut self) -> &mut [A::Item] {
|
||||
@@ -662,7 +662,7 @@ impl<A: Arrayish> BorrowMut<[A::Item]> for ArrayishVec<A> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish> Extend<A::Item> for ArrayishVec<A> {
|
||||
impl<A: Array> Extend<A::Item> for ArrayVec<A> {
|
||||
#[inline]
|
||||
fn extend<T: IntoIterator<Item = A::Item>>(&mut self, iter: T) {
|
||||
for t in iter {
|
||||
@@ -671,19 +671,19 @@ impl<A: Arrayish> Extend<A::Item> for ArrayishVec<A> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish> From<A> for ArrayishVec<A> {
|
||||
impl<A: Array> From<A> for ArrayVec<A> {
|
||||
#[inline(always)]
|
||||
#[must_use]
|
||||
/// The output has a length equal to the full array.
|
||||
///
|
||||
/// If you want to select a length, use
|
||||
/// [`from_array_len`](ArrayishVec::from_array_len)
|
||||
/// [`from_array_len`](ArrayVec::from_array_len)
|
||||
fn from(data: A) -> Self {
|
||||
Self { len: data.slice().len(), data }
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish + Default> FromIterator<A::Item> for ArrayishVec<A> {
|
||||
impl<A: Array + Default> FromIterator<A::Item> for ArrayVec<A> {
|
||||
#[inline]
|
||||
#[must_use]
|
||||
fn from_iter<T: IntoIterator<Item = A::Item>>(iter: T) -> Self {
|
||||
@@ -695,13 +695,13 @@ impl<A: Arrayish + Default> FromIterator<A::Item> for ArrayishVec<A> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Iterator for consuming an `ArrayishVec` and returning owned elements.
|
||||
pub struct ArrayishVecIterator<A: Arrayish> {
|
||||
/// Iterator for consuming an `ArrayVec` and returning owned elements.
|
||||
pub struct ArrayVecIterator<A: Array> {
|
||||
base: usize,
|
||||
len: usize,
|
||||
data: A,
|
||||
}
|
||||
impl<A: Arrayish> Iterator for ArrayishVecIterator<A> {
|
||||
impl<A: Array> Iterator for ArrayVecIterator<A> {
|
||||
type Item = A::Item;
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
@@ -741,17 +741,17 @@ impl<A: Arrayish> Iterator for ArrayishVecIterator<A> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish> IntoIterator for ArrayishVec<A> {
|
||||
impl<A: Array> IntoIterator for ArrayVec<A> {
|
||||
type Item = A::Item;
|
||||
type IntoIter = ArrayishVecIterator<A>;
|
||||
type IntoIter = ArrayVecIterator<A>;
|
||||
#[inline(always)]
|
||||
#[must_use]
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
ArrayishVecIterator { base: 0, len: self.len, data: self.data }
|
||||
ArrayVecIterator { base: 0, len: self.len, data: self.data }
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish> PartialEq for ArrayishVec<A>
|
||||
impl<A: Array> PartialEq for ArrayVec<A>
|
||||
where
|
||||
A::Item: PartialEq,
|
||||
{
|
||||
@@ -761,9 +761,9 @@ where
|
||||
self.deref().eq(other.deref())
|
||||
}
|
||||
}
|
||||
impl<A: Arrayish> Eq for ArrayishVec<A> where A::Item: Eq {}
|
||||
impl<A: Array> Eq for ArrayVec<A> where A::Item: Eq {}
|
||||
|
||||
impl<A: Arrayish> PartialOrd for ArrayishVec<A>
|
||||
impl<A: Array> PartialOrd for ArrayVec<A>
|
||||
where
|
||||
A::Item: PartialOrd,
|
||||
{
|
||||
@@ -773,7 +773,7 @@ where
|
||||
self.deref().partial_cmp(other.deref())
|
||||
}
|
||||
}
|
||||
impl<A: Arrayish> Ord for ArrayishVec<A>
|
||||
impl<A: Array> Ord for ArrayVec<A>
|
||||
where
|
||||
A::Item: Ord,
|
||||
{
|
||||
@@ -784,7 +784,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish> PartialEq<&A> for ArrayishVec<A>
|
||||
impl<A: Array> PartialEq<&A> for ArrayVec<A>
|
||||
where
|
||||
A::Item: PartialEq,
|
||||
{
|
||||
@@ -795,7 +795,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish> PartialEq<&[A::Item]> for ArrayishVec<A>
|
||||
impl<A: Array> PartialEq<&[A::Item]> for ArrayVec<A>
|
||||
where
|
||||
A::Item: PartialEq,
|
||||
{
|
||||
@@ -813,7 +813,7 @@ I think, in retrospect, this is useless?
|
||||
The `&mut [A::Item]` should coerce to `&[A::Item]` and use the above impl.
|
||||
I'll leave it here for now though since we already had it written out..
|
||||
|
||||
impl<A: Arrayish> PartialEq<&mut [A::Item]> for ArrayishVec<A>
|
||||
impl<A: Array> PartialEq<&mut [A::Item]> for ArrayVec<A>
|
||||
where
|
||||
A::Item: PartialEq,
|
||||
{
|
||||
@@ -829,7 +829,7 @@ where
|
||||
// Formatting impls
|
||||
// //
|
||||
|
||||
impl<A: Arrayish> Binary for ArrayishVec<A>
|
||||
impl<A: Array> Binary for ArrayVec<A>
|
||||
where
|
||||
A::Item: Binary,
|
||||
{
|
||||
@@ -846,7 +846,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish> Debug for ArrayishVec<A>
|
||||
impl<A: Array> Debug for ArrayVec<A>
|
||||
where
|
||||
A::Item: Debug,
|
||||
{
|
||||
@@ -863,7 +863,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish> Display for ArrayishVec<A>
|
||||
impl<A: Array> Display for ArrayVec<A>
|
||||
where
|
||||
A::Item: Display,
|
||||
{
|
||||
@@ -880,7 +880,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish> LowerExp for ArrayishVec<A>
|
||||
impl<A: Array> LowerExp for ArrayVec<A>
|
||||
where
|
||||
A::Item: LowerExp,
|
||||
{
|
||||
@@ -897,7 +897,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish> LowerHex for ArrayishVec<A>
|
||||
impl<A: Array> LowerHex for ArrayVec<A>
|
||||
where
|
||||
A::Item: LowerHex,
|
||||
{
|
||||
@@ -914,7 +914,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish> Octal for ArrayishVec<A>
|
||||
impl<A: Array> Octal for ArrayVec<A>
|
||||
where
|
||||
A::Item: Octal,
|
||||
{
|
||||
@@ -931,7 +931,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish> Pointer for ArrayishVec<A>
|
||||
impl<A: Array> Pointer for ArrayVec<A>
|
||||
where
|
||||
A::Item: Pointer,
|
||||
{
|
||||
@@ -948,7 +948,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish> UpperExp for ArrayishVec<A>
|
||||
impl<A: Array> UpperExp for ArrayVec<A>
|
||||
where
|
||||
A::Item: UpperExp,
|
||||
{
|
||||
@@ -965,7 +965,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish> UpperHex for ArrayishVec<A>
|
||||
impl<A: Array> UpperHex for ArrayVec<A>
|
||||
where
|
||||
A::Item: UpperHex,
|
||||
{
|
||||
+14
-6
@@ -23,15 +23,20 @@
|
||||
//! However, [quite a
|
||||
//! few](https://doc.rust-lang.org/std/default/trait.Default.html#implementors)
|
||||
//! types have a `Default` impl, so I think that for a lot of common cases you
|
||||
//! can use these vectors.
|
||||
//! can use these vecs.
|
||||
//! * [`ArrayVec`] is an array-backed vec-like where all slots are "live" in the
|
||||
//! Rust sense, but the structure tracks what the intended length is as you
|
||||
//! push and pop elements and so forth. If you try to grow the length past the
|
||||
//! array's capacity it'll error or panic (depending on the method used).
|
||||
//! * (Note: I am _very sorry_ that this type has the same name as the
|
||||
//! `ArrayVec` type in the `arrayvec` crate. We really couldn't think of
|
||||
//! another name for this sort of data structure. Please [contact
|
||||
//! us](https://github.com/Lokathor/tinyvec/issues) with a better name
|
||||
//! before this crate is 1.0 if you can think of one.)
|
||||
//! * [`TinyVec`] is an enum that's either an "inline" `ArrayVec` or a "heap"
|
||||
//! `Vec`. If it's in array mode and you try to grow the vec beyond it's
|
||||
//! capacity it'll quietly transition into heap mode for you and then continue
|
||||
//! operation.
|
||||
//! operation. This type is naturally behind the `alloc` feature gate.
|
||||
//!
|
||||
//! ## Stability Goal
|
||||
//!
|
||||
@@ -59,6 +64,9 @@
|
||||
//! are usually fairly long and perhaps even a little silly. It is the hope
|
||||
//! that this "convention" will prevent any potential name clash between
|
||||
//! our vec types and the standard `Vec` type.
|
||||
//! * That said, I'm not one of those "never 2.0" people, so if `Vec` lands
|
||||
//! some method with the same name as something we have, we'll just bite
|
||||
//! the bullet and fix it with a breaking change.
|
||||
|
||||
use core::{
|
||||
borrow::{Borrow, BorrowMut},
|
||||
@@ -78,11 +86,11 @@ use core::{
|
||||
#[cfg(feature = "alloc")]
|
||||
extern crate alloc;
|
||||
|
||||
mod arrayish;
|
||||
pub use arrayish::*;
|
||||
mod array;
|
||||
pub use array::*;
|
||||
|
||||
mod arrayish_vec;
|
||||
pub use arrayish_vec::*;
|
||||
mod arrayvec;
|
||||
pub use arrayvec::*;
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
mod tiny_vec;
|
||||
|
||||
+47
-44
@@ -21,17 +21,20 @@ macro_rules! tiny_vec {
|
||||
};
|
||||
}
|
||||
|
||||
/// A vector that starts inline, but can automatically move to the heap.
|
||||
///
|
||||
/// * Requires the `alloc` feature
|
||||
#[derive(Clone)]
|
||||
pub enum TinyVec<A: Arrayish> {
|
||||
Inline(ArrayishVec<A>),
|
||||
pub enum TinyVec<A: Array> {
|
||||
Inline(ArrayVec<A>),
|
||||
Heap(Vec<A::Item>)
|
||||
}
|
||||
impl<A: Arrayish + Default> Default for TinyVec<A> {
|
||||
impl<A: Array + Default> Default for TinyVec<A> {
|
||||
fn default() -> Self {
|
||||
TinyVec::Inline(ArrayishVec::default())
|
||||
TinyVec::Inline(ArrayVec::default())
|
||||
}
|
||||
}
|
||||
impl<A: Arrayish> TinyVec<A> {
|
||||
impl<A: Array> TinyVec<A> {
|
||||
/// Moves the content of the TinyVec to the heap, if it's inline.
|
||||
pub fn move_to_the_heap(&mut self) {
|
||||
match self {
|
||||
@@ -47,7 +50,7 @@ impl<A: Arrayish> TinyVec<A> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish> Deref for TinyVec<A> {
|
||||
impl<A: Array> Deref for TinyVec<A> {
|
||||
type Target = [A::Item];
|
||||
#[inline(always)]
|
||||
#[must_use]
|
||||
@@ -59,7 +62,7 @@ impl<A: Arrayish> Deref for TinyVec<A> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish> DerefMut for TinyVec<A> {
|
||||
impl<A: Array> DerefMut for TinyVec<A> {
|
||||
#[inline(always)]
|
||||
#[must_use]
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
@@ -70,7 +73,7 @@ impl<A: Arrayish> DerefMut for TinyVec<A> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish, I: SliceIndex<[A::Item]>> Index<I> for TinyVec<A> {
|
||||
impl<A: Array, I: SliceIndex<[A::Item]>> Index<I> for TinyVec<A> {
|
||||
type Output = <I as SliceIndex<[A::Item]>>::Output;
|
||||
#[inline(always)]
|
||||
#[must_use]
|
||||
@@ -79,7 +82,7 @@ impl<A: Arrayish, I: SliceIndex<[A::Item]>> Index<I> for TinyVec<A> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish, I: SliceIndex<[A::Item]>> IndexMut<I> for TinyVec<A> {
|
||||
impl<A: Array, I: SliceIndex<[A::Item]>> IndexMut<I> for TinyVec<A> {
|
||||
#[inline(always)]
|
||||
#[must_use]
|
||||
fn index_mut(&mut self, index: I) -> &mut Self::Output {
|
||||
@@ -87,7 +90,7 @@ impl<A: Arrayish, I: SliceIndex<[A::Item]>> IndexMut<I> for TinyVec<A> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish> TinyVec<A> {
|
||||
impl<A: Array> TinyVec<A> {
|
||||
/// Move all values from `other` into this vec.
|
||||
#[inline]
|
||||
pub fn append(&mut self, other: &mut Self) {
|
||||
@@ -339,7 +342,7 @@ impl<A: Arrayish> TinyVec<A> {
|
||||
|
||||
/// Place an element onto the end of the vec.
|
||||
///
|
||||
/// See also, [`try_push`](TinyVec::try_push)
|
||||
/// See also, [`try_push`](TinyVec::<A>::try_push)
|
||||
/// ## Panics
|
||||
/// * If the length of the vec would overflow the capacity.
|
||||
#[inline(always)]
|
||||
@@ -544,7 +547,7 @@ impl<A: Arrayish> TinyVec<A> {
|
||||
/// error, and you'll get the array back in the `Err`.
|
||||
#[inline]
|
||||
pub fn try_from_array_len(data: A, len: usize) -> Result<Self, A> {
|
||||
let arr = ArrayishVec::try_from_array_len(data, len)?;
|
||||
let arr = ArrayVec::try_from_array_len(data, len)?;
|
||||
Ok(TinyVec::Inline(arr))
|
||||
}
|
||||
|
||||
@@ -558,14 +561,14 @@ impl<A: Arrayish> TinyVec<A> {
|
||||
/// Draining iterator for `TinyVecDrain`
|
||||
///
|
||||
/// See [`TinyVecDrain::drain`](TinyVecDrain::<A>::drain)
|
||||
pub struct TinyVecDrain<'p, A: Arrayish> {
|
||||
pub struct TinyVecDrain<'p, A: Array> {
|
||||
parent: &'p mut TinyVec<A>,
|
||||
target_index: usize,
|
||||
target_count: usize,
|
||||
}
|
||||
// GoodFirstIssue: this entire type is correct but slow.
|
||||
// NIGHTLY: vec_drain_as_slice, https://github.com/rust-lang/rust/issues/58957
|
||||
impl<'p, A: Arrayish> Iterator for TinyVecDrain<'p, A> {
|
||||
impl<'p, A: Array> Iterator for TinyVecDrain<'p, A> {
|
||||
type Item = A::Item;
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
@@ -578,14 +581,14 @@ impl<'p, A: Arrayish> Iterator for TinyVecDrain<'p, A> {
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<'p, A: Arrayish> Drop for TinyVecDrain<'p, A> {
|
||||
impl<'p, A: Array> Drop for TinyVecDrain<'p, A> {
|
||||
#[inline]
|
||||
fn drop(&mut self) {
|
||||
for _ in self {}
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish> AsMut<[A::Item]> for TinyVec<A> {
|
||||
impl<A: Array> AsMut<[A::Item]> for TinyVec<A> {
|
||||
#[inline(always)]
|
||||
#[must_use]
|
||||
fn as_mut(&mut self) -> &mut [A::Item] {
|
||||
@@ -593,7 +596,7 @@ impl<A: Arrayish> AsMut<[A::Item]> for TinyVec<A> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish> AsRef<[A::Item]> for TinyVec<A> {
|
||||
impl<A: Array> AsRef<[A::Item]> for TinyVec<A> {
|
||||
#[inline(always)]
|
||||
#[must_use]
|
||||
fn as_ref(&self) -> &[A::Item] {
|
||||
@@ -601,7 +604,7 @@ impl<A: Arrayish> AsRef<[A::Item]> for TinyVec<A> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish> Borrow<[A::Item]> for TinyVec<A> {
|
||||
impl<A: Array> Borrow<[A::Item]> for TinyVec<A> {
|
||||
#[inline(always)]
|
||||
#[must_use]
|
||||
fn borrow(&self) -> &[A::Item] {
|
||||
@@ -609,7 +612,7 @@ impl<A: Arrayish> Borrow<[A::Item]> for TinyVec<A> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish> BorrowMut<[A::Item]> for TinyVec<A> {
|
||||
impl<A: Array> BorrowMut<[A::Item]> for TinyVec<A> {
|
||||
#[inline(always)]
|
||||
#[must_use]
|
||||
fn borrow_mut(&mut self) -> &mut [A::Item] {
|
||||
@@ -617,7 +620,7 @@ impl<A: Arrayish> BorrowMut<[A::Item]> for TinyVec<A> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish> Extend<A::Item> for TinyVec<A> {
|
||||
impl<A: Array> Extend<A::Item> for TinyVec<A> {
|
||||
#[inline]
|
||||
fn extend<T: IntoIterator<Item = A::Item>>(&mut self, iter: T) {
|
||||
for t in iter {
|
||||
@@ -626,15 +629,15 @@ impl<A: Arrayish> Extend<A::Item> for TinyVec<A> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish> From<ArrayishVec<A>> for TinyVec<A> {
|
||||
impl<A: Array> From<ArrayVec<A>> for TinyVec<A> {
|
||||
#[inline(always)]
|
||||
#[must_use]
|
||||
fn from(arr: ArrayishVec<A>) -> Self {
|
||||
fn from(arr: ArrayVec<A>) -> Self {
|
||||
TinyVec::Inline(arr)
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish + Default> FromIterator<A::Item> for TinyVec<A> {
|
||||
impl<A: Array + Default> FromIterator<A::Item> for TinyVec<A> {
|
||||
#[inline]
|
||||
#[must_use]
|
||||
fn from_iter<T: IntoIterator<Item = A::Item>>(iter: T) -> Self {
|
||||
@@ -647,11 +650,11 @@ impl<A: Arrayish + Default> FromIterator<A::Item> for TinyVec<A> {
|
||||
}
|
||||
|
||||
/// Iterator for consuming an `TinyVec` and returning owned elements.
|
||||
pub enum TinyVecIterator<A: Arrayish> {
|
||||
Inline(ArrayishVecIterator<A>),
|
||||
pub enum TinyVecIterator<A: Array> {
|
||||
Inline(ArrayVecIterator<A>),
|
||||
Heap(alloc::vec::IntoIter<A::Item>)
|
||||
}
|
||||
impl<A: Arrayish> Iterator for TinyVecIterator<A> {
|
||||
impl<A: Array> Iterator for TinyVecIterator<A> {
|
||||
type Item = A::Item;
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
@@ -676,7 +679,7 @@ impl<A: Arrayish> Iterator for TinyVecIterator<A> {
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
fn last(mut self) -> Option<Self::Item> {
|
||||
fn last(self) -> Option<Self::Item> {
|
||||
match self {
|
||||
TinyVecIterator::Inline(a) => a.last(),
|
||||
TinyVecIterator::Heap(v) => v.last(),
|
||||
@@ -691,7 +694,7 @@ impl<A: Arrayish> Iterator for TinyVecIterator<A> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish> IntoIterator for TinyVec<A> {
|
||||
impl<A: Array> IntoIterator for TinyVec<A> {
|
||||
type Item = A::Item;
|
||||
type IntoIter = TinyVecIterator<A>;
|
||||
#[inline(always)]
|
||||
@@ -704,7 +707,7 @@ impl<A: Arrayish> IntoIterator for TinyVec<A> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish> PartialEq for TinyVec<A>
|
||||
impl<A: Array> PartialEq for TinyVec<A>
|
||||
where
|
||||
A::Item: PartialEq,
|
||||
{
|
||||
@@ -714,9 +717,9 @@ where
|
||||
self.deref().eq(other.deref())
|
||||
}
|
||||
}
|
||||
impl<A: Arrayish> Eq for TinyVec<A> where A::Item: Eq {}
|
||||
impl<A: Array> Eq for TinyVec<A> where A::Item: Eq {}
|
||||
|
||||
impl<A: Arrayish> PartialOrd for TinyVec<A>
|
||||
impl<A: Array> PartialOrd for TinyVec<A>
|
||||
where
|
||||
A::Item: PartialOrd,
|
||||
{
|
||||
@@ -726,7 +729,7 @@ where
|
||||
self.deref().partial_cmp(other.deref())
|
||||
}
|
||||
}
|
||||
impl<A: Arrayish> Ord for TinyVec<A>
|
||||
impl<A: Array> Ord for TinyVec<A>
|
||||
where
|
||||
A::Item: Ord,
|
||||
{
|
||||
@@ -737,7 +740,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish> PartialEq<&A> for TinyVec<A>
|
||||
impl<A: Array> PartialEq<&A> for TinyVec<A>
|
||||
where
|
||||
A::Item: PartialEq,
|
||||
{
|
||||
@@ -748,7 +751,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish> PartialEq<&[A::Item]> for TinyVec<A>
|
||||
impl<A: Array> PartialEq<&[A::Item]> for TinyVec<A>
|
||||
where
|
||||
A::Item: PartialEq,
|
||||
{
|
||||
@@ -766,7 +769,7 @@ I think, in retrospect, this is useless?
|
||||
The `&mut [A::Item]` should coerce to `&[A::Item]` and use the above impl.
|
||||
I'll leave it here for now though since we already had it written out..
|
||||
|
||||
impl<A: Arrayish> PartialEq<&mut [A::Item]> for TinyVec<A>
|
||||
impl<A: Array> PartialEq<&mut [A::Item]> for TinyVec<A>
|
||||
where
|
||||
A::Item: PartialEq,
|
||||
{
|
||||
@@ -782,7 +785,7 @@ where
|
||||
// Formatting impls
|
||||
// //
|
||||
|
||||
impl<A: Arrayish> Binary for TinyVec<A>
|
||||
impl<A: Array> Binary for TinyVec<A>
|
||||
where
|
||||
A::Item: Binary,
|
||||
{
|
||||
@@ -799,7 +802,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish> Debug for TinyVec<A>
|
||||
impl<A: Array> Debug for TinyVec<A>
|
||||
where
|
||||
A::Item: Debug,
|
||||
{
|
||||
@@ -816,7 +819,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish> Display for TinyVec<A>
|
||||
impl<A: Array> Display for TinyVec<A>
|
||||
where
|
||||
A::Item: Display,
|
||||
{
|
||||
@@ -833,7 +836,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish> LowerExp for TinyVec<A>
|
||||
impl<A: Array> LowerExp for TinyVec<A>
|
||||
where
|
||||
A::Item: LowerExp,
|
||||
{
|
||||
@@ -850,7 +853,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish> LowerHex for TinyVec<A>
|
||||
impl<A: Array> LowerHex for TinyVec<A>
|
||||
where
|
||||
A::Item: LowerHex,
|
||||
{
|
||||
@@ -867,7 +870,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish> Octal for TinyVec<A>
|
||||
impl<A: Array> Octal for TinyVec<A>
|
||||
where
|
||||
A::Item: Octal,
|
||||
{
|
||||
@@ -884,7 +887,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish> Pointer for TinyVec<A>
|
||||
impl<A: Array> Pointer for TinyVec<A>
|
||||
where
|
||||
A::Item: Pointer,
|
||||
{
|
||||
@@ -901,7 +904,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish> UpperExp for TinyVec<A>
|
||||
impl<A: Array> UpperExp for TinyVec<A>
|
||||
where
|
||||
A::Item: UpperExp,
|
||||
{
|
||||
@@ -918,7 +921,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arrayish> UpperHex for TinyVec<A>
|
||||
impl<A: Array> UpperHex for TinyVec<A>
|
||||
where
|
||||
A::Item: UpperHex,
|
||||
{
|
||||
|
||||
@@ -5,7 +5,7 @@ use std::iter::FromIterator;
|
||||
|
||||
#[test]
|
||||
fn test_a_vec() {
|
||||
let mut expected: ArrayishVec<[i32; 4]> = Default::default();
|
||||
let mut expected: ArrayVec<[i32; 4]> = Default::default();
|
||||
expected.push(1);
|
||||
expected.push(2);
|
||||
expected.push(3);
|
||||
@@ -16,8 +16,8 @@ fn test_a_vec() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ArrayishVec_push_pop() {
|
||||
let mut av: ArrayishVec<[i32; 4]> = Default::default();
|
||||
fn ArrayVec_push_pop() {
|
||||
let mut av: ArrayVec<[i32; 4]> = Default::default();
|
||||
assert_eq!(av.len(), 0);
|
||||
assert_eq!(av.pop(), None);
|
||||
|
||||
@@ -50,16 +50,16 @@ fn ArrayishVec_push_pop() {
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn ArrayishVec_push_overflow() {
|
||||
let mut av: ArrayishVec<[i32; 0]> = Default::default();
|
||||
fn ArrayVec_push_overflow() {
|
||||
let mut av: ArrayVec<[i32; 0]> = Default::default();
|
||||
av.push(7);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ArrayishVec_formatting() {
|
||||
fn ArrayVec_formatting() {
|
||||
// check that we get the comma placement correct
|
||||
|
||||
let mut av: ArrayishVec<[i32; 4]> = Default::default();
|
||||
let mut av: ArrayVec<[i32; 4]> = Default::default();
|
||||
assert_eq!(format!("{:?}", av), "[]");
|
||||
av.push(10);
|
||||
assert_eq!(format!("{:?}", av), "[10]");
|
||||
@@ -71,23 +71,23 @@ fn ArrayishVec_formatting() {
|
||||
// below here just asserts that the impls exist.
|
||||
|
||||
//
|
||||
let av: ArrayishVec<[i32; 4]> = Default::default();
|
||||
let av: ArrayVec<[i32; 4]> = Default::default();
|
||||
assert_eq!(format!("{:b}", av), "[]");
|
||||
assert_eq!(format!("{:o}", av), "[]");
|
||||
assert_eq!(format!("{:x}", av), "[]");
|
||||
assert_eq!(format!("{:X}", av), "[]");
|
||||
assert_eq!(format!("{}", av), "[]");
|
||||
//
|
||||
let av: ArrayishVec<[f32; 4]> = Default::default();
|
||||
let av: ArrayVec<[f32; 4]> = Default::default();
|
||||
assert_eq!(format!("{:e}", av), "[]");
|
||||
assert_eq!(format!("{:E}", av), "[]");
|
||||
//
|
||||
let av: ArrayishVec<[&'static str; 4]> = Default::default();
|
||||
let av: ArrayVec<[&'static str; 4]> = Default::default();
|
||||
assert_eq!(format!("{:p}", av), "[]");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ArrayishVec_iteration() {
|
||||
fn ArrayVec_iteration() {
|
||||
let av = arr_vec!([i32; 4], 10, 11, 12, 13);
|
||||
|
||||
let mut i = av.into_iter();
|
||||
@@ -99,12 +99,12 @@ fn ArrayishVec_iteration() {
|
||||
|
||||
let av = arr_vec!([i32; 4], 10, 11, 12, 13);
|
||||
|
||||
let av2: ArrayishVec<[i32; 4]> = av.clone().into_iter().collect();
|
||||
let av2: ArrayVec<[i32; 4]> = av.clone().into_iter().collect();
|
||||
assert_eq!(av, av2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ArrayishVec_append() {
|
||||
fn ArrayVec_append() {
|
||||
let mut av = arr_vec!([i32; 8], 1, 2, 3);
|
||||
let mut av2 = arr_vec!([i32; 8], 4, 5, 6);
|
||||
//
|
||||
@@ -114,8 +114,8 @@ fn ArrayishVec_append() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ArrayishVec_remove() {
|
||||
let mut av: ArrayishVec<[i32; 10]> = Default::default();
|
||||
fn ArrayVec_remove() {
|
||||
let mut av: ArrayVec<[i32; 10]> = Default::default();
|
||||
av.push(1);
|
||||
av.push(2);
|
||||
av.push(3);
|
||||
@@ -124,8 +124,8 @@ fn ArrayishVec_remove() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ArrayishVec_swap_remove() {
|
||||
let mut av: ArrayishVec<[i32; 10]> = Default::default();
|
||||
fn ArrayVec_swap_remove() {
|
||||
let mut av: ArrayVec<[i32; 10]> = Default::default();
|
||||
av.push(1);
|
||||
av.push(2);
|
||||
av.push(3);
|
||||
@@ -141,8 +141,8 @@ fn ArrayishVec_swap_remove() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ArrayishVec_drain() {
|
||||
let mut av: ArrayishVec<[i32; 10]> = Default::default();
|
||||
fn ArrayVec_drain() {
|
||||
let mut av: ArrayVec<[i32; 10]> = Default::default();
|
||||
av.push(1);
|
||||
av.push(2);
|
||||
av.push(3);
|
||||
Reference in New Issue
Block a user