Skip to main content

TinyVec

Struct TinyVec 

Source
pub struct TinyVec<T> { /* private fields */ }
Expand description

Ultra-compact vector using a thin pointer (8 bytes on stack).

Memory layout:

  • Stack: single pointer (8 bytes) - null for empty
  • Heap: [len: u8][items: T...] - only allocated when non-empty

Compared to standard types:

  • Vec<T>: 24 bytes (ptr + len + cap)
  • Box<[T]>: 16 bytes (fat pointer)
  • TinyVec<T>: 8 bytes (thin pointer)

Limitations:

  • Max 255 items (u8 length)
  • Immutable after creation (no push/pop - recreate to modify)
  • Perfect for attachments/reactions which rarely change

Implementations§

Source§

impl<T> TinyVec<T>

Source

pub const fn new() -> Self

Create an empty TinyVec (no allocation)

Source

pub fn from_vec(vec: Vec<T>) -> Self

Create from a Vec, consuming it

Source

pub fn len(&self) -> usize

Number of items

Source

pub fn is_empty(&self) -> bool

Source

pub fn as_slice(&self) -> &[T]

Get a slice of the items

Source

pub fn as_mut_slice(&mut self) -> &mut [T]

Get a mutable slice of the items

Source

pub fn iter(&self) -> Iter<'_, T>

Iterate over items

Source

pub fn iter_mut(&mut self) -> IterMut<'_, T>

Iterate mutably

Source

pub fn to_vec(&self) -> Vec<T>
where T: Clone,

Convert to Vec (clones items)

Source

pub fn first(&self) -> Option<&T>

Get the first item (immutable)

Source

pub fn last(&self) -> Option<&T>

Get the last item (immutable)

Source

pub fn last_mut(&mut self) -> Option<&mut T>

Get the last item (mutable)

Source

pub fn get(&self, index: usize) -> Option<&T>

Get item by index (immutable)

Source

pub fn get_mut(&mut self, index: usize) -> Option<&mut T>

Get item by index (mutable)

Source

pub fn push(&mut self, item: T)
where T: Clone,

Push an item (rebuilds the entire allocation - use sparingly!)

Source

pub fn retain<F>(&mut self, f: F)
where T: Clone, F: FnMut(&T) -> bool,

Retain items matching a predicate (rebuilds the allocation)

Source

pub fn any<F>(&self, f: F) -> bool
where F: FnMut(&T) -> bool,

Check if any item matches a predicate

Trait Implementations§

Source§

impl<T: Clone> Clone for TinyVec<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for TinyVec<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Default for TinyVec<T>

Source§

fn default() -> Self

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

impl<T> Drop for TinyVec<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl<T> Index<usize> for TinyVec<T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

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

impl<T> IndexMut<usize> for TinyVec<T>

Source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

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

impl<'a, T> IntoIterator for &'a TinyVec<T>

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T> IntoIterator for &'a mut TinyVec<T>

Source§

type Item = &'a mut T

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T: Send> Send for TinyVec<T>

Source§

impl<T: Sync> Sync for TinyVec<T>

Auto Trait Implementations§

§

impl<T> Freeze for TinyVec<T>

§

impl<T> RefUnwindSafe for TinyVec<T>
where T: RefUnwindSafe,

§

impl<T> Unpin for TinyVec<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for TinyVec<T>

§

impl<T> UnwindSafe for TinyVec<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more