diff --git a/src/arrayish.rs b/src/array.rs
similarity index 58%
rename from src/arrayish.rs
rename to src/array.rs
index d0cbebd..8976918 100644
--- a/src/arrayish.rs
+++ b/src/array.rs
@@ -1,16 +1,21 @@
/// A trait for types that can be the backing store of an
-/// [`ArrayishVec`](ArrayishVec::).
+/// [`ArrayVec`](ArrayVec::).
///
-/// 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 Arrayish for [T; $len] {
+ $(impl 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,
diff --git a/src/arrayish_vec.rs b/src/arrayvec.rs
similarity index 86%
rename from src/arrayish_vec.rs
rename to src/arrayvec.rs
index f99955d..e8bb2a1 100644
--- a/src/arrayish_vec.rs
+++ b/src/arrayvec.rs
@@ -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 {
+pub struct ArrayVec {
len: usize,
data: A,
}
-impl Deref for ArrayishVec {
+impl Deref for ArrayVec {
type Target = [A::Item];
#[inline(always)]
#[must_use]
@@ -53,7 +53,7 @@ impl Deref for ArrayishVec {
}
}
-impl DerefMut for ArrayishVec {
+impl DerefMut for ArrayVec {
#[inline(always)]
#[must_use]
fn deref_mut(&mut self) -> &mut Self::Target {
@@ -61,7 +61,7 @@ impl DerefMut for ArrayishVec {
}
}
-impl> Index for ArrayishVec {
+impl> Index for ArrayVec {
type Output = >::Output;
#[inline(always)]
#[must_use]
@@ -70,7 +70,7 @@ impl> Index for ArrayishVec {
}
}
-impl> IndexMut for ArrayishVec {
+impl> IndexMut for ArrayVec {
#[inline(always)]
#[must_use]
fn index_mut(&mut self, index: I) -> &mut Self::Output {
@@ -78,7 +78,7 @@ impl> IndexMut for ArrayishVec {
}
}
-impl ArrayishVec {
+impl ArrayVec {
/// Move all values from `other` into this vec.
#[inline]
pub fn append(&mut self, other: &mut Self) {
@@ -123,7 +123,7 @@ impl ArrayishVec {
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 ArrayishVec {
/// ```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 ArrayishVec {
pub fn drain>(
&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 ArrayishVec {
};
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 ArrayishVec {
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 ArrayishVec {
}
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 ArrayishVec {
/// 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 ArrayishVec {
// 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 ArrayishVec {
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 ArrayishVec {
// LATER: try_remove ?
}
-/// Draining iterator for `ArrayishVecDrain`
+/// Draining iterator for `ArrayVecDrain`
///
-/// See [`ArrayishVecDrain::drain`](ArrayishVecDrain::::drain)
-pub struct ArrayishVecDrain<'p, A: Arrayish> {
- parent: &'p mut ArrayishVec,
+/// See [`ArrayVecDrain::drain`](ArrayVecDrain::::drain)
+pub struct ArrayVecDrain<'p, A: Array> {
+ parent: &'p mut ArrayVec,
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 {
@@ -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 AsMut<[A::Item]> for ArrayishVec {
+impl AsMut<[A::Item]> for ArrayVec {
#[inline(always)]
#[must_use]
fn as_mut(&mut self) -> &mut [A::Item] {
@@ -638,7 +638,7 @@ impl AsMut<[A::Item]> for ArrayishVec {
}
}
-impl AsRef<[A::Item]> for ArrayishVec {
+impl AsRef<[A::Item]> for ArrayVec {
#[inline(always)]
#[must_use]
fn as_ref(&self) -> &[A::Item] {
@@ -646,7 +646,7 @@ impl AsRef<[A::Item]> for ArrayishVec {
}
}
-impl Borrow<[A::Item]> for ArrayishVec {
+impl Borrow<[A::Item]> for ArrayVec {
#[inline(always)]
#[must_use]
fn borrow(&self) -> &[A::Item] {
@@ -654,7 +654,7 @@ impl Borrow<[A::Item]> for ArrayishVec {
}
}
-impl BorrowMut<[A::Item]> for ArrayishVec {
+impl BorrowMut<[A::Item]> for ArrayVec {
#[inline(always)]
#[must_use]
fn borrow_mut(&mut self) -> &mut [A::Item] {
@@ -662,7 +662,7 @@ impl BorrowMut<[A::Item]> for ArrayishVec {
}
}
-impl Extend for ArrayishVec {
+impl Extend for ArrayVec {
#[inline]
fn extend>(&mut self, iter: T) {
for t in iter {
@@ -671,19 +671,19 @@ impl Extend for ArrayishVec {
}
}
-impl From for ArrayishVec {
+impl From for ArrayVec {
#[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 FromIterator for ArrayishVec {
+impl FromIterator for ArrayVec {
#[inline]
#[must_use]
fn from_iter>(iter: T) -> Self {
@@ -695,13 +695,13 @@ impl FromIterator for ArrayishVec {
}
}
-/// Iterator for consuming an `ArrayishVec` and returning owned elements.
-pub struct ArrayishVecIterator {
+/// Iterator for consuming an `ArrayVec` and returning owned elements.
+pub struct ArrayVecIterator {
base: usize,
len: usize,
data: A,
}
-impl Iterator for ArrayishVecIterator {
+impl Iterator for ArrayVecIterator {
type Item = A::Item;
#[inline]
fn next(&mut self) -> Option {
@@ -741,17 +741,17 @@ impl Iterator for ArrayishVecIterator {
}
}
-impl IntoIterator for ArrayishVec {
+impl IntoIterator for ArrayVec {
type Item = A::Item;
- type IntoIter = ArrayishVecIterator;
+ type IntoIter = ArrayVecIterator;
#[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 PartialEq for ArrayishVec
+impl PartialEq for ArrayVec
where
A::Item: PartialEq,
{
@@ -761,9 +761,9 @@ where
self.deref().eq(other.deref())
}
}
-impl Eq for ArrayishVec where A::Item: Eq {}
+impl Eq for ArrayVec where A::Item: Eq {}
-impl PartialOrd for ArrayishVec
+impl PartialOrd for ArrayVec
where
A::Item: PartialOrd,
{
@@ -773,7 +773,7 @@ where
self.deref().partial_cmp(other.deref())
}
}
-impl Ord for ArrayishVec
+impl Ord for ArrayVec
where
A::Item: Ord,
{
@@ -784,7 +784,7 @@ where
}
}
-impl PartialEq<&A> for ArrayishVec
+impl PartialEq<&A> for ArrayVec
where
A::Item: PartialEq,
{
@@ -795,7 +795,7 @@ where
}
}
-impl PartialEq<&[A::Item]> for ArrayishVec
+impl PartialEq<&[A::Item]> for ArrayVec
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 PartialEq<&mut [A::Item]> for ArrayishVec
+impl PartialEq<&mut [A::Item]> for ArrayVec
where
A::Item: PartialEq,
{
@@ -829,7 +829,7 @@ where
// Formatting impls
// //
-impl Binary for ArrayishVec
+impl Binary for ArrayVec
where
A::Item: Binary,
{
@@ -846,7 +846,7 @@ where
}
}
-impl Debug for ArrayishVec
+impl Debug for ArrayVec
where
A::Item: Debug,
{
@@ -863,7 +863,7 @@ where
}
}
-impl Display for ArrayishVec
+impl Display for ArrayVec
where
A::Item: Display,
{
@@ -880,7 +880,7 @@ where
}
}
-impl LowerExp for ArrayishVec
+impl LowerExp for ArrayVec
where
A::Item: LowerExp,
{
@@ -897,7 +897,7 @@ where
}
}
-impl LowerHex for ArrayishVec
+impl LowerHex for ArrayVec
where
A::Item: LowerHex,
{
@@ -914,7 +914,7 @@ where
}
}
-impl Octal for ArrayishVec
+impl Octal for ArrayVec
where
A::Item: Octal,
{
@@ -931,7 +931,7 @@ where
}
}
-impl Pointer for ArrayishVec
+impl Pointer for ArrayVec
where
A::Item: Pointer,
{
@@ -948,7 +948,7 @@ where
}
}
-impl UpperExp for ArrayishVec
+impl UpperExp for ArrayVec
where
A::Item: UpperExp,
{
@@ -965,7 +965,7 @@ where
}
}
-impl UpperHex for ArrayishVec
+impl UpperHex for ArrayVec
where
A::Item: UpperHex,
{
diff --git a/src/lib.rs b/src/lib.rs
index 5b8e80c..8a17c35 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -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;
diff --git a/src/tiny_vec.rs b/src/tiny_vec.rs
index 6d2072f..025bd01 100644
--- a/src/tiny_vec.rs
+++ b/src/tiny_vec.rs
@@ -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 {
- Inline(ArrayishVec),
+pub enum TinyVec {
+ Inline(ArrayVec),
Heap(Vec)
}
-impl Default for TinyVec {
+impl Default for TinyVec {
fn default() -> Self {
- TinyVec::Inline(ArrayishVec::default())
+ TinyVec::Inline(ArrayVec::default())
}
}
-impl TinyVec {
+impl TinyVec {
/// 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 TinyVec {
}
}
-impl Deref for TinyVec {
+impl Deref for TinyVec {
type Target = [A::Item];
#[inline(always)]
#[must_use]
@@ -59,7 +62,7 @@ impl Deref for TinyVec {
}
}
-impl DerefMut for TinyVec {
+impl DerefMut for TinyVec {
#[inline(always)]
#[must_use]
fn deref_mut(&mut self) -> &mut Self::Target {
@@ -70,7 +73,7 @@ impl DerefMut for TinyVec {
}
}
-impl> Index for TinyVec {
+impl> Index for TinyVec {
type Output = >::Output;
#[inline(always)]
#[must_use]
@@ -79,7 +82,7 @@ impl> Index for TinyVec {
}
}
-impl> IndexMut for TinyVec {
+impl> IndexMut for TinyVec {
#[inline(always)]
#[must_use]
fn index_mut(&mut self, index: I) -> &mut Self::Output {
@@ -87,7 +90,7 @@ impl> IndexMut for TinyVec {
}
}
-impl TinyVec {
+impl TinyVec {
/// Move all values from `other` into this vec.
#[inline]
pub fn append(&mut self, other: &mut Self) {
@@ -339,7 +342,7 @@ impl TinyVec {
/// Place an element onto the end of the vec.
///
- /// See also, [`try_push`](TinyVec::try_push)
+ /// See also, [`try_push`](TinyVec::::try_push)
/// ## Panics
/// * If the length of the vec would overflow the capacity.
#[inline(always)]
@@ -544,7 +547,7 @@ impl TinyVec {
/// error, and you'll get the array back in the `Err`.
#[inline]
pub fn try_from_array_len(data: A, len: usize) -> Result {
- 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 TinyVec {
/// Draining iterator for `TinyVecDrain`
///
/// See [`TinyVecDrain::drain`](TinyVecDrain::::drain)
-pub struct TinyVecDrain<'p, A: Arrayish> {
+pub struct TinyVecDrain<'p, A: Array> {
parent: &'p mut TinyVec,
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 {
@@ -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 AsMut<[A::Item]> for TinyVec {
+impl AsMut<[A::Item]> for TinyVec {
#[inline(always)]
#[must_use]
fn as_mut(&mut self) -> &mut [A::Item] {
@@ -593,7 +596,7 @@ impl AsMut<[A::Item]> for TinyVec {
}
}
-impl AsRef<[A::Item]> for TinyVec {
+impl AsRef<[A::Item]> for TinyVec