Enum tinyvec::TinyVec[][src]

pub enum TinyVec<A: Array> {
    Inline(ArrayVec<A>),
    Heap(Vec<A::Item>),
}
This is supported on crate feature alloc only.
Expand description

A vector that starts inline, but can automatically move to the heap.

  • Requires the alloc feature

A TinyVec is either an Inline(ArrayVec) or Heap(Vec). The interface for the type as a whole is a bunch of methods that just match on the enum variant and then call the same method on the inner vec.

Construction

Because it’s an enum, you can construct a TinyVec simply by making an ArrayVec or Vec and then putting it into the enum.

There is also a macro

let empty_tv = tiny_vec!([u8; 16]);
let some_ints = tiny_vec!([i32; 4] => 1, 2, 3);

Variants

Inline(ArrayVec<A>)
Heap(Vec<A::Item>)

Implementations

Returns whether elements are on heap

Returns whether elements are on stack

Shrinks the capacity of the vector as much as possible.
It is inlined if length is less than A::CAPACITY.

use tinyvec::*;
let mut tv = tiny_vec!([i32; 2] => 1, 2, 3);
assert!(tv.is_heap());
let _ = tv.pop();
assert!(tv.is_heap());
tv.shrink_to_fit();
assert!(tv.is_inline());

Moves the content of the TinyVec to the heap, if it’s inline.

use tinyvec::*;
let mut tv = tiny_vec!([i32; 4] => 1, 2, 3);
assert!(tv.is_inline());
tv.move_to_the_heap();
assert!(tv.is_heap());

If TinyVec is inline, moves the content of it to the heap. Also reserves additional space.

use tinyvec::*;
let mut tv = tiny_vec!([i32; 4] => 1, 2, 3);
assert!(tv.is_inline());
tv.move_to_the_heap_and_reserve(32);
assert!(tv.is_heap());
assert!(tv.capacity() >= 35);

Reserves additional space. Moves to the heap if array can’t hold n more items

use tinyvec::*;
let mut tv = tiny_vec!([i32; 4] => 1, 2, 3, 4);
assert!(tv.is_inline());
tv.reserve(1);
assert!(tv.is_heap());
assert!(tv.capacity() >= 5);

Reserves additional space. Moves to the heap if array can’t hold n more items

From Vec::reserve_exact

Note that the allocator may give the collection more space than it requests.
Therefore, capacity can not be relied upon to be precisely minimal.
Prefer `reserve` if future insertions are expected.
use tinyvec::*;
let mut tv = tiny_vec!([i32; 4] => 1, 2, 3, 4);
assert!(tv.is_inline());
tv.reserve_exact(1);
assert!(tv.is_heap());
assert!(tv.capacity() >= 5);

Makes a new TinyVec with at least the given capacity.

If the requested capacity is less than or equal to the array capacity you get an inline vec. If it’s greater than you get a heap vec.

let t = TinyVec::<[u8; 10]>::with_capacity(5);
assert!(t.is_inline());
assert!(t.capacity() >= 5);

let t = TinyVec::<[u8; 10]>::with_capacity(20);
assert!(t.is_heap());
assert!(t.capacity() >= 20);

Move all values from other into this vec.

Remove an element, swapping the end of the vec into its place.

Panics

  • If the index is out of bounds.

Example

use tinyvec::*;
let mut tv = tiny_vec!([&str; 4] => "foo", "bar", "quack", "zap");

assert_eq!(tv.swap_remove(1), "bar");
assert_eq!(tv.as_slice(), &["foo", "zap", "quack"][..]);

assert_eq!(tv.swap_remove(0), "foo");
assert_eq!(tv.as_slice(), &["quack", "zap"][..]);

Remove and return the last element of the vec, if there is one.

Failure

  • If the vec is empty you get None.

Removes the item at index, shifting all others down by one index.

Returns the removed element.

Panics

If the index is out of bounds.

Example

use tinyvec::*;
let mut tv = tiny_vec!([i32; 4] => 1, 2, 3);
assert_eq!(tv.remove(1), 2);
assert_eq!(tv.as_slice(), &[1, 3][..]);

The length of the vec (in elements).

The capacity of the TinyVec.

When not heap allocated this is fixed based on the array type. Otherwise its the result of the underlying Vec::capacity.

Reduces the vec’s length to the given value.

If the vec is already shorter than the input, nothing happens.

A mutable pointer to the backing array.

Safety

This pointer has provenance over the entire backing array/buffer.

A const pointer to the backing array.

Safety

This pointer has provenance over the entire backing array/buffer.

Walk the vec and keep only the elements that pass the predicate given.

Example

use tinyvec::*;

let mut tv = tiny_vec!([i32; 10] => 1, 2, 3, 4);
tv.retain(|&x| x % 2 == 0);
assert_eq!(tv.as_slice(), &[2, 4][..]);

Helper for getting the mut slice.

Helper for getting the shared slice.

Removes all elements from the vec.

Creates a draining iterator that removes the specified range in the vector and yields the removed items.

Note: This method has significant performance issues compared to matching on the TinyVec and then calling drain on the Inline or Heap value inside. The draining iterator has to branch on every single access. It is provided for simplicity and compatability only.

Panics

  • If the start is greater than the end
  • If the end is past the edge of the vec.

Example

use tinyvec::*;
let mut tv = tiny_vec!([i32; 4] => 1, 2, 3);
let tv2: TinyVec<[i32; 4]> = tv.drain(1..).collect();
assert_eq!(tv.as_slice(), &[1][..]);
assert_eq!(tv2.as_slice(), &[2, 3][..]);

tv.drain(..);
assert_eq!(tv.as_slice(), &[]);

Clone each element of the slice into this vec.

use tinyvec::*;
let mut tv = tiny_vec!([i32; 4] => 1, 2);
tv.extend_from_slice(&[3, 4]);
assert_eq!(tv.as_slice(), [1, 2, 3, 4]);

Wraps up an array and uses the given length as the initial length.

Note that the From impl for arrays assumes the full length is used.

Panics

The length must be less than or equal to the capacity of the array.

Inserts an item at the position given, moving all following elements +1 index.

Panics

  • If index > len

Example

use tinyvec::*;
let mut tv = tiny_vec!([i32; 10] => 1, 2, 3);
tv.insert(1, 4);
assert_eq!(tv.as_slice(), &[1, 4, 2, 3]);
tv.insert(4, 5);
assert_eq!(tv.as_slice(), &[1, 4, 2, 3, 5]);

If the vec is empty.

Makes a new, empty vec.

Place an element onto the end of the vec.

Panics

  • If the length of the vec would overflow the capacity.
use tinyvec::*;
let mut tv = tiny_vec!([i32; 10] => 1, 2, 3);
tv.push(4);
assert_eq!(tv.as_slice(), &[1, 2, 3, 4]);

Resize the vec to the new length.

If it needs to be longer, it’s filled with clones of the provided value. If it needs to be shorter, it’s truncated.

Example

use tinyvec::*;

let mut tv = tiny_vec!([&str; 10] => "hello");
tv.resize(3, "world");
assert_eq!(tv.as_slice(), &["hello", "world", "world"][..]);

let mut tv = tiny_vec!([i32; 10] => 1, 2, 3, 4);
tv.resize(2, 0);
assert_eq!(tv.as_slice(), &[1, 2][..]);

Resize the vec to the new length.

If it needs to be longer, it’s filled with repeated calls to the provided function. If it needs to be shorter, it’s truncated.

Example

use tinyvec::*;

let mut tv = tiny_vec!([i32; 3] => 1, 2, 3);
tv.resize_with(5, Default::default);
assert_eq!(tv.as_slice(), &[1, 2, 3, 0, 0][..]);

let mut tv = tiny_vec!([i32; 2]);
let mut p = 1;
tv.resize_with(4, || {
  p *= 2;
  p
});
assert_eq!(tv.as_slice(), &[2, 4, 8, 16][..]);

Splits the collection at the point given.

  • [0, at) stays in this vec
  • [at, len) ends up in the new vec.

Panics

  • if at > len

Example

use tinyvec::*;
let mut tv = tiny_vec!([i32; 4] => 1, 2, 3);
let tv2 = tv.split_off(1);
assert_eq!(tv.as_slice(), &[1][..]);
assert_eq!(tv2.as_slice(), &[2, 3][..]);

Creates a splicing iterator that removes the specified range in the vector, yields the removed items, and replaces them with elements from the provided iterator.

splice fuses the provided iterator, so elements after the first None are ignored.

Panics

  • If the start is greater than the end.
  • If the end is past the edge of the vec.
  • If the provided iterator panics.

Example

use tinyvec::*;
let mut tv = tiny_vec!([i32; 4] => 1, 2, 3);
let tv2: TinyVec<[i32; 4]> = tv.splice(1.., 4..=6).collect();
assert_eq!(tv.as_slice(), &[1, 4, 5, 6][..]);
assert_eq!(tv2.as_slice(), &[2, 3][..]);

tv.splice(.., None);
assert_eq!(tv.as_slice(), &[]);

Wraps an array, using the given length as the starting length.

If you want to use the whole length of the array, you can just use the From impl.

Failure

If the given length is greater than the capacity of the array this will error, and you’ll get the array back in the Err.

Trait Implementations

Performs the conversion.

Performs the conversion.

Formats the value using the given formatter.

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

The resulting type after dereferencing.

Dereferences the value.

Mutably dereferences the value.

Deserialize this value from the given Serde deserializer. Read more

Formats the value using the given formatter. Read more

Extends a collection with the contents of an iterator. Read more

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Creates a value from an iterator. Read more

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

The returned type after indexing.

Performs the indexing (container[index]) operation. Read more

Performs the mutable indexing (container[index]) operation. Read more

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

Formats the value using the given formatter.

Formats the value using the given formatter.

Formats the value using the given formatter.

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Formats the value using the given formatter.

Serialize this value into the given Serde serializer. Read more

Formats the value using the given formatter.

Formats the value using the given formatter.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.